The Go Programming Language

Source file src/pkg/net/net_test.go

     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		"flag"
     9		"regexp"
    10		"testing"
    11	)
    12	
    13	var runErrorTest = flag.Bool("run_error_test", false, "let TestDialError check for dns errors")
    14	
    15	type DialErrorTest struct {
    16		Net     string
    17		Raddr   string
    18		Pattern string
    19	}
    20	
    21	var dialErrorTests = []DialErrorTest{
    22		{
    23			"datakit", "mh/astro/r70",
    24			"dial datakit mh/astro/r70: unknown network datakit",
    25		},
    26		{
    27			"tcp", "127.0.0.1:☺",
    28			"dial tcp 127.0.0.1:☺: unknown port tcp/☺",
    29		},
    30		{
    31			"tcp", "no-such-name.google.com.:80",
    32			"dial tcp no-such-name.google.com.:80: lookup no-such-name.google.com.( on .*)?: no (.*)",
    33		},
    34		{
    35			"tcp", "no-such-name.no-such-top-level-domain.:80",
    36			"dial tcp no-such-name.no-such-top-level-domain.:80: lookup no-such-name.no-such-top-level-domain.( on .*)?: no (.*)",
    37		},
    38		{
    39			"tcp", "no-such-name:80",
    40			`dial tcp no-such-name:80: lookup no-such-name\.(.*\.)?( on .*)?: no (.*)`,
    41		},
    42		{
    43			"tcp", "mh/astro/r70:http",
    44			"dial tcp mh/astro/r70:http: lookup mh/astro/r70: invalid domain name",
    45		},
    46		{
    47			"unix", "/etc/file-not-found",
    48			"dial unix /etc/file-not-found: no such file or directory",
    49		},
    50		{
    51			"unix", "/etc/",
    52			"dial unix /etc/: (permission denied|socket operation on non-socket|connection refused)",
    53		},
    54		{
    55			"unixpacket", "/etc/file-not-found",
    56			"dial unixpacket /etc/file-not-found: no such file or directory",
    57		},
    58		{
    59			"unixpacket", "/etc/",
    60			"dial unixpacket /etc/: (permission denied|socket operation on non-socket|connection refused)",
    61		},
    62	}
    63	
    64	func TestDialError(t *testing.T) {
    65		if !*runErrorTest {
    66			t.Logf("test disabled; use --run_error_test to enable")
    67			return
    68		}
    69		for i, tt := range dialErrorTests {
    70			c, e := Dial(tt.Net, tt.Raddr)
    71			if c != nil {
    72				c.Close()
    73			}
    74			if e == nil {
    75				t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern)
    76				continue
    77			}
    78			s := e.String()
    79			match, _ := regexp.MatchString(tt.Pattern, s)
    80			if !match {
    81				t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern)
    82			}
    83		}
    84	}
    85	
    86	var revAddrTests = []struct {
    87		Addr      string
    88		Reverse   string
    89		ErrPrefix string
    90	}{
    91		{"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
    92		{"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
    93		{"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
    94		{"::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", ""},
    95		{"1::", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.ip6.arpa.", ""},
    96		{"1234:567::89a:bcde", "e.d.c.b.a.9.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
    97		{"1234:567:fefe:bcbc:adad:9e4a:89a:bcde", "e.d.c.b.a.9.8.0.a.4.e.9.d.a.d.a.c.b.c.b.e.f.e.f.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
    98		{"1.2.3", "", "unrecognized address"},
    99		{"1.2.3.4.5", "", "unrecognized address"},
   100		{"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
   101		{"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
   102	}
   103	
   104	func TestReverseAddress(t *testing.T) {
   105		for i, tt := range revAddrTests {
   106			a, e := reverseaddr(tt.Addr)
   107			if len(tt.ErrPrefix) > 0 && e == nil {
   108				t.Errorf("#%d: expected %q, got <nil> (error)", i, tt.ErrPrefix)
   109				continue
   110			}
   111			if len(tt.ErrPrefix) == 0 && e != nil {
   112				t.Errorf("#%d: expected <nil>, got %q (error)", i, e)
   113			}
   114			if e != nil && e.(*DNSError).Error != tt.ErrPrefix {
   115				t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, e.(*DNSError).Error)
   116			}
   117			if a != tt.Reverse {
   118				t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
   119			}
   120		}
   121	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.