github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/ip_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  	"reflect"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  var parseIPTests = []struct {
    14  	in  string
    15  	out IP
    16  }{
    17  	{"127.0.1.2", IPv4(127, 0, 1, 2)},
    18  	{"127.0.0.1", IPv4(127, 0, 0, 1)},
    19  	{"127.0.0.256", nil},
    20  	{"abc", nil},
    21  	{"123:", nil},
    22  	{"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
    23  	{"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
    24  	{"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
    25  	{"fe80::1%lo0", nil},
    26  	{"fe80::1%911", nil},
    27  	{"", nil},
    28  }
    29  
    30  func TestParseIP(t *testing.T) {
    31  	for _, tt := range parseIPTests {
    32  		if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
    33  			t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
    34  		}
    35  		if tt.in == "" {
    36  			// Tested in TestMarshalEmptyIP below.
    37  			continue
    38  		}
    39  		var out IP
    40  		if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
    41  			t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
    42  		}
    43  	}
    44  }
    45  
    46  // Issue 6339
    47  func TestMarshalEmptyIP(t *testing.T) {
    48  	for _, in := range [][]byte{nil, []byte("")} {
    49  		var out = IP{1, 2, 3, 4}
    50  		if err := out.UnmarshalText(in); err != nil || out != nil {
    51  			t.Errorf("UnmarshalText(%v) = %v, %v; want nil, nil", in, out, err)
    52  		}
    53  	}
    54  	var ip IP
    55  	got, err := ip.MarshalText()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if !reflect.DeepEqual(got, []byte("")) {
    60  		t.Errorf(`got %#v, want []byte("")`, got)
    61  	}
    62  }
    63  
    64  var ipStringTests = []struct {
    65  	in  IP
    66  	out string // see RFC 5952
    67  }{
    68  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
    69  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1"},
    70  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, "2001:db8:0:1:0:1:0:1"},
    71  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, "2001:db8:1:0:1:0:1:0"},
    72  	{IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0:0:1"},
    73  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:db8:0:0:1::"},
    74  	{IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1:0:0:1"},
    75  	{IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, "2001:db8::a:b:c:d"},
    76  	{IPv4(192, 168, 0, 1), "192.168.0.1"},
    77  	{nil, ""},
    78  }
    79  
    80  func TestIPString(t *testing.T) {
    81  	for _, tt := range ipStringTests {
    82  		if tt.in != nil {
    83  			if out := tt.in.String(); out != tt.out {
    84  				t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
    85  			}
    86  		}
    87  		if out, err := tt.in.MarshalText(); string(out) != tt.out || err != nil {
    88  			t.Errorf("IP.MarshalText(%v) = %q, %v, want %q, nil", tt.in, out, err, tt.out)
    89  		}
    90  	}
    91  }
    92  
    93  var ipMaskTests = []struct {
    94  	in   IP
    95  	mask IPMask
    96  	out  IP
    97  }{
    98  	{IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
    99  	{IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
   100  	{IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
   101  	{IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
   102  	{ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
   103  	{ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
   104  }
   105  
   106  func TestIPMask(t *testing.T) {
   107  	for _, tt := range ipMaskTests {
   108  		if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
   109  			t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
   110  		}
   111  	}
   112  }
   113  
   114  var ipMaskStringTests = []struct {
   115  	in  IPMask
   116  	out string
   117  }{
   118  	{IPv4Mask(255, 255, 255, 240), "fffffff0"},
   119  	{IPv4Mask(255, 0, 128, 0), "ff008000"},
   120  	{IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
   121  	{IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
   122  	{nil, "<nil>"},
   123  }
   124  
   125  func TestIPMaskString(t *testing.T) {
   126  	for _, tt := range ipMaskStringTests {
   127  		if out := tt.in.String(); out != tt.out {
   128  			t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
   129  		}
   130  	}
   131  }
   132  
   133  var parseCIDRTests = []struct {
   134  	in  string
   135  	ip  IP
   136  	net *IPNet
   137  	err error
   138  }{
   139  	{"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
   140  	{"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   141  	{"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   142  	{"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
   143  	{"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
   144  	{"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
   145  	{"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
   146  	{"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
   147  	{"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
   148  	{"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
   149  	{"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
   150  	{"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
   151  	{"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
   152  	{"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
   153  	{"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
   154  	{"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
   155  	{"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
   156  	{"192.168.1.1/255.255.255.0", nil, nil, &ParseError{"CIDR address", "192.168.1.1/255.255.255.0"}},
   157  	{"192.168.1.1/35", nil, nil, &ParseError{"CIDR address", "192.168.1.1/35"}},
   158  	{"2001:db8::1/-1", nil, nil, &ParseError{"CIDR address", "2001:db8::1/-1"}},
   159  	{"", nil, nil, &ParseError{"CIDR address", ""}},
   160  }
   161  
   162  func TestParseCIDR(t *testing.T) {
   163  	for _, tt := range parseCIDRTests {
   164  		ip, net, err := ParseCIDR(tt.in)
   165  		if !reflect.DeepEqual(err, tt.err) {
   166  			t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
   167  		}
   168  		if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
   169  			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)
   170  		}
   171  	}
   172  }
   173  
   174  var ipNetContainsTests = []struct {
   175  	ip  IP
   176  	net *IPNet
   177  	ok  bool
   178  }{
   179  	{IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
   180  	{IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
   181  	{IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 0, 255, 252)}, true},
   182  	{IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 255, 0, 252)}, false},
   183  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: CIDRMask(47, 128)}, true},
   184  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: CIDRMask(47, 128)}, false},
   185  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:0:ffff::"))}, true},
   186  	{ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("0:0:0:ffff::"))}, false},
   187  }
   188  
   189  func TestIPNetContains(t *testing.T) {
   190  	for _, tt := range ipNetContainsTests {
   191  		if ok := tt.net.Contains(tt.ip); ok != tt.ok {
   192  			t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
   193  		}
   194  	}
   195  }
   196  
   197  var ipNetStringTests = []struct {
   198  	in  *IPNet
   199  	out string
   200  }{
   201  	{&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/26"},
   202  	{&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
   203  	{&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
   204  	{&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
   205  }
   206  
   207  func TestIPNetString(t *testing.T) {
   208  	for _, tt := range ipNetStringTests {
   209  		if out := tt.in.String(); out != tt.out {
   210  			t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
   211  		}
   212  	}
   213  }
   214  
   215  var cidrMaskTests = []struct {
   216  	ones int
   217  	bits int
   218  	out  IPMask
   219  }{
   220  	{0, 32, IPv4Mask(0, 0, 0, 0)},
   221  	{12, 32, IPv4Mask(255, 240, 0, 0)},
   222  	{24, 32, IPv4Mask(255, 255, 255, 0)},
   223  	{32, 32, IPv4Mask(255, 255, 255, 255)},
   224  	{0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   225  	{4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   226  	{48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
   227  	{128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
   228  	{33, 32, nil},
   229  	{32, 33, nil},
   230  	{-1, 128, nil},
   231  	{128, -1, nil},
   232  }
   233  
   234  func TestCIDRMask(t *testing.T) {
   235  	for _, tt := range cidrMaskTests {
   236  		if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
   237  			t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
   238  		}
   239  	}
   240  }
   241  
   242  var (
   243  	v4addr         = IP{192, 168, 0, 1}
   244  	v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
   245  	v6addr         = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
   246  	v4mask         = IPMask{255, 255, 255, 0}
   247  	v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
   248  	v6mask         = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
   249  	badaddr        = IP{192, 168, 0}
   250  	badmask        = IPMask{255, 255, 0}
   251  	v4maskzero     = IPMask{0, 0, 0, 0}
   252  )
   253  
   254  var networkNumberAndMaskTests = []struct {
   255  	in  IPNet
   256  	out IPNet
   257  }{
   258  	{IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
   259  	{IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
   260  	{IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
   261  	{IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
   262  	{IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
   263  	{IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
   264  	{IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mappedv6mask}},
   265  	{in: IPNet{IP: v6addr, Mask: v4mask}},
   266  	{in: IPNet{IP: v4addr, Mask: badmask}},
   267  	{in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
   268  	{in: IPNet{IP: v6addr, Mask: badmask}},
   269  	{in: IPNet{IP: badaddr, Mask: v4mask}},
   270  	{in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
   271  	{in: IPNet{IP: badaddr, Mask: v6mask}},
   272  	{in: IPNet{IP: badaddr, Mask: badmask}},
   273  }
   274  
   275  func TestNetworkNumberAndMask(t *testing.T) {
   276  	for _, tt := range networkNumberAndMaskTests {
   277  		ip, m := networkNumberAndMask(&tt.in)
   278  		out := &IPNet{IP: ip, Mask: m}
   279  		if !reflect.DeepEqual(&tt.out, out) {
   280  			t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
   281  		}
   282  	}
   283  }
   284  
   285  var splitJoinTests = []struct {
   286  	host string
   287  	port string
   288  	join string
   289  }{
   290  	{"www.google.com", "80", "www.google.com:80"},
   291  	{"127.0.0.1", "1234", "127.0.0.1:1234"},
   292  	{"::1", "80", "[::1]:80"},
   293  	{"fe80::1%lo0", "80", "[fe80::1%lo0]:80"},
   294  	{"localhost%lo0", "80", "[localhost%lo0]:80"},
   295  	{"", "0", ":0"},
   296  
   297  	{"google.com", "https%foo", "google.com:https%foo"}, // Go 1.0 behavior
   298  	{"127.0.0.1", "", "127.0.0.1:"},                     // Go 1.0 behaviour
   299  	{"www.google.com", "", "www.google.com:"},           // Go 1.0 behaviour
   300  }
   301  
   302  var splitFailureTests = []struct {
   303  	hostPort string
   304  	err      string
   305  }{
   306  	{"www.google.com", "missing port in address"},
   307  	{"127.0.0.1", "missing port in address"},
   308  	{"[::1]", "missing port in address"},
   309  	{"[fe80::1%lo0]", "missing port in address"},
   310  	{"[localhost%lo0]", "missing port in address"},
   311  	{"localhost%lo0", "missing port in address"},
   312  
   313  	{"::1", "too many colons in address"},
   314  	{"fe80::1%lo0", "too many colons in address"},
   315  	{"fe80::1%lo0:80", "too many colons in address"},
   316  
   317  	{"localhost%lo0:80", "missing brackets in address"},
   318  
   319  	// Test cases that didn't fail in Go 1.0
   320  
   321  	{"[foo:bar]", "missing port in address"},
   322  	{"[foo:bar]baz", "missing port in address"},
   323  	{"[foo]bar:baz", "missing port in address"},
   324  
   325  	{"[foo]:[bar]:baz", "too many colons in address"},
   326  
   327  	{"[foo]:[bar]baz", "unexpected '[' in address"},
   328  	{"foo[bar]:baz", "unexpected '[' in address"},
   329  
   330  	{"foo]bar:baz", "unexpected ']' in address"},
   331  }
   332  
   333  func TestSplitHostPort(t *testing.T) {
   334  	for _, tt := range splitJoinTests {
   335  		if host, port, err := SplitHostPort(tt.join); host != tt.host || port != tt.port || err != nil {
   336  			t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.join, host, port, err, tt.host, tt.port)
   337  		}
   338  	}
   339  	for _, tt := range splitFailureTests {
   340  		if _, _, err := SplitHostPort(tt.hostPort); err == nil {
   341  			t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
   342  		} else {
   343  			e := err.(*AddrError)
   344  			if e.Err != tt.err {
   345  				t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
   346  			}
   347  		}
   348  	}
   349  }
   350  
   351  func TestJoinHostPort(t *testing.T) {
   352  	for _, tt := range splitJoinTests {
   353  		if join := JoinHostPort(tt.host, tt.port); join != tt.join {
   354  			t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join)
   355  		}
   356  	}
   357  }
   358  
   359  var ipAddrFamilyTests = []struct {
   360  	in  IP
   361  	af4 bool
   362  	af6 bool
   363  }{
   364  	{IPv4bcast, true, false},
   365  	{IPv4allsys, true, false},
   366  	{IPv4allrouter, true, false},
   367  	{IPv4zero, true, false},
   368  	{IPv4(224, 0, 0, 1), true, false},
   369  	{IPv4(127, 0, 0, 1), true, false},
   370  	{IPv4(240, 0, 0, 1), true, false},
   371  	{IPv6unspecified, false, true},
   372  	{IPv6loopback, false, true},
   373  	{IPv6interfacelocalallnodes, false, true},
   374  	{IPv6linklocalallnodes, false, true},
   375  	{IPv6linklocalallrouters, false, true},
   376  	{ParseIP("ff05::a:b:c:d"), false, true},
   377  	{ParseIP("fe80::1:2:3:4"), false, true},
   378  	{ParseIP("2001:db8::123:12:1"), false, true},
   379  }
   380  
   381  func TestIPAddrFamily(t *testing.T) {
   382  	for _, tt := range ipAddrFamilyTests {
   383  		if af := tt.in.To4() != nil; af != tt.af4 {
   384  			t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
   385  		}
   386  		if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
   387  			t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
   388  		}
   389  	}
   390  }
   391  
   392  var ipAddrScopeTests = []struct {
   393  	scope func(IP) bool
   394  	in    IP
   395  	ok    bool
   396  }{
   397  	{IP.IsUnspecified, IPv4zero, true},
   398  	{IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
   399  	{IP.IsUnspecified, IPv6unspecified, true},
   400  	{IP.IsUnspecified, IPv6interfacelocalallnodes, false},
   401  	{IP.IsLoopback, IPv4(127, 0, 0, 1), true},
   402  	{IP.IsLoopback, IPv4(127, 255, 255, 254), true},
   403  	{IP.IsLoopback, IPv4(128, 1, 2, 3), false},
   404  	{IP.IsLoopback, IPv6loopback, true},
   405  	{IP.IsLoopback, IPv6linklocalallrouters, false},
   406  	{IP.IsMulticast, IPv4(224, 0, 0, 0), true},
   407  	{IP.IsMulticast, IPv4(239, 0, 0, 0), true},
   408  	{IP.IsMulticast, IPv4(240, 0, 0, 0), false},
   409  	{IP.IsMulticast, IPv6linklocalallnodes, true},
   410  	{IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   411  	{IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   412  	{IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
   413  	{IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
   414  	{IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
   415  	{IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   416  	{IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
   417  	{IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
   418  	{IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
   419  	{IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   420  	{IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
   421  	{IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
   422  	{IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
   423  	{IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
   424  	{IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   425  	{IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
   426  }
   427  
   428  func name(f interface{}) string {
   429  	return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
   430  }
   431  
   432  func TestIPAddrScope(t *testing.T) {
   433  	for _, tt := range ipAddrScopeTests {
   434  		if ok := tt.scope(tt.in); ok != tt.ok {
   435  			t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
   436  		}
   437  	}
   438  }