diff options
author | Diego Nehab <diego@impa.br> | 2015-08-22 19:52:01 -0300 |
---|---|---|
committer | Diego Nehab <diego@impa.br> | 2015-08-22 19:52:01 -0300 |
commit | 96965b179c7311f850f72a8629b9ba6d3a31d117 (patch) | |
tree | 05c93629654f686a99ee8923ef2361b7d2244ca8 /test | |
parent | b211838648c1cb092e96e3ae721932a212808d96 (diff) | |
download | luasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.tar.gz luasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.tar.bz2 luasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.zip |
New agnostic IPv4 IPv6 functions.
Also dealing with EPROTOTYPE Yosemite seems to be throwing
at us for no reason.
Diffstat (limited to 'test')
-rw-r--r-- | test/testclnt.lua | 100 | ||||
-rw-r--r-- | test/testsrvr.lua | 2 | ||||
-rw-r--r-- | test/udpconnectclnt.lua | 2 |
3 files changed, 51 insertions, 53 deletions
diff --git a/test/testclnt.lua b/test/testclnt.lua index 0014781..abf9608 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -8,7 +8,7 @@ function printf(...) | |||
8 | end | 8 | end |
9 | 9 | ||
10 | function pass(...) | 10 | function pass(...) |
11 | printf(...) | 11 | printf(...) |
12 | io.stderr:write("\n") | 12 | io.stderr:write("\n") |
13 | end | 13 | end |
14 | 14 | ||
@@ -45,30 +45,30 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | |||
45 | if not err then warn("must be buffered") | 45 | if not err then warn("must be buffered") |
46 | elseif err == "timeout" then pass("proper timeout") | 46 | elseif err == "timeout" then pass("proper timeout") |
47 | else fail("unexpected error '%s'", err) end | 47 | else fail("unexpected error '%s'", err) end |
48 | else | 48 | else |
49 | if err ~= "timeout" then fail("should have timed out") | 49 | if err ~= "timeout" then fail("should have timed out") |
50 | else pass("proper timeout") end | 50 | else pass("proper timeout") end |
51 | end | 51 | end |
52 | else | 52 | else |
53 | if mode == "total" then | 53 | if mode == "total" then |
54 | if elapsed > tm then | 54 | if elapsed > tm then |
55 | if err ~= "timeout" then fail("should have timed out") | 55 | if err ~= "timeout" then fail("should have timed out") |
56 | else pass("proper timeout") end | 56 | else pass("proper timeout") end |
57 | elseif elapsed < tm then | 57 | elseif elapsed < tm then |
58 | if err then fail(err) | 58 | if err then fail(err) |
59 | else pass("ok") end | 59 | else pass("ok") end |
60 | else | 60 | else |
61 | if alldone then | 61 | if alldone then |
62 | if err then fail("unexpected error '%s'", err) | 62 | if err then fail("unexpected error '%s'", err) |
63 | else pass("ok") end | 63 | else pass("ok") end |
64 | else | 64 | else |
65 | if err ~= "timeout" then fail(err) | 65 | if err ~= "timeout" then fail(err) |
66 | else pass("proper timeoutk") end | 66 | else pass("proper timeoutk") end |
67 | end | 67 | end |
68 | end | 68 | end |
69 | else | 69 | else |
70 | if err then fail(err) | 70 | if err then fail(err) |
71 | else pass("ok") end | 71 | else pass("ok") end |
72 | end | 72 | end |
73 | end | 73 | end |
74 | end | 74 | end |
@@ -104,8 +104,8 @@ control:setoption("tcp-nodelay", true) | |||
104 | ------------------------------------------------------------------------ | 104 | ------------------------------------------------------------------------ |
105 | function test_methods(sock, methods) | 105 | function test_methods(sock, methods) |
106 | for _, v in pairs(methods) do | 106 | for _, v in pairs(methods) do |
107 | if type(sock[v]) ~= "function" then | 107 | if type(sock[v]) ~= "function" then |
108 | fail(sock.class .. " method '" .. v .. "' not registered") | 108 | fail(sock.class .. " method '" .. v .. "' not registered") |
109 | end | 109 | end |
110 | end | 110 | end |
111 | pass(sock.class .. " methods are ok") | 111 | pass(sock.class .. " methods are ok") |
@@ -121,7 +121,7 @@ function test_mixed(len) | |||
121 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" | 121 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" |
122 | local p4 = "end" .. string.rep("w", inter) .. "bytes" | 122 | local p4 = "end" .. string.rep("w", inter) .. "bytes" |
123 | local bp1, bp2, bp3, bp4 | 123 | local bp1, bp2, bp3, bp4 |
124 | remote (string.format("str = data:receive(%d)", | 124 | remote (string.format("str = data:receive(%d)", |
125 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) | 125 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) |
126 | sent, err = data:send(p1..p2..p3..p4) | 126 | sent, err = data:send(p1..p2..p3..p4) |
127 | if err then fail(err) end | 127 | if err then fail(err) end |
@@ -166,7 +166,7 @@ function test_rawline(len) | |||
166 | io.stderr:write("length " .. len .. ": ") | 166 | io.stderr:write("length " .. len .. ": ") |
167 | local str, str10, back, err | 167 | local str, str10, back, err |
168 | str = string.rep(string.char(47), math.mod(len, 10)) | 168 | str = string.rep(string.char(47), math.mod(len, 10)) |
169 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), | 169 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), |
170 | math.floor(len/10)) | 170 | math.floor(len/10)) |
171 | str = str .. str10 | 171 | str = str .. str10 |
172 | remote "str = data:receive()" | 172 | remote "str = data:receive()" |
@@ -216,7 +216,7 @@ function test_totaltimeoutreceive(len, tm, sl) | |||
216 | data:settimeout(tm, "total") | 216 | data:settimeout(tm, "total") |
217 | local t = socket.gettime() | 217 | local t = socket.gettime() |
218 | str, err, partial, elapsed = data:receive(2*len) | 218 | str, err, partial, elapsed = data:receive(2*len) |
219 | check_timeout(tm, sl, elapsed, err, "receive", "total", | 219 | check_timeout(tm, sl, elapsed, err, "receive", "total", |
220 | string.len(str or partial) == 2*len) | 220 | string.len(str or partial) == 2*len) |
221 | end | 221 | end |
222 | 222 | ||
@@ -236,7 +236,7 @@ function test_totaltimeoutsend(len, tm, sl) | |||
236 | data:settimeout(tm, "total") | 236 | data:settimeout(tm, "total") |
237 | str = string.rep("a", 2*len) | 237 | str = string.rep("a", 2*len) |
238 | total, err, partial, elapsed = data:send(str) | 238 | total, err, partial, elapsed = data:send(str) |
239 | check_timeout(tm, sl, elapsed, err, "send", "total", | 239 | check_timeout(tm, sl, elapsed, err, "send", "total", |
240 | total == 2*len) | 240 | total == 2*len) |
241 | end | 241 | end |
242 | 242 | ||
@@ -256,7 +256,7 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
256 | ]], 2*tm, len, sl, sl)) | 256 | ]], 2*tm, len, sl, sl)) |
257 | data:settimeout(tm) | 257 | data:settimeout(tm) |
258 | str, err, partial, elapsed = data:receive(2*len) | 258 | str, err, partial, elapsed = data:receive(2*len) |
259 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", | 259 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", |
260 | string.len(str or partial) == 2*len) | 260 | string.len(str or partial) == 2*len) |
261 | end | 261 | end |
262 | 262 | ||
@@ -290,10 +290,10 @@ function empty_connect() | |||
290 | data = server:accept() | 290 | data = server:accept() |
291 | ]] | 291 | ]] |
292 | data, err = socket.connect("", port) | 292 | data, err = socket.connect("", port) |
293 | if not data then | 293 | if not data then |
294 | pass("ok") | 294 | pass("ok") |
295 | data = socket.connect(host, port) | 295 | data = socket.connect(host, port) |
296 | else | 296 | else |
297 | pass("gethostbyname returns localhost on empty string...") | 297 | pass("gethostbyname returns localhost on empty string...") |
298 | end | 298 | end |
299 | end | 299 | end |
@@ -327,7 +327,7 @@ function test_closed() | |||
327 | data:close() | 327 | data:close() |
328 | data = nil | 328 | data = nil |
329 | ]], str)) | 329 | ]], str)) |
330 | -- try to get a line | 330 | -- try to get a line |
331 | back, err, partial = data:receive() | 331 | back, err, partial = data:receive() |
332 | if not err then fail("should have gotten 'closed'.") | 332 | if not err then fail("should have gotten 'closed'.") |
333 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | 333 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") |
@@ -340,25 +340,25 @@ function test_closed() | |||
340 | data = nil | 340 | data = nil |
341 | ]] | 341 | ]] |
342 | total, err, partial = data:send(string.rep("ugauga", 100000)) | 342 | total, err, partial = data:send(string.rep("ugauga", 100000)) |
343 | if not err then | 343 | if not err then |
344 | pass("failed: output buffer is at least %d bytes long!", total) | 344 | pass("failed: output buffer is at least %d bytes long!", total) |
345 | elseif err ~= "closed" then | 345 | elseif err ~= "closed" then |
346 | fail("got '"..err.."' instead of 'closed'.") | 346 | fail("got '"..err.."' instead of 'closed'.") |
347 | else | 347 | else |
348 | pass("graceful 'closed' received after %d bytes were sent", partial) | 348 | pass("graceful 'closed' received after %d bytes were sent", partial) |
349 | end | 349 | end |
350 | end | 350 | end |
351 | 351 | ||
352 | ------------------------------------------------------------------------ | 352 | ------------------------------------------------------------------------ |
353 | function test_selectbugs() | 353 | function test_selectbugs() |
354 | local r, s, e = socket.select(nil, nil, 0.1) | 354 | local r, s, e = socket.select(nil, nil, 0.1) |
355 | assert(type(r) == "table" and type(s) == "table" and | 355 | assert(type(r) == "table" and type(s) == "table" and |
356 | (e == "timeout" or e == "error")) | 356 | (e == "timeout" or e == "error")) |
357 | pass("both nil: ok") | 357 | pass("both nil: ok") |
358 | local udp = socket.udp() | 358 | local udp = socket.udp() |
359 | udp:close() | 359 | udp:close() |
360 | r, s, e = socket.select({ udp }, { udp }, 0.1) | 360 | r, s, e = socket.select({ udp }, { udp }, 0.1) |
361 | assert(type(r) == "table" and type(s) == "table" and | 361 | assert(type(r) == "table" and type(s) == "table" and |
362 | (e == "timeout" or e == "error")) | 362 | (e == "timeout" or e == "error")) |
363 | pass("closed sockets: ok") | 363 | pass("closed sockets: ok") |
364 | e = pcall(socket.select, "wrong", 1, 0.1) | 364 | e = pcall(socket.select, "wrong", 1, 0.1) |
@@ -389,7 +389,7 @@ function accept_timeout() | |||
389 | local t = socket.gettime() | 389 | local t = socket.gettime() |
390 | s:settimeout(1) | 390 | s:settimeout(1) |
391 | local c, e = s:accept() | 391 | local c, e = s:accept() |
392 | assert(not c, "should not accept") | 392 | assert(not c, "should not accept") |
393 | assert(e == "timeout", string.format("wrong error message (%s)", e)) | 393 | assert(e == "timeout", string.format("wrong error message (%s)", e)) |
394 | t = socket.gettime() - t | 394 | t = socket.gettime() - t |
395 | assert(t < 2, string.format("took to long to give up (%gs)", t)) | 395 | assert(t < 2, string.format("took to long to give up (%gs)", t)) |
@@ -407,9 +407,9 @@ function connect_timeout() | |||
407 | local t = socket.gettime() | 407 | local t = socket.gettime() |
408 | local r, e = c:connect("10.0.0.1", 81) | 408 | local r, e = c:connect("10.0.0.1", 81) |
409 | assert(not r, "should not connect") | 409 | assert(not r, "should not connect") |
410 | assert(socket.gettime() - t < 2, "took too long to give up.") | 410 | assert(socket.gettime() - t < 2, "took too long to give up.") |
411 | c:close() | 411 | c:close() |
412 | pass("ok") | 412 | pass("ok") |
413 | end | 413 | end |
414 | 414 | ||
415 | ------------------------------------------------------------------------ | 415 | ------------------------------------------------------------------------ |
@@ -447,16 +447,14 @@ end | |||
447 | 447 | ||
448 | ------------------------------------------------------------------------ | 448 | ------------------------------------------------------------------------ |
449 | function rebind_test() | 449 | function rebind_test() |
450 | --local c ,c1 = socket.bind("localhost", 0) | ||
451 | local c ,c1 = socket.bind("127.0.0.1", 0) | 450 | local c ,c1 = socket.bind("127.0.0.1", 0) |
452 | if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end | 451 | if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end |
453 | assert(c,c1) | 452 | assert(c,c1) |
454 | |||
455 | local i, p = c:getsockname() | 453 | local i, p = c:getsockname() |
456 | local s, e = socket.tcp() | 454 | local s, e = socket.tcp() |
457 | assert(s, e) | 455 | assert(s, e) |
458 | s:setoption("reuseaddr", false) | 456 | s:setoption("reuseaddr", false) |
459 | r, e = s:bind("localhost", p) | 457 | r, e = s:bind(i, p) |
460 | assert(not r, "managed to rebind!") | 458 | assert(not r, "managed to rebind!") |
461 | assert(e) | 459 | assert(e) |
462 | pass("ok") | 460 | pass("ok") |
@@ -476,9 +474,9 @@ function getstats_test() | |||
476 | data:receive(c) | 474 | data:receive(c) |
477 | t = t + c | 475 | t = t + c |
478 | local r, s, a = data:getstats() | 476 | local r, s, a = data:getstats() |
479 | assert(r == t, "received count failed" .. tostring(r) | 477 | assert(r == t, "received count failed" .. tostring(r) |
480 | .. "/" .. tostring(t)) | 478 | .. "/" .. tostring(t)) |
481 | assert(s == t, "sent count failed" .. tostring(s) | 479 | assert(s == t, "sent count failed" .. tostring(s) |
482 | .. "/" .. tostring(t)) | 480 | .. "/" .. tostring(t)) |
483 | end | 481 | end |
484 | pass("ok") | 482 | pass("ok") |
@@ -486,7 +484,7 @@ end | |||
486 | 484 | ||
487 | 485 | ||
488 | ------------------------------------------------------------------------ | 486 | ------------------------------------------------------------------------ |
489 | function test_nonblocking(size) | 487 | function test_nonblocking(size) |
490 | reconnect() | 488 | reconnect() |
491 | printf("testing " .. 2*size .. " bytes: ") | 489 | printf("testing " .. 2*size .. " bytes: ") |
492 | remote(string.format([[ | 490 | remote(string.format([[ |
@@ -545,7 +543,7 @@ function test_readafterclose() | |||
545 | data:close() | 543 | data:close() |
546 | data = nil | 544 | data = nil |
547 | ]])) | 545 | ]])) |
548 | data:close() | 546 | data:close() |
549 | back, err, partial = data:receive("*a") | 547 | back, err, partial = data:receive("*a") |
550 | assert(back == nil and err == "closed", "should have returned 'closed'") | 548 | assert(back == nil and err == "closed", "should have returned 'closed'") |
551 | pass("ok") | 549 | pass("ok") |
@@ -555,7 +553,7 @@ function test_readafterclose() | |||
555 | data:close() | 553 | data:close() |
556 | data = nil | 554 | data = nil |
557 | ]])) | 555 | ]])) |
558 | data:close() | 556 | data:close() |
559 | back, err, partial = data:receive() | 557 | back, err, partial = data:receive() |
560 | assert(back == nil and err == "closed", "should have returned 'closed'") | 558 | assert(back == nil and err == "closed", "should have returned 'closed'") |
561 | pass("ok") | 559 | pass("ok") |
@@ -565,7 +563,7 @@ function test_readafterclose() | |||
565 | data:close() | 563 | data:close() |
566 | data = nil | 564 | data = nil |
567 | ]])) | 565 | ]])) |
568 | data:close() | 566 | data:close() |
569 | back, err, partial = data:receive(1) | 567 | back, err, partial = data:receive(1) |
570 | assert(back == nil and err == "closed", "should have returned 'closed'") | 568 | assert(back == nil and err == "closed", "should have returned 'closed'") |
571 | pass("ok") | 569 | pass("ok") |
@@ -575,7 +573,7 @@ function test_readafterclose() | |||
575 | data:close() | 573 | data:close() |
576 | data = nil | 574 | data = nil |
577 | ]])) | 575 | ]])) |
578 | data:close() | 576 | data:close() |
579 | back, err, partial = data:receive(0) | 577 | back, err, partial = data:receive(0) |
580 | assert(back == nil and err == "closed", "should have returned 'closed'") | 578 | assert(back == nil and err == "closed", "should have returned 'closed'") |
581 | pass("ok") | 579 | pass("ok") |
@@ -593,7 +591,7 @@ function test_writeafterclose() | |||
593 | while not err do | 591 | while not err do |
594 | sent, err, errsent, time = data:send(str) | 592 | sent, err, errsent, time = data:send(str) |
595 | end | 593 | end |
596 | assert(err == "closed", "should have returned 'closed'") | 594 | assert(err == "closed", "got " .. err .. " instead of 'closed'") |
597 | pass("ok") | 595 | pass("ok") |
598 | end | 596 | end |
599 | 597 | ||
@@ -648,18 +646,18 @@ else io.stderr:write("Warning! IPv6 does not support!\n") end | |||
648 | end | 646 | end |
649 | 647 | ||
650 | local udp_methods = { | 648 | local udp_methods = { |
651 | "close", | 649 | "close", |
652 | "dirty", | 650 | "dirty", |
653 | "getfamily", | 651 | "getfamily", |
654 | "getfd", | 652 | "getfd", |
655 | "getoption", | 653 | "getoption", |
656 | "getpeername", | 654 | "getpeername", |
657 | "getsockname", | 655 | "getsockname", |
658 | "receive", | 656 | "receive", |
659 | "receivefrom", | 657 | "receivefrom", |
660 | "send", | 658 | "send", |
661 | "sendto", | 659 | "sendto", |
662 | "setfd", | 660 | "setfd", |
663 | "setoption", | 661 | "setoption", |
664 | "setpeername", | 662 | "setpeername", |
665 | "setsockname", | 663 | "setsockname", |
@@ -674,6 +672,9 @@ if sock then test_methods(socket.udp6(), udp_methods) | |||
674 | else io.stderr:write("Warning! IPv6 does not support!\n") end | 672 | else io.stderr:write("Warning! IPv6 does not support!\n") end |
675 | end | 673 | end |
676 | 674 | ||
675 | test("closed connection detection: ") | ||
676 | test_closed() | ||
677 | |||
677 | test("partial receive") | 678 | test("partial receive") |
678 | test_partialrecv() | 679 | test_partialrecv() |
679 | 680 | ||
@@ -697,9 +698,6 @@ rebind_test() | |||
697 | test("active close: ") | 698 | test("active close: ") |
698 | active_close() | 699 | active_close() |
699 | 700 | ||
700 | test("closed connection detection: ") | ||
701 | test_closed() | ||
702 | |||
703 | test("accept function: ") | 701 | test("accept function: ") |
704 | accept_timeout() | 702 | accept_timeout() |
705 | accept_errors() | 703 | accept_errors() |
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 72b93ab..1eb2d5b 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
@@ -6,7 +6,7 @@ ack = "\n"; | |||
6 | while 1 do | 6 | while 1 do |
7 | print("server: waiting for client connection..."); | 7 | print("server: waiting for client connection..."); |
8 | control = assert(server:accept()); | 8 | control = assert(server:accept()); |
9 | while 1 do | 9 | while 1 do |
10 | command, emsg = control:receive(); | 10 | command, emsg = control:receive(); |
11 | if emsg == "closed" then | 11 | if emsg == "closed" then |
12 | control:close() | 12 | control:close() |
diff --git a/test/udpconnectclnt.lua b/test/udpconnectclnt.lua index effe13a..ad6ab6a 100644 --- a/test/udpconnectclnt.lua +++ b/test/udpconnectclnt.lua | |||
@@ -1,7 +1,7 @@ | |||
1 | local socket = require"socket" | 1 | local socket = require"socket" |
2 | local udp = socket.udp | 2 | local udp = socket.udp |
3 | local localhost = "127.0.0.1" | 3 | local localhost = "127.0.0.1" |
4 | local port = arg[1] | 4 | local port = assert(arg[1], "missing port argument") |
5 | 5 | ||
6 | se = udp(); se:setoption("reuseaddr", true) | 6 | se = udp(); se:setoption("reuseaddr", true) |
7 | se:setsockname(localhost, 5062) | 7 | se:setsockname(localhost, 5062) |