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

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

Issue 4244055: code review 4244055: net: drop laddr from Dial, cname from LookupHost; new f... (Closed)
Patch Set: diff -r 8b9c0b903333 https://go.googlecode.com/hg/ Created 13 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 | « src/pkg/net/ip.go ('k') | src/pkg/net/iprawsock.go » ('j') | 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 "testing" 10 "testing"
11 "os"
9 ) 12 )
10 13
11 func isEqual(a, b IP) bool { 14 func isEqual(a, b []byte) bool {
12 if a == nil && b == nil { 15 if a == nil && b == nil {
13 return true 16 return true
14 } 17 }
15 » if a == nil || b == nil || len(a) != len(b) { 18 » if a == nil || b == nil {
16 return false 19 return false
17 } 20 }
18 » for i := 0; i < len(a); i++ { 21 » return bytes.Equal(a, b)
19 » » if a[i] != b[i] {
20 » » » return false
21 » » }
22 » }
23 » return true
24 } 22 }
25 23
26 type parseIPTest struct { 24 var parseiptests = []struct {
27 in string 25 in string
28 out IP 26 out IP
29 } 27 }{
30
31 var parseiptests = []parseIPTest{
32 {"127.0.1.2", IPv4(127, 0, 1, 2)}, 28 {"127.0.1.2", IPv4(127, 0, 1, 2)},
33 {"127.0.0.1", IPv4(127, 0, 0, 1)}, 29 {"127.0.0.1", IPv4(127, 0, 0, 1)},
34 {"127.0.0.256", nil}, 30 {"127.0.0.256", nil},
35 {"abc", nil}, 31 {"abc", nil},
36 {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)}, 32 {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
37 {"2001:4860:0:2001::68", 33 {"2001:4860:0:2001::68",
38 IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 34 IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01,
39 0, 0, 0, 0, 0, 0, 0x00, 0x68, 35 0, 0, 0, 0, 0, 0, 0x00, 0x68,
40 }, 36 },
41 }, 37 },
42 {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)}, 38 {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
43 } 39 }
44 40
45 func TestParseIP(t *testing.T) { 41 func TestParseIP(t *testing.T) {
46 » for i := 0; i < len(parseiptests); i++ { 42 » for _, tt := range parseiptests {
47 » » tt := parseiptests[i]
48 if out := ParseIP(tt.in); !isEqual(out, tt.out) { 43 if out := ParseIP(tt.in); !isEqual(out, tt.out) {
49 t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.ou t) 44 t.Errorf("ParseIP(%#q) = %v, want %v", tt.in, out, tt.ou t)
50 } 45 }
51 } 46 }
52 } 47 }
53 48
54 type ipStringTest struct { 49 var ipstringtests = []struct {
55 in IP 50 in IP
56 out string 51 out string
57 } 52 }{
58
59 var ipstringtests = []ipStringTest{
60 // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation) 53 // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation)
61 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 54 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0,
62 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, 55 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
63 "2001:db8::123:12:1"}, 56 "2001:db8::123:12:1"},
64 {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 57 {IP{0x20, 0x1, 0xd, 0xb8, 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0, 75 {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0,
83 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, 76 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD},
84 "2001:db8::a:b:c:d"}, 77 "2001:db8::a:b:c:d"},
85 } 78 }
86 79
87 func TestIPString(t *testing.T) { 80 func TestIPString(t *testing.T) {
88 » for i := 0; i < len(ipstringtests); i++ { 81 » for _, tt := range ipstringtests {
89 » » tt := ipstringtests[i]
90 if out := tt.in.String(); out != tt.out { 82 if out := tt.in.String(); out != tt.out {
91 t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt .out) 83 t.Errorf("IP.String(%v) = %#q, want %#q", tt.in, out, tt .out)
92 } 84 }
93 } 85 }
94 } 86 }
87
88 var parsecidrtests = []struct {
89 in string
90 ip IP
91 mask IPMask
92 err os.Error
93 }{
94 {"135.104.0.0/32", IPv4(135, 104, 0, 0), IPv4Mask(255, 255, 255, 255), n il},
95 {"0.0.0.0/24", IPv4(0, 0, 0, 0), IPv4Mask(255, 255, 255, 0), nil},
96 {"135.104.0.0/24", IPv4(135, 104, 0, 0), IPv4Mask(255, 255, 255, 0), nil },
97 {"135.104.0.1/32", IPv4(135, 104, 0, 1), IPv4Mask(255, 255, 255, 255), n il},
98 {"135.104.0.1/24", nil, nil, &ParseError{"CIDR address", "135.104.0.1/24 "}},
99 {"::1/128", ParseIP("::1"), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:fff f:ffff:ffff")), nil},
100 {"abcd:2345::/127", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:ff ff:ffff:ffff:ffff:ffff:fffe")), nil},
101 {"abcd:2345::/65", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:fff f:ffff:8000::")), nil},
102 {"abcd:2345::/64", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:fff f:ffff::")), nil},
103 {"abcd:2345::/63", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:fff f:fffe::")), nil},
104 {"abcd:2345::/33", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:800 0::")), nil},
105 {"abcd:2345::/32", ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff::") ), nil},
106 {"abcd:2344::/31", ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::") ), nil},
107 {"abcd:2300::/24", ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::") ), nil},
108 {"abcd:2345::/24", nil, nil, &ParseError{"CIDR address", "abcd:2345::/24 "}},
109 {"2001:DB8::/48", ParseIP("2001:DB8::"), IPMask(ParseIP("ffff:ffff:ffff: :")), nil},
110 }
111
112 func TestParseCIDR(t *testing.T) {
113 for _, tt := range parsecidrtests {
114 if ip, mask, err := ParseCIDR(tt.in); !isEqual(ip, tt.ip) || !is Equal(mask, tt.mask) || !reflect.DeepEqual(err, tt.err) {
115 t.Errorf("ParseCIDR(%q) = %v, %v, %v; want %v, %v, %v", tt.in, ip, mask, err, tt.ip, tt.mask, tt.err)
116 }
117 }
118 }
119
120 var splitjointests = []struct {
121 Host string
122 Port string
123 Join string
124 }{
125 {"www.google.com", "80", "www.google.com:80"},
126 {"127.0.0.1", "1234", "127.0.0.1:1234"},
127 {"::1", "80", "[::1]:80"},
128 }
129
130 func TestSplitHostPort(t *testing.T) {
131 for _, tt := range splitjointests {
132 if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil {
133 t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, n il", tt.Join, host, port, err, tt.Host, tt.Port)
134 }
135 }
136 }
137
138 func TestJoinHostPort(t *testing.T) {
139 for _, tt := range splitjointests {
140 if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join {
141 t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.Host, tt.Port, join, tt.Join)
142 }
143 }
144 }
OLDNEW
« no previous file with comments | « src/pkg/net/ip.go ('k') | src/pkg/net/iprawsock.go » ('j') | no next file with comments »

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