test_core_net.odin 20 KB

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