From 46ab2f0d8f0ca72a9e969a302e074919b1a05a4e Mon Sep 17 00:00:00 2001 From: JT Olio Date: Wed, 10 Apr 2019 09:07:51 -0400 Subject: [PATCH] tlsopts: fix helper (#1515) --- pkg/peertls/tlsopts/options.go | 10 ++++---- pkg/peertls/tlsopts/options_internal_test.go | 26 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 pkg/peertls/tlsopts/options_internal_test.go diff --git a/pkg/peertls/tlsopts/options.go b/pkg/peertls/tlsopts/options.go index 8b852ba21..8a7f30925 100644 --- a/pkg/peertls/tlsopts/options.go +++ b/pkg/peertls/tlsopts/options.go @@ -174,11 +174,11 @@ func (vf *VerificationFuncs) ServerAdd(verificationFuncs ...peertls.PeerCertVeri } func removeNils(verificationFuncs []peertls.PeerCertVerificationFunc) []peertls.PeerCertVerificationFunc { - for i, f := range verificationFuncs { - if f == nil { - copy(verificationFuncs[i:], verificationFuncs[i+1:]) - verificationFuncs = verificationFuncs[:len(verificationFuncs)-1] + result := verificationFuncs[:0] + for _, f := range verificationFuncs { + if f != nil { + result = append(result, f) } } - return verificationFuncs + return result } diff --git a/pkg/peertls/tlsopts/options_internal_test.go b/pkg/peertls/tlsopts/options_internal_test.go new file mode 100644 index 000000000..dd560efe3 --- /dev/null +++ b/pkg/peertls/tlsopts/options_internal_test.go @@ -0,0 +1,26 @@ +// Copyright (C) 2019 Storj Labs, Inc. +// See LICENSE for copying information. + +package tlsopts + +import ( + "crypto/x509" + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "storj.io/storj/pkg/peertls" +) + +func TestRemoveNils(t *testing.T) { + e1 := fmt.Errorf("error 1") + f1 := peertls.PeerCertVerificationFunc(func([][]byte, [][]*x509.Certificate) error { return e1 }) + e2 := fmt.Errorf("error 2") + f2 := peertls.PeerCertVerificationFunc(func([][]byte, [][]*x509.Certificate) error { return e2 }) + + l := removeNils([]peertls.PeerCertVerificationFunc{f1, nil, nil, f2}) + require.Equal(t, len(l), 2) + require.Equal(t, l[0](nil, nil), e1) + require.Equal(t, l[1](nil, nil), e2) +}