tlsopts: fix helper (#1515)

This commit is contained in:
JT Olio 2019-04-10 09:07:51 -04:00 committed by Egon Elbre
parent ea8a0fd9ba
commit 46ab2f0d8f
2 changed files with 31 additions and 5 deletions

View File

@ -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
}

View File

@ -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)
}