2010-11-04 08:30:39 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2013-08-08 17:33:57 -06:00
|
|
|
"strings"
|
2010-11-04 08:30:39 -06:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
result bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []testCase{
|
|
|
|
// RFC2181, section 11.
|
2010-12-11 14:49:45 -07:00
|
|
|
{"_xmpp-server._tcp.google.com", true},
|
|
|
|
{"foo.com", true},
|
|
|
|
{"1foo.com", true},
|
|
|
|
{"26.0.0.73.com", true},
|
|
|
|
{"fo-o.com", true},
|
|
|
|
{"fo1o.com", true},
|
|
|
|
{"foo1.com", true},
|
|
|
|
{"a.b..com", false},
|
2013-08-08 17:33:57 -06:00
|
|
|
{"a.b-.com", false},
|
|
|
|
{"a.b.com-", false},
|
|
|
|
{"a.b..", false},
|
|
|
|
{"b.com.", true},
|
2010-11-04 08:30:39 -06:00
|
|
|
}
|
|
|
|
|
2011-01-21 14:57:52 -07:00
|
|
|
func getTestCases(ch chan<- testCase) {
|
2010-11-04 08:30:39 -06:00
|
|
|
defer close(ch)
|
|
|
|
var char59 = ""
|
|
|
|
var char63 = ""
|
|
|
|
var char64 = ""
|
|
|
|
for i := 0; i < 59; i++ {
|
|
|
|
char59 += "a"
|
|
|
|
}
|
|
|
|
char63 = char59 + "aaaa"
|
|
|
|
char64 = char63 + "a"
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2011-01-21 14:57:52 -07:00
|
|
|
ch <- tc
|
2010-11-04 08:30:39 -06:00
|
|
|
}
|
|
|
|
|
2011-01-21 14:57:52 -07:00
|
|
|
ch <- testCase{char63 + ".com", true}
|
|
|
|
ch <- testCase{char64 + ".com", false}
|
2010-11-04 08:30:39 -06:00
|
|
|
// 255 char name is fine:
|
2011-01-21 14:57:52 -07:00
|
|
|
ch <- testCase{char59 + "." + char63 + "." + char63 + "." +
|
2010-11-04 08:30:39 -06:00
|
|
|
char63 + ".com",
|
|
|
|
true}
|
|
|
|
// 256 char name is bad:
|
2011-01-21 14:57:52 -07:00
|
|
|
ch <- testCase{char59 + "a." + char63 + "." + char63 + "." +
|
2010-11-04 08:30:39 -06:00
|
|
|
char63 + ".com",
|
|
|
|
false}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSNames(t *testing.T) {
|
2011-01-21 14:57:52 -07:00
|
|
|
ch := make(chan testCase)
|
2010-11-04 08:30:39 -06:00
|
|
|
go getTestCases(ch)
|
|
|
|
for tc := range ch {
|
|
|
|
if isDomainName(tc.name) != tc.result {
|
|
|
|
t.Errorf("isDomainName(%v) failed: Should be %v",
|
|
|
|
tc.name, tc.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 17:33:57 -06:00
|
|
|
|
|
|
|
func BenchmarkDNSNames(b *testing.B) {
|
2015-03-31 21:21:15 -06:00
|
|
|
testHookUninstaller.Do(func() { uninstallTestHooks() })
|
net: add socket system call hooks for testing
This change adds socket system call hooks to existing test cases for
simulating a bit complicated network conditions to help making timeout
and dual IP stack test cases work more properly in followup changes.
Also test cases print debugging information in non-short mode like the
following:
Leaked goroutines:
net.TestWriteTimeout.func2(0xc20802a5a0, 0xc20801d000, 0x1000, 0x1000, 0xc2081d2ae0)
/go/src/net/timeout_test.go:170 +0x98
created by net.TestWriteTimeout
/go/src/net/timeout_test.go:173 +0x745
net.runDatagramPacketConnServer(0xc2080730e0, 0x2bd270, 0x3, 0x2c1770, 0xb, 0xc2081d2ba0, 0xc2081d2c00)
/go/src/net/server_test.go:398 +0x667
created by net.TestTimeoutUDP
/go/src/net/timeout_test.go:247 +0xc9
(snip)
Leaked sockets:
3: {Cookie:615726511685632 Err:<nil> SocketErr:0}
5: {Cookie:7934075906097152 Err:<nil> SocketErr:0}
Socket statistical information:
{Family:1 Type:805306370 Protocol:0 Opened:17 Accepted:0 Connected:5 Closed:17}
{Family:2 Type:805306369 Protocol:0 Opened:450 Accepted:234 Connected:279 Closed:636}
{Family:1 Type:805306369 Protocol:0 Opened:11 Accepted:5 Connected:5 Closed:16}
{Family:28 Type:805306369 Protocol:0 Opened:95 Accepted:22 Connected:16 Closed:116}
{Family:2 Type:805306370 Protocol:0 Opened:84 Accepted:0 Connected:34 Closed:83}
{Family:28 Type:805306370 Protocol:0 Opened:52 Accepted:0 Connected:4 Closed:52}
Change-Id: I0e84be59a0699bc31245c78e2249423459b8cdda
Reviewed-on: https://go-review.googlesource.com/6390
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-02-28 20:27:01 -07:00
|
|
|
|
2013-08-08 17:33:57 -06:00
|
|
|
benchmarks := append(tests, []testCase{
|
|
|
|
{strings.Repeat("a", 63), true},
|
|
|
|
{strings.Repeat("a", 64), false},
|
|
|
|
}...)
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for _, tc := range benchmarks {
|
|
|
|
if isDomainName(tc.name) != tc.result {
|
|
|
|
b.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|