github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 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 dnsNameTests {
    45  		ch <- tc
    46  	}
    47  
    48  	ch <- dnsNameTest{char63 + ".com", true}
    49  	ch <- dnsNameTest{char64 + ".com", false}
    50  	// 255 char name is fine:
    51  	ch <- dnsNameTest{char59 + "." + char63 + "." + char63 + "." +
    52  		char63 + ".com",
    53  		true}
    54  	// 256 char name is bad:
    55  	ch <- dnsNameTest{char59 + "a." + char63 + "." + char63 + "." +
    56  		char63 + ".com",
    57  		false}
    58  }
    59  
    60  func TestDNSName(t *testing.T) {
    61  	ch := make(chan dnsNameTest)
    62  	go emitDNSNameTest(ch)
    63  	for tc := range ch {
    64  		if isDomainName(tc.name) != tc.result {
    65  			t.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
    66  		}
    67  	}
    68  }
    69  
    70  func BenchmarkDNSName(b *testing.B) {
    71  	testHookUninstaller.Do(uninstallTestHooks)
    72  
    73  	benchmarks := append(dnsNameTests, []dnsNameTest{
    74  		{strings.Repeat("a", 63), true},
    75  		{strings.Repeat("a", 64), false},
    76  	}...)
    77  	for n := 0; n < b.N; n++ {
    78  		for _, tc := range benchmarks {
    79  			if isDomainName(tc.name) != tc.result {
    80  				b.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
    81  			}
    82  		}
    83  	}
    84  }