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

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: diff -r 7fec8679f10d https://go.googlecode.com/hg/ 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:
Left: Side by side diff | Download
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
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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:d b8: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::"},
58 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001 :db8::1: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"},
59 {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, " 2001:db8::a:b:c:d"}, 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 {nil, "<nil>"}, 60 {nil, "<nil>"},
61 } 61 }
62 62
63 func TestIPString(t *testing.T) { 63 func TestIPString(t *testing.T) {
64 for _, tt := range ipstringtests { 64 for _, tt := range ipstringtests {
65 if out := tt.in.String(); out != tt.out { 65 if out := tt.in.String(); out != tt.out {
66 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)
67 } 107 }
68 } 108 }
69 } 109 }
70 110
71 var parsecidrtests = []struct { 111 var parsecidrtests = []struct {
72 in string 112 in string
73 ip IP 113 ip IP
74 mask IPMask 114 mask IPMask
75 err os.Error 115 err os.Error
76 }{ 116 }{
(...skipping 12 matching lines...) Expand all
89 {"abcd:2344::/31", ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::") ), nil}, 129 {"abcd:2344::/31", ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::") ), nil},
90 {"abcd:2300::/24", ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::") ), nil}, 130 {"abcd:2300::/24", ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::") ), nil},
91 {"abcd:2345::/24", nil, nil, &ParseError{"CIDR address", "abcd:2345::/24 "}}, 131 {"abcd:2345::/24", nil, nil, &ParseError{"CIDR address", "abcd:2345::/24 "}},
92 {"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},
93 } 133 }
94 134
95 func TestParseCIDR(t *testing.T) { 135 func TestParseCIDR(t *testing.T) {
96 for _, tt := range parsecidrtests { 136 for _, tt := range parsecidrtests {
97 if ip, mask, err := ParseCIDR(tt.in); !tt.ip.Equal(ip) || !isEqu al(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) {
98 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)
99 }
100 }
101 }
102
103 var ipmasktests = []struct {
104 in IP
105 mask IPMask
106 out IP
107 }{
108 {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
109 {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 1 68, 1, 64)},
110 {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:f fff:ffe0")), IPv4(192, 168, 1, 96)},
111 {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
112 {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d 80::")},
113 {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d 08::")},
114 }
115
116 func TestIPMask(t *testing.T) {
117 for _, tt := range ipmasktests {
118 if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
119 t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask , out, tt.out)
120 }
121 }
122 }
123
124 var ipmaskstringtests = []struct {
125 in IPMask
126 out string
127 }{
128 {IPv4Mask(255, 255, 255, 240), "fffffff0"},
129 {IPv4Mask(255, 0, 128, 0), "ff008000"},
130 {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
131 {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000 "},
132 {nil, "<nil>"},
133 }
134
135 func TestIPMaskString(t *testing.T) {
136 for _, tt := range ipmaskstringtests {
137 if out := tt.in.String(); out != tt.out {
138 t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
139 } 139 }
140 } 140 }
141 } 141 }
142 142
143 var splitjointests = []struct { 143 var splitjointests = []struct {
144 Host string 144 Host string
145 Port string 145 Port string
146 Join string 146 Join string
147 }{ 147 }{
148 {"www.google.com", "80", "www.google.com:80"}, 148 {"www.google.com", "80", "www.google.com:80"},
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() 239 return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
240 } 240 }
241 241
242 func TestIPAddrScope(t *testing.T) { 242 func TestIPAddrScope(t *testing.T) {
243 for _, tt := range ipscopetests { 243 for _, tt := range ipscopetests {
244 if ok := tt.scope(tt.in); ok != tt.ok { 244 if ok := tt.scope(tt.in); ok != tt.ok {
245 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)
246 } 246 }
247 } 247 }
248 } 248 }
LEFTRIGHT

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