summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/miekg/dns/idn/punycode_test.go
blob: 9c9a15f0ba1d263b70fe8d5878e80417b8774e92 (plain)
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
package idn

import (
	"strings"
	"testing"
)

var testcases = [][2]string{
	{"", ""},
	{"a", "a"},
	{"a-b", "a-b"},
	{"a-b-c", "a-b-c"},
	{"abc", "abc"},
	{"я", "xn--41a"},
	{"zя", "xn--z-0ub"},
	{"яZ", "xn--z-zub"},
	{"а-я", "xn----7sb8g"},
	{"إختبار", "xn--kgbechtv"},
	{"آزمایشی", "xn--hgbk6aj7f53bba"},
	{"测试", "xn--0zwm56d"},
	{"測試", "xn--g6w251d"},
	{"испытание", "xn--80akhbyknj4f"},
	{"परीक्षा", "xn--11b5bs3a9aj6g"},
	{"δοκιμή", "xn--jxalpdlp"},
	{"테스트", "xn--9t4b11yi5a"},
	{"טעסט", "xn--deba0ad"},
	{"テスト", "xn--zckzah"},
	{"பரிட்சை", "xn--hlcj6aya9esc7a"},
	{"mamão-com-açúcar", "xn--mamo-com-acar-yeb1e6q"},
	{"σ", "xn--4xa"},
}

func TestEncodeDecodePunycode(t *testing.T) {
	for _, tst := range testcases {
		enc := encode([]byte(tst[0]))
		if string(enc) != tst[1] {
			t.Errorf("%s encodeded as %s but should be %s", tst[0], enc, tst[1])
		}
		dec := decode([]byte(tst[1]))
		if string(dec) != strings.ToLower(tst[0]) {
			t.Errorf("%s decoded as %s but should be %s", tst[1], dec, strings.ToLower(tst[0]))
		}
	}
}

func TestToFromPunycode(t *testing.T) {
	for _, tst := range testcases {
		// assert unicode.com == punycode.com
		full := ToPunycode(tst[0] + ".com")
		if full != tst[1]+".com" {
			t.Errorf("invalid result from string conversion to punycode, %s and should be %s.com", full, tst[1])
		}
		// assert punycode.punycode == unicode.unicode
		decoded := FromPunycode(tst[1] + "." + tst[1])
		if decoded != strings.ToLower(tst[0]+"."+tst[0]) {
			t.Errorf("invalid result from string conversion to punycode, %s and should be %s.%s", decoded, tst[0], tst[0])
		}
	}
}

func TestEncodeDecodeFinalPeriod(t *testing.T) {
	for _, tst := range testcases {
		// assert unicode.com. == punycode.com.
		full := ToPunycode(tst[0] + ".")
		if full != tst[1]+"." {
			t.Errorf("invalid result from string conversion to punycode when period added at the end, %#v and should be %#v", full, tst[1]+".")
		}
		// assert punycode.com. == unicode.com.
		decoded := FromPunycode(tst[1] + ".")
		if decoded != strings.ToLower(tst[0]+".") {
			t.Errorf("invalid result from string conversion to punycode when period added, %#v and should be %#v", decoded, tst[0]+".")
		}
		full = ToPunycode(tst[0])
		if full != tst[1] {
			t.Errorf("invalid result from string conversion to punycode when no period added at the end, %#v and should be %#v", full, tst[1]+".")
		}
		// assert punycode.com. == unicode.com.
		decoded = FromPunycode(tst[1])
		if decoded != strings.ToLower(tst[0]) {
			t.Errorf("invalid result from string conversion to punycode when no period added, %#v and should be %#v", decoded, tst[0]+".")
		}
	}
}

var invalidACEs = []string{
	"xn--*",
	"xn--",
	"xn---",
	"xn--a000000000",
}

func TestInvalidPunycode(t *testing.T) {
	for _, d := range invalidACEs {
		s := FromPunycode(d)
		if s != d {
			t.Errorf("Changed invalid name %s to %#v", d, s)
		}
	}
}

// You can verify the labels that are valid or not comparing to the Verisign
// website: http://mct.verisign-grs.com/
var invalidUnicodes = []string{
	"Σ",
	"ЯZ",
	"Испытание",
}

func TestInvalidUnicodes(t *testing.T) {
	for _, d := range invalidUnicodes {
		s := ToPunycode(d)
		if s != "" {
			t.Errorf("Changed invalid name %s to %#v", d, s)
		}
	}
}