libgo: update to Go 1.11
[official-gcc.git] / libgo / go / net / dnsname_test.go
blob806d8756cb5c5f0af4d8359ffb95456992ee7e1e
1 // 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.
5 // +build !js
7 package net
9 import (
10 "strings"
11 "testing"
14 type dnsNameTest struct {
15 name string
16 result bool
19 var dnsNameTests = []dnsNameTest{
20 // RFC 2181, section 11.
21 {"_xmpp-server._tcp.google.com", true},
22 {"foo.com", true},
23 {"1foo.com", true},
24 {"26.0.0.73.com", true},
25 {"fo-o.com", true},
26 {"fo1o.com", true},
27 {"foo1.com", true},
28 {"a.b..com", false},
29 {"a.b-.com", false},
30 {"a.b.com-", false},
31 {"a.b..", false},
32 {"b.com.", true},
35 func emitDNSNameTest(ch chan<- dnsNameTest) {
36 defer close(ch)
37 var char63 = ""
38 for i := 0; i < 63; i++ {
39 char63 += "a"
41 char64 := char63 + "a"
42 longDomain := strings.Repeat(char63+".", 5) + "example"
44 for _, tc := range dnsNameTests {
45 ch <- tc
48 ch <- dnsNameTest{char63 + ".com", true}
49 ch <- dnsNameTest{char64 + ".com", false}
51 // Remember: wire format is two octets longer than presentation
52 // (length octets for the first and [root] last labels).
53 // 253 is fine:
54 ch <- dnsNameTest{longDomain[len(longDomain)-253:], true}
55 // A terminal dot doesn't contribute to length:
56 ch <- dnsNameTest{longDomain[len(longDomain)-253:] + ".", true}
57 // 254 is bad:
58 ch <- dnsNameTest{longDomain[len(longDomain)-254:], false}
61 func TestDNSName(t *testing.T) {
62 ch := make(chan dnsNameTest)
63 go emitDNSNameTest(ch)
64 for tc := range ch {
65 if isDomainName(tc.name) != tc.result {
66 t.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
71 func BenchmarkDNSName(b *testing.B) {
72 testHookUninstaller.Do(uninstallTestHooks)
74 benchmarks := append(dnsNameTests, []dnsNameTest{
75 {strings.Repeat("a", 63), true},
76 {strings.Repeat("a", 64), false},
77 }...)
78 for n := 0; n < b.N; n++ {
79 for _, tc := range benchmarks {
80 if isDomainName(tc.name) != tc.result {
81 b.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)