-
Notifications
You must be signed in to change notification settings - Fork 44
/
options_test.go
114 lines (109 loc) · 3.23 KB
/
options_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package wrapping
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_GetOpts(t *testing.T) {
t.Parallel()
t.Run("nil", func(t *testing.T) {
assert := assert.New(t)
opts, err := GetOpts(nil)
assert.NoError(err)
assert.NotNil(opts)
})
t.Run("wrong-type", func(t *testing.T) {
assert := assert.New(t)
opts, err := GetOpts(Option(func() interface{} {
return nil
}))
assert.Error(err)
assert.Nil(opts)
})
t.Run("right-type", func(t *testing.T) {
assert := assert.New(t)
opts, err := GetOpts(Option(func() interface{} {
return OptionFunc(func(*Options) error {
return nil
})
}))
assert.NoError(err)
assert.NotNil(opts)
})
t.Run("WithAad", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithAad([]byte("foo")))
require.NoError(err)
require.NotNil(opts)
assert.Equal([]byte("foo"), opts.WithAad)
})
t.Run("WithKeyId", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithKeyId("bar"))
require.NoError(err)
require.NotNil(opts)
assert.Equal("bar", opts.WithKeyId)
})
t.Run("WithConfigMap", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
strOpts := map[string]string{"foo": "bar"}
opts, err := GetOpts(WithConfigMap(strOpts))
require.NoError(err)
require.NotNil(opts)
assert.Equal(strOpts, opts.WithConfigMap)
})
t.Run("WithKeyPurposes", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithKeyPurposes(KeyPurpose_Sign))
require.NoError(err)
require.NotNil(opts)
assert.Equal([]KeyPurpose{KeyPurpose_Sign}, opts.WithKeyPurposes)
})
t.Run("WithKeyType", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithKeyType(KeyType_Ed25519))
require.NoError(err)
require.NotNil(opts)
assert.Equal(KeyType_Ed25519, opts.WithKeyType)
})
t.Run("WithRandomBytes", func(t *testing.T) {
testBytes := []byte("test")
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithRandomBytes(testBytes))
require.NoError(err)
require.NotNil(opts)
assert.Equal(testBytes, opts.WithRandomBytes)
})
t.Run("WithKeyEncoding", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithKeyEncoding(KeyEncoding_Bytes))
require.NoError(err)
require.NotNil(opts)
assert.Equal(KeyEncoding_Bytes, opts.WithKeyEncoding)
})
t.Run("WithWrappedKeyEncoding", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithWrappedKeyEncoding(KeyEncoding_Bytes))
require.NoError(err)
require.NotNil(opts)
assert.Equal(KeyEncoding_Bytes, opts.WithWrappedKeyEncoding)
})
t.Run("WithDisallowEnvVars", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
opts, err := GetOpts(WithDisallowEnvVars(true))
require.NoError(err)
require.NotNil(opts)
assert.True(opts.WithDisallowEnvVars)
})
}
func testOptionWithError(t *testing.T) Option {
t.Helper()
return func() interface{} {
return OptionFunc(func(o *Options) error {
return errors.New("option error")
})
}
}