Commit 3560f3b9 authored by Mikio Hara's avatar Mikio Hara

net: make use of reflect.DeepEqual in IP manipulation tests

Also applies camel style to test case holders.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7884043
parent 5ccb7691
...@@ -5,23 +5,12 @@ ...@@ -5,23 +5,12 @@
package net package net
import ( import (
"bytes"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"
) )
func isEqual(a, b []byte) bool { var parseIPTests = []struct {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return bytes.Equal(a, b)
}
var parseiptests = []struct {
in string in string
out IP out IP
}{ }{
...@@ -37,18 +26,18 @@ var parseiptests = []struct { ...@@ -37,18 +26,18 @@ var parseiptests = []struct {
} }
func TestParseIP(t *testing.T) { func TestParseIP(t *testing.T) {
for _, tt := range parseiptests { for _, tt := range parseIPTests {
if out := ParseIP(tt.in); !isEqual(out, tt.out) { if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out) t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
} }
} }
} }
var ipstringtests = []struct { var ipStringTests = []struct {
in IP 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, 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, 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"}, {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 +50,14 @@ var ipstringtests = []struct { ...@@ -61,14 +50,14 @@ var ipstringtests = []struct {
} }
func TestIPString(t *testing.T) { func TestIPString(t *testing.T) {
for _, tt := range ipstringtests { for _, tt := range ipStringTests {
if out := tt.in.String(); out != tt.out { if out := tt.in.String(); out != tt.out {
t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out) t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
} }
} }
} }
var ipmasktests = []struct { var ipMaskTests = []struct {
in IP in IP
mask IPMask mask IPMask
out IP out IP
...@@ -82,14 +71,14 @@ var ipmasktests = []struct { ...@@ -82,14 +71,14 @@ var ipmasktests = []struct {
} }
func TestIPMask(t *testing.T) { 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) { 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) t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
} }
} }
} }
var ipmaskstringtests = []struct { var ipMaskStringTests = []struct {
in IPMask in IPMask
out string out string
}{ }{
...@@ -101,14 +90,14 @@ var ipmaskstringtests = []struct { ...@@ -101,14 +90,14 @@ var ipmaskstringtests = []struct {
} }
func TestIPMaskString(t *testing.T) { func TestIPMaskString(t *testing.T) {
for _, tt := range ipmaskstringtests { for _, tt := range ipMaskStringTests {
if out := tt.in.String(); out != tt.out { if out := tt.in.String(); out != tt.out {
t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out) t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
} }
} }
} }
var parsecidrtests = []struct { var parseCIDRTests = []struct {
in string in string
ip IP ip IP
net *IPNet net *IPNet
...@@ -138,18 +127,18 @@ var parsecidrtests = []struct { ...@@ -138,18 +127,18 @@ var parsecidrtests = []struct {
} }
func TestParseCIDR(t *testing.T) { func TestParseCIDR(t *testing.T) {
for _, tt := range parsecidrtests { for _, tt := range parseCIDRTests {
ip, net, err := ParseCIDR(tt.in) ip, net, err := ParseCIDR(tt.in)
if !reflect.DeepEqual(err, tt.err) { if !reflect.DeepEqual(err, tt.err) {
t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net) 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)) { 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) 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 ip IP
net *IPNet net *IPNet
ok bool ok bool
...@@ -165,14 +154,14 @@ var ipnetcontainstests = []struct { ...@@ -165,14 +154,14 @@ var ipnetcontainstests = []struct {
} }
func TestIPNetContains(t *testing.T) { func TestIPNetContains(t *testing.T) {
for _, tt := range ipnetcontainstests { for _, tt := range ipNetContainsTests {
if ok := tt.net.Contains(tt.ip); ok != tt.ok { 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) t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
} }
} }
} }
var ipnetstringtests = []struct { var ipNetStringTests = []struct {
in *IPNet in *IPNet
out string out string
}{ }{
...@@ -183,14 +172,14 @@ var ipnetstringtests = []struct { ...@@ -183,14 +172,14 @@ var ipnetstringtests = []struct {
} }
func TestIPNetString(t *testing.T) { func TestIPNetString(t *testing.T) {
for _, tt := range ipnetstringtests { for _, tt := range ipNetStringTests {
if out := tt.in.String(); out != tt.out { if out := tt.in.String(); out != tt.out {
t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out) t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
} }
} }
} }
var cidrmasktests = []struct { var cidrMaskTests = []struct {
ones int ones int
bits int bits int
out IPMask out IPMask
...@@ -210,8 +199,8 @@ var cidrmasktests = []struct { ...@@ -210,8 +199,8 @@ var cidrmasktests = []struct {
} }
func TestCIDRMask(t *testing.T) { func TestCIDRMask(t *testing.T) {
for _, tt := range cidrmasktests { for _, tt := range cidrMaskTests {
if out := CIDRMask(tt.ones, tt.bits); !isEqual(out, tt.out) { 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) t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
} }
} }
...@@ -229,7 +218,7 @@ var ( ...@@ -229,7 +218,7 @@ var (
v4maskzero = IPMask{0, 0, 0, 0} v4maskzero = IPMask{0, 0, 0, 0}
) )
var networknumberandmasktests = []struct { var networkNumberAndMaskTests = []struct {
in IPNet in IPNet
out IPNet out IPNet
}{ }{
...@@ -251,19 +240,19 @@ var networknumberandmasktests = []struct { ...@@ -251,19 +240,19 @@ var networknumberandmasktests = []struct {
} }
func TestNetworkNumberAndMask(t *testing.T) { func TestNetworkNumberAndMask(t *testing.T) {
for _, tt := range networknumberandmasktests { for _, tt := range networkNumberAndMaskTests {
ip, m := networkNumberAndMask(&tt.in) ip, m := networkNumberAndMask(&tt.in)
out := &IPNet{IP: ip, Mask: m} out := &IPNet{IP: ip, Mask: m}
if !reflect.DeepEqual(&tt.out, out) { 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 { var splitJoinTests = []struct {
Host string host string
Port string port string
Join string join string
}{ }{
{"www.google.com", "80", "www.google.com:80"}, {"www.google.com", "80", "www.google.com:80"},
{"127.0.0.1", "1234", "127.0.0.1:1234"}, {"127.0.0.1", "1234", "127.0.0.1:1234"},
...@@ -274,9 +263,9 @@ var splitjointests = []struct { ...@@ -274,9 +263,9 @@ var splitjointests = []struct {
{"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour {"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour
} }
var splitfailuretests = []struct { var splitFailureTests = []struct {
HostPort string hostPort string
Err string err string
}{ }{
{"www.google.com", "missing port in address"}, {"www.google.com", "missing port in address"},
{"127.0.0.1", "missing port in address"}, {"127.0.0.1", "missing port in address"},
...@@ -294,32 +283,32 @@ var splitfailuretests = []struct { ...@@ -294,32 +283,32 @@ var splitfailuretests = []struct {
} }
func TestSplitHostPort(t *testing.T) { func TestSplitHostPort(t *testing.T) {
for _, tt := range splitjointests { for _, tt := range splitJoinTests {
if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil { 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) t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.join, host, port, err, tt.host, tt.port)
} }
} }
for _, tt := range splitfailuretests { for _, tt := range splitFailureTests {
if _, _, err := SplitHostPort(tt.HostPort); err == nil { if _, _, err := SplitHostPort(tt.hostPort); err == nil {
t.Errorf("SplitHostPort(%q) should have failed", tt.HostPort) t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
} else { } else {
e := err.(*AddrError) e := err.(*AddrError)
if e.Err != tt.Err { if e.Err != tt.err {
t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.HostPort, e.Err, tt.Err) t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
} }
} }
} }
} }
func TestJoinHostPort(t *testing.T) { func TestJoinHostPort(t *testing.T) {
for _, tt := range splitjointests { for _, tt := range splitJoinTests {
if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join { 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) t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join)
} }
} }
} }
var ipaftests = []struct { var ipAddrFamilyTests = []struct {
in IP in IP
af4 bool af4 bool
af6 bool af6 bool
...@@ -342,7 +331,7 @@ var ipaftests = []struct { ...@@ -342,7 +331,7 @@ var ipaftests = []struct {
} }
func TestIPAddrFamily(t *testing.T) { func TestIPAddrFamily(t *testing.T) {
for _, tt := range ipaftests { for _, tt := range ipAddrFamilyTests {
if af := tt.in.To4() != nil; af != tt.af4 { 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) t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
} }
...@@ -352,7 +341,7 @@ func TestIPAddrFamily(t *testing.T) { ...@@ -352,7 +341,7 @@ func TestIPAddrFamily(t *testing.T) {
} }
} }
var ipscopetests = []struct { var ipAddrScopeTests = []struct {
scope func(IP) bool scope func(IP) bool
in IP in IP
ok bool ok bool
...@@ -393,7 +382,7 @@ func name(f interface{}) string { ...@@ -393,7 +382,7 @@ func name(f interface{}) string {
} }
func TestIPAddrScope(t *testing.T) { func TestIPAddrScope(t *testing.T) {
for _, tt := range ipscopetests { for _, tt := range ipAddrScopeTests {
if ok := tt.scope(tt.in); ok != tt.ok { if ok := tt.scope(tt.in); ok != tt.ok {
t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok) t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment