1
16
17 package util
18
19 import (
20 "strings"
21 "testing"
22 )
23
24 func TestGenerateBootstrapToken(t *testing.T) {
25 token, err := GenerateBootstrapToken()
26 if err != nil {
27 t.Fatalf("GenerateBootstrapToken returned an unexpected error: %+v", err)
28 }
29 if !IsValidBootstrapToken(token) {
30 t.Errorf("GenerateBootstrapToken didn't generate a valid token: %q", token)
31 }
32 }
33
34 func TestRandBytes(t *testing.T) {
35 var randTest = []int{
36 0,
37 1,
38 2,
39 3,
40 100,
41 }
42
43 for _, rt := range randTest {
44 actual, err := randBytes(rt)
45 if err != nil {
46 t.Errorf("failed randBytes: %v", err)
47 }
48 if len(actual) != rt {
49 t.Errorf("failed randBytes:\n\texpected: %d\n\t actual: %d\n", rt, len(actual))
50 }
51 }
52 }
53
54 func TestTokenFromIDAndSecret(t *testing.T) {
55 var tests = []struct {
56 id string
57 secret string
58 expected string
59 }{
60 {"foo", "bar", "foo.bar"},
61 {"abcdef", "abcdef0123456789", "abcdef.abcdef0123456789"},
62 {"h", "b", "h.b"},
63 }
64 for _, rt := range tests {
65 actual := TokenFromIDAndSecret(rt.id, rt.secret)
66 if actual != rt.expected {
67 t.Errorf(
68 "failed TokenFromIDAndSecret:\n\texpected: %s\n\t actual: %s",
69 rt.expected,
70 actual,
71 )
72 }
73 }
74 }
75
76 func TestIsValidBootstrapToken(t *testing.T) {
77 var tests = []struct {
78 token string
79 expected bool
80 }{
81 {token: "", expected: false},
82 {token: ".", expected: false},
83 {token: "1234567890123456789012", expected: false},
84 {token: "12345.1234567890123456", expected: false},
85 {token: ".1234567890123456", expected: false},
86 {token: "123456.", expected: false},
87 {token: "123456:1234567890.123456", expected: false},
88 {token: "abcdef:1234567890123456", expected: false},
89 {token: "Abcdef.1234567890123456", expected: false},
90 {token: "123456.AABBCCDDEEFFGGHH", expected: false},
91 {token: "123456.AABBCCD-EEFFGGHH", expected: false},
92 {token: "abc*ef.1234567890123456", expected: false},
93 {token: "abcdef.1234567890123456", expected: true},
94 {token: "123456.aabbccddeeffgghh", expected: true},
95 {token: "ABCDEF.abcdef0123456789", expected: false},
96 {token: "abcdef.abcdef0123456789", expected: true},
97 {token: "123456.1234560123456789", expected: true},
98 }
99 for _, rt := range tests {
100 actual := IsValidBootstrapToken(rt.token)
101 if actual != rt.expected {
102 t.Errorf(
103 "failed IsValidBootstrapToken for the token %q\n\texpected: %t\n\t actual: %t",
104 rt.token,
105 rt.expected,
106 actual,
107 )
108 }
109 }
110 }
111
112 func TestIsValidBootstrapTokenID(t *testing.T) {
113 var tests = []struct {
114 tokenID string
115 expected bool
116 }{
117 {tokenID: "", expected: false},
118 {tokenID: "1234567890123456789012", expected: false},
119 {tokenID: "12345", expected: false},
120 {tokenID: "Abcdef", expected: false},
121 {tokenID: "ABCDEF", expected: false},
122 {tokenID: "abcdef.", expected: false},
123 {tokenID: "abcdef", expected: true},
124 {tokenID: "123456", expected: true},
125 }
126 for _, rt := range tests {
127 actual := IsValidBootstrapTokenID(rt.tokenID)
128 if actual != rt.expected {
129 t.Errorf(
130 "failed IsValidBootstrapTokenID for the token %q\n\texpected: %t\n\t actual: %t",
131 rt.tokenID,
132 rt.expected,
133 actual,
134 )
135 }
136 }
137 }
138
139 func TestBootstrapTokenSecretName(t *testing.T) {
140 var tests = []struct {
141 tokenID string
142 expected string
143 }{
144 {"foo", "bootstrap-token-foo"},
145 {"bar", "bootstrap-token-bar"},
146 {"", "bootstrap-token-"},
147 {"abcdef", "bootstrap-token-abcdef"},
148 }
149 for _, rt := range tests {
150 actual := BootstrapTokenSecretName(rt.tokenID)
151 if actual != rt.expected {
152 t.Errorf(
153 "failed BootstrapTokenSecretName:\n\texpected: %s\n\t actual: %s",
154 rt.expected,
155 actual,
156 )
157 }
158 }
159 }
160
161 func TestValidateBootstrapGroupName(t *testing.T) {
162 tests := []struct {
163 name string
164 input string
165 valid bool
166 }{
167 {"valid", "system:bootstrappers:foo", true},
168 {"valid nested", "system:bootstrappers:foo:bar:baz", true},
169 {"valid with dashes and number", "system:bootstrappers:foo-bar-42", true},
170 {"invalid uppercase", "system:bootstrappers:Foo", false},
171 {"missing prefix", "foo", false},
172 {"prefix with no body", "system:bootstrappers:", false},
173 {"invalid spaces", "system:bootstrappers: ", false},
174 {"invalid asterisk", "system:bootstrappers:*", false},
175 {"trailing colon", "system:bootstrappers:foo:", false},
176 {"trailing dash", "system:bootstrappers:foo-", false},
177 {"script tags", "system:bootstrappers:<script> alert(\"scary?!\") </script>", false},
178 {"too long", "system:bootstrappers:" + strings.Repeat("x", 300), false},
179 }
180 for _, test := range tests {
181 err := ValidateBootstrapGroupName(test.input)
182 if err != nil && test.valid {
183 t.Errorf("test %q: ValidateBootstrapGroupName(%q) returned unexpected error: %v", test.name, test.input, err)
184 }
185 if err == nil && !test.valid {
186 t.Errorf("test %q: ValidateBootstrapGroupName(%q) was supposed to return an error but didn't", test.name, test.input)
187 }
188 }
189 }
190
191 func TestValidateUsages(t *testing.T) {
192 tests := []struct {
193 name string
194 input []string
195 valid bool
196 }{
197 {"valid of signing", []string{"signing"}, true},
198 {"valid of authentication", []string{"authentication"}, true},
199 {"all valid", []string{"authentication", "signing"}, true},
200 {"single invalid", []string{"authentication", "foo"}, false},
201 {"all invalid", []string{"foo", "bar"}, false},
202 }
203
204 for _, test := range tests {
205 err := ValidateUsages(test.input)
206 if err != nil && test.valid {
207 t.Errorf("test %q: ValidateUsages(%v) returned unexpected error: %v", test.name, test.input, err)
208 }
209 if err == nil && !test.valid {
210 t.Errorf("test %q: ValidateUsages(%v) was supposed to return an error but didn't", test.name, test.input)
211 }
212 }
213 }
214
View as plain text