aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDiego Nehab <diego.nehab@gmail.com>2015-08-25 15:43:48 -0300
committerDiego Nehab <diego.nehab@gmail.com>2015-08-25 15:43:48 -0300
commit4110e4125dace9df3a744067066e5dee62670561 (patch)
tree10927b57ea5c543054d01bd2bd538a4f97128678 /test
parent46d7e75f3e71d75bf07ae83d7df4aa276e484473 (diff)
parent77bba625d7aaa0f9e118879163687fcbcb0b5a7b (diff)
downloadluasocket-4110e4125dace9df3a744067066e5dee62670561.tar.gz
luasocket-4110e4125dace9df3a744067066e5dee62670561.tar.bz2
luasocket-4110e4125dace9df3a744067066e5dee62670561.zip
Merge branch 'agnostic'
Seems safe to move to master.
Diffstat (limited to 'test')
-rw-r--r--test/testclnt.lua119
-rw-r--r--test/testsrvr.lua2
-rw-r--r--test/udpconnectclnt.lua2
3 files changed, 63 insertions, 60 deletions
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 0014781..ee1201f 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -8,7 +8,7 @@ function printf(...)
8end 8end
9 9
10function pass(...) 10function pass(...)
11 printf(...) 11 printf(...)
12 io.stderr:write("\n") 12 io.stderr:write("\n")
13end 13end
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
74end 74end
@@ -104,8 +104,8 @@ control:setoption("tcp-nodelay", true)
104------------------------------------------------------------------------ 104------------------------------------------------------------------------
105function test_methods(sock, methods) 105function 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
124remote (string.format("str = data:receive(%d)", 124remote (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
172remote "str = data:receive()" 172remote "str = data:receive()"
@@ -216,7 +216,7 @@ function test_totaltimeoutreceive(len, tm, sl)
216 data:settimeout(tm, "total") 216 data:settimeout(tm, "total")
217local t = socket.gettime() 217local 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)
221end 221end
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)
241end 241end
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)
261end 261end
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
299end 299end
@@ -304,15 +304,20 @@ function isclosed(c)
304end 304end
305 305
306function active_close() 306function active_close()
307 reconnect() 307 local tcp = socket.tcp4()
308 if isclosed(data) then fail("should not be closed") end 308 if isclosed(tcp) then fail("should not be closed") end
309 data:close() 309 tcp:close()
310 if not isclosed(data) then fail("should be closed") end 310 if not isclosed(tcp) then fail("should be closed") end
311 data = nil 311 tcp = socket.tcp()
312 local udp = socket.udp() 312 if not isclosed(tcp) then fail("should be closed") end
313 tcp = nil
314 local udp = socket.udp4()
313 if isclosed(udp) then fail("should not be closed") end 315 if isclosed(udp) then fail("should not be closed") end
314 udp:close() 316 udp:close()
315 if not isclosed(udp) then fail("should be closed") end 317 if not isclosed(udp) then fail("should be closed") end
318 udp = socket.udp()
319 if not isclosed(udp) then fail("should be closed") end
320 udp = nil
316 pass("ok") 321 pass("ok")
317end 322end
318 323
@@ -327,7 +332,7 @@ function test_closed()
327 data:close() 332 data:close()
328 data = nil 333 data = nil
329 ]], str)) 334 ]], str))
330 -- try to get a line 335 -- try to get a line
331 back, err, partial = data:receive() 336 back, err, partial = data:receive()
332 if not err then fail("should have gotten 'closed'.") 337 if not err then fail("should have gotten 'closed'.")
333 elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") 338 elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.")
@@ -340,25 +345,25 @@ function test_closed()
340 data = nil 345 data = nil
341 ]] 346 ]]
342 total, err, partial = data:send(string.rep("ugauga", 100000)) 347 total, err, partial = data:send(string.rep("ugauga", 100000))
343 if not err then 348 if not err then
344 pass("failed: output buffer is at least %d bytes long!", total) 349 pass("failed: output buffer is at least %d bytes long!", total)
345 elseif err ~= "closed" then 350 elseif err ~= "closed" then
346 fail("got '"..err.."' instead of 'closed'.") 351 fail("got '"..err.."' instead of 'closed'.")
347 else 352 else
348 pass("graceful 'closed' received after %d bytes were sent", partial) 353 pass("graceful 'closed' received after %d bytes were sent", partial)
349 end 354 end
350end 355end
351 356
352------------------------------------------------------------------------ 357------------------------------------------------------------------------
353function test_selectbugs() 358function test_selectbugs()
354 local r, s, e = socket.select(nil, nil, 0.1) 359 local r, s, e = socket.select(nil, nil, 0.1)
355 assert(type(r) == "table" and type(s) == "table" and 360 assert(type(r) == "table" and type(s) == "table" and
356 (e == "timeout" or e == "error")) 361 (e == "timeout" or e == "error"))
357 pass("both nil: ok") 362 pass("both nil: ok")
358 local udp = socket.udp() 363 local udp = socket.udp()
359 udp:close() 364 udp:close()
360 r, s, e = socket.select({ udp }, { udp }, 0.1) 365 r, s, e = socket.select({ udp }, { udp }, 0.1)
361 assert(type(r) == "table" and type(s) == "table" and 366 assert(type(r) == "table" and type(s) == "table" and
362 (e == "timeout" or e == "error")) 367 (e == "timeout" or e == "error"))
363 pass("closed sockets: ok") 368 pass("closed sockets: ok")
364 e = pcall(socket.select, "wrong", 1, 0.1) 369 e = pcall(socket.select, "wrong", 1, 0.1)
@@ -368,7 +373,7 @@ function test_selectbugs()
368 pass("invalid input: ok") 373 pass("invalid input: ok")
369 local toomany = {} 374 local toomany = {}
370 for i = 1, socket._SETSIZE+1 do 375 for i = 1, socket._SETSIZE+1 do
371 toomany[#toomany+1] = socket.udp() 376 toomany[#toomany+1] = socket.udp4()
372 end 377 end
373 if #toomany > socket._SETSIZE then 378 if #toomany > socket._SETSIZE then
374 local e = pcall(socket.select, toomany, nil, 0.1) 379 local e = pcall(socket.select, toomany, nil, 0.1)
@@ -389,7 +394,7 @@ function accept_timeout()
389 local t = socket.gettime() 394 local t = socket.gettime()
390 s:settimeout(1) 395 s:settimeout(1)
391 local c, e = s:accept() 396 local c, e = s:accept()
392 assert(not c, "should not accept") 397 assert(not c, "should not accept")
393 assert(e == "timeout", string.format("wrong error message (%s)", e)) 398 assert(e == "timeout", string.format("wrong error message (%s)", e))
394 t = socket.gettime() - t 399 t = socket.gettime() - t
395 assert(t < 2, string.format("took to long to give up (%gs)", t)) 400 assert(t < 2, string.format("took to long to give up (%gs)", t))
@@ -407,9 +412,9 @@ function connect_timeout()
407 local t = socket.gettime() 412 local t = socket.gettime()
408 local r, e = c:connect("10.0.0.1", 81) 413 local r, e = c:connect("10.0.0.1", 81)
409 assert(not r, "should not connect") 414 assert(not r, "should not connect")
410 assert(socket.gettime() - t < 2, "took too long to give up.") 415 assert(socket.gettime() - t < 2, "took too long to give up.")
411 c:close() 416 c:close()
412 pass("ok") 417 pass("ok")
413end 418end
414 419
415------------------------------------------------------------------------ 420------------------------------------------------------------------------
@@ -447,16 +452,14 @@ end
447 452
448------------------------------------------------------------------------ 453------------------------------------------------------------------------
449function rebind_test() 454function rebind_test()
450 --local c ,c1 = socket.bind("localhost", 0)
451 local c ,c1 = socket.bind("127.0.0.1", 0) 455 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 456 if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end
453 assert(c,c1) 457 assert(c,c1)
454
455 local i, p = c:getsockname() 458 local i, p = c:getsockname()
456 local s, e = socket.tcp() 459 local s, e = socket.tcp()
457 assert(s, e) 460 assert(s, e)
458 s:setoption("reuseaddr", false) 461 s:setoption("reuseaddr", false)
459 r, e = s:bind("localhost", p) 462 r, e = s:bind(i, p)
460 assert(not r, "managed to rebind!") 463 assert(not r, "managed to rebind!")
461 assert(e) 464 assert(e)
462 pass("ok") 465 pass("ok")
@@ -476,9 +479,9 @@ function getstats_test()
476 data:receive(c) 479 data:receive(c)
477 t = t + c 480 t = t + c
478 local r, s, a = data:getstats() 481 local r, s, a = data:getstats()
479 assert(r == t, "received count failed" .. tostring(r) 482 assert(r == t, "received count failed" .. tostring(r)
480 .. "/" .. tostring(t)) 483 .. "/" .. tostring(t))
481 assert(s == t, "sent count failed" .. tostring(s) 484 assert(s == t, "sent count failed" .. tostring(s)
482 .. "/" .. tostring(t)) 485 .. "/" .. tostring(t))
483 end 486 end
484 pass("ok") 487 pass("ok")
@@ -486,7 +489,7 @@ end
486 489
487 490
488------------------------------------------------------------------------ 491------------------------------------------------------------------------
489function test_nonblocking(size) 492function test_nonblocking(size)
490 reconnect() 493 reconnect()
491 printf("testing " .. 2*size .. " bytes: ") 494 printf("testing " .. 2*size .. " bytes: ")
492remote(string.format([[ 495remote(string.format([[
@@ -545,7 +548,7 @@ function test_readafterclose()
545 data:close() 548 data:close()
546 data = nil 549 data = nil
547 ]])) 550 ]]))
548 data:close() 551 data:close()
549 back, err, partial = data:receive("*a") 552 back, err, partial = data:receive("*a")
550 assert(back == nil and err == "closed", "should have returned 'closed'") 553 assert(back == nil and err == "closed", "should have returned 'closed'")
551 pass("ok") 554 pass("ok")
@@ -555,7 +558,7 @@ function test_readafterclose()
555 data:close() 558 data:close()
556 data = nil 559 data = nil
557 ]])) 560 ]]))
558 data:close() 561 data:close()
559 back, err, partial = data:receive() 562 back, err, partial = data:receive()
560 assert(back == nil and err == "closed", "should have returned 'closed'") 563 assert(back == nil and err == "closed", "should have returned 'closed'")
561 pass("ok") 564 pass("ok")
@@ -565,7 +568,7 @@ function test_readafterclose()
565 data:close() 568 data:close()
566 data = nil 569 data = nil
567 ]])) 570 ]]))
568 data:close() 571 data:close()
569 back, err, partial = data:receive(1) 572 back, err, partial = data:receive(1)
570 assert(back == nil and err == "closed", "should have returned 'closed'") 573 assert(back == nil and err == "closed", "should have returned 'closed'")
571 pass("ok") 574 pass("ok")
@@ -575,7 +578,7 @@ function test_readafterclose()
575 data:close() 578 data:close()
576 data = nil 579 data = nil
577 ]])) 580 ]]))
578 data:close() 581 data:close()
579 back, err, partial = data:receive(0) 582 back, err, partial = data:receive(0)
580 assert(back == nil and err == "closed", "should have returned 'closed'") 583 assert(back == nil and err == "closed", "should have returned 'closed'")
581 pass("ok") 584 pass("ok")
@@ -593,7 +596,7 @@ function test_writeafterclose()
593 while not err do 596 while not err do
594 sent, err, errsent, time = data:send(str) 597 sent, err, errsent, time = data:send(str)
595 end 598 end
596 assert(err == "closed", "should have returned 'closed'") 599 assert(err == "closed", "got " .. err .. " instead of 'closed'")
597 pass("ok") 600 pass("ok")
598end 601end
599 602
@@ -648,18 +651,18 @@ else io.stderr:write("Warning! IPv6 does not support!\n") end
648end 651end
649 652
650local udp_methods = { 653local udp_methods = {
651 "close", 654 "close",
652 "dirty", 655 "dirty",
653 "getfamily", 656 "getfamily",
654 "getfd", 657 "getfd",
655 "getoption", 658 "getoption",
656 "getpeername", 659 "getpeername",
657 "getsockname", 660 "getsockname",
658 "receive", 661 "receive",
659 "receivefrom", 662 "receivefrom",
660 "send", 663 "send",
661 "sendto", 664 "sendto",
662 "setfd", 665 "setfd",
663 "setoption", 666 "setoption",
664 "setpeername", 667 "setpeername",
665 "setsockname", 668 "setsockname",
@@ -674,6 +677,9 @@ if sock then test_methods(socket.udp6(), udp_methods)
674else io.stderr:write("Warning! IPv6 does not support!\n") end 677else io.stderr:write("Warning! IPv6 does not support!\n") end
675end 678end
676 679
680test("closed connection detection: ")
681test_closed()
682
677test("partial receive") 683test("partial receive")
678test_partialrecv() 684test_partialrecv()
679 685
@@ -697,9 +703,6 @@ rebind_test()
697test("active close: ") 703test("active close: ")
698active_close() 704active_close()
699 705
700test("closed connection detection: ")
701test_closed()
702
703test("accept function: ") 706test("accept function: ")
704accept_timeout() 707accept_timeout()
705accept_errors() 708accept_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";
6while 1 do 6while 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 @@
1local socket = require"socket" 1local socket = require"socket"
2local udp = socket.udp 2local udp = socket.udp
3local localhost = "127.0.0.1" 3local localhost = "127.0.0.1"
4local port = arg[1] 4local port = assert(arg[1], "missing port argument")
5 5
6se = udp(); se:setoption("reuseaddr", true) 6se = udp(); se:setoption("reuseaddr", true)
7se:setsockname(localhost, 5062) 7se:setsockname(localhost, 5062)