test_core_net.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. Copyright 2021 Jeroen van Rijn <[email protected]>.
  3. Copyright 2024 Feoramund <[email protected]>.
  4. Made available under Odin's BSD-3 license.
  5. List of contributors:
  6. Jeroen van Rijn: Initial implementation.
  7. graphitemaster: pton/ntop IANA test vectors
  8. Feoramund: FreeBSD-specific tests.
  9. A test suite for `core:net`
  10. */
  11. #+build !netbsd
  12. #+build !openbsd
  13. #+feature dynamic-literals
  14. package test_core_net
  15. import "core:testing"
  16. import "core:net"
  17. import "core:strconv"
  18. import "core:sync"
  19. import "core:time"
  20. import "core:thread"
  21. import "core:fmt"
  22. import "core:log"
  23. @test
  24. address_parsing_test :: proc(t: ^testing.T) {
  25. for vector in IP_Address_Parsing_Test_Vectors {
  26. kind := ""
  27. switch vector.family {
  28. case .IP4: kind = "[IPv4]"
  29. case .IP4_Alt: kind = "[IPv4 Non-Decimal]"
  30. case .IP6: kind = "[IPv6]"
  31. case: panic("Add support to the test for this type.")
  32. }
  33. valid := len(vector.binstr) > 0
  34. switch vector.family {
  35. case .IP4, .IP4_Alt:
  36. // Does `net.parse_ip4_address` think we parsed the address properly?
  37. non_decimal := vector.family == .IP4_Alt
  38. any_addr := net.parse_address(vector.input, non_decimal)
  39. parsed_ok := any_addr != nil
  40. parsed: net.IP4_Address
  41. // Ensure that `parse_address` doesn't parse IPv4 addresses into IPv6 addreses by mistake.
  42. switch addr in any_addr {
  43. case net.IP4_Address:
  44. parsed = addr
  45. case net.IP6_Address:
  46. parsed_ok = false
  47. testing.expectf(t, false, "parse_address mistook %v as IPv6 address %04x", vector.input, addr)
  48. }
  49. if !parsed_ok && valid {
  50. testing.expectf(t, parsed_ok == valid, "parse_ip4_address failed to parse %v, expected %v", vector.input, binstr_to_address(t, vector.binstr))
  51. } else if parsed_ok && !valid {
  52. testing.expectf(t, parsed_ok == valid, "parse_ip4_address parsed %v into %v, expected failure", vector.input, parsed)
  53. }
  54. if valid && parsed_ok {
  55. actual_binary := address_to_binstr(parsed)
  56. testing.expectf(t, actual_binary == vector.binstr, "parse_ip4_address parsed %v into %v, expected %v", vector.input, actual_binary, vector.binstr)
  57. // Do we turn an address back into the same string properly? No point in testing the roundtrip if the first part failed.
  58. if len(vector.output) > 0 && actual_binary == vector.binstr {
  59. stringified := net.address_to_string(parsed)
  60. testing.expectf(t, stringified == vector.output, "address_to_string turned %v into %v, expected %v", parsed, stringified, vector.output)
  61. }
  62. }
  63. case .IP6:
  64. // Do we parse the address properly?
  65. parsed, parsed_ok := net.parse_ip6_address(vector.input)
  66. if !parsed_ok && valid {
  67. testing.expectf(t, parsed_ok == valid, "parse_ip6_address failed to parse %v, expected %04x", vector.input, binstr_to_address(t, vector.binstr))
  68. } else if parsed_ok && !valid {
  69. testing.expectf(t, parsed_ok == valid, "parse_ip6_address parsed %v into %04x, expected failure", vector.input, parsed)
  70. }
  71. if valid && parsed_ok {
  72. actual_binary := address_to_binstr(parsed)
  73. testing.expectf(t, actual_binary == vector.binstr, "parse_ip6_address parsed %v into %v, expected %v", vector.input, actual_binary, vector.binstr)
  74. // Do we turn an address back into the same string properly? No point in testing the roundtrip if the first part failed.
  75. if len(vector.output) > 0 && actual_binary == vector.binstr {
  76. stringified := net.address_to_string(parsed)
  77. testing.expectf(t, stringified == vector.output, "address_to_string turned %v into %v, expected %v", parsed, stringified, vector.output)
  78. }
  79. }
  80. }
  81. }
  82. }
  83. Kind :: enum {
  84. IP4, // Decimal IPv4
  85. IP4_Alt, // Non-decimal address
  86. IP6, // Hex IPv6 or mixed IPv4/IPv6.
  87. }
  88. IP_Address_Parsing_Test_Vector :: struct {
  89. // Give it to the IPv4 or IPv6 parser?
  90. family: Kind,
  91. // Input address to try and parse.
  92. input: string,
  93. // Hexadecimal representation of the expected numeric value of the address. Zero length means input is invalid and the parser should report failure.
  94. binstr: string,
  95. // Expected `address_to_string` output, if a valid input and this string is non-empty.
  96. output: string,
  97. }
  98. IP_Address_Parsing_Test_Vectors :: []IP_Address_Parsing_Test_Vector{
  99. // dotted-decimal notation
  100. { .IP4, "0.0.0.0", "00000000", "0.0.0.0" },
  101. { .IP4, "127.0.0.1", "7f000001", "127.0.0.1" },
  102. { .IP4, "10.0.128.31", "0a00801f", "10.0.128.31" },
  103. { .IP4, "255.255.255.255", "ffffffff", "255.255.255.255"},
  104. // Odin custom: Address + port, valid
  105. { .IP4, "0.0.0.0:80", "00000000", "0.0.0.0" },
  106. { .IP4, "127.0.0.1:80", "7f000001", "127.0.0.1" },
  107. { .IP4, "10.0.128.31:80", "0a00801f", "10.0.128.31" },
  108. { .IP4, "255.255.255.255:80", "ffffffff", "255.255.255.255"},
  109. { .IP4, "[0.0.0.0]:80", "00000000", "0.0.0.0" },
  110. { .IP4, "[127.0.0.1]:80", "7f000001", "127.0.0.1" },
  111. { .IP4, "[10.0.128.31]:80", "0a00801f", "10.0.128.31" },
  112. { .IP4, "[255.255.255.255]:80", "ffffffff", "255.255.255.255"},
  113. // Odin custom: Address + port, invalid
  114. { .IP4, "[]:80", "", ""},
  115. { .IP4, "[0.0.0.0]", "", ""},
  116. { .IP4, "[127.0.0.1]:", "", ""},
  117. { .IP4, "[10.0.128.31] :80", "", ""},
  118. { .IP4, "[255.255.255.255]:65536", "", ""},
  119. // numbers-and-dots notation, but not dotted-decimal
  120. { .IP4_Alt, "1.2.03.4", "01020304", ""},
  121. { .IP4_Alt, "1.2.0x33.4", "01023304", ""},
  122. { .IP4_Alt, "1.2.0XAB.4", "0102ab04", ""},
  123. { .IP4_Alt, "1.2.0xabcd", "0102abcd", ""},
  124. { .IP4_Alt, "1.0xabcdef", "01abcdef", ""},
  125. { .IP4_Alt, "0x01abcdef", "01abcdef", ""},
  126. { .IP4_Alt, "00377.0x0ff.65534", "fffffffe", ""},
  127. // invalid as decimal address
  128. { .IP4, "", "", ""},
  129. { .IP4, ".1.2.3", "", ""},
  130. { .IP4, "1..2.3", "", ""},
  131. { .IP4, "1.2.3.", "", ""},
  132. { .IP4, "1.2.3.4.5", "", ""},
  133. { .IP4, "1.2.3.a", "", ""},
  134. { .IP4, "1.256.2.3", "", ""},
  135. { .IP4, "1.2.4294967296.3", "", ""},
  136. { .IP4, "1.2.-4294967295.3", "", ""},
  137. { .IP4, "1.2. 3.4", "", ""},
  138. // invalid as non-decimal address
  139. { .IP4_Alt, "", "", ""},
  140. { .IP4_Alt, ".1.2.3", "", ""},
  141. { .IP4_Alt, "1..2.3", "", ""},
  142. { .IP4_Alt, "1.2.3.", "", ""},
  143. { .IP4_Alt, "1.2.3.4.5", "", ""},
  144. { .IP4_Alt, "1.2.3.a", "", ""},
  145. { .IP4_Alt, "1.256.2.3", "", ""},
  146. { .IP4_Alt, "1.2.4294967296.3", "", ""},
  147. { .IP4_Alt, "1.2.-4294967295.3", "", ""},
  148. { .IP4_Alt, "1.2. 3.4", "", ""},
  149. // Valid IPv6 addresses
  150. { .IP6, "::", "00000000000000000000000000000000", "::"},
  151. { .IP6, "::1", "00000000000000000000000000000001", "::1"},
  152. { .IP6, "::192.168.1.1", "000000000000000000000000c0a80101", "::c0a8:101"},
  153. { .IP6, "0000:0000:0000:0000:0000:ffff:255.255.255.255", "00000000000000000000ffffffffffff", "::ffff:ffff:ffff"},
  154. { .IP6, "0:0:0:0:0:0:192.168.1.1", "000000000000000000000000c0a80101", "::c0a8:101"},
  155. { .IP6, "0:0::0:0:0:192.168.1.1", "000000000000000000000000c0a80101", "::c0a8:101"},
  156. { .IP6, "::ffff:192.168.1.1", "00000000000000000000ffffc0a80101", "::ffff:c0a8:101"},
  157. { .IP6, "a:0b:00c:000d:E:F::", "000a000b000c000d000e000f00000000", "a:b:c:d:e:f::"},
  158. { .IP6, "1:2:3:4:5:6::", "00010002000300040005000600000000", "1:2:3:4:5:6::"},
  159. { .IP6, "1:2:3:4:5:6:7::", "00010002000300040005000600070000", "1:2:3:4:5:6:7:0"},
  160. { .IP6, "::1:2:3:4:5:6", "00000000000100020003000400050006", "::1:2:3:4:5:6"},
  161. { .IP6, "::1:2:3:4:5:6:7", "00000001000200030004000500060007", "0:1:2:3:4:5:6:7"},
  162. { .IP6, "a:b::c:d:e:f", "000a000b00000000000c000d000e000f", "a:b::c:d:e:f"},
  163. { .IP6, "0:0:0:0:0:ffff:c0a8:5e4", "00000000000000000000ffffc0a805e4", "::ffff:c0a8:5e4"},
  164. { .IP6, "0::ffff:c0a8:5e4", "00000000000000000000ffffc0a805e4", "::ffff:c0a8:5e4"},
  165. // If multiple zero runs are present, shorten the longest one.
  166. { .IP6, "1:0:0:2:0:0:0:3", "00010000000000020000000000000003", "1:0:0:2::3"},
  167. // Invalid IPv6 addresses
  168. { .IP6, "", "", ""},
  169. { .IP6, ":", "", ""},
  170. { .IP6, ":::", "", ""},
  171. { .IP6, "192.168.1.1", "", ""},
  172. { .IP6, ":192.168.1.1", "", ""},
  173. { .IP6, "::012.34.56.78", "", ""},
  174. { .IP6, ":ffff:192.168.1.1", "", ""},
  175. { .IP6, ".192.168.1.1", "", ""},
  176. { .IP6, ":.192.168.1.1", "", ""},
  177. { .IP6, "a:0b:00c:000d:0000e:f::", "", ""},
  178. { .IP6, "1:2:3:4:5:6:7:8::", "", ""},
  179. { .IP6, "1:2:3:4:5:6:7::9", "", ""},
  180. { .IP6, "::1:2:3:4:5:6:7:8", "", ""},
  181. { .IP6, "ffff:c0a8:5e4", "", ""},
  182. { .IP6, ":ffff:c0a8:5e4", "", ""},
  183. { .IP6, "0:0:0:0:ffff:c0a8:5e4", "", ""},
  184. { .IP6, "::0::ffff:c0a8:5e4", "", ""},
  185. { .IP6, "c0a8", "", ""},
  186. }
  187. @(test)
  188. two_servers_binding_same_endpoint :: proc(t: ^testing.T) {
  189. skt1, err1 := net.listen_tcp({address=net.IP4_Address{127, 0, 0, 1}, port=0})
  190. defer net.close(skt1)
  191. ep, perr := net.bound_endpoint(skt1)
  192. skt2, err2 := net.listen_tcp(ep)
  193. defer net.close(skt2)
  194. testing.expect(t, err1 == nil, "expected first server binding to endpoint to do so without error")
  195. testing.expect_value(t, perr, nil)
  196. testing.expect(t, err2 == net.Bind_Error.Address_In_Use, "expected second server to bind to an endpoint to return .Address_In_Use")
  197. }
  198. @(test)
  199. client_sends_server_data :: proc(t: ^testing.T) {
  200. CONTENT: string: "Hellope!"
  201. SEND_TIMEOUT :: time.Duration(1 * time.Second)
  202. RECV_TIMEOUT :: time.Duration(1 * time.Second)
  203. @static endpoint: net.Endpoint
  204. endpoint.address = net.IP4_Address{127, 0, 0, 1}
  205. Thread_Data :: struct {
  206. t: ^testing.T,
  207. skt: net.Any_Socket,
  208. err: net.Network_Error,
  209. tid: ^thread.Thread,
  210. data: [1024]u8, // Received data and its length
  211. length: int,
  212. wg: ^sync.Wait_Group,
  213. }
  214. tcp_client :: proc(thread_data: rawptr) {
  215. r := cast(^Thread_Data)thread_data
  216. defer sync.wait_group_done(r.wg)
  217. if r.skt, r.err = net.dial_tcp(endpoint); r.err != nil {
  218. testing.expectf(r.t, false, "[tcp_client:dial_tcp] %v", r.err)
  219. return
  220. }
  221. net.set_option(r.skt, .Send_Timeout, SEND_TIMEOUT)
  222. _, r.err = net.send(r.skt, transmute([]byte)CONTENT)
  223. }
  224. tcp_server :: proc(thread_data: rawptr) {
  225. r := cast(^Thread_Data)thread_data
  226. defer sync.wait_group_done(r.wg)
  227. if r.skt, r.err = net.listen_tcp(endpoint); r.err != nil {
  228. sync.wait_group_done(r.wg)
  229. testing.expectf(r.t, false, "[tcp_server:listen_tcp] %v", r.err)
  230. return
  231. }
  232. endpoint, r.err = net.bound_endpoint(r.skt.(net.TCP_Socket))
  233. if !testing.expect_value(r.t, r.err, nil) {
  234. return
  235. }
  236. sync.wait_group_done(r.wg)
  237. client: net.TCP_Socket
  238. if client, _, r.err = net.accept_tcp(r.skt.(net.TCP_Socket)); r.err != nil {
  239. testing.expectf(r.t, false, "[tcp_server:accept_tcp] %v", r.err)
  240. return
  241. }
  242. defer net.close(client)
  243. net.set_option(client, .Receive_Timeout, RECV_TIMEOUT)
  244. r.length, r.err = net.recv_tcp(client, r.data[:])
  245. return
  246. }
  247. thread_data := [2]Thread_Data{}
  248. wg: sync.Wait_Group
  249. sync.wait_group_add(&wg, 1)
  250. thread_data[0].t = t
  251. thread_data[0].wg = &wg
  252. thread_data[0].tid = thread.create_and_start_with_data(&thread_data[0], tcp_server, context)
  253. sync.wait_group_wait(&wg)
  254. sync.wait_group_add(&wg, 2)
  255. thread_data[1].t = t
  256. thread_data[1].wg = &wg
  257. thread_data[1].tid = thread.create_and_start_with_data(&thread_data[1], tcp_client, context)
  258. defer {
  259. net.close(thread_data[0].skt)
  260. thread.destroy(thread_data[0].tid)
  261. net.close(thread_data[1].skt)
  262. thread.destroy(thread_data[1].tid)
  263. }
  264. sync.wait_group_wait(&wg)
  265. okay := thread_data[0].err == nil && thread_data[1].err == nil
  266. testing.expectf(t, okay, "Expected client and server to return `nil`, got %v and %v", thread_data[0].err, thread_data[1].err)
  267. received := string(thread_data[0].data[:thread_data[0].length])
  268. okay = received == CONTENT
  269. testing.expectf(t, okay, "Expected client to send \"{}\", got \"{}\"", CONTENT, received)
  270. }
  271. URL_Test :: struct {
  272. scheme, host, path: string,
  273. queries: map[string]string,
  274. fragment: string,
  275. url: []string,
  276. }
  277. @test
  278. split_url_test :: proc(t: ^testing.T) {
  279. test_cases := []URL_Test{
  280. {
  281. "http", "example.com", "/",
  282. {}, "",
  283. {"http://example.com"},
  284. },
  285. {
  286. "https", "odin-lang.org", "/",
  287. {}, "",
  288. {"https://odin-lang.org"},
  289. },
  290. {
  291. "https", "odin-lang.org", "/docs/",
  292. {}, "",
  293. {"https://odin-lang.org/docs/"},
  294. },
  295. {
  296. "https", "odin-lang.org", "/docs/overview",
  297. {}, "",
  298. {"https://odin-lang.org/docs/overview"},
  299. },
  300. {
  301. "http", "example.com", "/",
  302. {"a" = "b"}, "",
  303. {"http://example.com?a=b"},
  304. },
  305. {
  306. "http", "example.com", "/",
  307. {"a" = ""}, "",
  308. {"http://example.com?a"},
  309. },
  310. {
  311. "http", "example.com", "/",
  312. {"a" = "b", "c" = "d"}, "",
  313. {"http://example.com?a=b&c=d"},
  314. },
  315. {
  316. "http", "example.com", "/",
  317. {"a" = "", "c" = "d"}, "",
  318. {"http://example.com?a&c=d"},
  319. },
  320. {
  321. "http", "example.com", "/example",
  322. {"a" = "", "b" = ""}, "",
  323. {"http://example.com/example?a&b"},
  324. },
  325. {
  326. "https", "example.com", "/callback",
  327. {"redirect" = "https://other.com/login"}, "",
  328. {"https://example.com/callback?redirect=https://other.com/login"},
  329. },
  330. {
  331. "http", "example.com", "/",
  332. {}, "Hellope",
  333. {"http://example.com#Hellope"},
  334. },
  335. {
  336. "https", "odin-lang.org", "/",
  337. {"a" = ""}, "Hellope",
  338. {"https://odin-lang.org?a#Hellope"},
  339. },
  340. {
  341. "http", "example.com", "/",
  342. {"a" = "b"}, "BeesKnees",
  343. {"http://example.com?a=b#BeesKnees"},
  344. },
  345. {
  346. "https", "odin-lang.org", "/docs/overview/",
  347. {}, "hellope",
  348. {"https://odin-lang.org/docs/overview/#hellope"},
  349. },
  350. }
  351. for test in test_cases {
  352. scheme, host, path, queries, fragment := net.split_url(test.url[0])
  353. defer {
  354. delete(queries)
  355. delete(test.queries)
  356. }
  357. testing.expectf(t, scheme == test.scheme, "Expected `net.split_url` to return %s, got %s", test.scheme, scheme)
  358. testing.expectf(t, host == test.host, "Expected `net.split_url` to return %s, got %s", test.host, host)
  359. testing.expectf(t, path == test.path, "Expected `net.split_url` to return %s, got %s", test.path, path)
  360. testing.expectf(t, len(queries) == len(test.queries), "Expected `net.split_url` to return %d queries, got %d queries", len(test.queries), len(queries))
  361. for k, v in queries {
  362. expected := test.queries[k]
  363. testing.expectf(t, v == expected, "Expected `net.split_url` to return %s, got %s", expected, v)
  364. }
  365. testing.expectf(t, fragment == test.fragment, "Expected `net.split_url` to return %s, got %s", test.fragment, fragment)
  366. }
  367. }
  368. @test
  369. join_url_test :: proc(t: ^testing.T) {
  370. test_cases := []URL_Test{
  371. {
  372. "http", "example.com", "/",
  373. {}, "",
  374. {"http://example.com/"},
  375. },
  376. {
  377. "https", "odin-lang.org", "/",
  378. {}, "",
  379. {"https://odin-lang.org/"},
  380. },
  381. {
  382. "https", "odin-lang.org", "/docs/",
  383. {}, "",
  384. {"https://odin-lang.org/docs/"},
  385. },
  386. {
  387. "https", "odin-lang.org", "/docs/overview",
  388. {}, "",
  389. {"https://odin-lang.org/docs/overview"},
  390. },
  391. {
  392. "http", "example.com", "/",
  393. {"a" = "b"}, "",
  394. {"http://example.com/?a=b"},
  395. },
  396. {
  397. "http", "example.com", "/",
  398. {"a" = ""}, "",
  399. {"http://example.com/?a"},
  400. },
  401. {
  402. "http", "example.com", "/",
  403. {"a" = "b", "c" = "d"}, "",
  404. {"http://example.com/?a=b&c=d", "http://example.com/?c=d&a=b"},
  405. },
  406. {
  407. "http", "example.com", "/",
  408. {"a" = "", "c" = "d"}, "",
  409. {"http://example.com/?a&c=d", "http://example.com/?c=d&a"},
  410. },
  411. {
  412. "http", "example.com", "/example",
  413. {"a" = "", "b" = ""}, "",
  414. {"http://example.com/example?a&b", "http://example.com/example?b&a"},
  415. },
  416. {
  417. "http", "example.com", "/",
  418. {}, "Hellope",
  419. {"http://example.com/#Hellope"},
  420. },
  421. {
  422. "https", "odin-lang.org", "/",
  423. {"a" = ""}, "Hellope",
  424. {"https://odin-lang.org/?a#Hellope"},
  425. },
  426. {
  427. "http", "example.com", "/",
  428. {"a" = "b"}, "BeesKnees",
  429. {"http://example.com/?a=b#BeesKnees"},
  430. },
  431. {
  432. "https", "odin-lang.org", "/docs/overview/",
  433. {}, "hellope",
  434. {"https://odin-lang.org/docs/overview/#hellope"},
  435. },
  436. }
  437. for test in test_cases {
  438. url := net.join_url(test.scheme, test.host, test.path, test.queries, test.fragment)
  439. defer {
  440. delete(url)
  441. delete(test.queries)
  442. }
  443. pass := false
  444. for test_url in test.url {
  445. pass |= url == test_url
  446. }
  447. testing.expectf(t, pass, "Expected `net.join_url` to return one of %s, got %s", test.url, url)
  448. }
  449. }
  450. @test
  451. test_udp_echo :: proc(t: ^testing.T) {
  452. endpoint := net.Endpoint{address=net.IP4_Address{127, 0, 0, 1}, port=0}
  453. server, make_server_err := net.make_unbound_udp_socket(.IP4)
  454. if !testing.expect_value(t, make_server_err, nil) {
  455. return
  456. }
  457. defer net.close(server)
  458. bind_server_err := net.bind(server, endpoint)
  459. if !testing.expect_value(t, bind_server_err, nil) {
  460. return
  461. }
  462. perr: net.Network_Error
  463. endpoint, perr = net.bound_endpoint(server)
  464. if !testing.expect_value(t, perr, nil) {
  465. return
  466. }
  467. client, make_client_err := net.make_unbound_udp_socket(.IP4)
  468. if !testing.expect_value(t, make_client_err, nil) {
  469. return
  470. }
  471. defer net.close(client)
  472. msg := "Hellope world!"
  473. buf: [64]u8
  474. bytes_written, send_err := net.send_udp(client, transmute([]u8)msg[:], endpoint)
  475. if !testing.expect_value(t, send_err, nil) {
  476. return
  477. }
  478. if !testing.expect_value(t, bytes_written, len(msg)) {
  479. return
  480. }
  481. bytes_read, _, read_err := net.recv_udp(server, buf[:])
  482. if !testing.expect_value(t, read_err, nil) {
  483. return
  484. }
  485. if !testing.expect_value(t, bytes_read, len(msg)) {
  486. return
  487. }
  488. testing.expect_value(t, msg, transmute(string)buf[:bytes_read])
  489. }
  490. @test
  491. test_dns_resolve :: proc(t: ^testing.T) {
  492. // NOTE: This test depends on external factors, so if it fails, an IP
  493. // address may have changed or become unavailable.
  494. // The net API returns only one address per protocol version, and DNS
  495. // records can store many, so we'll have to check all possibilities.
  496. ep4, ep6, resolve_err := net.resolve("dns.quad9.net")
  497. if !testing.expect_value(t, resolve_err, nil) {
  498. return
  499. }
  500. ip4, ip4_ok := ep4.address.(net.IP4_Address)
  501. if !testing.expect(t, ip4_ok, "Unable to resolve IP4") {
  502. return
  503. }
  504. valid_ip4_a := net.IP4_Address{ 9, 9, 9, 9}
  505. valid_ip4_b := net.IP4_Address{149, 112, 112, 112}
  506. if ip4 != valid_ip4_a && ip4 != valid_ip4_b {
  507. log.errorf("DNS resolved to invalid IP4: %v, expected %v or %v", ip4, valid_ip4_a, valid_ip4_b)
  508. }
  509. ip6, ip6_ok := ep6.address.(net.IP6_Address)
  510. if !testing.expect(t, ip6_ok, "Unable to resolve IP6") {
  511. return
  512. }
  513. valid_ip6_a := net.IP6_Address{0x2620, 0xfe, 0, 0, 0, 0, 0, 9}
  514. valid_ip6_b := net.IP6_Address{0x2620, 0xfe, 0, 0, 0, 0, 0, 0xfe}
  515. if ip6 != valid_ip6_a && ip6 != valid_ip6_b {
  516. log.errorf("DNS resolved to invalid IP6: %v, expected %v or %v", ip6, valid_ip6_a, valid_ip6_b)
  517. }
  518. }
  519. @test
  520. test_nonblocking_option :: proc(t: ^testing.T) {
  521. server, listen_err := net.listen_tcp({address=net.IP4_Address{127, 0, 0, 1}, port=0})
  522. if !testing.expect_value(t, listen_err, nil) {
  523. return
  524. }
  525. defer net.close(server)
  526. testing.set_fail_timeout(t, 2 * time.Second)
  527. // If the nonblocking option isn't set correctly in the operating system,
  528. // this should block until the timeout hits.
  529. net.set_blocking(server, false)
  530. _, _, accept_err := net.accept_tcp(server)
  531. if !testing.expect_value(t, accept_err, net.Accept_Error.Would_Block) {
  532. return
  533. }
  534. }
  535. @(private)
  536. address_to_binstr :: proc(address: net.Address) -> (binstr: string) {
  537. switch t in address {
  538. case net.IP4_Address:
  539. b := transmute(u32be)t
  540. return fmt.tprintf("%08x", b)
  541. case net.IP6_Address:
  542. b := transmute(u128be)t
  543. return fmt.tprintf("%32x", b)
  544. case:
  545. return ""
  546. }
  547. unreachable()
  548. }
  549. @(private)
  550. binstr_to_address :: proc(t: ^testing.T, binstr: string) -> (address: net.Address) {
  551. switch len(binstr) {
  552. case 8: // IPv4
  553. a, ok := strconv.parse_u64_of_base(binstr, 16)
  554. testing.expect(t, ok, "failed to parse test case bin string")
  555. ipv4 := u32be(a)
  556. return net.IP4_Address(transmute([4]u8)ipv4)
  557. case 32: // IPv6
  558. a, ok := strconv.parse_u128_of_base(binstr, 16)
  559. testing.expect(t, ok, "failed to parse test case bin string")
  560. ipv4 := u128be(a)
  561. return net.IP6_Address(transmute([8]u16be)ipv4)
  562. case 0:
  563. return nil
  564. }
  565. panic("Invalid test case")
  566. }