-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
types_test.go
193 lines (159 loc) · 4.37 KB
/
types_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
package dns
import (
"testing"
)
func TestCmToM(t *testing.T) {
s := cmToM((0 << 4) + 0)
if s != "0.00" {
t.Error("0, 0")
}
s = cmToM((1 << 4) + 0)
if s != "0.01" {
t.Error("1, 0")
}
s = cmToM((3 << 4) + 1)
if s != "0.30" {
t.Error("3, 1")
}
s = cmToM((4 << 4) + 2)
if s != "4" {
t.Error("4, 2")
}
s = cmToM((5 << 4) + 3)
if s != "50" {
t.Error("5, 3")
}
s = cmToM((7 << 4) + 5)
if s != "7000" {
t.Error("7, 5")
}
s = cmToM((9 << 4) + 9)
if s != "90000000" {
t.Error("9, 9")
}
}
func TestSplitN(t *testing.T) {
xs := splitN("abc", 5)
if len(xs) != 1 && xs[0] != "abc" {
t.Errorf("failure to split abc")
}
s := ""
for i := 0; i < 255; i++ {
s += "a"
}
xs = splitN(s, 255)
if len(xs) != 1 && xs[0] != s {
t.Errorf("failure to split 255 char long string")
}
s += "b"
xs = splitN(s, 255)
if len(xs) != 2 || xs[1] != "b" {
t.Errorf("failure to split 256 char long string: %d", len(xs))
}
// Make s longer
for i := 0; i < 255; i++ {
s += "a"
}
xs = splitN(s, 255)
if len(xs) != 3 || xs[2] != "a" {
t.Errorf("failure to split 510 char long string: %d", len(xs))
}
}
func TestSprintName(t *testing.T) {
tests := map[string]string{
// Non-numeric escaping of special printable characters.
" '@;()\"\\..example": `\ \'\@\;\(\)\"\..example`,
"\\032\\039\\064\\059\\040\\041\\034\\046\\092.example": `\ \'\@\;\(\)\"\.\\.example`,
// Numeric escaping of nonprintable characters.
"\x00\x07\x09\x0a\x1f.\x7f\x80\xad\xef\xff": `\000\007\009\010\031.\127\128\173\239\255`,
"\\000\\007\\009\\010\\031.\\127\\128\\173\\239\\255": `\000\007\009\010\031.\127\128\173\239\255`,
// No escaping of other printable characters, at least after a prior escape.
";[a-zA-Z0-9_]+/*.~": `\;[a-zA-Z0-9_]+/*.~`,
";\\091\\097\\045\\122\\065\\045\\090\\048\\045\\057\\095\\093\\043\\047\\042.\\126": `\;[a-zA-Z0-9_]+/*.~`,
// "\\091\\097\\045\\122\\065\\045\\090\\048\\045\\057\\095\\093\\043\\047\\042.\\126": `[a-zA-Z0-9_]+/*.~`,
// Incomplete "dangling" escapes are dropped regardless of prior escaping.
"a\\": `a`,
";\\": `\;`,
// Escaped dots stay escaped regardless of prior escaping.
"a\\.\\046.\\.\\046": `a\.\..\.\.`,
"a\\046\\..\\046\\.": `a\.\..\.\.`,
}
for input, want := range tests {
got := sprintName(input)
if got != want {
t.Errorf("input %q: expected %q, got %q", input, want, got)
}
}
}
func TestSprintTxtOctet(t *testing.T) {
got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "\"abc\\.def\\007\\\"W@\\173\\005\\239\""; got != want {
t.Errorf("expected %q, got %q", want, got)
}
}
func TestSprintTxt(t *testing.T) {
got := sprintTxt([]string{
"abc\\.def\007\"\127@\255\x05\xef\\",
"example.com",
})
if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want {
t.Errorf("expected %q, got %q", want, got)
}
}
func TestRPStringer(t *testing.T) {
rp := &RP{
Hdr: RR_Header{
Name: "test.example.com.",
Rrtype: TypeRP,
Class: ClassINET,
Ttl: 600,
},
Mbox: "\x05first.example.com.",
Txt: "second.\x07example.com.",
}
const expected = "test.example.com.\t600\tIN\tRP\t\\005first.example.com. second.\\007example.com."
if rp.String() != expected {
t.Errorf("expected %v, got %v", expected, rp)
}
_, err := NewRR(rp.String())
if err != nil {
t.Fatalf("error parsing %q: %v", rp, err)
}
}
func BenchmarkSprintName(b *testing.B) {
for n := 0; n < b.N; n++ {
got := sprintName("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "abc\\.def\\007\\\"W\\@\\173\\005\\239"; got != want {
b.Fatalf("expected %q, got %q", want, got)
}
}
}
func BenchmarkSprintName_NoEscape(b *testing.B) {
for n := 0; n < b.N; n++ {
got := sprintName("large.example.com")
if want := "large.example.com"; got != want {
b.Fatalf("expected %q, got %q", want, got)
}
}
}
func BenchmarkSprintTxtOctet(b *testing.B) {
for n := 0; n < b.N; n++ {
got := sprintTxtOctet("abc\\.def\007\"\127@\255\x05\xef\\")
if want := "\"abc\\.def\\007\\\"W@\\173\\005\\239\""; got != want {
b.Fatalf("expected %q, got %q", want, got)
}
}
}
func BenchmarkSprintTxt(b *testing.B) {
txt := []string{
"abc\\.def\007\"\127@\255\x05\xef\\",
"example.com",
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
got := sprintTxt(txt)
if want := "\"abc.def\\007\\\"W@\\173\\005\\239\" \"example.com\""; got != want {
b.Fatalf("expected %q, got %q", got, want)
}
}
}