...

Source file src/net/dnsname_test.go

Documentation: net

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

View as plain text