Debian Codegraph @

pkg/validation/strfmt/default_test.go

raw
1 // Copyright 2015 go-swagger maintainers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package strfmt
16
17 import (
18 "encoding"
19 "encoding/base64"
20 "encoding/json"
21 "fmt"
22 "reflect"
23 "strings"
24 "testing"
25
26 "github.com/google/uuid"
27 "github.com/stretchr/testify/assert"
28 )
29
30 func TestFormatURI(t *testing.T) {
31 uri := URI("http://somewhere.com")
32 str := "http://somewhereelse.com"
33 testStringFormat(t, &uri, "uri", str, []string{}, []string{"somewhere.com"})
34 }
35
36 func TestFormatEmail(t *testing.T) {
37 email := Email("[email protected]")
38 str := string("[email protected]")
39 validEmails := []string{
40 "[email protected]",
41 "[email protected]",
42 "[email protected]",
43 `" "@example.com`,
44 `"Abc\@def"@example.com`,
45 `"Fred Bloggs"@example.com`,
46 `"Joe\\Blow"@example.com`,
47 `"Abc@def"@example.com`,
48 "customer/[email protected]",
49 "[email protected]",
50 "!def!xyz%[email protected]",
51 "[email protected]",
52 "!#$%&'*+-/=?^_`{}|[email protected]",
53 "Miles.O'[email protected]",
54 "postmaster@☁→❄→☃→☀→☺→☂→☹→✝.ws",
55 "root@localhost",
56 "john@com",
57 "[email protected]",
58 }
59
60 testStringFormat(t, &email, "email", str, validEmails, []string{"somebody@somewhere@com"})
61 }
62
63 func TestFormatHostname(t *testing.T) {
64 hostname := Hostname("somewhere.com")
65 str := string("somewhere.com")
66 veryLongStr := strings.Repeat("a", 256)
67 longStr := strings.Repeat("a", 64)
68 longAddrSegment := strings.Join([]string{"x", "y", longStr}, ".")
69 invalidHostnames := []string{
70 "somewhere.com!",
71 "[email protected]",
72 "1.1.1.1",
73 veryLongStr,
74 longAddrSegment,
75 // dashes
76 "www.example-.org",
77 "www.--example.org",
78 "-www.example.org",
79 "www-.example.org",
80 "www.d-.org",
81 "www.-d.org",
82 "www-",
83 "-www",
84 // other characters (not in symbols)
85 "www.ex ample.org",
86 "_www.example.org",
87 "www.ex;ample.org",
88 "www.example_underscored.org",
89 // short top-level domains
90 "www.詹姆斯.x",
91 "a.b.c.d",
92 "-xyz",
93 "xyz-",
94 "x.",
95 "a.b.c.dot-",
96 "a.b.c.é;ö",
97 }
98 validHostnames := []string{
99 "somewhere.com",
100 "888.com",
101 "a.com",
102 "a.b.com",
103 "a.b.c.com",
104 "a.b.c.d.com",
105 "a.b.c.d.e.com",
106 "1.com",
107 "1.2.com",
108 "1.2.3.com",
109 "1.2.3.4.com",
110 "99.domain.com",
111 "99.99.domain.com",
112 "1wwworg.example.com", // valid, per RFC1123
113 "1000wwworg.example.com",
114 "xn--bcher-kva.example.com", // puny encoded
115 "xn-80ak6aa92e.co",
116 "xn-80ak6aa92e.com",
117 "xn--ls8h.la",
118 "☁→❄→☃→☀→☺→☂→☹→✝.ws",
119 "www.example.onion",
120 "www.example.ôlà",
121 "ôlà.ôlà",
122 "ôlà.ôlà.ôlà",
123 "ex$ample",
124 "localhost",
125 "example",
126 "x",
127 "x-y",
128 "a.b.c.dot",
129 "www.example.org",
130 "a.b.c.d.e.f.g.dot",
131 // extended symbol alphabet
132 "ex=ample.com",
133 "<foo>",
134 "www.example-hyphenated.org",
135 // localized hostnames
136 "www.詹姆斯.org",
137 "www.élégigôö.org",
138 // long top-level domains
139 "www.詹姆斯.london",
140 }
141
142 testStringFormat(t, &hostname, "hostname", str, []string{}, invalidHostnames)
143 testStringFormat(t, &hostname, "hostname", str, validHostnames, []string{})
144 }
145
146 func TestFormatIPv4(t *testing.T) {
147 ipv4 := IPv4("192.168.254.1")
148 str := string("192.168.254.2")
149 testStringFormat(t, &ipv4, "ipv4", str, []string{
150 "127.0.0.1", // local
151 "192.168.1.1", // normal
152 "192.168.1.01", // leading zeros (compatibility with go < 1.17)
153 }, []string{"198.168.254.2.2"})
154 }
155
156 func TestFormatIPv6(t *testing.T) {
157 ipv6 := IPv6("::1")
158 str := string("::2")
159 // TODO: test ipv6 zones
160 testStringFormat(t, &ipv6, "ipv6", str, []string{}, []string{"127.0.0.1"})
161 }
162
163 func TestFormatCIDR(t *testing.T) {
164 cidr := CIDR("192.168.254.1/24")
165 str := string("192.168.254.2/24")
166 testStringFormat(t, &cidr, "cidr", str, []string{
167 "192.0.2.1/24", // normal
168 "192.0.02.1/24", // leading zeros (compatibility with go < 1.17)
169 "2001:db8:a0b:12f0::1/32",
170 }, []string{"198.168.254.2", "2001:db8:a0b:12f0::1"})
171 }
172
173 func TestFormatMAC(t *testing.T) {
174 mac := MAC("01:02:03:04:05:06")
175 str := string("06:05:04:03:02:01")
176 testStringFormat(t, &mac, "mac", str, []string{}, []string{"01:02:03:04:05"})
177 }
178
179 func TestFormatUUID3(t *testing.T) {
180 first3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhere.com"))
181 other3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhereelse.com"))
182 uuid3 := UUID3(first3.String())
183 str := other3.String()
184 testStringFormat(t, &uuid3, "uuid3", str, []string{}, []string{"not-a-uuid"})
185
186 // special case for zero UUID
187 var uuidZero UUID3
188 err := uuidZero.UnmarshalJSON([]byte(jsonNull))
189 assert.NoError(t, err)
190 assert.EqualValues(t, UUID3(""), uuidZero)
191 }
192
193 func TestFormatUUID4(t *testing.T) {
194 first4 := uuid.Must(uuid.NewRandom())
195 other4 := uuid.Must(uuid.NewRandom())
196 uuid4 := UUID4(first4.String())
197 str := other4.String()
198 testStringFormat(t, &uuid4, "uuid4", str, []string{}, []string{"not-a-uuid"})
199
200 // special case for zero UUID
201 var uuidZero UUID4
202 err := uuidZero.UnmarshalJSON([]byte(jsonNull))
203 assert.NoError(t, err)
204 assert.EqualValues(t, UUID4(""), uuidZero)
205 }
206
207 func TestFormatUUID5(t *testing.T) {
208 first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
209 other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
210 uuid5 := UUID5(first5.String())
211 str := other5.String()
212 testStringFormat(t, &uuid5, "uuid5", str, []string{}, []string{"not-a-uuid"})
213
214 // special case for zero UUID
215 var uuidZero UUID5
216 err := uuidZero.UnmarshalJSON([]byte(jsonNull))
217 assert.NoError(t, err)
218 assert.EqualValues(t, UUID5(""), uuidZero)
219 }
220
221 func TestFormatUUID(t *testing.T) {
222 first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
223 other5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhereelse.com"))
224 uuid := UUID(first5.String())
225 str := other5.String()
226 testStringFormat(t, &uuid, "uuid", str, []string{}, []string{"not-a-uuid"})
227
228 // special case for zero UUID
229 var uuidZero UUID
230 err := uuidZero.UnmarshalJSON([]byte(jsonNull))
231 assert.NoError(t, err)
232 assert.EqualValues(t, UUID(""), uuidZero)
233 }
234
235 func TestFormatISBN(t *testing.T) {
236 isbn := ISBN("0321751043")
237 str := string("0321751043")
238 testStringFormat(t, &isbn, "isbn", str, []string{}, []string{"836217463"}) // bad checksum
239 }
240
241 func TestFormatISBN10(t *testing.T) {
242 isbn10 := ISBN10("0321751043")
243 str := string("0321751043")
244 testStringFormat(t, &isbn10, "isbn10", str, []string{}, []string{"836217463"}) // bad checksum
245 }
246
247 func TestFormatISBN13(t *testing.T) {
248 isbn13 := ISBN13("978-0321751041")
249 str := string("978-0321751041")
250 testStringFormat(t, &isbn13, "isbn13", str, []string{}, []string{"978-0321751042"}) // bad checksum
251 }
252
253 func TestFormatHexColor(t *testing.T) {
254 hexColor := HexColor("#FFFFFF")
255 str := string("#000000")
256 testStringFormat(t, &hexColor, "hexcolor", str, []string{}, []string{"#fffffffz"})
257 }
258
259 func TestFormatRGBColor(t *testing.T) {
260 rgbColor := RGBColor("rgb(255,255,255)")
261 str := string("rgb(0,0,0)")
262 testStringFormat(t, &rgbColor, "rgbcolor", str, []string{}, []string{"rgb(300,0,0)"})
263 }
264
265 func TestFormatSSN(t *testing.T) {
266 ssn := SSN("111-11-1111")
267 str := string("999 99 9999")
268 testStringFormat(t, &ssn, "ssn", str, []string{}, []string{"999 99 999"})
269 }
270
271 func TestFormatCreditCard(t *testing.T) {
272 creditCard := CreditCard("4111-1111-1111-1111")
273 str := string("4012-8888-8888-1881")
274 testStringFormat(t, &creditCard, "creditcard", str, []string{}, []string{"9999-9999-9999-999"})
275 }
276
277 func TestFormatPassword(t *testing.T) {
278 password := Password("super secret stuff here")
279 testStringFormat(t, &password, "password", "super secret!!!", []string{"even more secret"}, []string{})
280 }
281
282 func TestFormatBase64(t *testing.T) {
283 const b64 string = "This is a byte array with unprintable chars, but it also isn"
284 str := base64.URLEncoding.EncodeToString([]byte(b64))
285 b := []byte(b64)
286 expected := Base64(b)
287 bj := []byte("\"" + str + "\"")
288
289 var subj Base64
290 err := subj.UnmarshalText([]byte(str))
291 assert.NoError(t, err)
292 assert.EqualValues(t, expected, subj)
293
294 b, err = subj.MarshalText()
295 assert.NoError(t, err)
296 assert.Equal(t, []byte(str), b)
297
298 var subj2 Base64
299 err = subj2.UnmarshalJSON(bj)
300 assert.NoError(t, err)
301 assert.EqualValues(t, expected, subj2)
302
303 b, err = subj2.MarshalJSON()
304 assert.NoError(t, err)
305 assert.Equal(t, bj, b)
306
307 testValid(t, "byte", str)
308 testInvalid(t, "byte", "ZWxpemFiZXRocG9zZXk") // missing pad char
309 }
310
311 type testableFormat interface {
312 encoding.TextMarshaler
313 encoding.TextUnmarshaler
314 json.Marshaler
315 json.Unmarshaler
316 fmt.Stringer
317 }
318
319 func testStringFormat(t *testing.T, what testableFormat, format, with string, validSamples, invalidSamples []string) {
320 testStringFormatWithRegistry(t, Default, what, format, with, validSamples, invalidSamples)
321 }
322
323 func testStringFormatWithRegistry(t *testing.T, registry Registry, what testableFormat, format, with string, validSamples, invalidSamples []string) {
324 // text encoding interface
325 b := []byte(with)
326 err := what.UnmarshalText(b)
327 assert.NoError(t, err)
328
329 val := reflect.Indirect(reflect.ValueOf(what))
330 strVal := val.String()
331 assert.Equalf(t, with, strVal, "[%s]UnmarshalText: expected %v and %v to be value equal", format, strVal, with)
332
333 b, err = what.MarshalText()
334 assert.NoError(t, err)
335 assert.Equalf(t, []byte(with), b, "[%s]MarshalText: expected %v and %v to be value equal as []byte", format, string(b), with)
336
337 // Stringer
338 strVal = what.String()
339 assert.Equalf(t, []byte(with), b, "[%s]String: expected %v and %v to be equal", strVal, with)
340
341 // JSON encoding interface
342 bj := []byte("\"" + with + "\"")
343 err = what.UnmarshalJSON(bj)
344 assert.NoError(t, err)
345 val = reflect.Indirect(reflect.ValueOf(what))
346 strVal = val.String()
347 assert.EqualValuesf(t, with, strVal, "[%s]UnmarshalJSON: expected %v and %v to be value equal", format, strVal, with)
348
349 b, err = what.MarshalJSON()
350 assert.NoError(t, err)
351 assert.Equalf(t, bj, b, "[%s]MarshalJSON: expected %v and %v to be value equal as []byte", format, string(b), with)
352
353 // validation with Registry
354 t.Run("valid", func(t *testing.T) {
355 for _, valid := range append(validSamples, with) {
356 t.Run(valid, func(t *testing.T) {
357 testValidWithRegistry(t, registry, format, valid)
358 })
359 }
360 })
361 t.Run("invalid", func(t *testing.T) {
362 for _, invalid := range invalidSamples {
363 t.Run(invalid, func(t *testing.T) {
364 testInvalidWithRegistry(t, registry, format, invalid)
365 })
366 }
367 })
368 }
369
370 func testValid(t *testing.T, name, value string) {
371 testValidWithRegistry(t, Default, name, value)
372 }
373
374 func testValidWithRegistry(t *testing.T, registry Registry, name, value string) {
375 ok := registry.Validates(name, value)
376 if !ok {
377 t.Errorf("expected %q of type %s to be valid", value, name)
378 }
379 }
380
381 func testInvalid(t *testing.T, name, value string) {
382 testInvalidWithRegistry(t, Default, name, value)
383 }
384
385 func testInvalidWithRegistry(t *testing.T, registry Registry, name, value string) {
386 ok := registry.Validates(name, value)
387 if ok {
388 t.Errorf("expected %q of type %s to be invalid", value, name)
389 }
390 }
391
392 func TestDeepCopyBase64(t *testing.T) {
393 b64 := Base64("ZWxpemFiZXRocG9zZXk=")
394 in := &b64
395
396 out := new(Base64)
397 in.DeepCopyInto(out)
398 assert.Equal(t, in, out)
399
400 out2 := in.DeepCopy()
401 assert.Equal(t, in, out2)
402
403 var inNil *Base64
404 out3 := inNil.DeepCopy()
405 assert.Nil(t, out3)
406 }
407
408 func TestDeepCopyURI(t *testing.T) {
409 uri := URI("http://somewhere.com")
410 in := &uri
411
412 out := new(URI)
413 in.DeepCopyInto(out)
414 assert.Equal(t, in, out)
415
416 out2 := in.DeepCopy()
417 assert.Equal(t, in, out2)
418
419 var inNil *URI
420 out3 := inNil.DeepCopy()
421 assert.Nil(t, out3)
422 }
423
424 func TestDeepCopyEmail(t *testing.T) {
425 email := Email("[email protected]")
426 in := &email
427
428 out := new(Email)
429 in.DeepCopyInto(out)
430 assert.Equal(t, in, out)
431
432 out2 := in.DeepCopy()
433 assert.Equal(t, in, out2)
434
435 var inNil *Email
436 out3 := inNil.DeepCopy()
437 assert.Nil(t, out3)
438 }
439
440 func TestDeepCopyHostname(t *testing.T) {
441 hostname := Hostname("somewhere.com")
442 in := &hostname
443
444 out := new(Hostname)
445 in.DeepCopyInto(out)
446 assert.Equal(t, in, out)
447
448 out2 := in.DeepCopy()
449 assert.Equal(t, in, out2)
450
451 var inNil *Hostname
452 out3 := inNil.DeepCopy()
453 assert.Nil(t, out3)
454 }
455
456 func TestDeepCopyIPv4(t *testing.T) {
457 ipv4 := IPv4("192.168.254.1")
458 in := &ipv4
459
460 out := new(IPv4)
461 in.DeepCopyInto(out)
462 assert.Equal(t, in, out)
463
464 out2 := in.DeepCopy()
465 assert.Equal(t, in, out2)
466
467 var inNil *IPv4
468 out3 := inNil.DeepCopy()
469 assert.Nil(t, out3)
470 }
471
472 func TestDeepCopyIPv6(t *testing.T) {
473 ipv6 := IPv6("::1")
474 in := &ipv6
475
476 out := new(IPv6)
477 in.DeepCopyInto(out)
478 assert.Equal(t, in, out)
479
480 out2 := in.DeepCopy()
481 assert.Equal(t, in, out2)
482
483 var inNil *IPv6
484 out3 := inNil.DeepCopy()
485 assert.Nil(t, out3)
486 }
487
488 func TestDeepCopyCIDR(t *testing.T) {
489 cidr := CIDR("192.0.2.1/24")
490 in := &cidr
491
492 out := new(CIDR)
493 in.DeepCopyInto(out)
494 assert.Equal(t, in, out)
495
496 out2 := in.DeepCopy()
497 assert.Equal(t, in, out2)
498
499 var inNil *CIDR
500 out3 := inNil.DeepCopy()
501 assert.Nil(t, out3)
502 }
503
504 func TestDeepCopyMAC(t *testing.T) {
505 mac := MAC("01:02:03:04:05:06")
506 in := &mac
507
508 out := new(MAC)
509 in.DeepCopyInto(out)
510 assert.Equal(t, in, out)
511
512 out2 := in.DeepCopy()
513 assert.Equal(t, in, out2)
514
515 var inNil *MAC
516 out3 := inNil.DeepCopy()
517 assert.Nil(t, out3)
518 }
519
520 func TestDeepCopyUUID(t *testing.T) {
521 first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
522 uuid := UUID(first5.String())
523 in := &uuid
524
525 out := new(UUID)
526 in.DeepCopyInto(out)
527 assert.Equal(t, in, out)
528
529 out2 := in.DeepCopy()
530 assert.Equal(t, in, out2)
531
532 var inNil *UUID
533 out3 := inNil.DeepCopy()
534 assert.Nil(t, out3)
535 }
536
537 func TestDeepCopyUUID3(t *testing.T) {
538 first3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("somewhere.com"))
539 uuid3 := UUID3(first3.String())
540 in := &uuid3
541
542 out := new(UUID3)
543 in.DeepCopyInto(out)
544 assert.Equal(t, in, out)
545
546 out2 := in.DeepCopy()
547 assert.Equal(t, in, out2)
548
549 var inNil *UUID3
550 out3 := inNil.DeepCopy()
551 assert.Nil(t, out3)
552 }
553
554 func TestDeepCopyUUID4(t *testing.T) {
555 first4 := uuid.Must(uuid.NewRandom())
556 uuid4 := UUID4(first4.String())
557 in := &uuid4
558
559 out := new(UUID4)
560 in.DeepCopyInto(out)
561 assert.Equal(t, in, out)
562
563 out2 := in.DeepCopy()
564 assert.Equal(t, in, out2)
565
566 var inNil *UUID4
567 out3 := inNil.DeepCopy()
568 assert.Nil(t, out3)
569 }
570
571 func TestDeepCopyUUID5(t *testing.T) {
572 first5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("somewhere.com"))
573 uuid5 := UUID5(first5.String())
574 in := &uuid5
575
576 out := new(UUID5)
577 in.DeepCopyInto(out)
578 assert.Equal(t, in, out)
579
580 out2 := in.DeepCopy()
581 assert.Equal(t, in, out2)
582
583 var inNil *UUID5
584 out3 := inNil.DeepCopy()
585 assert.Nil(t, out3)
586 }
587
588 func TestDeepCopyISBN(t *testing.T) {
589 isbn := ISBN("0321751043")
590 in := &isbn
591
592 out := new(ISBN)
593 in.DeepCopyInto(out)
594 assert.Equal(t, in, out)
595
596 out2 := in.DeepCopy()
597 assert.Equal(t, in, out2)
598
599 var inNil *ISBN
600 out3 := inNil.DeepCopy()
601 assert.Nil(t, out3)
602 }
603
604 func TestDeepCopyISBN10(t *testing.T) {
605 isbn10 := ISBN10("0321751043")
606 in := &isbn10
607
608 out := new(ISBN10)
609 in.DeepCopyInto(out)
610 assert.Equal(t, in, out)
611
612 out2 := in.DeepCopy()
613 assert.Equal(t, in, out2)
614
615 var inNil *ISBN10
616 out3 := inNil.DeepCopy()
617 assert.Nil(t, out3)
618 }
619
620 func TestDeepCopyISBN13(t *testing.T) {
621 isbn13 := ISBN13("978-0321751041")
622 in := &isbn13
623
624 out := new(ISBN13)
625 in.DeepCopyInto(out)
626 assert.Equal(t, in, out)
627
628 out2 := in.DeepCopy()
629 assert.Equal(t, in, out2)
630
631 var inNil *ISBN13
632 out3 := inNil.DeepCopy()
633 assert.Nil(t, out3)
634 }
635
636 func TestDeepCopyCreditCard(t *testing.T) {
637 creditCard := CreditCard("4111-1111-1111-1111")
638 in := &creditCard
639
640 out := new(CreditCard)
641 in.DeepCopyInto(out)
642 assert.Equal(t, in, out)
643
644 out2 := in.DeepCopy()
645 assert.Equal(t, in, out2)
646
647 var inNil *CreditCard
648 out3 := inNil.DeepCopy()
649 assert.Nil(t, out3)
650 }
651
652 func TestDeepCopySSN(t *testing.T) {
653 ssn := SSN("111-11-1111")
654 in := &ssn
655
656 out := new(SSN)
657 in.DeepCopyInto(out)
658 assert.Equal(t, in, out)
659
660 out2 := in.DeepCopy()
661 assert.Equal(t, in, out2)
662
663 var inNil *SSN
664 out3 := inNil.DeepCopy()
665 assert.Nil(t, out3)
666 }
667
668 func TestDeepCopyHexColor(t *testing.T) {
669 hexColor := HexColor("#FFFFFF")
670 in := &hexColor
671
672 out := new(HexColor)
673 in.DeepCopyInto(out)
674 assert.Equal(t, in, out)
675
676 out2 := in.DeepCopy()
677 assert.Equal(t, in, out2)
678
679 var inNil *HexColor
680 out3 := inNil.DeepCopy()
681 assert.Nil(t, out3)
682 }
683
684 func TestDeepCopyRGBColor(t *testing.T) {
685 rgbColor := RGBColor("rgb(255,255,255)")
686 in := &rgbColor
687
688 out := new(RGBColor)
689 in.DeepCopyInto(out)
690 assert.Equal(t, in, out)
691
692 out2 := in.DeepCopy()
693 assert.Equal(t, in, out2)
694
695 var inNil *RGBColor
696 out3 := inNil.DeepCopy()
697 assert.Nil(t, out3)
698 }
699
700 func TestDeepCopyPassword(t *testing.T) {
701 password := Password("super secret stuff here")
702 in := &password
703
704 out := new(Password)
705 in.DeepCopyInto(out)
706 assert.Equal(t, in, out)
707
708 out2 := in.DeepCopy()
709 assert.Equal(t, in, out2)
710
711 var inNil *Password
712 out3 := inNil.DeepCopy()
713 assert.Nil(t, out3)
714 }