Source file src/net/ip_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  	"bytes"
     9  	"math/rand"
    10  	"reflect"
    11  	"runtime"
    12  	"testing"
    13  )
    14  
    15  var parseIPTests = []struct {
    16  	in  string
    17  	out IP
    18  }{
    19  	{"127.0.1.2", IPv4(127, 0, 1, 2)},
    20  	{"127.0.0.1", IPv4(127, 0, 0, 1)},
    21  	{"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
    22  	{"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
    23  	{"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
    24  	{"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
    25  	{"0:0:0:0::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
    26  
    27  	{"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
    28  	{"2001:4860:0000:2001:0000:0000:0000:0068", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
    29  
    30  	{"-0.0.0.0", nil},
    31  	{"0.-1.0.0", nil},
    32  	{"0.0.-2.0", nil},
    33  	{"0.0.0.-3", nil},
    34  	{"127.0.0.256", nil},
    35  	{"abc", nil},
    36  	{"123:", nil},
    37  	{"fe80::1%lo0", nil},
    38  	{"fe80::1%911", nil},
    39  	{"", nil},
    40  	{"a1:a2:a3:a4::b1:b2:b3:b4", nil}, // Issue 6628
    41  	{"127.001.002.003", nil},
    42  	{"::ffff:127.001.002.003", nil},
    43  	{"123.000.000.000", nil},
    44  	{"1.2..4", nil},
    45  	{"0123.0.0.1", nil},
    46  }
    47  
    48  func TestParseIP(t *testing.T) {
    49  	for _, tt := range parseIPTests {
    50  		if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
    51  			t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
    52  		}
    53  		if tt.in == "" {
    54  			// Tested in TestMarshalEmptyIP below.
    55  			continue
    56  		}
    57  		var out IP
    58  		if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
    59  			t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
    60  		}
    61  	}
    62  }
    63  
    64  func TestLookupWithIP(t *testing.T) {
    65  	_, err := LookupIP("")
    66  	if err == nil {
    67  		t.Errorf(`LookupIP("") succeeded, should fail`)
    68  	}
    69  	_, err = LookupHost("")
    70  	if err == nil {
    71  		t.Errorf(`LookupIP("") succeeded, should fail`)
    72  	}
    73  
    74  	// Test that LookupHost and LookupIP, which normally
    75  	// expect host names, work with IP addresses.
    76  	for _, tt := range parseIPTests {
    77  		if tt.out != nil {
    78  			addrs, err := LookupHost(tt.in)
    79  			if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
    80  				t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
    81  			}
    82  		} else if !testing.Short() {
    83  			// We can't control what the host resolver does; if it can resolve, say,
    84  			// 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
    85  			// Warn about these discrepancies but don't fail the test.
    86  			addrs, err := LookupHost(tt.in)
    87  			if err == nil {
    88  				t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
    89  			}
    90  		}
    91  
    92  		if tt.out != nil {
    93  			ips, err := LookupIP(tt.in)
    94  			if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
    95  				t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
    96  			}
    97  		} else if !testing.Short() {
    98  			ips, err := LookupIP(tt.in)
    99  			// We can't control what the host resolver does. See above.
   100  			if err == nil {
   101  				t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
   102  			}
   103  		}
   104  	}
   105  }
   106  
   107  func BenchmarkParseIP(b *testing.B) {
   108  	testHookUninstaller.Do(uninstallTestHooks)
   109  
   110  	for i := 0; i < b.N; i++ {
   111  		for _, tt := range parseIPTests {
   112  			ParseIP(tt.in)
   113  		}
   114  	}
   115  }
   116  
   117  func BenchmarkParseIPValidIPv4(b *testing.B) {
   118  	testHookUninstaller.Do(uninstallTestHooks)
   119  
   120  	for i := 0; i < b.N; i++ {
   121  		ParseIP("192.0.2.1")
   122  	}
   123  }
   124  
   125  func BenchmarkParseIPValidIPv6(b *testing.B) {
   126  	testHookUninstaller.Do(uninstallTestHooks)
   127  
   128  	for i := 0; i < b.N; i++ {
   129  		ParseIP("2001:DB8::1")
   130  	}
   131  }
   132  
   133  // Issue 6339
   134  func TestMarshalEmptyIP(t *testing.T) {
   135  	for _, in := range [][]byte{nil, []byte("")} {
   136  		var out = IP{1, 2, 3, 4}
   137  		if err := out.UnmarshalText(in); err != nil || out != nil {
   138  			t.Errorf("UnmarshalText(%v) = %v, %v; want nil, nil", in, out, err)
   139  		}
   140  	}
   141  	var ip IP
   142  	got, err := ip.MarshalText()
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	if !reflect.DeepEqual(got, []byte("")) {
   147  		t.Errorf(`got %#v, want []byte("")`, got)
   148  	}
   149  }
   150  
   151  var ipStringTests = []*struct {
   152  	in  IP     // see RFC 791 and RFC 4291
   153  	str string // see RFC 791, RFC 4291 and RFC 5952
   154  	byt []byte
   155  	error
   156  }{
   157  	// IPv4 address
   158  	{
   159  		IP{192, 0, 2, 1},
   160  		"192.0.2.1",
   161  		[]byte("192.0.2.1"),
   162  		nil,
   163  	},
   164  	{
   165  		IP{0, 0, 0, 0},
   166  		"0.0.0.0",
   167  		[]byte("0.0.0.0"),
   168  		nil,
   169  	},
   170  
   171  	// IPv4-mapped IPv6 address
   172  	{
   173  		IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 0, 2, 1},
   174  		"192.0.2.1",
   175  		[]byte("192.0.2.1"),
   176  		nil,
   177  	},
   178  	{
   179  		IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0},
   180  		"0.0.0.0",
   181  		[]byte("0.0.0.0"),
   182  		nil,
   183  	},
   184  
   185  	// IPv6 address
   186  	{
   187  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
   188  		"2001:db8::123:12:1",
   189  		[]byte("2001:db8::123:12:1"),
   190  		nil,
   191  	},
   192  	{
   193  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1},
   194  		"2001:db8::1",
   195  		[]byte("2001:db8::1"),
   196  		nil,
   197  	},
   198  	{
   199  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1},
   200  		"2001:db8:0:1:0:1:0:1",
   201  		[]byte("2001:db8:0:1:0:1:0:1"),
   202  		nil,
   203  	},
   204  	{
   205  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0},
   206  		"2001:db8:1:0:1:0:1:0",
   207  		[]byte("2001:db8:1:0:1:0:1:0"),
   208  		nil,
   209  	},
   210  	{
   211  		IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
   212  		"2001::1:0:0:1",
   213  		[]byte("2001::1:0:0:1"),
   214  		nil,
   215  	},
   216  	{
   217  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0},
   218  		"2001:db8:0:0:1::",
   219  		[]byte("2001:db8:0:0:1::"),
   220  		nil,
   221  	},
   222  	{
   223  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
   224  		"2001:db8::1:0:0:1",
   225  		[]byte("2001:db8::1:0:0:1"),
   226  		nil,
   227  	},
   228  	{
   229  		IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0xa, 0, 0xb, 0, 0xc, 0, 0xd},
   230  		"2001:db8::a:b:c:d",
   231  		[]byte("2001:db8::a:b:c:d"),
   232  		nil,
   233  	},
   234  	{
   235  		IPv6unspecified,
   236  		"::",
   237  		[]byte("::"),
   238  		nil,
   239  	},
   240  
   241  	// IP wildcard equivalent address in Dial/Listen API
   242  	{
   243  		nil,
   244  		"<nil>",
   245  		nil,
   246  		nil,
   247  	},
   248  
   249  	// Opaque byte sequence
   250  	{
   251  		IP{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
   252  		"?0123456789abcdef",
   253  		nil,
   254  		&AddrError{Err: "invalid IP address", Addr: "0123456789abcdef"},
   255  	},
   256  }
   257  
   258  func TestIPString(t *testing.T) {
   259  	for _, tt := range ipStringTests {
   260  		if out := tt.in.String(); out != tt.str {
   261  			t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.str)
   262  		}
   263  		if out, err := tt.in.MarshalText(); !bytes.Equal(out, tt.byt) || !reflect.DeepEqual(err, tt.error) {
   264  			t.Errorf("IP.MarshalText(%v) = %v, %v, want %v, %v", tt.in, out, err, tt.byt, tt.error)
   265  		}
   266  	}
   267  }
   268  
   269  var sink string
   270  
   271  func BenchmarkIPString(b *testing.B) {
   272  	testHookUninstaller.Do(uninstallTestHooks)
   273  
   274  	b.Run("IPv4", func(b *testing.B) {
   275  		benchmarkIPString(b, IPv4len)
   276  	})
   277  
   278  	b.Run("IPv6", func(b *testing.B) {
   279  		benchmarkIPString(b, IPv6len)
   280  	})
   281  }
   282  
   283  func benchmarkIPString(b *testing.B, size int) {
   284  	b.ReportAllocs()
   285  	b.ResetTimer()
   286  	for i := 0; i < b.N; i++ {
   287  		for _, tt := range ipStringTests {
   288  			if tt.in != nil && len(tt.in) == size {
   289  				sink = tt.in.String()
   290  			}
   291  		}
   292  	}
   293  }
   294  
   295  var ipMaskTests = []struct {
   296  	in   IP
   297  	mask IPMask
   298  	out  IP
   299  }{
   300  	{IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
   301  	{IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
   302  	{IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
   303  	{IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
   304  	{ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
   305  	{ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
   306  }
   307  
   308  func TestIPMask(t *testing.T) {
   309  	for _, tt := range ipMaskTests {
   310  		if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
   311  			t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
   312  		}
   313  	}
   314  }
   315  
   316  var ipMaskStringTests = []struct {
   317  	in  IPMask
   318  	out string
   319  }{
   320  	{IPv4Mask(255, 255, 255, 240), "fffffff0"},
   321  	{IPv4Mask(255, 0, 128, 0), "ff008000"},
   322  	{IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
   323  	{IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
   324  	{nil, "<nil>"},
   325  }
   326  
   327  func TestIPMaskString(t *testing.T) {
   328  	for _, tt := range ipMaskStringTests {
   329  		if out := tt.in.String(); out != tt.out {
   330  			t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
   331  		}
   332  	}
   333  }
   334  
   335  func BenchmarkIPMaskString(b *testing.B) {
   336  	testHookUninstaller.Do(uninstallTestHooks)
   337  
   338  	for i := 0; i < b.N; i++ {
   339  		for _, tt := range ipMaskStringTests {
   340  			sink = tt.in.String()
   341  		}
   342  	}
   343  }
   344  
   345  var parseCIDRTests = []struct {
   346  	in  string
   347  	ip  IP
   348  	net *IPNet
   349  	err error
   350  }{
   351  	{"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
   352  	{"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   353  	{"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   354  	{"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
   355  	{"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   356  	{"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
   357  	{"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
   358  	{"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
   359  	{"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
   360  	{"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
   361  	{"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
   362  	{"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
   363  	{"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
   364  	{"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
   365  	{"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
   366  	{"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
   367  	{"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
   368  	{"192.168.1.1/255.255.255.0", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/255.255.255.0"}},
   369  	{"192.168.1.1/35", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/35"}},
   370  	{"2001:db8::1/-1", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-1"}},
   371  	{"2001:db8::1/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-0"}},
   372  	{"-0.0.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "-0.0.0.0/32"}},
   373  	{"0.-1.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.-1.0.0/32"}},
   374  	{"0.0.-2.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.-2.0/32"}},
   375  	{"0.0.0.-3/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.-3/32"}},
   376  	{"0.0.0.0/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.0/-0"}},
   377  	{"127.000.000.001/32", nil, nil, &ParseError{Type: "CIDR address", Text: "127.000.000.001/32"}},
   378  	{"", nil, nil, &ParseError{Type: "CIDR address", Text: ""}},
   379  }
   380  
   381  func TestParseCIDR(t *testing.T) {
   382  	for _, tt := range parseCIDRTests {
   383  		ip, net, err := ParseCIDR(tt.in)
   384  		if !reflect.DeepEqual(err, tt.err) {
   385  			t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
   386  		}
   387  		if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
   388  			t.Errorf("ParseCIDR(%q) = %v, {%v, %v}; want %v, {%v, %v}", tt.in, ip, net.IP, net.Mask, tt.ip, tt.net.IP, tt.net.Mask)
   389  		}
   390  	}
   391  }
   392  
   393  var ipNetContainsTests = []struct {
   394  	ip  IP
   395  	net *IPNet
   396  	ok  bool
   397  }{
   398  	{IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
   399  	{IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
   400  	{IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 0, 255, 252)}, true},
   401  	{IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 255, 0, 252)}, false},
   402  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: CIDRMask(47, 128)}, true},
   403  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: CIDRMask(47, 128)}, false},
   404  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:0:ffff::"))}, true},
   405  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("0:0:0:ffff::"))}, false},
   406  }
   407  
   408  func TestIPNetContains(t *testing.T) {
   409  	for _, tt := range ipNetContainsTests {
   410  		if ok := tt.net.Contains(tt.ip); ok != tt.ok {
   411  			t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
   412  		}
   413  	}
   414  }
   415  
   416  var ipNetStringTests = []struct {
   417  	in  *IPNet
   418  	out string
   419  }{
   420  	{&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/26"},
   421  	{&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
   422  	{&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
   423  	{&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
   424  	{nil, "<nil>"},
   425  }
   426  
   427  func TestIPNetString(t *testing.T) {
   428  	for _, tt := range ipNetStringTests {
   429  		if out := tt.in.String(); out != tt.out {
   430  			t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
   431  		}
   432  	}
   433  }
   434  
   435  var cidrMaskTests = []struct {
   436  	ones int
   437  	bits int
   438  	out  IPMask
   439  }{
   440  	{0, 32, IPv4Mask(0, 0, 0, 0)},
   441  	{12, 32, IPv4Mask(255, 240, 0, 0)},
   442  	{24, 32, IPv4Mask(255, 255, 255, 0)},
   443  	{32, 32, IPv4Mask(255, 255, 255, 255)},
   444  	{0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   445  	{4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   446  	{48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   447  	{128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
   448  	{33, 32, nil},
   449  	{32, 33, nil},
   450  	{-1, 128, nil},
   451  	{128, -1, nil},
   452  }
   453  
   454  func TestCIDRMask(t *testing.T) {
   455  	for _, tt := range cidrMaskTests {
   456  		if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
   457  			t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
   458  		}
   459  	}
   460  }
   461  
   462  var (
   463  	v4addr         = IP{192, 168, 0, 1}
   464  	v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
   465  	v6addr         = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
   466  	v4mask         = IPMask{255, 255, 255, 0}
   467  	v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
   468  	v6mask         = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
   469  	badaddr        = IP{192, 168, 0}
   470  	badmask        = IPMask{255, 255, 0}
   471  	v4maskzero     = IPMask{0, 0, 0, 0}
   472  )
   473  
   474  var networkNumberAndMaskTests = []struct {
   475  	in  IPNet
   476  	out IPNet
   477  }{
   478  	{IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
   479  	{IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
   480  	{IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
   481  	{IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
   482  	{IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
   483  	{IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
   484  	{IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mappedv6mask}},
   485  	{in: IPNet{IP: v6addr, Mask: v4mask}},
   486  	{in: IPNet{IP: v4addr, Mask: badmask}},
   487  	{in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
   488  	{in: IPNet{IP: v6addr, Mask: badmask}},
   489  	{in: IPNet{IP: badaddr, Mask: v4mask}},
   490  	{in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
   491  	{in: IPNet{IP: badaddr, Mask: v6mask}},
   492  	{in: IPNet{IP: badaddr, Mask: badmask}},
   493  }
   494  
   495  func TestNetworkNumberAndMask(t *testing.T) {
   496  	for _, tt := range networkNumberAndMaskTests {
   497  		ip, m := networkNumberAndMask(&tt.in)
   498  		out := &IPNet{IP: ip, Mask: m}
   499  		if !reflect.DeepEqual(&tt.out, out) {
   500  			t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
   501  		}
   502  	}
   503  }
   504  
   505  func TestSplitHostPort(t *testing.T) {
   506  	for _, tt := range []struct {
   507  		hostPort string
   508  		host     string
   509  		port     string
   510  	}{
   511  		// Host name
   512  		{"localhost:http", "localhost", "http"},
   513  		{"localhost:80", "localhost", "80"},
   514  
   515  		// Go-specific host name with zone identifier
   516  		{"localhost%lo0:http", "localhost%lo0", "http"},
   517  		{"localhost%lo0:80", "localhost%lo0", "80"},
   518  		{"[localhost%lo0]:http", "localhost%lo0", "http"}, // Go 1 behavior
   519  		{"[localhost%lo0]:80", "localhost%lo0", "80"},     // Go 1 behavior
   520  
   521  		// IP literal
   522  		{"127.0.0.1:http", "127.0.0.1", "http"},
   523  		{"127.0.0.1:80", "127.0.0.1", "80"},
   524  		{"[::1]:http", "::1", "http"},
   525  		{"[::1]:80", "::1", "80"},
   526  
   527  		// IP literal with zone identifier
   528  		{"[::1%lo0]:http", "::1%lo0", "http"},
   529  		{"[::1%lo0]:80", "::1%lo0", "80"},
   530  
   531  		// Go-specific wildcard for host name
   532  		{":http", "", "http"}, // Go 1 behavior
   533  		{":80", "", "80"},     // Go 1 behavior
   534  
   535  		// Go-specific wildcard for service name or transport port number
   536  		{"golang.org:", "golang.org", ""}, // Go 1 behavior
   537  		{"127.0.0.1:", "127.0.0.1", ""},   // Go 1 behavior
   538  		{"[::1]:", "::1", ""},             // Go 1 behavior
   539  
   540  		// Opaque service name
   541  		{"golang.org:https%foo", "golang.org", "https%foo"}, // Go 1 behavior
   542  	} {
   543  		if host, port, err := SplitHostPort(tt.hostPort); host != tt.host || port != tt.port || err != nil {
   544  			t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.hostPort, host, port, err, tt.host, tt.port)
   545  		}
   546  	}
   547  
   548  	for _, tt := range []struct {
   549  		hostPort string
   550  		err      string
   551  	}{
   552  		{"golang.org", "missing port in address"},
   553  		{"127.0.0.1", "missing port in address"},
   554  		{"[::1]", "missing port in address"},
   555  		{"[fe80::1%lo0]", "missing port in address"},
   556  		{"[localhost%lo0]", "missing port in address"},
   557  		{"localhost%lo0", "missing port in address"},
   558  
   559  		{"::1", "too many colons in address"},
   560  		{"fe80::1%lo0", "too many colons in address"},
   561  		{"fe80::1%lo0:80", "too many colons in address"},
   562  
   563  		// Test cases that didn't fail in Go 1
   564  
   565  		{"[foo:bar]", "missing port in address"},
   566  		{"[foo:bar]baz", "missing port in address"},
   567  		{"[foo]bar:baz", "missing port in address"},
   568  
   569  		{"[foo]:[bar]:baz", "too many colons in address"},
   570  
   571  		{"[foo]:[bar]baz", "unexpected '[' in address"},
   572  		{"foo[bar]:baz", "unexpected '[' in address"},
   573  
   574  		{"foo]bar:baz", "unexpected ']' in address"},
   575  	} {
   576  		if host, port, err := SplitHostPort(tt.hostPort); err == nil {
   577  			t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
   578  		} else {
   579  			e := err.(*AddrError)
   580  			if e.Err != tt.err {
   581  				t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
   582  			}
   583  			if host != "" || port != "" {
   584  				t.Errorf("SplitHostPort(%q) = %q, %q, err; want %q, %q, err on failure", tt.hostPort, host, port, "", "")
   585  			}
   586  		}
   587  	}
   588  }
   589  
   590  func TestJoinHostPort(t *testing.T) {
   591  	for _, tt := range []struct {
   592  		host     string
   593  		port     string
   594  		hostPort string
   595  	}{
   596  		// Host name
   597  		{"localhost", "http", "localhost:http"},
   598  		{"localhost", "80", "localhost:80"},
   599  
   600  		// Go-specific host name with zone identifier
   601  		{"localhost%lo0", "http", "localhost%lo0:http"},
   602  		{"localhost%lo0", "80", "localhost%lo0:80"},
   603  
   604  		// IP literal
   605  		{"127.0.0.1", "http", "127.0.0.1:http"},
   606  		{"127.0.0.1", "80", "127.0.0.1:80"},
   607  		{"::1", "http", "[::1]:http"},
   608  		{"::1", "80", "[::1]:80"},
   609  
   610  		// IP literal with zone identifier
   611  		{"::1%lo0", "http", "[::1%lo0]:http"},
   612  		{"::1%lo0", "80", "[::1%lo0]:80"},
   613  
   614  		// Go-specific wildcard for host name
   615  		{"", "http", ":http"}, // Go 1 behavior
   616  		{"", "80", ":80"},     // Go 1 behavior
   617  
   618  		// Go-specific wildcard for service name or transport port number
   619  		{"golang.org", "", "golang.org:"}, // Go 1 behavior
   620  		{"127.0.0.1", "", "127.0.0.1:"},   // Go 1 behavior
   621  		{"::1", "", "[::1]:"},             // Go 1 behavior
   622  
   623  		// Opaque service name
   624  		{"golang.org", "https%foo", "golang.org:https%foo"}, // Go 1 behavior
   625  	} {
   626  		if hostPort := JoinHostPort(tt.host, tt.port); hostPort != tt.hostPort {
   627  			t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, hostPort, tt.hostPort)
   628  		}
   629  	}
   630  }
   631  
   632  var ipAddrFamilyTests = []struct {
   633  	in  IP
   634  	af4 bool
   635  	af6 bool
   636  }{
   637  	{IPv4bcast, true, false},
   638  	{IPv4allsys, true, false},
   639  	{IPv4allrouter, true, false},
   640  	{IPv4zero, true, false},
   641  	{IPv4(224, 0, 0, 1), true, false},
   642  	{IPv4(127, 0, 0, 1), true, false},
   643  	{IPv4(240, 0, 0, 1), true, false},
   644  	{IPv6unspecified, false, true},
   645  	{IPv6loopback, false, true},
   646  	{IPv6interfacelocalallnodes, false, true},
   647  	{IPv6linklocalallnodes, false, true},
   648  	{IPv6linklocalallrouters, false, true},
   649  	{ParseIP("ff05::a:b:c:d"), false, true},
   650  	{ParseIP("fe80::1:2:3:4"), false, true},
   651  	{ParseIP("2001:db8::123:12:1"), false, true},
   652  }
   653  
   654  func TestIPAddrFamily(t *testing.T) {
   655  	for _, tt := range ipAddrFamilyTests {
   656  		if af := tt.in.To4() != nil; af != tt.af4 {
   657  			t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
   658  		}
   659  		if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
   660  			t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
   661  		}
   662  	}
   663  }
   664  
   665  var ipAddrScopeTests = []struct {
   666  	scope func(IP) bool
   667  	in    IP
   668  	ok    bool
   669  }{
   670  	{IP.IsUnspecified, IPv4zero, true},
   671  	{IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
   672  	{IP.IsUnspecified, IPv6unspecified, true},
   673  	{IP.IsUnspecified, IPv6interfacelocalallnodes, false},
   674  	{IP.IsUnspecified, nil, false},
   675  	{IP.IsLoopback, IPv4(127, 0, 0, 1), true},
   676  	{IP.IsLoopback, IPv4(127, 255, 255, 254), true},
   677  	{IP.IsLoopback, IPv4(128, 1, 2, 3), false},
   678  	{IP.IsLoopback, IPv6loopback, true},
   679  	{IP.IsLoopback, IPv6linklocalallrouters, false},
   680  	{IP.IsLoopback, nil, false},
   681  	{IP.IsMulticast, IPv4(224, 0, 0, 0), true},
   682  	{IP.IsMulticast, IPv4(239, 0, 0, 0), true},
   683  	{IP.IsMulticast, IPv4(240, 0, 0, 0), false},
   684  	{IP.IsMulticast, IPv6linklocalallnodes, true},
   685  	{IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   686  	{IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   687  	{IP.IsMulticast, nil, false},
   688  	{IP.IsInterfaceLocalMulticast, IPv4(224, 0, 0, 0), false},
   689  	{IP.IsInterfaceLocalMulticast, IPv4(0xff, 0x01, 0, 0), false},
   690  	{IP.IsInterfaceLocalMulticast, IPv6interfacelocalallnodes, true},
   691  	{IP.IsInterfaceLocalMulticast, nil, false},
   692  	{IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
   693  	{IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
   694  	{IP.IsLinkLocalMulticast, IPv4(0xff, 0x02, 0, 0), false},
   695  	{IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
   696  	{IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   697  	{IP.IsLinkLocalMulticast, nil, false},
   698  	{IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
   699  	{IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
   700  	{IP.IsLinkLocalUnicast, IPv4(0xfe, 0x80, 0, 0), false},
   701  	{IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   702  	{IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   703  	{IP.IsLinkLocalUnicast, nil, false},
   704  	{IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
   705  	{IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
   706  	{IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
   707  	{IP.IsGlobalUnicast, IPv4bcast, false},
   708  	{IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
   709  	{IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   710  	{IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   711  	{IP.IsGlobalUnicast, nil, false},
   712  	{IP.IsPrivate, nil, false},
   713  	{IP.IsPrivate, IPv4(1, 1, 1, 1), false},
   714  	{IP.IsPrivate, IPv4(9, 255, 255, 255), false},
   715  	{IP.IsPrivate, IPv4(10, 0, 0, 0), true},
   716  	{IP.IsPrivate, IPv4(10, 255, 255, 255), true},
   717  	{IP.IsPrivate, IPv4(11, 0, 0, 0), false},
   718  	{IP.IsPrivate, IPv4(172, 15, 255, 255), false},
   719  	{IP.IsPrivate, IPv4(172, 16, 0, 0), true},
   720  	{IP.IsPrivate, IPv4(172, 16, 255, 255), true},
   721  	{IP.IsPrivate, IPv4(172, 23, 18, 255), true},
   722  	{IP.IsPrivate, IPv4(172, 31, 255, 255), true},
   723  	{IP.IsPrivate, IPv4(172, 31, 0, 0), true},
   724  	{IP.IsPrivate, IPv4(172, 32, 0, 0), false},
   725  	{IP.IsPrivate, IPv4(192, 167, 255, 255), false},
   726  	{IP.IsPrivate, IPv4(192, 168, 0, 0), true},
   727  	{IP.IsPrivate, IPv4(192, 168, 255, 255), true},
   728  	{IP.IsPrivate, IPv4(192, 169, 0, 0), false},
   729  	{IP.IsPrivate, IP{0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, false},
   730  	{IP.IsPrivate, IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   731  	{IP.IsPrivate, IP{0xfc, 0xff, 0x12, 0, 0, 0, 0, 0x44, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   732  	{IP.IsPrivate, IP{0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, true},
   733  	{IP.IsPrivate, IP{0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   734  }
   735  
   736  func name(f any) string {
   737  	return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
   738  }
   739  
   740  func TestIPAddrScope(t *testing.T) {
   741  	for _, tt := range ipAddrScopeTests {
   742  		if ok := tt.scope(tt.in); ok != tt.ok {
   743  			t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
   744  		}
   745  		ip := tt.in.To4()
   746  		if ip == nil {
   747  			continue
   748  		}
   749  		if ok := tt.scope(ip); ok != tt.ok {
   750  			t.Errorf("%s(%q) = %v, want %v", name(tt.scope), ip, ok, tt.ok)
   751  		}
   752  	}
   753  }
   754  
   755  func BenchmarkIPEqual(b *testing.B) {
   756  	b.Run("IPv4", func(b *testing.B) {
   757  		benchmarkIPEqual(b, IPv4len)
   758  	})
   759  	b.Run("IPv6", func(b *testing.B) {
   760  		benchmarkIPEqual(b, IPv6len)
   761  	})
   762  }
   763  
   764  func benchmarkIPEqual(b *testing.B, size int) {
   765  	ips := make([]IP, 1000)
   766  	for i := range ips {
   767  		ips[i] = make(IP, size)
   768  		rand.Read(ips[i])
   769  	}
   770  	// Half of the N are equal.
   771  	for i := 0; i < b.N/2; i++ {
   772  		x := ips[i%len(ips)]
   773  		y := ips[i%len(ips)]
   774  		x.Equal(y)
   775  	}
   776  	// The other half are not equal.
   777  	for i := 0; i < b.N/2; i++ {
   778  		x := ips[i%len(ips)]
   779  		y := ips[(i+1)%len(ips)]
   780  		x.Equal(y)
   781  	}
   782  }
   783  

View as plain text