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

Delta Between Two Patch Sets: src/pkg/net/ip_test.go

Issue 4950046: code review 4950046: net: change the internal form of IPMask for IPv4 (Closed)
Left Patch Set: Created 13 years, 7 months ago
Right Patch Set: diff -r 7fec8679f10d https://go.googlecode.com/hg/ Created 13 years, 7 months 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:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/net/ip.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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" 8 "bytes"
9 "reflect" 9 "reflect"
10 "testing" 10 "testing"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.ou t) 42 t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.ou t)
43 } 43 }
44 } 44 }
45 } 45 }
46 46
47 var ipstringtests = []struct { 47 var ipstringtests = []struct {
48 in IP 48 in IP
49 out string 49 out string
50 }{ 50 }{
51 // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation) 51 // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation)
52 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, 52 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
53 » » "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"},
54 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, 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"},
55 » » "2001:db8::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"},
56 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, 56 » {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0 :0:1"},
57 » » "2001:db8:0:1:0:1: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::"},
58 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, 58 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001 :db8::1:0:0:1"},
59 » » "2001:db8:1:0:1:0:1:0"}, 59 » {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, " 2001:db8::a:b:c:d"},
60 » {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, 60 » {nil, "<nil>"},
61 » » "2001::1:0:0:1"},
62 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0},
63 » » "2001:db8:0:0:1::"},
64 » {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
65 » » "2001:db8::1:0:0:1"},
66 » {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD},
67 » » "2001:db8::a:b:c:d"},
68 } 61 }
69 62
70 func TestIPString(t *testing.T) { 63 func TestIPString(t *testing.T) {
71 for _, tt := range ipstringtests { 64 for _, tt := range ipstringtests {
72 if out := tt.in.String(); out != tt.out { 65 if out := tt.in.String(); out != tt.out {
73 t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt .out) 66 t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt .out)
67 }
68 }
69 }
70
71 var ipmasktests = []struct {
72 in IP
73 mask IPMask
74 out IP
75 }{
76 {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)},
78 {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)},
80 {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::")},
82 }
83
84 func TestIPMask(t *testing.T) {
85 for _, tt := range ipmasktests {
86 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)
88 }
89 }
90 }
91
92 var ipmaskstringtests = []struct {
93 in IPMask
94 out string
95 }{
96 {IPv4Mask(255, 255, 255, 240), "fffffff0"},
97 {IPv4Mask(255, 0, 128, 0), "ff008000"},
98 {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
99 {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000 "},
100 {nil, "<nil>"},
101 }
102
103 func TestIPMaskString(t *testing.T) {
104 for _, tt := range ipmaskstringtests {
105 if out := tt.in.String(); out != tt.out {
106 t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
74 } 107 }
75 } 108 }
76 } 109 }
77 110
78 var parsecidrtests = []struct { 111 var parsecidrtests = []struct {
79 in string 112 in string
80 ip IP 113 ip IP
81 mask IPMask 114 mask IPMask
82 err os.Error 115 err os.Error
83 }{ 116 }{
(...skipping 10 matching lines...) Expand all
94 {"abcd:2345::/33", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:800 0::")), nil}, 127 {"abcd:2345::/33", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:800 0::")), nil},
95 {"abcd:2345::/32", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff::") ), nil}, 128 {"abcd:2345::/32", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff::") ), nil},
96 {"abcd:2344::/31", ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::") ), nil}, 129 {"abcd:2344::/31", ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::") ), nil},
97 {"abcd:2300::/24", ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::") ), nil}, 130 {"abcd:2300::/24", ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::") ), nil},
98 {"abcd:2345::/24", nil, nil, &ParseError{"CIDR address", "abcd:2345::/24 "}}, 131 {"abcd:2345::/24", nil, nil, &ParseError{"CIDR address", "abcd:2345::/24 "}},
99 {"2001:DB8::/48", ParseIP("2001:DB8::"), IPMask(ParseIP("ffff:ffff:ffff: :")), nil}, 132 {"2001:DB8::/48", ParseIP("2001:DB8::"), IPMask(ParseIP("ffff:ffff:ffff: :")), nil},
100 } 133 }
101 134
102 func TestParseCIDR(t *testing.T) { 135 func TestParseCIDR(t *testing.T) {
103 for _, tt := range parsecidrtests { 136 for _, tt := range parsecidrtests {
104 » » if ip, mask, err := ParseCIDR(tt.in); !isEqual(ip, tt.ip) || !is Equal(mask, tt.mask) || !reflect.DeepEqual(err, tt.err) { 137 » » if ip, mask, err := ParseCIDR(tt.in); !tt.ip.Equal(ip) || !isEqu al(mask, tt.mask) || !reflect.DeepEqual(err, tt.err) {
105 t.Errorf("ParseCIDR(%q) = %v, %v, %v; want %v, %v, %v", tt.in, ip, mask, err, tt.ip, tt.mask, tt.err) 138 t.Errorf("ParseCIDR(%q) = %v, %v, %v; want %v, %v, %v", tt.in, ip, mask, err, tt.ip, tt.mask, tt.err)
106 } 139 }
107 } 140 }
108 } 141 }
109 142
110 var splitjointests = []struct { 143 var splitjointests = []struct {
111 Host string 144 Host string
112 Port string 145 Port string
113 Join string 146 Join string
114 }{ 147 }{
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() 239 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
207 } 240 }
208 241
209 func TestIPAddrScope(t *testing.T) { 242 func TestIPAddrScope(t *testing.T) {
210 for _, tt := range ipscopetests { 243 for _, tt := range ipscopetests {
211 if ok := tt.scope(tt.in); ok != tt.ok { 244 if ok := tt.scope(tt.in); ok != tt.ok {
212 t.Errorf("%s(%#q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok) 245 t.Errorf("%s(%#q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
213 } 246 }
214 } 247 }
215 } 248 }
LEFTRIGHT

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