2019-03-25 21:52:12 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package extensions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testpeertls"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/peertls/extensions"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-03-25 21:52:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandlers_Register(t *testing.T) {
|
|
|
|
var (
|
|
|
|
handlers = extensions.HandlerFactories{}
|
|
|
|
ids []*extensions.ExtensionID
|
|
|
|
opts []*extensions.Options
|
|
|
|
exts []pkix.Extension
|
|
|
|
chains [][][]*x509.Certificate
|
|
|
|
)
|
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
for idx := 0; idx < 5; idx++ {
|
|
|
|
i := idx
|
|
|
|
|
2019-03-25 21:52:12 +00:00
|
|
|
ids = append(ids, &extensions.ExtensionID{2, 999, 999, i})
|
|
|
|
opts = append(opts, &extensions.Options{})
|
|
|
|
exts = append(exts, pkix.Extension{Id: *ids[i]})
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
_, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
chains = append(chains, identity.ToChains(chain))
|
|
|
|
|
|
|
|
testHandler := extensions.NewHandlerFactory(
|
|
|
|
ids[i],
|
|
|
|
func(opt *extensions.Options) extensions.HandlerFunc {
|
|
|
|
assert.Equal(t, opts[i], opt)
|
|
|
|
assert.NotNil(t, opt)
|
|
|
|
|
|
|
|
return func(ext pkix.Extension, chain [][]*x509.Certificate) error {
|
|
|
|
assert.NotNil(t, ext)
|
|
|
|
assert.Equal(t, exts[i], ext)
|
|
|
|
|
|
|
|
assert.NotNil(t, ext.Id)
|
|
|
|
assert.Equal(t, *ids[i], ext.Id)
|
|
|
|
|
|
|
|
assert.NotNil(t, chain)
|
|
|
|
assert.Equal(t, chains[i], chain)
|
|
|
|
return errs.New(strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
handlers.Register(testHandler)
|
|
|
|
|
|
|
|
err = handlers[i].NewHandlerFunc(opts[i])(exts[i], chains[i])
|
|
|
|
assert.Errorf(t, err, strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlers_WithOptions(t *testing.T) {
|
|
|
|
var (
|
|
|
|
handlers = extensions.HandlerFactories{}
|
|
|
|
ids []*extensions.ExtensionID
|
|
|
|
opts []*extensions.Options
|
|
|
|
exts []pkix.Extension
|
|
|
|
chains [][][]*x509.Certificate
|
|
|
|
)
|
|
|
|
|
2019-05-29 14:30:16 +01:00
|
|
|
for idx := 0; idx < 5; idx++ {
|
|
|
|
i := idx
|
|
|
|
|
2019-03-25 21:52:12 +00:00
|
|
|
ids = append(ids, &extensions.ExtensionID{2, 999, 999, i})
|
|
|
|
opts = append(opts, &extensions.Options{})
|
|
|
|
exts = append(exts, pkix.Extension{Id: *ids[i]})
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
_, chain, err := testpeertls.NewCertChain(2, storj.LatestIDVersion().Number)
|
2019-03-25 21:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
chains = append(chains, identity.ToChains(chain))
|
|
|
|
|
|
|
|
testHandler := extensions.NewHandlerFactory(
|
|
|
|
ids[i],
|
|
|
|
func(opt *extensions.Options) extensions.HandlerFunc {
|
|
|
|
assert.Equal(t, opts[i], opt)
|
|
|
|
assert.NotNil(t, opt)
|
|
|
|
|
|
|
|
return func(ext pkix.Extension, chain [][]*x509.Certificate) error {
|
|
|
|
assert.NotNil(t, ext)
|
|
|
|
assert.Equal(t, exts[i], ext)
|
|
|
|
|
|
|
|
assert.NotNil(t, ext.Id)
|
|
|
|
assert.Equal(t, *ids[i], ext.Id)
|
|
|
|
|
|
|
|
assert.NotNil(t, chain)
|
|
|
|
assert.Equal(t, chains[i], chain)
|
|
|
|
return errs.New(strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
handlers.Register(testHandler)
|
|
|
|
|
|
|
|
handlerFuncMap := handlers.WithOptions(&extensions.Options{})
|
|
|
|
|
|
|
|
id := handlers[i].ID()
|
|
|
|
require.NotNil(t, id)
|
|
|
|
|
|
|
|
handleFunc, ok := handlerFuncMap[id]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, handleFunc)
|
|
|
|
}
|
|
|
|
}
|