ip_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. //go:build !js
  5. package net
  6. import (
  7. "bytes"
  8. "math/rand"
  9. "reflect"
  10. "runtime"
  11. "testing"
  12. )
  13. var parseIPTests = []struct {
  14. in string
  15. out IP
  16. }{
  17. {"127.0.1.2", IPv4(127, 0, 1, 2)},
  18. {"127.0.0.1", IPv4(127, 0, 0, 1)},
  19. {"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  20. {"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
  21. {"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  22. {"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  23. {"0:0:0:0::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
  24. {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
  25. {"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}},
  26. {"-0.0.0.0", nil},
  27. {"0.-1.0.0", nil},
  28. {"0.0.-2.0", nil},
  29. {"0.0.0.-3", nil},
  30. {"127.0.0.256", nil},
  31. {"abc", nil},
  32. {"123:", nil},
  33. {"fe80::1%lo0", nil},
  34. {"fe80::1%911", nil},
  35. {"", nil},
  36. {"a1:a2:a3:a4::b1:b2:b3:b4", nil}, // Issue 6628
  37. {"127.001.002.003", nil},
  38. {"::ffff:127.001.002.003", nil},
  39. {"123.000.000.000", nil},
  40. {"1.2..4", nil},
  41. {"0123.0.0.1", nil},
  42. }
  43. func TestParseIP(t *testing.T) {
  44. for _, tt := range parseIPTests {
  45. if out := ParseIP(tt.in); !reflect.DeepEqual(out, tt.out) {
  46. t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
  47. }
  48. if tt.in == "" {
  49. // Tested in TestMarshalEmptyIP below.
  50. continue
  51. }
  52. var out IP
  53. if err := out.UnmarshalText([]byte(tt.in)); !reflect.DeepEqual(out, tt.out) || (tt.out == nil) != (err != nil) {
  54. t.Errorf("IP.UnmarshalText(%q) = %v, %v, want %v", tt.in, out, err, tt.out)
  55. }
  56. }
  57. }
  58. func TestLookupWithIP(t *testing.T) {
  59. _, err := LookupIP("")
  60. if err == nil {
  61. t.Errorf(`LookupIP("") succeeded, should fail`)
  62. }
  63. _, err = LookupHost("")
  64. if err == nil {
  65. t.Errorf(`LookupIP("") succeeded, should fail`)
  66. }
  67. // Test that LookupHost and LookupIP, which normally
  68. // expect host names, work with IP addresses.
  69. for _, tt := range parseIPTests {
  70. if tt.out != nil {
  71. addrs, err := LookupHost(tt.in)
  72. if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
  73. t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
  74. }
  75. } else if !testing.Short() {
  76. // We can't control what the host resolver does; if it can resolve, say,
  77. // 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
  78. // Warn about these discrepancies but don't fail the test.
  79. addrs, err := LookupHost(tt.in)
  80. if err == nil {
  81. t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
  82. }
  83. }
  84. if tt.out != nil {
  85. ips, err := LookupIP(tt.in)
  86. if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
  87. t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
  88. }
  89. } else if !testing.Short() {
  90. ips, err := LookupIP(tt.in)
  91. // We can't control what the host resolver does. See above.
  92. if err == nil {
  93. t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
  94. }
  95. }
  96. }
  97. }
  98. func BenchmarkParseIP(b *testing.B) {
  99. testHookUninstaller.Do(uninstallTestHooks)
  100. for i := 0; i < b.N; i++ {
  101. for _, tt := range parseIPTests {
  102. ParseIP(tt.in)
  103. }
  104. }
  105. }
  106. // Issue 6339
  107. func TestMarshalEmptyIP(t *testing.T) {
  108. for _, in := range [][]byte{nil, []byte("")} {
  109. var out = IP{1, 2, 3, 4}
  110. if err := out.UnmarshalText(in); err != nil || out != nil {
  111. t.Errorf("UnmarshalText(%v) = %v, %v; want nil, nil", in, out, err)
  112. }
  113. }
  114. var ip IP
  115. got, err := ip.MarshalText()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if !reflect.DeepEqual(got, []byte("")) {
  120. t.Errorf(`got %#v, want []byte("")`, got)
  121. }
  122. }
  123. var ipStringTests = []*struct {
  124. in IP // see RFC 791 and RFC 4291
  125. str string // see RFC 791, RFC 4291 and RFC 5952
  126. byt []byte
  127. error
  128. }{
  129. // IPv4 address
  130. {
  131. IP{192, 0, 2, 1},
  132. "192.0.2.1",
  133. []byte("192.0.2.1"),
  134. nil,
  135. },
  136. {
  137. IP{0, 0, 0, 0},
  138. "0.0.0.0",
  139. []byte("0.0.0.0"),
  140. nil,
  141. },
  142. // IPv4-mapped IPv6 address
  143. {
  144. IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 0, 2, 1},
  145. "192.0.2.1",
  146. []byte("192.0.2.1"),
  147. nil,
  148. },
  149. {
  150. IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0},
  151. "0.0.0.0",
  152. []byte("0.0.0.0"),
  153. nil,
  154. },
  155. // IPv6 address
  156. {
  157. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1},
  158. "2001:db8::123:12:1",
  159. []byte("2001:db8::123:12:1"),
  160. nil,
  161. },
  162. {
  163. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1},
  164. "2001:db8::1",
  165. []byte("2001:db8::1"),
  166. nil,
  167. },
  168. {
  169. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1},
  170. "2001:db8:0:1:0:1:0:1",
  171. []byte("2001:db8:0:1:0:1:0:1"),
  172. nil,
  173. },
  174. {
  175. IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0},
  176. "2001:db8:1:0:1:0:1:0",
  177. []byte("2001:db8:1:0:1:0:1:0"),
  178. nil,
  179. },
  180. {
  181. IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
  182. "2001::1:0:0:1",
  183. []byte("2001::1:0:0:1"),
  184. nil,
  185. },
  186. {
  187. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0},
  188. "2001:db8:0:0:1::",
  189. []byte("2001:db8:0:0:1::"),
  190. nil,
  191. },
  192. {
  193. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1},
  194. "2001:db8::1:0:0:1",
  195. []byte("2001:db8::1:0:0:1"),
  196. nil,
  197. },
  198. {
  199. IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0xa, 0, 0xb, 0, 0xc, 0, 0xd},
  200. "2001:db8::a:b:c:d",
  201. []byte("2001:db8::a:b:c:d"),
  202. nil,
  203. },
  204. {
  205. IPv6unspecified,
  206. "::",
  207. []byte("::"),
  208. nil,
  209. },
  210. // IP wildcard equivalent address in Dial/Listen API
  211. {
  212. nil,
  213. "<nil>",
  214. nil,
  215. nil,
  216. },
  217. // Opaque byte sequence
  218. {
  219. IP{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
  220. "?0123456789abcdef",
  221. nil,
  222. &AddrError{Err: "invalid IP address", Addr: "0123456789abcdef"},
  223. },
  224. }
  225. func TestIPString(t *testing.T) {
  226. for _, tt := range ipStringTests {
  227. if out := tt.in.String(); out != tt.str {
  228. t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.str)
  229. }
  230. if out, err := tt.in.MarshalText(); !bytes.Equal(out, tt.byt) || !reflect.DeepEqual(err, tt.error) {
  231. t.Errorf("IP.MarshalText(%v) = %v, %v, want %v, %v", tt.in, out, err, tt.byt, tt.error)
  232. }
  233. }
  234. }
  235. var sink string
  236. func BenchmarkIPString(b *testing.B) {
  237. testHookUninstaller.Do(uninstallTestHooks)
  238. b.Run("IPv4", func(b *testing.B) {
  239. benchmarkIPString(b, IPv4len)
  240. })
  241. b.Run("IPv6", func(b *testing.B) {
  242. benchmarkIPString(b, IPv6len)
  243. })
  244. }
  245. func benchmarkIPString(b *testing.B, size int) {
  246. b.ReportAllocs()
  247. b.ResetTimer()
  248. for i := 0; i < b.N; i++ {
  249. for _, tt := range ipStringTests {
  250. if tt.in != nil && len(tt.in) == size {
  251. sink = tt.in.String()
  252. }
  253. }
  254. }
  255. }
  256. var ipMaskTests = []struct {
  257. in IP
  258. mask IPMask
  259. out IP
  260. }{
  261. {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
  262. {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
  263. {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
  264. {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
  265. {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
  266. {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
  267. }
  268. func TestIPMask(t *testing.T) {
  269. for _, tt := range ipMaskTests {
  270. if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
  271. t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
  272. }
  273. }
  274. }
  275. var ipMaskStringTests = []struct {
  276. in IPMask
  277. out string
  278. }{
  279. {IPv4Mask(255, 255, 255, 240), "fffffff0"},
  280. {IPv4Mask(255, 0, 128, 0), "ff008000"},
  281. {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
  282. {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
  283. {nil, "<nil>"},
  284. }
  285. func TestIPMaskString(t *testing.T) {
  286. for _, tt := range ipMaskStringTests {
  287. if out := tt.in.String(); out != tt.out {
  288. t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
  289. }
  290. }
  291. }
  292. func BenchmarkIPMaskString(b *testing.B) {
  293. testHookUninstaller.Do(uninstallTestHooks)
  294. for i := 0; i < b.N; i++ {
  295. for _, tt := range ipMaskStringTests {
  296. sink = tt.in.String()
  297. }
  298. }
  299. }
  300. var parseCIDRTests = []struct {
  301. in string
  302. ip IP
  303. net *IPNet
  304. err error
  305. }{
  306. {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
  307. {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IP: IPv4(0, 0, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  308. {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  309. {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 1), Mask: IPv4Mask(255, 255, 255, 255)}, nil},
  310. {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IP: IPv4(135, 104, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)}, nil},
  311. {"::1/128", ParseIP("::1"), &IPNet{IP: ParseIP("::1"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
  312. {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
  313. {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
  314. {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
  315. {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
  316. {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
  317. {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2345::"), Mask: IPMask(ParseIP("ffff:ffff::"))}, nil},
  318. {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{IP: ParseIP("abcd:2344::"), Mask: IPMask(ParseIP("ffff:fffe::"))}, nil},
  319. {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
  320. {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{IP: ParseIP("abcd:2300::"), Mask: IPMask(ParseIP("ffff:ff00::"))}, nil},
  321. {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
  322. {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{IP: ParseIP("2001:DB8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
  323. {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/255.255.255.0"}},
  324. {"192.168.1.1/35", nil, nil, &ParseError{Type: "CIDR address", Text: "192.168.1.1/35"}},
  325. {"2001:db8::1/-1", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-1"}},
  326. {"2001:db8::1/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "2001:db8::1/-0"}},
  327. {"-0.0.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "-0.0.0.0/32"}},
  328. {"0.-1.0.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.-1.0.0/32"}},
  329. {"0.0.-2.0/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.-2.0/32"}},
  330. {"0.0.0.-3/32", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.-3/32"}},
  331. {"0.0.0.0/-0", nil, nil, &ParseError{Type: "CIDR address", Text: "0.0.0.0/-0"}},
  332. {"127.000.000.001/32", nil, nil, &ParseError{Type: "CIDR address", Text: "127.000.000.001/32"}},
  333. {"", nil, nil, &ParseError{Type: "CIDR address", Text: ""}},
  334. }
  335. func TestParseCIDR(t *testing.T) {
  336. for _, tt := range parseCIDRTests {
  337. ip, net, err := ParseCIDR(tt.in)
  338. if !reflect.DeepEqual(err, tt.err) {
  339. t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
  340. }
  341. if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !reflect.DeepEqual(net.Mask, tt.net.Mask)) {
  342. 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)
  343. }
  344. }
  345. }
  346. var ipNetContainsTests = []struct {
  347. ip IP
  348. net *IPNet
  349. ok bool
  350. }{
  351. {IPv4(172, 16, 1, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(12, 32)}, true},
  352. {IPv4(172, 24, 0, 1), &IPNet{IP: IPv4(172, 16, 0, 0), Mask: CIDRMask(13, 32)}, false},
  353. {IPv4(192, 168, 0, 3), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 0, 255, 252)}, true},
  354. {IPv4(192, 168, 0, 4), &IPNet{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(0, 255, 0, 252)}, false},
  355. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: CIDRMask(47, 128)}, true},
  356. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:2::"), Mask: CIDRMask(47, 128)}, false},
  357. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:0:ffff::"))}, true},
  358. {ParseIP("2001:db8:1:2::1"), &IPNet{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("0:0:0:ffff::"))}, false},
  359. }
  360. func TestIPNetContains(t *testing.T) {
  361. for _, tt := range ipNetContainsTests {
  362. if ok := tt.net.Contains(tt.ip); ok != tt.ok {
  363. t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
  364. }
  365. }
  366. }
  367. var ipNetStringTests = []struct {
  368. in *IPNet
  369. out string
  370. }{
  371. {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: CIDRMask(26, 32)}, "192.168.1.0/26"},
  372. {&IPNet{IP: IPv4(192, 168, 1, 0), Mask: IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
  373. {&IPNet{IP: ParseIP("2001:db8::"), Mask: CIDRMask(55, 128)}, "2001:db8::/55"},
  374. {&IPNet{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
  375. {nil, "<nil>"},
  376. }
  377. func TestIPNetString(t *testing.T) {
  378. for _, tt := range ipNetStringTests {
  379. if out := tt.in.String(); out != tt.out {
  380. t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
  381. }
  382. }
  383. }
  384. var cidrMaskTests = []struct {
  385. ones int
  386. bits int
  387. out IPMask
  388. }{
  389. {0, 32, IPv4Mask(0, 0, 0, 0)},
  390. {12, 32, IPv4Mask(255, 240, 0, 0)},
  391. {24, 32, IPv4Mask(255, 255, 255, 0)},
  392. {32, 32, IPv4Mask(255, 255, 255, 255)},
  393. {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  394. {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  395. {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  396. {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
  397. {33, 32, nil},
  398. {32, 33, nil},
  399. {-1, 128, nil},
  400. {128, -1, nil},
  401. }
  402. func TestCIDRMask(t *testing.T) {
  403. for _, tt := range cidrMaskTests {
  404. if out := CIDRMask(tt.ones, tt.bits); !reflect.DeepEqual(out, tt.out) {
  405. t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
  406. }
  407. }
  408. }
  409. var (
  410. v4addr = IP{192, 168, 0, 1}
  411. v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
  412. v6addr = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
  413. v4mask = IPMask{255, 255, 255, 0}
  414. v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
  415. v6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
  416. badaddr = IP{192, 168, 0}
  417. badmask = IPMask{255, 255, 0}
  418. v4maskzero = IPMask{0, 0, 0, 0}
  419. )
  420. var networkNumberAndMaskTests = []struct {
  421. in IPNet
  422. out IPNet
  423. }{
  424. {IPNet{IP: v4addr, Mask: v4mask}, IPNet{IP: v4addr, Mask: v4mask}},
  425. {IPNet{IP: v4addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
  426. {IPNet{IP: v4mappedv6addr, Mask: v4mappedv6mask}, IPNet{IP: v4addr, Mask: v4mask}},
  427. {IPNet{IP: v4mappedv6addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
  428. {IPNet{IP: v4addr, Mask: v6mask}, IPNet{IP: v4addr, Mask: v4maskzero}},
  429. {IPNet{IP: v6addr, Mask: v6mask}, IPNet{IP: v6addr, Mask: v6mask}},
  430. {IPNet{IP: v6addr, Mask: v4mappedv6mask}, IPNet{IP: v6addr, Mask: v4mappedv6mask}},
  431. {in: IPNet{IP: v6addr, Mask: v4mask}},
  432. {in: IPNet{IP: v4addr, Mask: badmask}},
  433. {in: IPNet{IP: v4mappedv6addr, Mask: badmask}},
  434. {in: IPNet{IP: v6addr, Mask: badmask}},
  435. {in: IPNet{IP: badaddr, Mask: v4mask}},
  436. {in: IPNet{IP: badaddr, Mask: v4mappedv6mask}},
  437. {in: IPNet{IP: badaddr, Mask: v6mask}},
  438. {in: IPNet{IP: badaddr, Mask: badmask}},
  439. }
  440. func TestNetworkNumberAndMask(t *testing.T) {
  441. for _, tt := range networkNumberAndMaskTests {
  442. ip, m := networkNumberAndMask(&tt.in)
  443. out := &IPNet{IP: ip, Mask: m}
  444. if !reflect.DeepEqual(&tt.out, out) {
  445. t.Errorf("networkNumberAndMask(%v) = %v, want %v", tt.in, out, &tt.out)
  446. }
  447. }
  448. }
  449. func TestSplitHostPort(t *testing.T) {
  450. for _, tt := range []struct {
  451. hostPort string
  452. host string
  453. port string
  454. }{
  455. // Host name
  456. {"localhost:http", "localhost", "http"},
  457. {"localhost:80", "localhost", "80"},
  458. // Go-specific host name with zone identifier
  459. {"localhost%lo0:http", "localhost%lo0", "http"},
  460. {"localhost%lo0:80", "localhost%lo0", "80"},
  461. {"[localhost%lo0]:http", "localhost%lo0", "http"}, // Go 1 behavior
  462. {"[localhost%lo0]:80", "localhost%lo0", "80"}, // Go 1 behavior
  463. // IP literal
  464. {"127.0.0.1:http", "127.0.0.1", "http"},
  465. {"127.0.0.1:80", "127.0.0.1", "80"},
  466. {"[::1]:http", "::1", "http"},
  467. {"[::1]:80", "::1", "80"},
  468. // IP literal with zone identifier
  469. {"[::1%lo0]:http", "::1%lo0", "http"},
  470. {"[::1%lo0]:80", "::1%lo0", "80"},
  471. // Go-specific wildcard for host name
  472. {":http", "", "http"}, // Go 1 behavior
  473. {":80", "", "80"}, // Go 1 behavior
  474. // Go-specific wildcard for service name or transport port number
  475. {"golang.org:", "golang.org", ""}, // Go 1 behavior
  476. {"127.0.0.1:", "127.0.0.1", ""}, // Go 1 behavior
  477. {"[::1]:", "::1", ""}, // Go 1 behavior
  478. // Opaque service name
  479. {"golang.org:https%foo", "golang.org", "https%foo"}, // Go 1 behavior
  480. } {
  481. if host, port, err := SplitHostPort(tt.hostPort); host != tt.host || port != tt.port || err != nil {
  482. t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.hostPort, host, port, err, tt.host, tt.port)
  483. }
  484. }
  485. for _, tt := range []struct {
  486. hostPort string
  487. err string
  488. }{
  489. {"golang.org", "missing port in address"},
  490. {"127.0.0.1", "missing port in address"},
  491. {"[::1]", "missing port in address"},
  492. {"[fe80::1%lo0]", "missing port in address"},
  493. {"[localhost%lo0]", "missing port in address"},
  494. {"localhost%lo0", "missing port in address"},
  495. {"::1", "too many colons in address"},
  496. {"fe80::1%lo0", "too many colons in address"},
  497. {"fe80::1%lo0:80", "too many colons in address"},
  498. // Test cases that didn't fail in Go 1
  499. {"[foo:bar]", "missing port in address"},
  500. {"[foo:bar]baz", "missing port in address"},
  501. {"[foo]bar:baz", "missing port in address"},
  502. {"[foo]:[bar]:baz", "too many colons in address"},
  503. {"[foo]:[bar]baz", "unexpected '[' in address"},
  504. {"foo[bar]:baz", "unexpected '[' in address"},
  505. {"foo]bar:baz", "unexpected ']' in address"},
  506. } {
  507. if host, port, err := SplitHostPort(tt.hostPort); err == nil {
  508. t.Errorf("SplitHostPort(%q) should have failed", tt.hostPort)
  509. } else {
  510. e := err.(*AddrError)
  511. if e.Err != tt.err {
  512. t.Errorf("SplitHostPort(%q) = _, _, %q; want %q", tt.hostPort, e.Err, tt.err)
  513. }
  514. if host != "" || port != "" {
  515. t.Errorf("SplitHostPort(%q) = %q, %q, err; want %q, %q, err on failure", tt.hostPort, host, port, "", "")
  516. }
  517. }
  518. }
  519. }
  520. func TestJoinHostPort(t *testing.T) {
  521. for _, tt := range []struct {
  522. host string
  523. port string
  524. hostPort string
  525. }{
  526. // Host name
  527. {"localhost", "http", "localhost:http"},
  528. {"localhost", "80", "localhost:80"},
  529. // Go-specific host name with zone identifier
  530. {"localhost%lo0", "http", "localhost%lo0:http"},
  531. {"localhost%lo0", "80", "localhost%lo0:80"},
  532. // IP literal
  533. {"127.0.0.1", "http", "127.0.0.1:http"},
  534. {"127.0.0.1", "80", "127.0.0.1:80"},
  535. {"::1", "http", "[::1]:http"},
  536. {"::1", "80", "[::1]:80"},
  537. // IP literal with zone identifier
  538. {"::1%lo0", "http", "[::1%lo0]:http"},
  539. {"::1%lo0", "80", "[::1%lo0]:80"},
  540. // Go-specific wildcard for host name
  541. {"", "http", ":http"}, // Go 1 behavior
  542. {"", "80", ":80"}, // Go 1 behavior
  543. // Go-specific wildcard for service name or transport port number
  544. {"golang.org", "", "golang.org:"}, // Go 1 behavior
  545. {"127.0.0.1", "", "127.0.0.1:"}, // Go 1 behavior
  546. {"::1", "", "[::1]:"}, // Go 1 behavior
  547. // Opaque service name
  548. {"golang.org", "https%foo", "golang.org:https%foo"}, // Go 1 behavior
  549. } {
  550. if hostPort := JoinHostPort(tt.host, tt.port); hostPort != tt.hostPort {
  551. t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.host, tt.port, hostPort, tt.hostPort)
  552. }
  553. }
  554. }
  555. var ipAddrFamilyTests = []struct {
  556. in IP
  557. af4 bool
  558. af6 bool
  559. }{
  560. {IPv4bcast, true, false},
  561. {IPv4allsys, true, false},
  562. {IPv4allrouter, true, false},
  563. {IPv4zero, true, false},
  564. {IPv4(224, 0, 0, 1), true, false},
  565. {IPv4(127, 0, 0, 1), true, false},
  566. {IPv4(240, 0, 0, 1), true, false},
  567. {IPv6unspecified, false, true},
  568. {IPv6loopback, false, true},
  569. {IPv6interfacelocalallnodes, false, true},
  570. {IPv6linklocalallnodes, false, true},
  571. {IPv6linklocalallrouters, false, true},
  572. {ParseIP("ff05::a:b:c:d"), false, true},
  573. {ParseIP("fe80::1:2:3:4"), false, true},
  574. {ParseIP("2001:db8::123:12:1"), false, true},
  575. }
  576. func TestIPAddrFamily(t *testing.T) {
  577. for _, tt := range ipAddrFamilyTests {
  578. if af := tt.in.To4() != nil; af != tt.af4 {
  579. t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
  580. }
  581. if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
  582. t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
  583. }
  584. }
  585. }
  586. var ipAddrScopeTests = []struct {
  587. scope func(IP) bool
  588. in IP
  589. ok bool
  590. }{
  591. {IP.IsUnspecified, IPv4zero, true},
  592. {IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
  593. {IP.IsUnspecified, IPv6unspecified, true},
  594. {IP.IsUnspecified, IPv6interfacelocalallnodes, false},
  595. {IP.IsUnspecified, nil, false},
  596. {IP.IsLoopback, IPv4(127, 0, 0, 1), true},
  597. {IP.IsLoopback, IPv4(127, 255, 255, 254), true},
  598. {IP.IsLoopback, IPv4(128, 1, 2, 3), false},
  599. {IP.IsLoopback, IPv6loopback, true},
  600. {IP.IsLoopback, IPv6linklocalallrouters, false},
  601. {IP.IsLoopback, nil, false},
  602. {IP.IsMulticast, IPv4(224, 0, 0, 0), true},
  603. {IP.IsMulticast, IPv4(239, 0, 0, 0), true},
  604. {IP.IsMulticast, IPv4(240, 0, 0, 0), false},
  605. {IP.IsMulticast, IPv6linklocalallnodes, true},
  606. {IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  607. {IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  608. {IP.IsMulticast, nil, false},
  609. {IP.IsInterfaceLocalMulticast, IPv4(224, 0, 0, 0), false},
  610. {IP.IsInterfaceLocalMulticast, IPv4(0xff, 0x01, 0, 0), false},
  611. {IP.IsInterfaceLocalMulticast, IPv6interfacelocalallnodes, true},
  612. {IP.IsInterfaceLocalMulticast, nil, false},
  613. {IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
  614. {IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
  615. {IP.IsLinkLocalMulticast, IPv4(0xff, 0x02, 0, 0), false},
  616. {IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
  617. {IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  618. {IP.IsLinkLocalMulticast, nil, false},
  619. {IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
  620. {IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
  621. {IP.IsLinkLocalUnicast, IPv4(0xfe, 0x80, 0, 0), false},
  622. {IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  623. {IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  624. {IP.IsLinkLocalUnicast, nil, false},
  625. {IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
  626. {IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
  627. {IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
  628. {IP.IsGlobalUnicast, IPv4bcast, false},
  629. {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
  630. {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  631. {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  632. {IP.IsGlobalUnicast, nil, false},
  633. {IP.IsPrivate, nil, false},
  634. {IP.IsPrivate, IPv4(1, 1, 1, 1), false},
  635. {IP.IsPrivate, IPv4(9, 255, 255, 255), false},
  636. {IP.IsPrivate, IPv4(10, 0, 0, 0), true},
  637. {IP.IsPrivate, IPv4(10, 255, 255, 255), true},
  638. {IP.IsPrivate, IPv4(11, 0, 0, 0), false},
  639. {IP.IsPrivate, IPv4(172, 15, 255, 255), false},
  640. {IP.IsPrivate, IPv4(172, 16, 0, 0), true},
  641. {IP.IsPrivate, IPv4(172, 16, 255, 255), true},
  642. {IP.IsPrivate, IPv4(172, 23, 18, 255), true},
  643. {IP.IsPrivate, IPv4(172, 31, 255, 255), true},
  644. {IP.IsPrivate, IPv4(172, 31, 0, 0), true},
  645. {IP.IsPrivate, IPv4(172, 32, 0, 0), false},
  646. {IP.IsPrivate, IPv4(192, 167, 255, 255), false},
  647. {IP.IsPrivate, IPv4(192, 168, 0, 0), true},
  648. {IP.IsPrivate, IPv4(192, 168, 255, 255), true},
  649. {IP.IsPrivate, IPv4(192, 169, 0, 0), false},
  650. {IP.IsPrivate, IP{0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, false},
  651. {IP.IsPrivate, IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  652. {IP.IsPrivate, IP{0xfc, 0xff, 0x12, 0, 0, 0, 0, 0x44, 0, 0, 0, 0, 0, 0, 0, 0}, true},
  653. {IP.IsPrivate, IP{0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, true},
  654. {IP.IsPrivate, IP{0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
  655. }
  656. func name(f any) string {
  657. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  658. }
  659. func TestIPAddrScope(t *testing.T) {
  660. for _, tt := range ipAddrScopeTests {
  661. if ok := tt.scope(tt.in); ok != tt.ok {
  662. t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
  663. }
  664. ip := tt.in.To4()
  665. if ip == nil {
  666. continue
  667. }
  668. if ok := tt.scope(ip); ok != tt.ok {
  669. t.Errorf("%s(%q) = %v, want %v", name(tt.scope), ip, ok, tt.ok)
  670. }
  671. }
  672. }
  673. func BenchmarkIPEqual(b *testing.B) {
  674. b.Run("IPv4", func(b *testing.B) {
  675. benchmarkIPEqual(b, IPv4len)
  676. })
  677. b.Run("IPv6", func(b *testing.B) {
  678. benchmarkIPEqual(b, IPv6len)
  679. })
  680. }
  681. func benchmarkIPEqual(b *testing.B, size int) {
  682. ips := make([]IP, 1000)
  683. for i := range ips {
  684. ips[i] = make(IP, size)
  685. rand.Read(ips[i])
  686. }
  687. // Half of the N are equal.
  688. for i := 0; i < b.N/2; i++ {
  689. x := ips[i%len(ips)]
  690. y := ips[i%len(ips)]
  691. x.Equal(y)
  692. }
  693. // The other half are not equal.
  694. for i := 0; i < b.N/2; i++ {
  695. x := ips[i%len(ips)]
  696. y := ips[(i+1)%len(ips)]
  697. x.Equal(y)
  698. }
  699. }