-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathoptions_test.go
156 lines (146 loc) · 4.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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")
})
}
}
func TestParsePaths(t *testing.T) {
type options struct {
optionA string
optionB string
}
test := options{
optionA: "Hello, World!",
optionB: "file://test-fixtures/secret.txt",
}
if err := ParsePaths(&test.optionA, &test.optionB); err != nil {
t.Fatal(err)
}
if test.optionB != "Top Secret" {
t.Fatalf("expected TopSecret, got %s", test.optionB)
}
// Test nil handling
test = options{
optionA: "file://test-fixtures/secret.txt",
}
if err := ParsePaths(nil, &test.optionA, nil); err != nil {
t.Fatal(err)
}
if test.optionA != "Top Secret" {
t.Fatalf("expected TopSecret, got %s", test.optionB)
}
// Test errors and error atomicity
test = options{
optionA: "file://test-fixtures/secret.txt",
optionB: "file://test-fixtures/missing.txt",
}
if err := ParsePaths(&test.optionA, &test.optionB); err == nil {
t.Fatal("expected error but didn't get one")
}
if test.optionA != "file://test-fixtures/secret.txt" {
t.Fatalf("optionA was overwritten despite encountering an error")
}
}