-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathformat_test.go
552 lines (433 loc) · 21 KB
/
format_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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package conflate
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
cryptoName = "crypto"
htmlName = "html"
regexName = "regex"
xmlName = "xml"
)
func TestFormatErrors_Get(t *testing.T) {
formatErrs.clear()
defer func() { formatErrs.clear() }()
formatErrs["name#value"] = errTest
err := formatErrs.get("name", "value")
assert.Equal(t, errTest, err)
}
func TestFormatErrors_Add(t *testing.T) {
formatErrs.clear()
defer func() { formatErrs.clear() }()
formatErrs.add("name", "value", errTest)
assert.Equal(t, errTest, formatErrs["name#value"])
}
func TestFormatErrors_Clear(t *testing.T) {
formatErrs.clear()
defer func() { formatErrs.clear() }()
formatErrs["stuff"] = nil
formatErrs.clear()
assert.Empty(t, formatErrs)
}
// --------
func TestXmlFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := xmlName
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "the value is not a string")
}
func TestXmlFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := xmlName
givenValue := "<test>Value</test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}
func TestXmlFormatCheckerIsFormat_NotValid(t *testing.T) {
givenName := xmlName
givenValue := "<test1>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to parse xml")
}
// --------
func TestXmlTemplateFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := xmlName
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "the value is not a string")
}
func TestXmlTemplateFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := xmlName
givenValue := "<test>{{.Value}}</test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}
func TestXmlTemplateFormatCheckerIsFormat_NotValid(t *testing.T) {
givenName := xmlName
givenValue := "<test>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newXMLTemplateFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to parse xml")
}
// --------
func TestHtmlFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := htmlName
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newHTMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "the value is not a string")
}
func TestHtmlFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := htmlName
givenValue := "<html>{{.Value}}</html>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newHTMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}
/*
func TestHtmlFormatCheckerIsFormat_NotValid(t *testing.T) {
givenName := "html"
givenValue := "<!DOCTYPE wibble> </html/>"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newHTMLFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Failed to parse html")
}
*/
// --------
func testCryptoFormatCheckerIsFormatNotString(t *testing.T, cryptoType cryptoType) {
t.Helper()
givenName := cryptoName
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newCryptoFormatChecker(givenName, cryptoType)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "the value is not a string")
}
func TestCryptoFormatCheckerIsFormat_NotString(t *testing.T) {
testCryptoFormatCheckerIsFormatNotString(t, pkcs1PrivateKey)
testCryptoFormatCheckerIsFormatNotString(t, pkcs1PublicKey)
testCryptoFormatCheckerIsFormatNotString(t, pkcs8PrivateKey)
testCryptoFormatCheckerIsFormatNotString(t, pkixPublicKey)
testCryptoFormatCheckerIsFormatNotString(t, x509Certificate)
}
func testCryptoFormatCheckerIsFormatNotValid(t *testing.T, cryptoType cryptoType) {
t.Helper()
givenName := cryptoName
givenValue := "dGhpcyBpcyBub3QgYSB2YWxpZCBjZXJ0aWZpY2F0ZQo="
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newCryptoFormatChecker(givenName, cryptoType)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to parse")
}
func TestCryptoFormatCheckerIsFormat_NotValid(t *testing.T) {
testCryptoFormatCheckerIsFormatNotValid(t, pkcs1PrivateKey)
testCryptoFormatCheckerIsFormatNotValid(t, pkcs1PublicKey)
testCryptoFormatCheckerIsFormatNotValid(t, pkcs8PrivateKey)
testCryptoFormatCheckerIsFormatNotValid(t, pkixPublicKey)
testCryptoFormatCheckerIsFormatNotValid(t, x509Certificate)
}
func testCryptoFormatCheckerIsFormatValid(t *testing.T, cryptoType cryptoType, givenValue string) {
t.Helper()
givenName := cryptoName
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newCryptoFormatChecker(givenName, cryptoType)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}
//nolint:funlen // test case
func TestCryptoFormatCheckerIsFormat_Valid(t *testing.T) {
testCryptoFormatCheckerKeys := []struct {
ct cryptoType
val string
}{
{
ct: pkcs1PrivateKey,
val: `MIIEogIBAAKCAQEA+4pTx7LDtTdXrMCYYZUHbLkIyOV/DPwJVJo4V7CPjv9CxKT/+1s75Zf0Ek64I6UFoK39Adlneqnhw6OShkM9UlBXOWVN+eVucKo4WeuiEwYgmyPpFq3yYt1nVIZMeNcho9AramnCS2D0wSXdSQLCtpGTeKbPklDS8CH6Br/fXTgcMQs4PlRuJt6utggjTHfk4cuQUMB1xuvqCdNPvlz/IQRb+pkEcnE5tw41WTOoSySV7RKThsIy3fP14UZrse+f6rZ5rqm5XreHV7FR4eN04gY06Kf2kPPPoqDX5tsRS7MGsK1tuSabahSRQzpmRUkXQBx0H5FusmyUZvkBxH0KRQIDAQABAoIBAQCmJUAhb5qFYACxQqVkDyPQVIfQ6oV49iDTmOoOQbkRItnfOX07cY+mny+/x+8o3B9TepjbV9ZZ10wgBTMLK+6dKCP07U0L+tdv439FJbbwCeQPlTCookyvsL5Hvk1UpAS8RwvmReToFSqVSYVYbFJgdNeLoeMJKgmqQ0b6nlHTJ6V6anwjWHN9yWB/2T1q0VALFMNeQS3YHjrI3ldzQ37KuyPWWahh+OTXcFP22e45aJ68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Z9CJoMQCcvDf0dy
ABbXee2b/9+HlBE78Fye+BGTi2cK//Vi4i+j6UFGtF8Ougsde1J8q3Dir1m/u6TXI/N1etMOifNi
aw3GIiLIpfoGAAaUVS8qcB9G94PUWIKSze9cHb0/FF1J3uTdu5vqz5CWaSuHtmj7nT0tWxqT0AiW
boU2Ixx0QOjn5XoHskWdY065eOc14bSGCHutiiAYBwJMnGol18X7D/hUKEiAfggG8927rnRGzGpN
otero2Uksx3F/9g6PtyQx2WRah4A8eA9kQ==
-----END CERTIFICATE-----`,
},
}
for _, c := range testCryptoFormatCheckerKeys {
testCryptoFormatCheckerIsFormatValid(t, c.ct, c.val)
}
}
func TestCryptoFormatCheckerIsFormat_NotBase64(t *testing.T) {
givenName := cryptoName
givenValue := "not base-64"
cryptoType := cryptoType(9999)
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newCryptoFormatChecker(givenName, cryptoType)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to decode the data")
}
func TestCryptoFormatCheckerIsFormat_UnsupportedType(t *testing.T) {
givenName := cryptoName
givenValue := ""
cryptoType := cryptoType(9999)
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newCryptoFormatChecker(givenName, cryptoType)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "unsupported type")
}
// --------
func TestRegexFormatCheckerIsFormat_NotString(t *testing.T) {
givenName := regexName
givenValue := 1
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newRegexFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "the value is not a string")
}
func TestRegexFormatCheckerIsFormat_Valid(t *testing.T) {
givenName := regexName
givenValue := "^.*$"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newRegexFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.True(t, result)
err := formatErrs.get(name, givenValue)
assert.Nil(t, err)
}
func TestRegexFormatCheckerIsFormat_NotValid(t *testing.T) {
givenName := regexName
givenValue := "^(.*$"
formatErrs.clear()
defer func() { formatErrs.clear() }()
name, checker := newRegexFormatChecker(givenName)
assert.Equal(t, givenName, name)
result := checker.IsFormat(givenValue)
assert.False(t, result)
err := formatErrs.get(name, givenValue)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "failed to parse regular expression")
}