diff options
Diffstat (limited to 'libgo/go/net/ip_test.go')
-rw-r--r-- | libgo/go/net/ip_test.go | 137 |
1 files changed, 87 insertions, 50 deletions
diff --git a/libgo/go/net/ip_test.go b/libgo/go/net/ip_test.go index 8324d2a327c..16f30d446b5 100644 --- a/libgo/go/net/ip_test.go +++ b/libgo/go/net/ip_test.go @@ -5,23 +5,12 @@ package net import ( - "bytes" "reflect" "runtime" "testing" ) -func isEqual(a, b []byte) bool { - if a == nil && b == nil { - return true - } - if a == nil || b == nil { - return false - } - return bytes.Equal(a, b) -} - -var parseiptests = []struct { +var parseIPTests = []struct { in string out IP }{ @@ -33,22 +22,23 @@ var parseiptests = []struct { {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)}, {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}}, {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)}, + {"fe80::1%lo0", nil}, + {"fe80::1%911", nil}, {"", nil}, } func TestParseIP(t *testing.T) { - for _, tt := range parseiptests { - if out := ParseIP(tt.in); !isEqual(out, tt.out) { + for _, tt := range parseIPTests { + if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) { t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out) } } } -var ipstringtests = []struct { +var ipStringTests = []struct { in IP - out string + out string // see RFC 5952 }{ - // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation) {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"}, {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1"}, {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"}, @@ -61,14 +51,14 @@ var ipstringtests = []struct { } func TestIPString(t *testing.T) { - for _, tt := range ipstringtests { + for _, tt := range ipStringTests { if out := tt.in.String(); out != tt.out { t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out) } } } -var ipmasktests = []struct { +var ipMaskTests = []struct { in IP mask IPMask out IP @@ -82,14 +72,14 @@ var ipmasktests = []struct { } func TestIPMask(t *testing.T) { - for _, tt := range ipmasktests { + for _, tt := range ipMaskTests { if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) { t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out) } } } -var ipmaskstringtests = []struct { +var ipMaskStringTests = []struct { in IPMask out string }{ @@ -101,14 +91,14 @@ var ipmaskstringtests = []struct { } func TestIPMaskString(t *testing.T) { - for _, tt := range ipmaskstringtests { + for _, tt := range ipMaskStringTests { if out := tt.in.String(); out != tt.out { t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out) } } } -var parsecidrtests = []struct { +var parseCIDRTests = []struct { in string ip IP net *IPNet @@ -138,18 +128,18 @@ var parsecidrtests = []struct { } func TestParseCIDR(t *testing.T) { - for _, tt := range parsecidrtests { + for _, tt := range parseCIDRTests { ip, net, err := ParseCIDR(tt.in) if !reflect.DeepEqual(err, tt.err) { t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net) } - if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !isEqual(net.Mask, tt.net.Mask)) { - 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) + if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) { + 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) } } } -var ipnetcontainstests = []struct { +var ipNetContainsTests = []struct { ip IP net *IPNet ok bool @@ -165,14 +155,14 @@ var ipnetcontainstests = []struct { } func TestIPNetContains(t *testing.T) { - for _, tt := range ipnetcontainstests { + for _, tt := range ipNetContainsTests { if ok := tt.net.Contains(tt.ip); ok != tt.ok { t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok) } } } -var ipnetstringtests = []struct { +var ipNetStringTests = []struct { in *IPNet out string }{ @@ -183,14 +173,14 @@ var ipnetstringtests = []struct { } func TestIPNetString(t *testing.T) { - for _, tt := range ipnetstringtests { + for _, tt := range ipNetStringTests { if out := tt.in.String(); out != tt.out { t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out) } } } -var cidrmasktests = []struct { +var cidrMaskTests = []struct { ones int bits int out IPMask @@ -210,8 +200,8 @@ var cidrmasktests = []struct { } func TestCIDRMask(t *testing.T) { - for _, tt := range cidrmasktests { - if out := CIDRMask(tt.ones, tt.bits); !isEqual(out, tt.out) { + for _, tt := range cidrMaskTests { + if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) { t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out) } } @@ -229,7 +219,7 @@ var ( v4maskzero = IPMask{0, 0, 0, 0} ) -var networknumberandmasktests = []struct { +var networkNumberAndMaskTests = []struct { in IPNet out IPNet }{ @@ -251,43 +241,90 @@ var networknumberandmasktests = []struct { } func TestNetworkNumberAndMask(t *testing.T) { - for _, tt := range networknumberandmasktests { + for _, tt := range networkNumberAndMaskTests { ip, m := networkNumberAndMask(&tt.in) out := &IPNet{IP: ip, Mask: m} if !reflect.DeepEqual(&tt.out, out) { - t.Errorf("networkNumberAndMask(%v) = %v; want %v", tt.in, out, &tt.out) + t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out) } } } -var splitjointests = []struct { - Host string - Port string - Join string +var splitJoinTests = []struct { + host string + port string + join string }{ {"www.google.com", "80", "www.google.com:80"}, {"127.0.0.1", "1234", "127.0.0.1:1234"}, {"::1", "80", "[::1]:80"}, + {"fe80::1%lo0", "80", "[fe80::1%lo0]:80"}, + {"localhost%lo0", "80", "[localhost%lo0]:80"}, + {"", "0", ":0"}, + {"google.com", "https%foo", "google.com:https%foo"}, // Go 1.0 behavior + {"127.0.0.1", "", "127.0.0.1:"}, // Go 1.0 behaviour + {"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour +} + +var splitFailureTests = []struct { + hostPort string + err string +}{ + {"www.google.com", "missing port in address"}, + {"127.0.0.1", "missing port in address"}, + {"[::1]", "missing port in address"}, + {"[fe80::1%lo0]", "missing port in address"}, + {"[localhost%lo0]", "missing port in address"}, + {"localhost%lo0", "missing port in address"}, + + {"::1", "too many colons in address"}, + {"fe80::1%lo0", "too many colons in address"}, + {"fe80::1%lo0:80", "too many colons in address"}, + + {"localhost%lo0:80", "missing brackets in address"}, + + // Test cases that didn't fail in Go 1.0 + + {"[foo:bar]", "missing port in address"}, + {"[foo:bar]baz", "missing port in address"}, + {"[foo]bar:baz", "missing port in address"}, + + {"[foo]:[bar]:baz", "too many colons in address"}, + + {"[foo]:[bar]baz", "unexpected '[' in address"}, + {"foo[bar]:baz", "unexpected '[' in address"}, + + {"foo]bar:baz", "unexpected ']' in address"}, } func TestSplitHostPort(t *testing.T) { - for _, tt := range splitjointests { - if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil { - t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.Join, host, port, err, tt.Host, tt.Port) + for _, tt := range splitJoinTests { + if host, port, err := SplitHostPort(tt.join); host != tt.host || port != tt.port || err != nil { + t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.join, host, port, err, tt.host, tt.port) + } + } + for _, tt := range splitFailureTests { + if _, _, err := SplitHostPort(tt.hostPort); err == nil { + t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort) + } else { + e := err.(*AddrError) + if e.Err != tt.err { + t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err) + } } } } func TestJoinHostPort(t *testing.T) { - for _, tt := range splitjointests { - if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join { - t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.Host, tt.Port, join, tt.Join) + for _, tt := range splitJoinTests { + if join := JoinHostPort(tt.host, tt.port); join != tt.join { + t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join) } } } -var ipaftests = []struct { +var ipAddrFamilyTests = []struct { in IP af4 bool af6 bool @@ -310,7 +347,7 @@ var ipaftests = []struct { } func TestIPAddrFamily(t *testing.T) { - for _, tt := range ipaftests { + for _, tt := range ipAddrFamilyTests { if af := tt.in.To4() != nil; af != tt.af4 { t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4) } @@ -320,7 +357,7 @@ func TestIPAddrFamily(t *testing.T) { } } -var ipscopetests = []struct { +var ipAddrScopeTests = []struct { scope func(IP) bool in IP ok bool @@ -361,7 +398,7 @@ func name(f interface{}) string { } func TestIPAddrScope(t *testing.T) { - for _, tt := range ipscopetests { + for _, tt := range ipAddrScopeTests { if ok := tt.scope(tt.in); ok != tt.ok { t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok) } |