PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/third_party/gofrontend/libgo/go/net/ip_test.go

http://github.com/axw/llgo
Go | 542 lines | 480 code | 50 blank | 12 comment | 118 complexity | 697ec280ce67b7625ee9c73144d7c2a1 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package net
  5. import (
  6. "reflect"
  7. "runtime"
  8. "testing"
  9. )
  10. var parseIPTests = []struct {
  11. in string
  12. out IP
  13. }{
  14. {"127.0.1.2", IPv4(127, 0, 1, 2)},
  15. {"127.0.0.1", IPv4(127, 0, 0, 1)},
  16. {"127.001.002.003", IPv4(127, 1, 2, 3)},
  17. {"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  18. {"::ffff:127.001.002.003", IPv4(127, 1, 2, 3)},
  19. {"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
  20. {"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  21. {"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  22. {"0:0:0:0::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  23. {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
  24. {"2001:4860:0000:2001:0000:0000:0000:0068", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
  25. {"127.0.0.256", nil},
  26. {"abc", nil},
  27. {"123:", nil},
  28. {"fe80::1%lo0", nil},
  29. {"fe80::1%911", nil},
  30. {"", nil},
  31. {"a1:a2:a3:a4::b1:b2:b3:b4", nil}, // Issue 6628
  32. }
  33. func TestParseIP(t *testing.T) {
  34. for _, tt := range parseIPTests {
  35. if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
  36. t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
  37. }
  38. if tt.in == "" {
  39. // Tested in TestMarshalEmptyIP below.
  40. continue
  41. }
  42. var out IP
  43. if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
  44. t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
  45. }
  46. }
  47. }
  48. func TestLookupWithIP(t *testing.T) {
  49. _, err := LookupIP("")
  50. if err == nil {
  51. t.Errorf(`LookupIP("") succeeded, should fail`)
  52. }
  53. _, err = LookupHost("")
  54. if err == nil {
  55. t.Errorf(`LookupIP("") succeeded, should fail`)
  56. }
  57. // Test that LookupHost and LookupIP, which normally
  58. // expect host names, work with IP addresses.
  59. for _, tt := range parseIPTests {
  60. if tt.out != nil {
  61. addrs, err := LookupHost(tt.in)
  62. if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
  63. t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
  64. }
  65. } else if !testing.Short() {
  66. // We can't control what the host resolver does; if it can resolve, say,
  67. // 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
  68. // Warn about these discrepancies but don't fail the test.
  69. addrs, err := LookupHost(tt.in)
  70. if err == nil {
  71. t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
  72. }
  73. }
  74. if tt.out != nil {
  75. ips, err := LookupIP(tt.in)
  76. if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
  77. t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
  78. }
  79. } else if !testing.Short() {
  80. ips, err := LookupIP(tt.in)
  81. // We can't control what the host resolver does. See above.
  82. if err == nil {
  83. t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
  84. }
  85. }
  86. }
  87. }
  88. func BenchmarkParseIP(b *testing.B) {
  89. testHookUninstaller.Do(uninstallTestHooks)
  90. for i := 0; i < b.N; i++ {
  91. for _, tt := range parseIPTests {
  92. ParseIP(tt.in)
  93. }
  94. }
  95. }
  96. // Issue 6339
  97. func TestMarshalEmptyIP(t *testing.T) {
  98. for _, in := range [][]byte{nil, []byte("")} {
  99. var out = IP{1, 2, 3, 4}
  100. if err := out.UnmarshalText(in); err != nil || out != nil {
  101. t.Errorf("UnmarshalText(%v) = %v, %v; want nil, nil", in, out, err)
  102. }
  103. }
  104. var ip IP
  105. got, err := ip.MarshalText()
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if !reflect.DeepEqual(got, []byte("")) {
  110. t.Errorf(`got %#v, want []byte("")`, got)
  111. }
  112. }
  113. var ipStringTests = []struct {
  114. in IP
  115. out string // see RFC 5952
  116. }{
  117. {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
  118. {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1"},
  119. {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"},
  120. {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, "2001:db8:1:0:1:0:1:0"},
  121. {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0:0:1"},
  122. {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:db8:0:0:1::"},
  123. {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1:0:0:1"},
  124. {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, "2001:db8::a:b:c:d"},
  125. {IPv4(192, 168, 0, 1), "192.168.0.1"},
  126. {nil, ""},
  127. }
  128. func TestIPString(t *testing.T) {
  129. for _, tt := range ipStringTests {
  130. if tt.in != nil {
  131. if out := tt.in.String(); out != tt.out {
  132. t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
  133. }
  134. }
  135. if out, err := tt.in.MarshalText(); string(out) != tt.out || err != nil {
  136. t.Errorf("IP.MarshalText(%v) = %q, %v, want %q, nil", tt.in, out, err, tt.out)
  137. }
  138. }
  139. }
  140. func BenchmarkIPString(b *testing.B) {
  141. testHookUninstaller.Do(uninstallTestHooks)
  142. for i := 0; i < b.N; i++ {
  143. for _, tt := range ipStringTests {
  144. if tt.in != nil {
  145. tt.in.String()
  146. }
  147. }
  148. }
  149. }
  150. var ipMaskTests = []struct {
  151. in IP
  152. mask IPMask
  153. out IP
  154. }{
  155. {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
  156. {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
  157. {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
  158. {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
  159. {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
  160. {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
  161. }
  162. func TestIPMask(t *testing.T) {
  163. for _, tt := range ipMaskTests {
  164. if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
  165. t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
  166. }
  167. }
  168. }
  169. var ipMaskStringTests = []struct {
  170. in IPMask
  171. out string
  172. }{
  173. {IPv4Mask(255, 255, 255, 240), "fffffff0"},
  174. {IPv4Mask(255, 0, 128, 0), "ff008000"},
  175. {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
  176. {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
  177. {nil, "<nil>"},
  178. }
  179. func TestIPMaskString(t *testing.T) {
  180. for _, tt := range ipMaskStringTests {
  181. if out := tt.in.String(); out != tt.out {
  182. t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
  183. }
  184. }
  185. }
  186. func BenchmarkIPMaskString(b *testing.B) {
  187. testHookUninstaller.Do(uninstallTestHooks)
  188. for i := 0; i < b.N; i++ {
  189. for _, tt := range ipMaskStringTests {
  190. tt.in.String()
  191. }
  192. }
  193. }
  194. var parseCIDRTests = []struct {
  195. in string
  196. ip IP
  197. net *IPNet
  198. err error
  199. }{
  200. {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
  201. {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  202. {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  203. {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
  204. {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  205. {"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
  206. {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
  207. {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
  208. {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
  209. {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
  210. {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
  211. {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
  212. {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
  213. {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
  214. {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
  215. {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
  216. {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
  217. {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/255.255.255.0"}},
  218. {"192.168.1.1/35", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/35"}},
  219. {"2001:db8::1/-1", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-1"}},
  220. {"", nil, nil, &ParseError{Type: "CIDR address", Text: ""}},
  221. }
  222. func TestParseCIDR(t *testing.T) {
  223. for _, tt := range parseCIDRTests {
  224. ip, net, err := ParseCIDR(tt.in)
  225. if !reflect.DeepEqual(err, tt.err) {
  226. t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
  227. }
  228. if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
  229. 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)
  230. }
  231. }
  232. }
  233. var ipNetContainsTests = []struct {
  234. ip IP
  235. net *IPNet
  236. ok bool
  237. }{
  238. {IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
  239. {IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
  240. {IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 0, 255, 252)}, true},
  241. {IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 255, 0, 252)}, false},
  242. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: CIDRMask(47, 128)}, true},
  243. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: CIDRMask(47, 128)}, false},
  244. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:0:ffff::"))}, true},
  245. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("0:0:0:ffff::"))}, false},
  246. }
  247. func TestIPNetContains(t *testing.T) {
  248. for _, tt := range ipNetContainsTests {
  249. if ok := tt.net.Contains(tt.ip); ok != tt.ok {
  250. t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
  251. }
  252. }
  253. }
  254. var ipNetStringTests = []struct {
  255. in *IPNet
  256. out string
  257. }{
  258. {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/26"},
  259. {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
  260. {&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
  261. {&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
  262. }
  263. func TestIPNetString(t *testing.T) {
  264. for _, tt := range ipNetStringTests {
  265. if out := tt.in.String(); out != tt.out {
  266. t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
  267. }
  268. }
  269. }
  270. var cidrMaskTests = []struct {
  271. ones int
  272. bits int
  273. out IPMask
  274. }{
  275. {0, 32, IPv4Mask(0, 0, 0, 0)},
  276. {12, 32, IPv4Mask(255, 240, 0, 0)},
  277. {24, 32, IPv4Mask(255, 255, 255, 0)},
  278. {32, 32, IPv4Mask(255, 255, 255, 255)},
  279. {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  280. {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  281. {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  282. {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  283. {33, 32, nil},
  284. {32, 33, nil},
  285. {-1, 128, nil},
  286. {128, -1, nil},
  287. }
  288. func TestCIDRMask(t *testing.T) {
  289. for _, tt := range cidrMaskTests {
  290. if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
  291. t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
  292. }
  293. }
  294. }
  295. var (
  296. v4addr = IP{192, 168, 0, 1}
  297. v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
  298. v6addr = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
  299. v4mask = IPMask{255, 255, 255, 0}
  300. v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
  301. v6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
  302. badaddr = IP{192, 168, 0}
  303. badmask = IPMask{255, 255, 0}
  304. v4maskzero = IPMask{0, 0, 0, 0}
  305. )
  306. var networkNumberAndMaskTests = []struct {
  307. in IPNet
  308. out IPNet
  309. }{
  310. {IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
  311. {IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
  312. {IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
  313. {IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
  314. {IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
  315. {IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
  316. {IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mappedv6mask}},
  317. {in: IPNet{IP: v6addr, Mask: v4mask}},
  318. {in: IPNet{IP: v4addr, Mask: badmask}},
  319. {in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
  320. {in: IPNet{IP: v6addr, Mask: badmask}},
  321. {in: IPNet{IP: badaddr, Mask: v4mask}},
  322. {in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
  323. {in: IPNet{IP: badaddr, Mask: v6mask}},
  324. {in: IPNet{IP: badaddr, Mask: badmask}},
  325. }
  326. func TestNetworkNumberAndMask(t *testing.T) {
  327. for _, tt := range networkNumberAndMaskTests {
  328. ip, m := networkNumberAndMask(&tt.in)
  329. out := &IPNet{IP: ip, Mask: m}
  330. if !reflect.DeepEqual(&tt.out, out) {
  331. t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
  332. }
  333. }
  334. }
  335. var splitJoinTests = []struct {
  336. host string
  337. port string
  338. join string
  339. }{
  340. {"www.google.com", "80", "www.google.com:80"},
  341. {"127.0.0.1", "1234", "127.0.0.1:1234"},
  342. {"::1", "80", "[::1]:80"},
  343. {"fe80::1%lo0", "80", "[fe80::1%lo0]:80"},
  344. {"localhost%lo0", "80", "[localhost%lo0]:80"},
  345. {"", "0", ":0"},
  346. {"google.com", "https%foo", "google.com:https%foo"}, // Go 1.0 behavior
  347. {"127.0.0.1", "", "127.0.0.1:"}, // Go 1.0 behaviour
  348. {"www.google.com", "", "www.google.com:"}, // Go 1.0 behaviour
  349. }
  350. var splitFailureTests = []struct {
  351. hostPort string
  352. err string
  353. }{
  354. {"www.google.com", "missing port in address"},
  355. {"127.0.0.1", "missing port in address"},
  356. {"[::1]", "missing port in address"},
  357. {"[fe80::1%lo0]", "missing port in address"},
  358. {"[localhost%lo0]", "missing port in address"},
  359. {"localhost%lo0", "missing port in address"},
  360. {"::1", "too many colons in address"},
  361. {"fe80::1%lo0", "too many colons in address"},
  362. {"fe80::1%lo0:80", "too many colons in address"},
  363. {"localhost%lo0:80", "missing brackets in address"},
  364. // Test cases that didn't fail in Go 1.0
  365. {"[foo:bar]", "missing port in address"},
  366. {"[foo:bar]baz", "missing port in address"},
  367. {"[foo]bar:baz", "missing port in address"},
  368. {"[foo]:[bar]:baz", "too many colons in address"},
  369. {"[foo]:[bar]baz", "unexpected '[' in address"},
  370. {"foo[bar]:baz", "unexpected '[' in address"},
  371. {"foo]bar:baz", "unexpected ']' in address"},
  372. }
  373. func TestSplitHostPort(t *testing.T) {
  374. for _, tt := range splitJoinTests {
  375. if host, port, err := SplitHostPort(tt.join); host != tt.host || port != tt.port || err != nil {
  376. t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.join, host, port, err, tt.host, tt.port)
  377. }
  378. }
  379. for _, tt := range splitFailureTests {
  380. if _, _, err := SplitHostPort(tt.hostPort); err == nil {
  381. t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
  382. } else {
  383. e := err.(*AddrError)
  384. if e.Err != tt.err {
  385. t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
  386. }
  387. }
  388. }
  389. }
  390. func TestJoinHostPort(t *testing.T) {
  391. for _, tt := range splitJoinTests {
  392. if join := JoinHostPort(tt.host, tt.port); join != tt.join {
  393. t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, join, tt.join)
  394. }
  395. }
  396. }
  397. var ipAddrFamilyTests = []struct {
  398. in IP
  399. af4 bool
  400. af6 bool
  401. }{
  402. {IPv4bcast, true, false},
  403. {IPv4allsys, true, false},
  404. {IPv4allrouter, true, false},
  405. {IPv4zero, true, false},
  406. {IPv4(224, 0, 0, 1), true, false},
  407. {IPv4(127, 0, 0, 1), true, false},
  408. {IPv4(240, 0, 0, 1), true, false},
  409. {IPv6unspecified, false, true},
  410. {IPv6loopback, false, true},
  411. {IPv6interfacelocalallnodes, false, true},
  412. {IPv6linklocalallnodes, false, true},
  413. {IPv6linklocalallrouters, false, true},
  414. {ParseIP("ff05::a:b:c:d"), false, true},
  415. {ParseIP("fe80::1:2:3:4"), false, true},
  416. {ParseIP("2001:db8::123:12:1"), false, true},
  417. }
  418. func TestIPAddrFamily(t *testing.T) {
  419. for _, tt := range ipAddrFamilyTests {
  420. if af := tt.in.To4() != nil; af != tt.af4 {
  421. t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
  422. }
  423. if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
  424. t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
  425. }
  426. }
  427. }
  428. var ipAddrScopeTests = []struct {
  429. scope func(IP) bool
  430. in IP
  431. ok bool
  432. }{
  433. {IP.IsUnspecified, IPv4zero, true},
  434. {IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
  435. {IP.IsUnspecified, IPv6unspecified, true},
  436. {IP.IsUnspecified, IPv6interfacelocalallnodes, false},
  437. {IP.IsUnspecified, nil, false},
  438. {IP.IsLoopback, IPv4(127, 0, 0, 1), true},
  439. {IP.IsLoopback, IPv4(127, 255, 255, 254), true},
  440. {IP.IsLoopback, IPv4(128, 1, 2, 3), false},
  441. {IP.IsLoopback, IPv6loopback, true},
  442. {IP.IsLoopback, IPv6linklocalallrouters, false},
  443. {IP.IsLoopback, nil, false},
  444. {IP.IsMulticast, IPv4(224, 0, 0, 0), true},
  445. {IP.IsMulticast, IPv4(239, 0, 0, 0), true},
  446. {IP.IsMulticast, IPv4(240, 0, 0, 0), false},
  447. {IP.IsMulticast, IPv6linklocalallnodes, true},
  448. {IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  449. {IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  450. {IP.IsMulticast, nil, false},
  451. {IP.IsInterfaceLocalMulticast, IPv4(224, 0, 0, 0), false},
  452. {IP.IsInterfaceLocalMulticast, IPv4(0xff, 0x01, 0, 0), false},
  453. {IP.IsInterfaceLocalMulticast, IPv6interfacelocalallnodes, true},
  454. {IP.IsInterfaceLocalMulticast, nil, false},
  455. {IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
  456. {IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
  457. {IP.IsLinkLocalMulticast, IPv4(0xff, 0x02, 0, 0), false},
  458. {IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
  459. {IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  460. {IP.IsLinkLocalMulticast, nil, false},
  461. {IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
  462. {IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
  463. {IP.IsLinkLocalUnicast, IPv4(0xfe, 0x80, 0, 0), false},
  464. {IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  465. {IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  466. {IP.IsLinkLocalUnicast, nil, false},
  467. {IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
  468. {IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
  469. {IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
  470. {IP.IsGlobalUnicast, IPv4bcast, false},
  471. {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
  472. {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  473. {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  474. {IP.IsGlobalUnicast, nil, false},
  475. }
  476. func name(f interface{}) string {
  477. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  478. }
  479. func TestIPAddrScope(t *testing.T) {
  480. for _, tt := range ipAddrScopeTests {
  481. if ok := tt.scope(tt.in); ok != tt.ok {
  482. t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
  483. }
  484. ip := tt.in.To4()
  485. if ip == nil {
  486. continue
  487. }
  488. if ok := tt.scope(ip); ok != tt.ok {
  489. t.Errorf("%s(%q) = %v, want %v", name(tt.scope), ip, ok, tt.ok)
  490. }
  491. }
  492. }