blob: 57dd25fe4c6dec1752858c13c926ed1bc228a74a [file] [log] [blame]
Chris Jones25b1e832010-11-04 10:30:39 -04001// Copyright 2009 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
5package net
6
7import (
Volker Dobler654f3582013-08-08 16:33:57 -07008 "strings"
Chris Jones25b1e832010-11-04 10:30:39 -04009 "testing"
10)
11
12type testCase struct {
13 name string
14 result bool
15}
16
17var tests = []testCase{
18 // RFC2181, section 11.
Mikio Hara7ec69c12010-12-11 13:49:45 -080019 {"_xmpp-server._tcp.google.com", true},
Mikio Hara7ec69c12010-12-11 13:49:45 -080020 {"foo.com", true},
21 {"1foo.com", true},
22 {"26.0.0.73.com", true},
23 {"fo-o.com", true},
24 {"fo1o.com", true},
25 {"foo1.com", true},
26 {"a.b..com", false},
Volker Dobler654f3582013-08-08 16:33:57 -070027 {"a.b-.com", false},
28 {"a.b.com-", false},
29 {"a.b..", false},
30 {"b.com.", true},
Chris Jones25b1e832010-11-04 10:30:39 -040031}
32
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080033func getTestCases(ch chan<- testCase) {
Chris Jones25b1e832010-11-04 10:30:39 -040034 defer close(ch)
35 var char59 = ""
36 var char63 = ""
37 var char64 = ""
38 for i := 0; i < 59; i++ {
39 char59 += "a"
40 }
41 char63 = char59 + "aaaa"
42 char64 = char63 + "a"
43
44 for _, tc := range tests {
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080045 ch <- tc
Chris Jones25b1e832010-11-04 10:30:39 -040046 }
47
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080048 ch <- testCase{char63 + ".com", true}
49 ch <- testCase{char64 + ".com", false}
Chris Jones25b1e832010-11-04 10:30:39 -040050 // 255 char name is fine:
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080051 ch <- testCase{char59 + "." + char63 + "." + char63 + "." +
Chris Jones25b1e832010-11-04 10:30:39 -040052 char63 + ".com",
53 true}
54 // 256 char name is bad:
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080055 ch <- testCase{char59 + "a." + char63 + "." + char63 + "." +
Chris Jones25b1e832010-11-04 10:30:39 -040056 char63 + ".com",
57 false}
58}
59
60func TestDNSNames(t *testing.T) {
Ian Lance Taylor9e441e52011-01-21 13:57:52 -080061 ch := make(chan testCase)
Chris Jones25b1e832010-11-04 10:30:39 -040062 go getTestCases(ch)
63 for tc := range ch {
64 if isDomainName(tc.name) != tc.result {
65 t.Errorf("isDomainName(%v) failed: Should be %v",
66 tc.name, tc.result)
67 }
68 }
69}
Volker Dobler654f3582013-08-08 16:33:57 -070070
71func BenchmarkDNSNames(b *testing.B) {
72 benchmarks := append(tests, []testCase{
73 {strings.Repeat("a", 63), true},
74 {strings.Repeat("a", 64), false},
75 }...)
76 for n := 0; n < b.N; n++ {
77 for _, tc := range benchmarks {
78 if isDomainName(tc.name) != tc.result {
79 b.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
80 }
81 }
82 }
83}