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