golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/net/dnsname_test.go (about)

     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.
     4  
     5  package net
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  type dnsNameTest struct {
    13  	name   string
    14  	result bool
    15  }
    16  
    17  var dnsNameTests = []dnsNameTest{
    18  	// RFC 2181, section 11.
    19  	{"_xmpp-server._tcp.google.com", true},
    20  	{"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},
    27  	{"a.b-.com", false},
    28  	{"a.b.com-", false},
    29  	{"a.b..", false},
    30  	{"b.com.", true},
    31  }
    32  
    33  func emitDNSNameTest(ch chan<- dnsNameTest) {
    34  	defer close(ch)
    35  	var char63 = ""
    36  	for i := 0; i < 63; i++ {
    37  		char63 += "a"
    38  	}
    39  	char64 := char63 + "a"
    40  	longDomain := strings.Repeat(char63+".", 5) + "example"
    41  
    42  	for _, tc := range dnsNameTests {
    43  		ch <- tc
    44  	}
    45  
    46  	ch <- dnsNameTest{char63 + ".com", true}
    47  	ch <- dnsNameTest{char64 + ".com", false}
    48  
    49  	// Remember: wire format is two octets longer than presentation
    50  	// (length octets for the first and [root] last labels).
    51  	// 253 is fine:
    52  	ch <- dnsNameTest{longDomain[len(longDomain)-253:], true}
    53  	// A terminal dot doesn't contribute to length:
    54  	ch <- dnsNameTest{longDomain[len(longDomain)-253:] + ".", true}
    55  	// 254 is bad:
    56  	ch <- dnsNameTest{longDomain[len(longDomain)-254:], false}
    57  }
    58  
    59  func TestDNSName(t *testing.T) {
    60  	ch := make(chan dnsNameTest)
    61  	go emitDNSNameTest(ch)
    62  	for tc := range ch {
    63  		if isDomainName(tc.name) != tc.result {
    64  			t.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
    65  		}
    66  	}
    67  }
    68  
    69  func BenchmarkDNSName(b *testing.B) {
    70  	testHookUninstaller.Do(uninstallTestHooks)
    71  
    72  	benchmarks := append(dnsNameTests, []dnsNameTest{
    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  }