Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1266)

Side by Side Diff: src/pkg/net/ip_test.go

Issue 7884043: code review 7884043: net: make use of reflect.DeepEqual in IP manipulation tests (Closed)
Patch Set: diff -r b4128b295144 https://code.google.com/p/go Created 12 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package net 5 package net
6 6
7 import ( 7 import (
8 "bytes"
9 "reflect" 8 "reflect"
10 "runtime" 9 "runtime"
11 "testing" 10 "testing"
12 ) 11 )
13 12
14 func isEqual(a, b []byte) bool { 13 var parseIPTests = []struct {
15 » if a == nil && b == nil {
16 » » return true
17 » }
18 » if a == nil || b == nil {
19 » » return false
20 » }
21 » return bytes.Equal(a, b)
22 }
23
24 var parseiptests = []struct {
25 in string 14 in string
26 out IP 15 out IP
27 }{ 16 }{
28 {"127.0.1.2", IPv4(127, 0, 1, 2)}, 17 {"127.0.1.2", IPv4(127, 0, 1, 2)},
29 {"127.0.0.1", IPv4(127, 0, 0, 1)}, 18 {"127.0.0.1", IPv4(127, 0, 0, 1)},
30 {"127.0.0.256", nil}, 19 {"127.0.0.256", nil},
31 {"abc", nil}, 20 {"abc", nil},
32 {"123:", nil}, 21 {"123:", nil},
33 {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)}, 22 {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
34 {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}}, 23 {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
35 {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)}, 24 {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
36 {"", nil}, 25 {"", nil},
37 } 26 }
38 27
39 func TestParseIP(t *testing.T) { 28 func TestParseIP(t *testing.T) {
40 » for _, tt := range parseiptests { 29 » for _, tt := range parseIPTests {
41 » » if out := ParseIP(tt.in); !isEqual(out, tt.out) { 30 » » if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
42 t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out ) 31 t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out )
43 } 32 }
44 } 33 }
45 } 34 }
46 35
47 var ipstringtests = []struct { 36 var ipStringTests = []struct {
48 in IP 37 in IP
49 » out string 38 » out string // see RFC 5952
50 }{ 39 }{
51 » // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation) 40
52 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"}, 41 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
53 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:d b8::1"}, 42 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:d b8::1"},
54 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, "20 01:db8:0:1:0:1:0:1"}, 43 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, "20 01:db8:0:1:0:1:0:1"},
55 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, "20 01:db8:1:0:1:0:1:0"}, 44 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, "20 01:db8:1:0:1:0:1:0"},
56 {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0 :0:1"}, 45 {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0 :0:1"},
57 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:d b8:0:0:1::"}, 46 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:d b8:0:0:1::"},
58 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001 :db8::1:0:0:1"}, 47 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001 :db8::1:0:0:1"},
59 {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, " 2001:db8::a:b:c:d"}, 48 {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, " 2001:db8::a:b:c:d"},
60 {nil, "<nil>"}, 49 {nil, "<nil>"},
61 } 50 }
62 51
63 func TestIPString(t *testing.T) { 52 func TestIPString(t *testing.T) {
64 » for _, tt := range ipstringtests { 53 » for _, tt := range ipStringTests {
65 if out := tt.in.String(); out != tt.out { 54 if out := tt.in.String(); out != tt.out {
66 t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.o ut) 55 t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.o ut)
67 } 56 }
68 } 57 }
69 } 58 }
70 59
71 var ipmasktests = []struct { 60 var ipMaskTests = []struct {
72 in IP 61 in IP
73 mask IPMask 62 mask IPMask
74 out IP 63 out IP
75 }{ 64 }{
76 {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)}, 65 {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
77 {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 1 68, 1, 64)}, 66 {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 1 68, 1, 64)},
78 {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:f fff:ffe0")), IPv4(192, 168, 1, 96)}, 67 {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:f fff:ffe0")), IPv4(192, 168, 1, 96)},
79 {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)}, 68 {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
80 {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d 80::")}, 69 {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d 80::")},
81 {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d 08::")}, 70 {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d 08::")},
82 } 71 }
83 72
84 func TestIPMask(t *testing.T) { 73 func TestIPMask(t *testing.T) {
85 » for _, tt := range ipmasktests { 74 » for _, tt := range ipMaskTests {
86 if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) { 75 if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
87 t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask , out, tt.out) 76 t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask , out, tt.out)
88 } 77 }
89 } 78 }
90 } 79 }
91 80
92 var ipmaskstringtests = []struct { 81 var ipMaskStringTests = []struct {
93 in IPMask 82 in IPMask
94 out string 83 out string
95 }{ 84 }{
96 {IPv4Mask(255, 255, 255, 240), "fffffff0"}, 85 {IPv4Mask(255, 255, 255, 240), "fffffff0"},
97 {IPv4Mask(255, 0, 128, 0), "ff008000"}, 86 {IPv4Mask(255, 0, 128, 0), "ff008000"},
98 {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"}, 87 {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
99 {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000 "}, 88 {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000 "},
100 {nil, "<nil>"}, 89 {nil, "<nil>"},
101 } 90 }
102 91
103 func TestIPMaskString(t *testing.T) { 92 func TestIPMaskString(t *testing.T) {
104 » for _, tt := range ipmaskstringtests { 93 » for _, tt := range ipMaskStringTests {
105 if out := tt.in.String(); out != tt.out { 94 if out := tt.in.String(); out != tt.out {
106 t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out) 95 t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
107 } 96 }
108 } 97 }
109 } 98 }
110 99
111 var parsecidrtests = []struct { 100 var parseCIDRTests = []struct {
112 in string 101 in string
113 ip IP 102 ip IP
114 net *IPNet 103 net *IPNet
115 err error 104 err error
116 }{ 105 }{
117 {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 255)}, nil}, 106 {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 255)}, nil},
118 {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4 Mask(255, 255, 255, 0)}, nil}, 107 {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4 Mask(255, 255, 255, 0)}, nil},
119 {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 0)}, nil}, 108 {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 0)}, nil},
120 {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1) , Mask: IPv4Mask(255, 255, 255, 255)}, nil}, 109 {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1) , Mask: IPv4Mask(255, 255, 255, 255)}, nil},
121 {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 0)}, nil}, 110 {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0) , Mask: IPv4Mask(255, 255, 255, 0)}, nil},
122 {"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(Pars eIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil}, 111 {"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(Pars eIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
123 {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:234 5::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil}, 112 {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:234 5::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
124 {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil}, 113 {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
125 {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil}, 114 {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
126 {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil}, 115 {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
127 {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil}, 116 {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
128 {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil}, 117 {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345 ::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
129 {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344 ::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil}, 118 {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344 ::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
130 {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300 ::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil}, 119 {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300 ::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
131 {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300 ::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil}, 120 {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300 ::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
132 {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::" ), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil}, 121 {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::" ), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
133 {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8: :"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil}, 122 {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8: :"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
134 {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{"CIDR address", "192 .168.1.1/255.255.255.0"}}, 123 {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{"CIDR address", "192 .168.1.1/255.255.255.0"}},
135 {"192.168.1.1/35", nil, nil, &ParseError{"CIDR address", "192.168.1.1/35 "}}, 124 {"192.168.1.1/35", nil, nil, &ParseError{"CIDR address", "192.168.1.1/35 "}},
136 {"2001:db8::1/-1", nil, nil, &ParseError{"CIDR address", "2001:db8::1/-1 "}}, 125 {"2001:db8::1/-1", nil, nil, &ParseError{"CIDR address", "2001:db8::1/-1 "}},
137 {"", nil, nil, &ParseError{"CIDR address", ""}}, 126 {"", nil, nil, &ParseError{"CIDR address", ""}},
138 } 127 }
139 128
140 func TestParseCIDR(t *testing.T) { 129 func TestParseCIDR(t *testing.T) {
141 » for _, tt := range parsecidrtests { 130 » for _, tt := range parseCIDRTests {
142 ip, net, err := ParseCIDR(tt.in) 131 ip, net, err := ParseCIDR(tt.in)
143 if !reflect.DeepEqual(err, tt.err) { 132 if !reflect.DeepEqual(err, tt.err) {
144 t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, i p, net, tt.ip, tt.net) 133 t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, i p, net, tt.ip, tt.net)
145 } 134 }
146 » » if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) | | !isEqual(net.Mask, tt.net.Mask)) { 135 » » if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) | | !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
147 » » » 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) 136 » » » 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)
148 } 137 }
149 } 138 }
150 } 139 }
151 140
152 var ipnetcontainstests = []struct { 141 var ipNetContainsTests = []struct {
153 ip IP 142 ip IP
154 net *IPNet 143 net *IPNet
155 ok bool 144 ok bool
156 }{ 145 }{
157 {IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true}, 146 {IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
158 {IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false}, 147 {IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
159 {IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0 , 0, 255, 252)}, true}, 148 {IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0 , 0, 255, 252)}, true},
160 {IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0 , 255, 0, 252)}, false}, 149 {IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0 , 255, 0, 252)}, false},
161 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: C IDRMask(47, 128)}, true}, 150 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: C IDRMask(47, 128)}, true},
162 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: C IDRMask(47, 128)}, false}, 151 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: C IDRMask(47, 128)}, false},
163 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: I PMask(ParseIP("ffff:0:ffff::"))}, true}, 152 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: I PMask(ParseIP("ffff:0:ffff::"))}, true},
164 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: I PMask(ParseIP("0:0:0:ffff::"))}, false}, 153 {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: I PMask(ParseIP("0:0:0:ffff::"))}, false},
165 } 154 }
166 155
167 func TestIPNetContains(t *testing.T) { 156 func TestIPNetContains(t *testing.T) {
168 » for _, tt := range ipnetcontainstests { 157 » for _, tt := range ipNetContainsTests {
169 if ok := tt.net.Contains(tt.ip); ok != tt.ok { 158 if ok := tt.net.Contains(tt.ip); ok != tt.ok {
170 t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok) 159 t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
171 } 160 }
172 } 161 }
173 } 162 }
174 163
175 var ipnetstringtests = []struct { 164 var ipNetStringTests = []struct {
176 in *IPNet 165 in *IPNet
177 out string 166 out string
178 }{ 167 }{
179 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/ 26"}, 168 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/ 26"},
180 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192. 168.1.0/ff00ff00"}, 169 {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192. 168.1.0/ff00ff00"},
181 {&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8:: /55"}, 170 {&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8:: /55"},
182 {&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:caf e::"))}, "2001:db8::/8000f1230000cafe0000000000000000"}, 171 {&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:caf e::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
183 } 172 }
184 173
185 func TestIPNetString(t *testing.T) { 174 func TestIPNetString(t *testing.T) {
186 » for _, tt := range ipnetstringtests { 175 » for _, tt := range ipNetStringTests {
187 if out := tt.in.String(); out != tt.out { 176 if out := tt.in.String(); out != tt.out {
188 t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, t t.out) 177 t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, t t.out)
189 } 178 }
190 } 179 }
191 } 180 }
192 181
193 var cidrmasktests = []struct { 182 var cidrMaskTests = []struct {
194 ones int 183 ones int
195 bits int 184 bits int
196 out IPMask 185 out IPMask
197 }{ 186 }{
198 {0, 32, IPv4Mask(0, 0, 0, 0)}, 187 {0, 32, IPv4Mask(0, 0, 0, 0)},
199 {12, 32, IPv4Mask(255, 240, 0, 0)}, 188 {12, 32, IPv4Mask(255, 240, 0, 0)},
200 {24, 32, IPv4Mask(255, 255, 255, 0)}, 189 {24, 32, IPv4Mask(255, 255, 255, 0)},
201 {32, 32, IPv4Mask(255, 255, 255, 255)}, 190 {32, 32, IPv4Mask(255, 255, 255, 255)},
202 {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, 191 {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
203 {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, 192 {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
204 {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0}}, 193 {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0}},
205 {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 194 {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
206 {33, 32, nil}, 195 {33, 32, nil},
207 {32, 33, nil}, 196 {32, 33, nil},
208 {-1, 128, nil}, 197 {-1, 128, nil},
209 {128, -1, nil}, 198 {128, -1, nil},
210 } 199 }
211 200
212 func TestCIDRMask(t *testing.T) { 201 func TestCIDRMask(t *testing.T) {
213 » for _, tt := range cidrmasktests { 202 » for _, tt := range cidrMaskTests {
214 » » if out := CIDRMask(tt.ones, tt.bits); !isEqual(out, tt.out) { 203 » » if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt .out) {
215 t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.b its, out, tt.out) 204 t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.b its, out, tt.out)
216 } 205 }
217 } 206 }
218 } 207 }
219 208
220 var ( 209 var (
221 v4addr = IP{192, 168, 0, 1} 210 v4addr = IP{192, 168, 0, 1}
222 v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1} 211 v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
223 v6addr = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0 , 0x12, 0, 0x1} 212 v6addr = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0 , 0x12, 0, 0x1}
224 v4mask = IPMask{255, 255, 255, 0} 213 v4mask = IPMask{255, 255, 255, 0}
225 v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0} 214 v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
226 v6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0} 215 v6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
227 badaddr = IP{192, 168, 0} 216 badaddr = IP{192, 168, 0}
228 badmask = IPMask{255, 255, 0} 217 badmask = IPMask{255, 255, 0}
229 v4maskzero = IPMask{0, 0, 0, 0} 218 v4maskzero = IPMask{0, 0, 0, 0}
230 ) 219 )
231 220
232 var networknumberandmasktests = []struct { 221 var networkNumberAndMaskTests = []struct {
233 in IPNet 222 in IPNet
234 out IPNet 223 out IPNet
235 }{ 224 }{
236 {IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}}, 225 {IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
237 {IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask }}, 226 {IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask }},
238 {IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask : v4mask}}, 227 {IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask : v4mask}},
239 {IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4mask zero}}, 228 {IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4mask zero}},
240 {IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}}, 229 {IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
241 {IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}}, 230 {IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
242 {IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mapp edv6mask}}, 231 {IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mapp edv6mask}},
243 {in: IPNet{IP: v6addr, Mask: v4mask}}, 232 {in: IPNet{IP: v6addr, Mask: v4mask}},
244 {in: IPNet{IP: v4addr, Mask: badmask}}, 233 {in: IPNet{IP: v4addr, Mask: badmask}},
245 {in: IPNet{IP: v4mappedv6addr, Mask: badmask}}, 234 {in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
246 {in: IPNet{IP: v6addr, Mask: badmask}}, 235 {in: IPNet{IP: v6addr, Mask: badmask}},
247 {in: IPNet{IP: badaddr, Mask: v4mask}}, 236 {in: IPNet{IP: badaddr, Mask: v4mask}},
248 {in: IPNet{IP: badaddr, Mask: v4mappedv6mask}}, 237 {in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
249 {in: IPNet{IP: badaddr, Mask: v6mask}}, 238 {in: IPNet{IP: badaddr, Mask: v6mask}},
250 {in: IPNet{IP: badaddr, Mask: badmask}}, 239 {in: IPNet{IP: badaddr, Mask: badmask}},
251 } 240 }
252 241
253 func TestNetworkNumberAndMask(t *testing.T) { 242 func TestNetworkNumberAndMask(t *testing.T) {
254 » for _, tt := range networknumberandmasktests { 243 » for _, tt := range networkNumberAndMaskTests {
255 ip, m := networkNumberAndMask(&tt.in) 244 ip, m := networkNumberAndMask(&tt.in)
256 out := &IPNet{IP: ip, Mask: m} 245 out := &IPNet{IP: ip, Mask: m}
257 if !reflect.DeepEqual(&tt.out, out) { 246 if !reflect.DeepEqual(&tt.out, out) {
258 » » » t.Errorf("networkNumberAndMask(%v) = %v; want %v", tt.in , out, &tt.out) 247 » » » t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in , out, &tt.out)
259 } 248 }
260 } 249 }
261 } 250 }
262 251
263 var splitjointests = []struct { 252 var splitJoinTests = []struct {
264 » Host string 253 » host string
265 » Port string 254 » port string
266 » Join string 255 » join string
267 }{ 256 }{
268 {"www.google.com", "80", "www.google.com:80"}, 257 {"www.google.com", "80", "www.google.com:80"},
269 {"127.0.0.1", "1234", "127.0.0.1:1234"}, 258 {"127.0.0.1", "1234", "127.0.0.1:1234"},
270 {"::1", "80", "[::1]:80"}, 259 {"::1", "80", "[::1]:80"},
271 {"google.com", "https%foo", "google.com:https%foo"}, // Go 1.0 behavior 260 {"google.com", "https%foo", "google.com:https%foo"}, // Go 1.0 behavior
272 {"", "0", ":0"}, 261 {"", "0", ":0"},
273 {"127.0.0.1", "", "127.0.0.1:"}, // Go 1.0 behaviour 262 {"127.0.0.1", "", "127.0.0.1:"}, // Go 1.0 behaviour
274 {"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour 263 {"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour
275 } 264 }
276 265
277 var splitfailuretests = []struct { 266 var splitFailureTests = []struct {
278 » HostPort string 267 » hostPort string
279 » Err string 268 » err string
280 }{ 269 }{
281 {"www.google.com", "missing port in address"}, 270 {"www.google.com", "missing port in address"},
282 {"127.0.0.1", "missing port in address"}, 271 {"127.0.0.1", "missing port in address"},
283 {"[::1]", "missing port in address"}, 272 {"[::1]", "missing port in address"},
284 {"::1", "too many colons in address"}, 273 {"::1", "too many colons in address"},
285 274
286 // Test cases that didn't fail in Go 1.0 275 // Test cases that didn't fail in Go 1.0
287 {"[foo:bar]", "missing port in address"}, 276 {"[foo:bar]", "missing port in address"},
288 {"[foo:bar]baz", "missing port in address"}, 277 {"[foo:bar]baz", "missing port in address"},
289 {"[foo]:[bar]:baz", "too many colons in address"}, 278 {"[foo]:[bar]:baz", "too many colons in address"},
290 {"[foo]bar:baz", "missing port in address"}, 279 {"[foo]bar:baz", "missing port in address"},
291 {"[foo]:[bar]baz", "unexpected '[' in address"}, 280 {"[foo]:[bar]baz", "unexpected '[' in address"},
292 {"foo[bar]:baz", "unexpected '[' in address"}, 281 {"foo[bar]:baz", "unexpected '[' in address"},
293 {"foo]bar:baz", "unexpected ']' in address"}, 282 {"foo]bar:baz", "unexpected ']' in address"},
294 } 283 }
295 284
296 func TestSplitHostPort(t *testing.T) { 285 func TestSplitHostPort(t *testing.T) {
297 » for _, tt := range splitjointests { 286 » for _, tt := range splitJoinTests {
298 » » if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil { 287 » » if host, port, err := SplitHostPort(tt.join); host != tt.host || port != tt.port || err != nil {
299 » » » t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, n il", tt.Join, host, port, err, tt.Host, tt.Port) 288 » » » t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, n il", tt.join, host, port, err, tt.host, tt.port)
300 } 289 }
301 } 290 }
302 » for _, tt := range splitfailuretests { 291 » for _, tt := range splitFailureTests {
303 » » if _, _, err := SplitHostPort(tt.HostPort); err == nil { 292 » » if _, _, err := SplitHostPort(tt.hostPort); err == nil {
304 » » » t.Errorf("SplitHostPort(%q) should have failed", tt.Host Port) 293 » » » t.Errorf("SplitHostPort(%q) should have failed", tt.host Port)
305 } else { 294 } else {
306 e := err.(*AddrError) 295 e := err.(*AddrError)
307 » » » if e.Err != tt.Err { 296 » » » if e.Err != tt.err {
308 » » » » t.Errorf("SplitHostPort(%q) = _, _, %q; want %q" , tt.HostPort, e.Err, tt.Err) 297 » » » » t.Errorf("SplitHostPort(%q) = _, _, %q; want %q" , tt.hostPort, e.Err, tt.err)
309 } 298 }
310 } 299 }
311 } 300 }
312 } 301 }
313 302
314 func TestJoinHostPort(t *testing.T) { 303 func TestJoinHostPort(t *testing.T) {
315 » for _, tt := range splitjointests { 304 » for _, tt := range splitJoinTests {
316 » » if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join { 305 » » if join := JoinHostPort(tt.host, tt.port); join != tt.join {
317 » » » t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.Host, tt.Port, join, tt.Join) 306 » » » t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join)
318 } 307 }
319 } 308 }
320 } 309 }
321 310
322 var ipaftests = []struct { 311 var ipAddrFamilyTests = []struct {
323 in IP 312 in IP
324 af4 bool 313 af4 bool
325 af6 bool 314 af6 bool
326 }{ 315 }{
327 {IPv4bcast, true, false}, 316 {IPv4bcast, true, false},
328 {IPv4allsys, true, false}, 317 {IPv4allsys, true, false},
329 {IPv4allrouter, true, false}, 318 {IPv4allrouter, true, false},
330 {IPv4zero, true, false}, 319 {IPv4zero, true, false},
331 {IPv4(224, 0, 0, 1), true, false}, 320 {IPv4(224, 0, 0, 1), true, false},
332 {IPv4(127, 0, 0, 1), true, false}, 321 {IPv4(127, 0, 0, 1), true, false},
333 {IPv4(240, 0, 0, 1), true, false}, 322 {IPv4(240, 0, 0, 1), true, false},
334 {IPv6unspecified, false, true}, 323 {IPv6unspecified, false, true},
335 {IPv6loopback, false, true}, 324 {IPv6loopback, false, true},
336 {IPv6interfacelocalallnodes, false, true}, 325 {IPv6interfacelocalallnodes, false, true},
337 {IPv6linklocalallnodes, false, true}, 326 {IPv6linklocalallnodes, false, true},
338 {IPv6linklocalallrouters, false, true}, 327 {IPv6linklocalallrouters, false, true},
339 {ParseIP("ff05::a:b:c:d"), false, true}, 328 {ParseIP("ff05::a:b:c:d"), false, true},
340 {ParseIP("fe80::1:2:3:4"), false, true}, 329 {ParseIP("fe80::1:2:3:4"), false, true},
341 {ParseIP("2001:db8::123:12:1"), false, true}, 330 {ParseIP("2001:db8::123:12:1"), false, true},
342 } 331 }
343 332
344 func TestIPAddrFamily(t *testing.T) { 333 func TestIPAddrFamily(t *testing.T) {
345 » for _, tt := range ipaftests { 334 » for _, tt := range ipAddrFamilyTests {
346 if af := tt.in.To4() != nil; af != tt.af4 { 335 if af := tt.in.To4() != nil; af != tt.af4 {
347 t.Errorf("verifying IPv4 address family for %q = %v, wan t %v", tt.in, af, tt.af4) 336 t.Errorf("verifying IPv4 address family for %q = %v, wan t %v", tt.in, af, tt.af4)
348 } 337 }
349 if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.a f6 { 338 if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.a f6 {
350 t.Errorf("verifying IPv6 address family for %q = %v, wan t %v", tt.in, af, tt.af6) 339 t.Errorf("verifying IPv6 address family for %q = %v, wan t %v", tt.in, af, tt.af6)
351 } 340 }
352 } 341 }
353 } 342 }
354 343
355 var ipscopetests = []struct { 344 var ipAddrScopeTests = []struct {
356 scope func(IP) bool 345 scope func(IP) bool
357 in IP 346 in IP
358 ok bool 347 ok bool
359 }{ 348 }{
360 {IP.IsUnspecified, IPv4zero, true}, 349 {IP.IsUnspecified, IPv4zero, true},
361 {IP.IsUnspecified, IPv4(127, 0, 0, 1), false}, 350 {IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
362 {IP.IsUnspecified, IPv6unspecified, true}, 351 {IP.IsUnspecified, IPv6unspecified, true},
363 {IP.IsUnspecified, IPv6interfacelocalallnodes, false}, 352 {IP.IsUnspecified, IPv6interfacelocalallnodes, false},
364 {IP.IsLoopback, IPv4(127, 0, 0, 1), true}, 353 {IP.IsLoopback, IPv4(127, 0, 0, 1), true},
365 {IP.IsLoopback, IPv4(127, 255, 255, 254), true}, 354 {IP.IsLoopback, IPv4(127, 255, 255, 254), true},
(...skipping 20 matching lines...) Expand all
386 {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x2 3, 0, 0x12, 0, 0x1}, true}, 375 {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x2 3, 0, 0x12, 0, 0x1}, true},
387 {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, 376 {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
388 {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false}, 377 {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
389 } 378 }
390 379
391 func name(f interface{}) string { 380 func name(f interface{}) string {
392 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() 381 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
393 } 382 }
394 383
395 func TestIPAddrScope(t *testing.T) { 384 func TestIPAddrScope(t *testing.T) {
396 » for _, tt := range ipscopetests { 385 » for _, tt := range ipAddrScopeTests {
397 if ok := tt.scope(tt.in); ok != tt.ok { 386 if ok := tt.scope(tt.in); ok != tt.ok {
398 t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok) 387 t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
399 } 388 }
400 } 389 }
401 } 390 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b