github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/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 testCase struct {
    13  	name   string
    14  	result bool
    15  }
    16  
    17  var tests = []testCase{
    18  	// RFC2181, 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 getTestCases(ch chan<- testCase) {
    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 tests {
    45  		ch <- tc
    46  	}
    47  
    48  	ch <- testCase{char63 + ".com", true}
    49  	ch <- testCase{char64 + ".com", false}
    50  	// 255 char name is fine:
    51  	ch <- testCase{char59 + "." + char63 + "." + char63 + "." +
    52  		char63 + ".com",
    53  		true}
    54  	// 256 char name is bad:
    55  	ch <- testCase{char59 + "a." + char63 + "." + char63 + "." +
    56  		char63 + ".com",
    57  		false}
    58  }
    59  
    60  func TestDNSNames(t *testing.T) {
    61  	ch := make(chan testCase)
    62  	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  }
    70  
    71  func 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  }