-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathinit_test.go
More file actions
204 lines (196 loc) · 4.48 KB
/
init_test.go
File metadata and controls
204 lines (196 loc) · 4.48 KB
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
package ca
import (
"flag"
"reflect"
"testing"
"github.com/smallstep/certificates/pki"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
_ "go.step.sm/crypto/kms/azurekms"
)
func Test_processDNSValue(t *testing.T) {
tests := []struct {
name string
dnsValue string
want []string
wantErr bool
}{
{
name: "fail/empty",
dnsValue: "",
want: nil,
wantErr: true,
},
{
name: "fail/empty-multiple",
dnsValue: ",,",
want: nil,
wantErr: true,
},
{
name: "fail/dns",
dnsValue: "ca.smallstep.com:8443",
want: nil,
wantErr: true,
},
{
name: "fail/ipv4",
dnsValue: "127.0.0.1:8080",
want: nil,
wantErr: true,
},
{
name: "fail/ipv6",
dnsValue: ":::1",
want: nil,
wantErr: true,
},
{
name: "ok/dns",
dnsValue: "ca.smallstep.com",
want: []string{"ca.smallstep.com"},
wantErr: false,
},
{
name: "ok/multi-dns",
dnsValue: "ca.smallstep.com,ca.localhost",
want: []string{"ca.smallstep.com", "ca.localhost"},
wantErr: false,
},
{
name: "ok/multi-dns-with-skip",
dnsValue: "ca.smallstep.com,ca.localhost,,test.localhost",
want: []string{"ca.smallstep.com", "ca.localhost", "test.localhost"},
wantErr: false,
},
{
name: "ok/multi-space-dns",
dnsValue: "ca.smallstep.com ca.localhost",
want: []string{"ca.smallstep.com", "ca.localhost"},
wantErr: false,
},
{
name: "ok/ipv4",
dnsValue: "127.0.0.1",
want: []string{"127.0.0.1"},
wantErr: false,
},
{
name: "ok/multi-ipv4",
dnsValue: "127.0.0.1,127.0.0.2",
want: []string{"127.0.0.1", "127.0.0.2"},
wantErr: false,
},
{
name: "ok/ipv6-no-brackets",
dnsValue: "::1",
want: []string{"::1"},
wantErr: false,
},
{
name: "ok/multi-ipv6-no-brackets",
dnsValue: "::1,::2",
want: []string{"::1", "::2"},
wantErr: false,
},
{
name: "ok/ipv6-with-brackets",
dnsValue: "[::1]",
want: []string{"::1"},
wantErr: false,
},
{
name: "ok/multi-ipv6-with-brackets",
dnsValue: "[::1] [::2]",
want: []string{"::1", "::2"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := processDNSValue(tt.dnsValue)
if (err != nil) != tt.wantErr {
t.Errorf("processDNSValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("processDNSValue() = %v, want %v", got, tt.want)
}
})
}
}
func Test_promptDeploymentType(t *testing.T) {
app := &cli.App{}
tests := []struct {
name string
deploymentType string
isRA bool
want pki.DeploymentType
wantErr string
}{
{
name: "ok/standalone",
deploymentType: "standalone",
isRA: false,
want: pki.StandaloneDeployment,
},
{
name: "ok/standalone-ra",
deploymentType: "standalone",
isRA: true,
want: pki.StandaloneDeployment,
},
{
name: "ok/hosted",
deploymentType: "hosted",
isRA: false,
want: pki.HostedDeployment,
},
{
name: "fail/linked-deprecated",
deploymentType: "linked",
isRA: false,
wantErr: "Creating new Linked CAs is no longer supported in open-source step-ca",
},
{
name: "fail/linked-deprecated-ra",
deploymentType: "linked",
isRA: true,
wantErr: "Creating new Linked CAs is no longer supported in open-source step-ca",
},
{
name: "fail/invalid-type",
deploymentType: "invalid",
isRA: false,
wantErr: "invalid value",
},
{
name: "fail/invalid-type-ra",
deploymentType: "invalid",
isRA: true,
wantErr: "invalid value",
},
{
name: "fail/hosted-ra",
deploymentType: "hosted",
isRA: true,
wantErr: "invalid value",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
set := flag.NewFlagSet("test", 0)
_ = set.String("deployment-type", "", "")
ctx := cli.NewContext(app, set, nil)
_ = ctx.Set("deployment-type", tt.deploymentType)
got, err := promptDeploymentType(ctx, tt.isRA)
if tt.wantErr != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}