Debian Codegraph @

labels_test.go

raw symbols SCIP
1 package dns
2
3 import "testing"
4
5 func TestCompareDomainName(t *testing.T) {
6 s1 := "www.miek.nl."
7 s2 := "miek.nl."
8 s3 := "www.bla.nl."
9 s4 := "nl.www.bla."
10 s5 := "nl."
11 s6 := "miek.nl."
12
13 if CompareDomainName(s1, s2) != 2 {
14 t.Errorf("%s with %s should be %d", s1, s2, 2)
15 }
16 if CompareDomainName(s1, s3) != 1 {
17 t.Errorf("%s with %s should be %d", s1, s3, 1)
18 }
19 if CompareDomainName(s3, s4) != 0 {
20 t.Errorf("%s with %s should be %d", s3, s4, 0)
21 }
22 // Non qualified tests
23 if CompareDomainName(s1, s5) != 1 {
24 t.Errorf("%s with %s should be %d", s1, s5, 1)
25 }
26 if CompareDomainName(s1, s6) != 2 {
27 t.Errorf("%s with %s should be %d", s1, s5, 2)
28 }
29
30 if CompareDomainName(s1, ".") != 0 {
31 t.Errorf("%s with %s should be %d", s1, s5, 0)
32 }
33 if CompareDomainName(".", ".") != 0 {
34 t.Errorf("%s with %s should be %d", ".", ".", 0)
35 }
36 if CompareDomainName("test.com.", "TEST.COM.") != 2 {
37 t.Errorf("test.com. and TEST.COM. should be an exact match")
38 }
39 }
40
41 func TestSplit(t *testing.T) {
42 splitter := map[string]int{
43 "www.miek.nl.": 3,
44 "www.miek.nl": 3,
45 "www..miek.nl": 4,
46 `www\.miek.nl.`: 2,
47 `www\\.miek.nl.`: 3,
48 `www\\\.miek.nl.`: 2,
49 ".": 0,
50 "nl.": 1,
51 "nl": 1,
52 "com.": 1,
53 ".com.": 2,
54 }
55 for s, i := range splitter {
56 if x := len(Split(s)); x != i {
57 t.Errorf("labels should be %d, got %d: %s %v", i, x, s, Split(s))
58 }
59 }
60 }
61
62 func TestSplit2(t *testing.T) {
63 splitter := map[string][]int{
64 "www.miek.nl.": {0, 4, 9},
65 "www.miek.nl": {0, 4, 9},
66 "nl": {0},
67 }
68 for s, i := range splitter {
69 x := Split(s)
70 switch len(i) {
71 case 1:
72 if x[0] != i[0] {
73 t.Errorf("labels should be %v, got %v: %s", i, x, s)
74 }
75 default:
76 if x[0] != i[0] || x[1] != i[1] || x[2] != i[2] {
77 t.Errorf("labels should be %v, got %v: %s", i, x, s)
78 }
79 }
80 }
81 }
82
83 func TestNextLabel(t *testing.T) {
84 type next struct {
85 string
86 int
87 }
88 nexts := map[next]int{
89 {"", 1}: 0,
90 {"www.miek.nl.", 0}: 4,
91 {"www.miek.nl.", 4}: 9,
92 {"www.miek.nl.", 9}: 12,
93 }
94 for s, i := range nexts {
95 x, ok := NextLabel(s.string, s.int)
96 if i != x {
97 t.Errorf("label should be %d, got %d, %t: next %d, %s", i, x, ok, s.int, s.string)
98 }
99 }
100 }
101
102 func TestPrevLabel(t *testing.T) {
103 type prev struct {
104 string
105 int
106 }
107 prever := map[prev]int{
108 {"", 1}: 0,
109 {"www.miek.nl.", 0}: 12,
110 {"www.miek.nl.", 1}: 9,
111 {"www.miek.nl.", 2}: 4,
112
113 {"www.miek.nl", 0}: 11,
114 {"www.miek.nl", 1}: 9,
115 {"www.miek.nl", 2}: 4,
116
117 {"www.miek.nl.", 5}: 0,
118 {"www.miek.nl", 5}: 0,
119
120 {"www.miek.nl.", 3}: 0,
121 {"www.miek.nl", 3}: 0,
122 }
123 for s, i := range prever {
124 x, ok := PrevLabel(s.string, s.int)
125 if i != x {
126 t.Errorf("label should be %d, got %d, %t: previous %d, %s", i, x, ok, s.int, s.string)
127 }
128 }
129 }
130
131 func TestCountLabel(t *testing.T) {
132 splitter := map[string]int{
133 "www.miek.nl.": 3,
134 "www.miek.nl": 3,
135 "nl": 1,
136 ".": 0,
137 }
138 for s, i := range splitter {
139 x := CountLabel(s)
140 if x != i {
141 t.Errorf("CountLabel should have %d, got %d", i, x)
142 }
143 }
144 }
145
146 func TestSplitDomainName(t *testing.T) {
147 labels := map[string][]string{
148 "miek.nl": {"miek", "nl"},
149 ".": nil,
150 "www.miek.nl.": {"www", "miek", "nl"},
151 "www.miek.nl": {"www", "miek", "nl"},
152 "www..miek.nl": {"www", "", "miek", "nl"},
153 `www\.miek.nl`: {`www\.miek`, "nl"},
154 `www\\.miek.nl`: {`www\\`, "miek", "nl"},
155 ".www.miek.nl.": {"", "www", "miek", "nl"},
156 }
157 domainLoop:
158 for domain, splits := range labels {
159 parts := SplitDomainName(domain)
160 if len(parts) != len(splits) {
161 t.Errorf("SplitDomainName returned %v for %s, expected %v", parts, domain, splits)
162 continue domainLoop
163 }
164 for i := range parts {
165 if parts[i] != splits[i] {
166 t.Errorf("SplitDomainName returned %v for %s, expected %v", parts, domain, splits)
167 continue domainLoop
168 }
169 }
170 }
171 }
172
173 func TestIsDomainName(t *testing.T) {
174 type ret struct {
175 ok bool
176 lab int
177 }
178 names := map[string]*ret{
179 ".": {true, 1},
180 "..": {false, 0},
181 "double-dot..test": {false, 1},
182 ".leading-dot.test": {false, 0},
183 "@.": {true, 1},
184 "www.example.com": {true, 3},
185 "www.e%ample.com": {true, 3},
186 "www.example.com.": {true, 3},
187 "mi\\k.nl.": {true, 2},
188 "mi\\k.nl": {true, 2},
189 longestDomain: {true, 4},
190 longestUnprintableDomain: {true, 4},
191 }
192 for d, ok := range names {
193 l, k := IsDomainName(d)
194 if ok.ok != k || ok.lab != l {
195 t.Errorf(" got %v %d for %s ", k, l, d)
196 t.Errorf("have %v %d for %s ", ok.ok, ok.lab, d)
197 }
198 }
199 }
200
201 func TestIsFqdnEscaped(t *testing.T) {
202 for s, expect := range map[string]bool{
203 ".": true,
204 "\\.": false,
205 "\\\\.": true,
206 "\\\\\\.": false,
207 "\\\\\\\\.": true,
208 "a.": true,
209 "a\\.": false,
210 "a\\\\.": true,
211 "a\\\\\\.": false,
212 "ab.": true,
213 "ab\\.": false,
214 "ab\\\\.": true,
215 "ab\\\\\\.": false,
216 "..": true,
217 ".\\.": false,
218 ".\\\\.": true,
219 ".\\\\\\.": false,
220 "example.org.": true,
221 "example.org\\.": false,
222 "example.org\\\\.": true,
223 "example.org\\\\\\.": false,
224 "example\\.org.": true,
225 "example\\\\.org.": true,
226 "example\\\\\\.org.": true,
227 "\\example.org.": true,
228 "\\\\example.org.": true,
229 "\\\\\\example.org.": true,
230 } {
231 if got := IsFqdn(s); got != expect {
232 t.Errorf("IsFqdn(%q) = %t, expected %t", s, got, expect)
233 }
234 }
235 }
236
237 func TestCanonicalName(t *testing.T) {
238 for s, expect := range map[string]string{
239 "": ".",
240 ".": ".",
241 "tld": "tld.",
242 "tld.": "tld.",
243 "example.test": "example.test.",
244 "Lower.CASE.test.": "lower.case.test.",
245 "*.Test": "*.test.",
246 "ÉxamplE.com": "Éxample.com.",
247 "É.com": "É.com.",
248 } {
249 if got := CanonicalName(s); got != expect {
250 t.Errorf("CanonicalName(%q) = %q, expected %q", s, got, expect)
251 }
252 }
253 }
254
255 func BenchmarkSplitLabels(b *testing.B) {
256 for i := 0; i < b.N; i++ {
257 Split("www.example.com.")
258 }
259 }
260
261 func BenchmarkLenLabels(b *testing.B) {
262 for i := 0; i < b.N; i++ {
263 CountLabel("www.example.com.")
264 }
265 }
266
267 func BenchmarkCompareDomainName(b *testing.B) {
268 b.ReportAllocs()
269 for i := 0; i < b.N; i++ {
270 CompareDomainName("www.example.com.", "aa.example.com.")
271 }
272 }
273
274 func BenchmarkIsSubDomain(b *testing.B) {
275 b.ReportAllocs()
276 for i := 0; i < b.N; i++ {
277 IsSubDomain("www.example.com.", "aa.example.com.")
278 IsSubDomain("example.com.", "aa.example.com.")
279 IsSubDomain("miek.nl.", "aa.example.com.")
280 }
281 }
282
283 func BenchmarkNextLabelSimple(b *testing.B) {
284 b.ReportAllocs()
285 for i := 0; i < b.N; i++ {
286 NextLabel("www.example.com", 0)
287 NextLabel("www.example.com", 5)
288 NextLabel("www.example.com", 12)
289 }
290 }
291
292 func BenchmarkPrevLabelSimple(b *testing.B) {
293 b.ReportAllocs()
294 for i := 0; i < b.N; i++ {
295 PrevLabel("www.example.com", 0)
296 PrevLabel("www.example.com", 5)
297 PrevLabel("www.example.com", 12)
298 }
299 }
300
301 func BenchmarkNextLabelComplex(b *testing.B) {
302 b.ReportAllocs()
303 for i := 0; i < b.N; i++ {
304 NextLabel(`www\.example.com`, 0)
305 NextLabel(`www\\.example.com`, 0)
306 NextLabel(`www\\\.example.com`, 0)
307 }
308 }
309
310 func BenchmarkPrevLabelComplex(b *testing.B) {
311 b.ReportAllocs()
312 for i := 0; i < b.N; i++ {
313 PrevLabel(`www\.example.com`, 10)
314 PrevLabel(`www\\.example.com`, 10)
315 PrevLabel(`www\\\.example.com`, 10)
316 }
317 }
318
319 func BenchmarkNextLabelMixed(b *testing.B) {
320 b.ReportAllocs()
321 for i := 0; i < b.N; i++ {
322 NextLabel("www.example.com", 0)
323 NextLabel(`www\.example.com`, 0)
324 NextLabel("www.example.com", 5)
325 NextLabel(`www\\.example.com`, 0)
326 NextLabel("www.example.com", 12)
327 NextLabel(`www\\\.example.com`, 0)
328 }
329 }
330
331 func BenchmarkPrevLabelMixed(b *testing.B) {
332 b.ReportAllocs()
333 for i := 0; i < b.N; i++ {
334 PrevLabel("www.example.com", 0)
335 PrevLabel(`www\.example.com`, 10)
336 PrevLabel("www.example.com", 5)
337 PrevLabel(`www\\.example.com`, 10)
338 PrevLabel("www.example.com", 12)
339 PrevLabel(`www\\\.example.com`, 10)
340 }
341 }