File: options_test.go

package info (click to toggle)
golang-github-smallstep-crypto 0.57.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,284 kB
  • sloc: sh: 53; makefile: 36
file content (232 lines) | stat: -rw-r--r-- 6,714 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package apiv1

import (
	"context"
	"crypto"
	"errors"
	"fmt"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

type fakeKM struct{}

func (f *fakeKM) GetPublicKey(req *GetPublicKeyRequest) (crypto.PublicKey, error) {
	return nil, NotImplementedError{}
}
func (f *fakeKM) CreateKey(req *CreateKeyRequest) (*CreateKeyResponse, error) {
	return nil, NotImplementedError{}
}
func (f *fakeKM) CreateSigner(req *CreateSignerRequest) (crypto.Signer, error) {
	return nil, NotImplementedError{}
}
func (f *fakeKM) Close() error { return NotImplementedError{} }

func TestMain(m *testing.M) {
	Register(Type("fake"), func(ctx context.Context, opts Options) (KeyManager, error) {
		return &fakeKM{}, nil
	})
	os.Exit(m.Run())
}

func TestOptions_Validate(t *testing.T) {
	tests := []struct {
		name    string
		options *Options
		wantErr bool
	}{
		{"nil", nil, false},
		{"softkms", &Options{Type: "softkms"}, false},
		{"cloudkms", &Options{Type: "cloudkms"}, false},
		{"awskms", &Options{Type: "awskms"}, false},
		{"sshagentkms", &Options{Type: "sshagentkms"}, false},
		{"pkcs11", &Options{Type: "pkcs11"}, false},
		{"unsupported", &Options{Type: "unsupported"}, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := tt.options.Validate(); (err != nil) != tt.wantErr {
				t.Errorf("Options.Validate() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestOptions_GetType(t *testing.T) {
	type fields struct {
		Type Type
		URI  string
	}
	tests := []struct {
		name    string
		fields  fields
		want    Type
		wantErr bool
	}{
		{"ok", fields{PKCS11, ""}, PKCS11, false},
		{"ok default", fields{"", ""}, SoftKMS, false},
		{"ok by uri", fields{"", "PKCS11:foo=bar"}, PKCS11, false},
		{"ok by uri", fields{"", "softkms:foo=bar"}, SoftKMS, false},
		{"ok by uri", fields{"", "cloudkms:foo=bar"}, CloudKMS, false},
		{"ok by uri", fields{"", "awskms:foo=bar"}, AmazonKMS, false},
		{"ok by uri", fields{"", "pkcs11:foo=bar"}, PKCS11, false},
		{"ok by uri", fields{"", "yubikey:foo=bar"}, YubiKey, false},
		{"ok by uri", fields{"", "sshagentkms:foo=bar"}, SSHAgentKMS, false},
		{"ok by uri", fields{"", "azurekms:foo=bar"}, AzureKMS, false},
		{"fail uri", fields{"", "foo=bar"}, DefaultKMS, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			o := &Options{
				Type: tt.fields.Type,
				URI:  tt.fields.URI,
			}
			got, err := o.GetType()
			if (err != nil) != tt.wantErr {
				t.Errorf("Options.GetType() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("Options.GetType() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestErrNotImplemented_Error(t *testing.T) {
	type fields struct {
		msg string
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{"default", fields{}, "not implemented"},
		{"custom", fields{"custom message: not implemented"}, "custom message: not implemented"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			e := NotImplementedError{
				Message: tt.fields.msg,
			}
			if got := e.Error(); got != tt.want {
				t.Errorf("ErrNotImplemented.Error() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestErrAlreadyExists_Error(t *testing.T) {
	type fields struct {
		msg string
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{"default", fields{}, "already exists"},
		{"custom", fields{"custom message: key already exists"}, "custom message: key already exists"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			e := AlreadyExistsError{
				Message: tt.fields.msg,
			}
			if got := e.Error(); got != tt.want {
				t.Errorf("ErrAlreadyExists.Error() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestNotFoundError_Error(t *testing.T) {
	type fields struct {
		msg string
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{"default", fields{}, "not found"},
		{"custom", fields{"custom message: not found"}, "custom message: not found"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			e := NotFoundError{
				Message: tt.fields.msg,
			}
			if got := e.Error(); got != tt.want {
				t.Errorf("ErrAlreadyExists.Error() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestTypeOf(t *testing.T) {
	type args struct {
		rawuri string
	}
	tests := []struct {
		name    string
		args    args
		want    Type
		wantErr bool
	}{
		{"ok softkms", args{"softkms:foo=bar"}, SoftKMS, false},
		{"ok cloudkms", args{"CLOUDKMS:"}, CloudKMS, false},
		{"ok amazonkms", args{"awskms:foo=bar"}, AmazonKMS, false},
		{"ok pkcs11", args{"PKCS11:foo=bar"}, PKCS11, false},
		{"ok yubikey", args{"yubikey:foo=bar"}, YubiKey, false},
		{"ok sshagentkms", args{"sshagentkms:"}, SSHAgentKMS, false},
		{"ok azurekms", args{"azurekms:foo=bar"}, AzureKMS, false},
		{"ok capi", args{"CAPI:foo-bar"}, CAPIKMS, false},
		{"ok tpmkms", args{"tpmkms:"}, TPMKMS, false},
		{"ok registered", args{"FAKE:"}, Type("fake"), false},
		{"fail empty", args{""}, DefaultKMS, true},
		{"fail parse", args{"softkms"}, DefaultKMS, true},
		{"fail kms", args{"foobar:foo=bar"}, DefaultKMS, true},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := TypeOf(tt.args.rawuri)
			if (err != nil) != tt.wantErr {
				t.Errorf("TypeOf() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("TypeOf() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestError_Is(t *testing.T) {
	tests := []struct {
		name   string
		err    error
		target error
		want   bool
	}{
		{"ok not implemented", NotImplementedError{}, NotImplementedError{}, true},
		{"ok not implemented with message", NotImplementedError{Message: "something"}, NotImplementedError{}, true},
		{"ok already exists", AlreadyExistsError{}, AlreadyExistsError{}, true},
		{"ok already exists with message", AlreadyExistsError{Message: "something"}, AlreadyExistsError{}, true},
		{"ok not found", NotFoundError{}, NotFoundError{}, true},
		{"ok not found with message", NotFoundError{Message: "something"}, NotFoundError{}, true},
		{"fail not implemented", errors.New("not implemented"), NotImplementedError{}, false},
		{"fail already exists", errors.New("already exists"), AlreadyExistsError{}, false},
		{"fail not found", errors.New("not found"), NotFoundError{}, false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.want, errors.Is(tt.err, tt.target))
			assert.Equal(t, tt.want, errors.Is(fmt.Errorf("wrap 1: %w", tt.err), tt.target))
			assert.Equal(t, tt.want, errors.Is(fmt.Errorf("wrap 1: %w", fmt.Errorf("wrap 2: %w", tt.err)), tt.target))
		})
	}
}