-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_test.go
358 lines (340 loc) · 7.17 KB
/
hash_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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package is
import "testing"
// TestBase64 tests Base64 function.
func TestBase64(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "Empty string",
input: "",
want: false,
},
{
name: "Valid standard base64",
input: "SGVsbG8sIHdvcmxkIQ==",
want: true,
},
{
name: "Not valid standard base64 (missing padding)",
input: "SGVsbG8sIHdvcmxkIQ",
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Base64(test.input)
if got != test.want {
t.Errorf("Base64(%q) = %v; want %v",
test.input, got, test.want)
}
})
}
}
// TestBase64URL tests Base64URL function.
func TestBase64URL(t *testing.T) {
tests := []struct {
name string
in string
want bool
}{
{
name: "Valid Base64URL string without padding",
in: "SGVsbG8sIHdvcmxkIQ",
want: true,
},
{
name: "Valid Base64URL string with padding",
in: "SGVsbG8sIHdvcmxkIQ==",
want: true,
},
{
name: "Invalid Base64URL string with incorrect padding",
in: "SGVsbG8sIHdvcmxkIQ===",
want: false,
},
{
name: "Invalid Base64URL string with invalid characters",
in: "SGVsbG8sIHdvcmxkIQ@#",
want: false,
},
{
name: "Empty string",
in: "",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Base64URL(tt.in)
if got != tt.want {
t.Errorf("Base64URL(%q) = %v; want %v", tt.in, got, tt.want)
}
})
}
//validBase64URL := "SGVsbG8sIHdvcmxkIQ"
//invalidBase64URL := "notabase64url"
//
//validResult := Base64URL(validBase64URL)
//if !validResult {
// t.Errorf("Expected valid Base64URL to return true, got false")
//}
//
//invalidResult := Base64URL(invalidBase64URL)
//if invalidResult {
// t.Errorf("Expected invalid Base64URL to return false, got true")
//}
}
// TestHex tests Hex function.
func TestHex(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "Valid hex string",
input: "deadbeef",
want: true,
},
{
name: "Valid hex string with uppercase",
input: "DEADBEEF",
want: true,
},
{
name: "Valid hex string with 0x prefix",
input: "0xdeadbeef",
want: true,
},
{
name: "Invalid hex string",
input: "deadbefg",
want: false,
},
{
name: "Invalid hex string with 0x prefix",
input: "0xdeadbefg",
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Hex(test.input)
if got != test.want {
t.Errorf("Hex(%q) = %v; want %v",
test.input, got, test.want)
}
})
}
}
// TestHexColor tests HexColor function.
func TestHexColor(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "Valid hex color",
input: "#ff7377",
want: true,
},
{
name: "Valid hex color without # prefix",
input: "ff7377",
want: true,
},
{
name: "Valid hex color with uppercase",
input: "#FF7377",
want: true,
},
{
name: "Valid hex color but with 0x prefix",
input: "0xff7377",
want: false,
},
{
name: "Invalid hex color",
input: "#ff7377ff",
want: false,
},
{
name: "Invalid hex color with 0x prefix",
input: "0xff7377ff",
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := HexColor(test.input)
if got != test.want {
t.Errorf("HexColor(%q) = %v; want %v",
test.input, got, test.want)
}
})
}
}
// TestRGBColor tests RGBColor function.
func TestRGBColor(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "Valid RGB color",
input: "rgb(255, 115, 119)",
want: true,
},
{
name: "Valid RGB color with spaces",
input: "rgb( 255, 115, 119 )",
want: true,
},
{
name: "Valid RGB color with tabs",
input: "rgb(\t255,\t115,\t119\t)",
want: true,
},
{
name: "Valid RGB color with uppercase",
input: "RGB(255, 115, 119)",
want: true,
},
{
name: "Invalid RGB color",
input: "rgb(255, 115, 119, 255)",
want: false,
},
{
name: "Invalid RGB color with spaces",
input: "rgb( 255, 115, 119, 255 )",
want: false,
},
{
name: "Invalid RGB color with tabs",
input: "rgb(\t255,\t115,\t119,\t255\t)",
want: false,
},
{
name: "Invalid RGB color with uppercase",
input: "RGB(255, 115, 119, 255)",
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := RGBColor(test.input)
if got != test.want {
t.Errorf("RGBColor(%q) = %v; want %v",
test.input, got, test.want)
}
})
}
}
// TestBin tests Bin function.
func TestBin(t *testing.T) {
testCases := []struct {
name string
v string
want bool
}{
{
name: "Valid Binary String",
v: "1010",
want: true,
},
{
name: "Valid Binary String With Prefix",
v: "0b1010",
want: true,
},
{
name: "Invalid Binary String",
v: "10201",
want: false,
},
{
name: "String with Non-Binary Characters",
v: "abc101",
want: false,
},
{
name: "Empty String",
v: "",
want: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := Bin(tc.v)
if got != tc.want {
t.Errorf("Bin(%q) = %v; want %v", tc.v, got, tc.want)
}
})
}
}
// TestJWT tests JWT function.
func TestJWT(t *testing.T) {
tests := []struct {
name string
in string
want bool
}{
{
name: "Valid JWT with secret base64 encoded",
in: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlJ1c3NpYW5zIGFyZSBtYW5pYWNzIGFuZCBmcmVha3MiLCJpYXQiOjE1MTYyMzkwMjJ9.3tG1rYCCxE7umetsvJPEYAZWCMQy-uwY5hA7QGvVqnM",
want: true,
},
{
name: "Valid JWT",
in: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910",
want: true,
},
{
name: "Not valid JWT",
in: "eyHHGJHgJciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ZSI6IlB1dGl",
want: false,
},
{
name: "All parts are valid Base64URL strings",
in: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
want: true,
},
{
name: "One part is not a valid Base64URL string",
in: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.#notbase64url#.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
want: false,
},
{
name: "Multiple parts are not valid Base64URL strings",
in: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.+notbase64url+.$notbase64url$",
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := JWT(tc.in)
if got != tc.want {
t.Errorf("JWT(%q) = %v; want %v", tc.in, got, tc.want)
}
})
}
}
// TestMD5 tests MD5 function.
func TestMD5(t *testing.T) {
validMD5 := "d41d8cd98f00b204e9800998ecf8427e"
invalidMD5 := "notamd5hash"
validResult := MD5(validMD5)
if !validResult {
t.Errorf("Expected valid MD5 to return true, got false")
}
invalidResult := MD5(invalidMD5)
if invalidResult {
t.Errorf("Expected invalid MD5 to return false, got true")
}
}