idna_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package idna
  5. import (
  6. "testing"
  7. )
  8. var idnaTestCases = [...]struct {
  9. ascii, unicode string
  10. }{
  11. // Labels.
  12. {"books", "books"},
  13. {"xn--bcher-kva", "bücher"},
  14. // Domains.
  15. {"foo--xn--bar.org", "foo--xn--bar.org"},
  16. {"golang.org", "golang.org"},
  17. {"example.xn--p1ai", "example.рф"},
  18. {"xn--czrw28b.tw", "商業.tw"},
  19. {"www.xn--mller-kva.de", "www.müller.de"},
  20. }
  21. func TestIDNA(t *testing.T) {
  22. for _, tc := range idnaTestCases {
  23. if a, err := ToASCII(tc.unicode); err != nil {
  24. t.Errorf("ToASCII(%q): %v", tc.unicode, err)
  25. } else if a != tc.ascii {
  26. t.Errorf("ToASCII(%q): got %q, want %q", tc.unicode, a, tc.ascii)
  27. }
  28. if u, err := ToUnicode(tc.ascii); err != nil {
  29. t.Errorf("ToUnicode(%q): %v", tc.ascii, err)
  30. } else if u != tc.unicode {
  31. t.Errorf("ToUnicode(%q): got %q, want %q", tc.ascii, u, tc.unicode)
  32. }
  33. }
  34. }
  35. func TestIDNASeparators(t *testing.T) {
  36. type subCase struct {
  37. unicode string
  38. wantASCII string
  39. wantErr bool
  40. }
  41. testCases := []struct {
  42. name string
  43. profile *Profile
  44. subCases []subCase
  45. }{
  46. {
  47. name: "Punycode", profile: Punycode,
  48. subCases: []subCase{
  49. {"example\u3002jp", "xn--examplejp-ck3h", false},
  50. {"東京\uFF0Ejp", "xn--jp-l92cn98g071o", false},
  51. {"大阪\uFF61jp", "xn--jp-ku9cz72u463f", false},
  52. },
  53. },
  54. {
  55. name: "Lookup", profile: Lookup,
  56. subCases: []subCase{
  57. {"example\u3002jp", "example.jp", false},
  58. {"東京\uFF0Ejp", "xn--1lqs71d.jp", false},
  59. {"大阪\uFF61jp", "xn--pssu33l.jp", false},
  60. },
  61. },
  62. {
  63. name: "Display", profile: Display,
  64. subCases: []subCase{
  65. {"example\u3002jp", "example.jp", false},
  66. {"東京\uFF0Ejp", "xn--1lqs71d.jp", false},
  67. {"大阪\uFF61jp", "xn--pssu33l.jp", false},
  68. },
  69. },
  70. {
  71. name: "Registration", profile: Registration,
  72. subCases: []subCase{
  73. {"example\u3002jp", "", true},
  74. {"東京\uFF0Ejp", "", true},
  75. {"大阪\uFF61jp", "", true},
  76. },
  77. },
  78. }
  79. for _, tc := range testCases {
  80. t.Run(tc.name, func(t *testing.T) {
  81. for _, c := range tc.subCases {
  82. gotA, err := tc.profile.ToASCII(c.unicode)
  83. if c.wantErr {
  84. if err == nil {
  85. t.Errorf("ToASCII(%q): got no error, but an error expected", c.unicode)
  86. }
  87. } else {
  88. if err != nil {
  89. t.Errorf("ToASCII(%q): got err=%v, but no error expected", c.unicode, err)
  90. } else if gotA != c.wantASCII {
  91. t.Errorf("ToASCII(%q): got %q, want %q", c.unicode, gotA, c.wantASCII)
  92. }
  93. }
  94. }
  95. })
  96. }
  97. }
  98. // TODO(nigeltao): test errors, once we've specified when ToASCII and ToUnicode
  99. // return errors.