From 96965b179c7311f850f72a8629b9ba6d3a31d117 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Sat, 22 Aug 2015 19:52:01 -0300 Subject: New agnostic IPv4 IPv6 functions. Also dealing with EPROTOTYPE Yosemite seems to be throwing at us for no reason. --- test/testclnt.lua | 100 ++++++++++++++++++++++++------------------------ test/testsrvr.lua | 2 +- test/udpconnectclnt.lua | 2 +- 3 files changed, 51 insertions(+), 53 deletions(-) (limited to 'test') 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(...) end function pass(...) - printf(...) + printf(...) io.stderr:write("\n") end @@ -45,30 +45,30 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) if not err then warn("must be buffered") elseif err == "timeout" then pass("proper timeout") else fail("unexpected error '%s'", err) end - else - if err ~= "timeout" then fail("should have timed out") + else + if err ~= "timeout" then fail("should have timed out") else pass("proper timeout") end end else if mode == "total" then - if elapsed > tm then + if elapsed > tm then if err ~= "timeout" then fail("should have timed out") else pass("proper timeout") end elseif elapsed < tm then - if err then fail(err) + if err then fail(err) else pass("ok") end - else - if alldone then - if err then fail("unexpected error '%s'", err) + else + if alldone then + if err then fail("unexpected error '%s'", err) else pass("ok") end else - if err ~= "timeout" then fail(err) + if err ~= "timeout" then fail(err) else pass("proper timeoutk") end end end - else - if err then fail(err) - else pass("ok") end + else + if err then fail(err) + else pass("ok") end end end end @@ -104,8 +104,8 @@ control:setoption("tcp-nodelay", true) ------------------------------------------------------------------------ function test_methods(sock, methods) for _, v in pairs(methods) do - if type(sock[v]) ~= "function" then - fail(sock.class .. " method '" .. v .. "' not registered") + if type(sock[v]) ~= "function" then + fail(sock.class .. " method '" .. v .. "' not registered") end end pass(sock.class .. " methods are ok") @@ -121,7 +121,7 @@ function test_mixed(len) local p3 = "raw " .. string.rep("z", inter) .. "bytes" local p4 = "end" .. string.rep("w", inter) .. "bytes" local bp1, bp2, bp3, bp4 -remote (string.format("str = data:receive(%d)", +remote (string.format("str = data:receive(%d)", string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) sent, err = data:send(p1..p2..p3..p4) if err then fail(err) end @@ -166,7 +166,7 @@ function test_rawline(len) io.stderr:write("length " .. len .. ": ") local str, str10, back, err str = string.rep(string.char(47), math.mod(len, 10)) - str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), + str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), math.floor(len/10)) str = str .. str10 remote "str = data:receive()" @@ -216,7 +216,7 @@ function test_totaltimeoutreceive(len, tm, sl) data:settimeout(tm, "total") local t = socket.gettime() str, err, partial, elapsed = data:receive(2*len) - check_timeout(tm, sl, elapsed, err, "receive", "total", + check_timeout(tm, sl, elapsed, err, "receive", "total", string.len(str or partial) == 2*len) end @@ -236,7 +236,7 @@ function test_totaltimeoutsend(len, tm, sl) data:settimeout(tm, "total") str = string.rep("a", 2*len) total, err, partial, elapsed = data:send(str) - check_timeout(tm, sl, elapsed, err, "send", "total", + check_timeout(tm, sl, elapsed, err, "send", "total", total == 2*len) end @@ -256,7 +256,7 @@ function test_blockingtimeoutreceive(len, tm, sl) ]], 2*tm, len, sl, sl)) data:settimeout(tm) str, err, partial, elapsed = data:receive(2*len) - check_timeout(tm, sl, elapsed, err, "receive", "blocking", + check_timeout(tm, sl, elapsed, err, "receive", "blocking", string.len(str or partial) == 2*len) end @@ -290,10 +290,10 @@ function empty_connect() data = server:accept() ]] data, err = socket.connect("", port) - if not data then + if not data then pass("ok") data = socket.connect(host, port) - else + else pass("gethostbyname returns localhost on empty string...") end end @@ -327,7 +327,7 @@ function test_closed() data:close() data = nil ]], str)) - -- try to get a line + -- try to get a line back, err, partial = data:receive() if not err then fail("should have gotten 'closed'.") elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") @@ -340,25 +340,25 @@ function test_closed() data = nil ]] total, err, partial = data:send(string.rep("ugauga", 100000)) - if not err then + if not err then pass("failed: output buffer is at least %d bytes long!", total) - elseif err ~= "closed" then + elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") - else - pass("graceful 'closed' received after %d bytes were sent", partial) + else + pass("graceful 'closed' received after %d bytes were sent", partial) end end ------------------------------------------------------------------------ function test_selectbugs() local r, s, e = socket.select(nil, nil, 0.1) - assert(type(r) == "table" and type(s) == "table" and + assert(type(r) == "table" and type(s) == "table" and (e == "timeout" or e == "error")) pass("both nil: ok") local udp = socket.udp() udp:close() r, s, e = socket.select({ udp }, { udp }, 0.1) - assert(type(r) == "table" and type(s) == "table" and + assert(type(r) == "table" and type(s) == "table" and (e == "timeout" or e == "error")) pass("closed sockets: ok") e = pcall(socket.select, "wrong", 1, 0.1) @@ -389,7 +389,7 @@ function accept_timeout() local t = socket.gettime() s:settimeout(1) local c, e = s:accept() - assert(not c, "should not accept") + assert(not c, "should not accept") assert(e == "timeout", string.format("wrong error message (%s)", e)) t = socket.gettime() - t assert(t < 2, string.format("took to long to give up (%gs)", t)) @@ -407,9 +407,9 @@ function connect_timeout() local t = socket.gettime() local r, e = c:connect("10.0.0.1", 81) assert(not r, "should not connect") - assert(socket.gettime() - t < 2, "took too long to give up.") + assert(socket.gettime() - t < 2, "took too long to give up.") c:close() - pass("ok") + pass("ok") end ------------------------------------------------------------------------ @@ -447,16 +447,14 @@ end ------------------------------------------------------------------------ function rebind_test() - --local c ,c1 = socket.bind("localhost", 0) local c ,c1 = socket.bind("127.0.0.1", 0) if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1)) return end assert(c,c1) - local i, p = c:getsockname() local s, e = socket.tcp() assert(s, e) s:setoption("reuseaddr", false) - r, e = s:bind("localhost", p) + r, e = s:bind(i, p) assert(not r, "managed to rebind!") assert(e) pass("ok") @@ -476,9 +474,9 @@ function getstats_test() data:receive(c) t = t + c local r, s, a = data:getstats() - assert(r == t, "received count failed" .. tostring(r) + assert(r == t, "received count failed" .. tostring(r) .. "/" .. tostring(t)) - assert(s == t, "sent count failed" .. tostring(s) + assert(s == t, "sent count failed" .. tostring(s) .. "/" .. tostring(t)) end pass("ok") @@ -486,7 +484,7 @@ end ------------------------------------------------------------------------ -function test_nonblocking(size) +function test_nonblocking(size) reconnect() printf("testing " .. 2*size .. " bytes: ") remote(string.format([[ @@ -545,7 +543,7 @@ function test_readafterclose() data:close() data = nil ]])) - data:close() + data:close() back, err, partial = data:receive("*a") assert(back == nil and err == "closed", "should have returned 'closed'") pass("ok") @@ -555,7 +553,7 @@ function test_readafterclose() data:close() data = nil ]])) - data:close() + data:close() back, err, partial = data:receive() assert(back == nil and err == "closed", "should have returned 'closed'") pass("ok") @@ -565,7 +563,7 @@ function test_readafterclose() data:close() data = nil ]])) - data:close() + data:close() back, err, partial = data:receive(1) assert(back == nil and err == "closed", "should have returned 'closed'") pass("ok") @@ -575,7 +573,7 @@ function test_readafterclose() data:close() data = nil ]])) - data:close() + data:close() back, err, partial = data:receive(0) assert(back == nil and err == "closed", "should have returned 'closed'") pass("ok") @@ -593,7 +591,7 @@ function test_writeafterclose() while not err do sent, err, errsent, time = data:send(str) end - assert(err == "closed", "should have returned 'closed'") + assert(err == "closed", "got " .. err .. " instead of 'closed'") pass("ok") end @@ -648,18 +646,18 @@ else io.stderr:write("Warning! IPv6 does not support!\n") end end local udp_methods = { - "close", + "close", "dirty", "getfamily", "getfd", "getoption", "getpeername", "getsockname", - "receive", - "receivefrom", - "send", - "sendto", - "setfd", + "receive", + "receivefrom", + "send", + "sendto", + "setfd", "setoption", "setpeername", "setsockname", @@ -674,6 +672,9 @@ if sock then test_methods(socket.udp6(), udp_methods) else io.stderr:write("Warning! IPv6 does not support!\n") end end +test("closed connection detection: ") +test_closed() + test("partial receive") test_partialrecv() @@ -697,9 +698,6 @@ rebind_test() test("active close: ") active_close() -test("closed connection detection: ") -test_closed() - test("accept function: ") accept_timeout() 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"; while 1 do print("server: waiting for client connection..."); control = assert(server:accept()); - while 1 do + while 1 do command, emsg = control:receive(); if emsg == "closed" then 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 @@ local socket = require"socket" local udp = socket.udp local localhost = "127.0.0.1" -local port = arg[1] +local port = assert(arg[1], "missing port argument") se = udp(); se:setoption("reuseaddr", true) se:setsockname(localhost, 5062) -- cgit v1.2.3-55-g6feb