Saving before big changes to support IPv6.
This commit is contained in:
parent
bce60be30f
commit
3a8ba90dfb
30 changed files with 948 additions and 850 deletions
|
@ -3,15 +3,20 @@ local socket = require"socket"
|
|||
host = host or "localhost"
|
||||
port = port or "8383"
|
||||
|
||||
function pass(...)
|
||||
function printf(...)
|
||||
local s = string.format(unpack(arg))
|
||||
io.stderr:write(s, "\n")
|
||||
io.stderr:write(s)
|
||||
end
|
||||
|
||||
function pass(...)
|
||||
printf(...)
|
||||
io.stderr:write("\n")
|
||||
end
|
||||
|
||||
function fail(...)
|
||||
local s = string.format(unpack(arg))
|
||||
io.stderr:write("ERROR: ", s, "!\n")
|
||||
socket.sleep(3)
|
||||
io.stderr:write("ERROR: ")
|
||||
printf(...)
|
||||
io.stderr:write("!\n")
|
||||
os.exit()
|
||||
end
|
||||
|
||||
|
@ -80,7 +85,6 @@ io.stderr:write("----------------------------------------------\n",
|
|||
start = socket.gettime()
|
||||
|
||||
function reconnect()
|
||||
io.stderr:write("attempting data connection... ")
|
||||
if data then data:close() end
|
||||
remote [[
|
||||
if data then data:close() data = nil end
|
||||
|
@ -88,12 +92,11 @@ function reconnect()
|
|||
data:setoption("tcp-nodelay", true)
|
||||
]]
|
||||
data, err = socket.connect(host, port)
|
||||
if not data then fail(err)
|
||||
else pass("connected!") end
|
||||
if not data then fail(err) end
|
||||
data:setoption("tcp-nodelay", true)
|
||||
end
|
||||
|
||||
pass("attempting control connection...")
|
||||
printf("attempting control connection...")
|
||||
control, err = socket.connect(host, port)
|
||||
if err then fail(err)
|
||||
else pass("connected!") end
|
||||
|
@ -112,6 +115,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_mixed(len)
|
||||
reconnect()
|
||||
io.stderr:write("length " .. len .. ": ")
|
||||
local inter = math.ceil(len/4)
|
||||
local p1 = "unix " .. string.rep("x", inter) .. "line\n"
|
||||
local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
|
||||
|
@ -139,6 +143,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_asciiline(len)
|
||||
reconnect()
|
||||
io.stderr:write("length " .. len .. ": ")
|
||||
local str, str10, back, err
|
||||
str = string.rep("x", math.mod(len, 10))
|
||||
str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
|
||||
|
@ -156,6 +161,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_rawline(len)
|
||||
reconnect()
|
||||
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),
|
||||
|
@ -174,6 +180,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_raw(len)
|
||||
reconnect()
|
||||
io.stderr:write("length " .. len .. ": ")
|
||||
local half = math.floor(len/2)
|
||||
local s1, s2, back, err
|
||||
s1 = string.rep("x", half)
|
||||
|
@ -194,7 +201,7 @@ end
|
|||
function test_totaltimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||
printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = string.rep('a', %d)
|
||||
|
@ -215,7 +222,7 @@ end
|
|||
function test_totaltimeoutsend(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||
printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = data:receive(%d)
|
||||
|
@ -235,7 +242,7 @@ end
|
|||
function test_blockingtimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||
printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = string.rep('a', %d)
|
||||
|
@ -255,7 +262,7 @@ end
|
|||
function test_blockingtimeoutsend(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||
printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
str = data:receive(%d)
|
||||
|
@ -273,6 +280,7 @@ end
|
|||
|
||||
------------------------------------------------------------------------
|
||||
function empty_connect()
|
||||
printf("empty connect: ")
|
||||
reconnect()
|
||||
if data then data:close() data = nil end
|
||||
remote [[
|
||||
|
@ -284,7 +292,7 @@ function empty_connect()
|
|||
pass("ok")
|
||||
data = socket.connect(host, port)
|
||||
else
|
||||
pass("gethostbyname returns localhost on empty string...")
|
||||
pass("gethostbyname returns localhost on empty string...")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -311,7 +319,7 @@ function test_closed()
|
|||
local back, partial, err
|
||||
local str = 'little string'
|
||||
reconnect()
|
||||
pass("trying read detection")
|
||||
printf("trying read detection: ")
|
||||
remote (string.format ([[
|
||||
data:send('%s')
|
||||
data:close()
|
||||
|
@ -324,7 +332,7 @@ function test_closed()
|
|||
elseif str ~= partial then fail("didn't receive partial result.")
|
||||
else pass("graceful 'closed' received") end
|
||||
reconnect()
|
||||
pass("trying write detection")
|
||||
printf("trying write detection: ")
|
||||
remote [[
|
||||
data:close()
|
||||
data = nil
|
||||
|
@ -342,7 +350,6 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_selectbugs()
|
||||
local r, s, e = socket.select(nil, nil, 0.1)
|
||||
print(r, s, e)
|
||||
assert(type(r) == "table" and type(s) == "table" and
|
||||
(e == "timeout" or e == "error"))
|
||||
pass("both nil: ok")
|
||||
|
@ -374,7 +381,7 @@ end
|
|||
|
||||
------------------------------------------------------------------------
|
||||
function accept_timeout()
|
||||
io.stderr:write("accept with timeout (if it hangs, it failed): ")
|
||||
printf("accept with timeout (if it hangs, it failed): ")
|
||||
local s, e = socket.bind("*", 0, 0)
|
||||
assert(s, e)
|
||||
local t = socket.gettime()
|
||||
|
@ -390,23 +397,22 @@ end
|
|||
|
||||
------------------------------------------------------------------------
|
||||
function connect_timeout()
|
||||
io.stderr:write("connect with timeout (if it hangs, it failed!): ")
|
||||
printf("connect with timeout (if it hangs, it failed!): ")
|
||||
local t = socket.gettime()
|
||||
local c, e = socket.tcp()
|
||||
assert(c, e)
|
||||
c:settimeout(0.1)
|
||||
local t = socket.gettime()
|
||||
local r, e = c:connect("10.0.0.1", 81)
|
||||
print(r, e)
|
||||
assert(not r, "should not connect")
|
||||
assert(socket.gettime() - t < 2, "took too long to give up.")
|
||||
c:close()
|
||||
print("ok")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function accept_errors()
|
||||
io.stderr:write("not listening: ")
|
||||
printf("not listening: ")
|
||||
local d, e = socket.bind("*", 0)
|
||||
assert(d, e);
|
||||
local c, e = socket.tcp();
|
||||
|
@ -415,26 +421,26 @@ function accept_errors()
|
|||
d:settimeout(2)
|
||||
local r, e = d:accept()
|
||||
assert(not r and e)
|
||||
print("ok: ", e)
|
||||
io.stderr:write("not supported: ")
|
||||
pass("ok")
|
||||
printf("not supported: ")
|
||||
local c, e = socket.udp()
|
||||
assert(c, e);
|
||||
d:setfd(c:getfd())
|
||||
local r, e = d:accept()
|
||||
assert(not r and e)
|
||||
print("ok: ", e)
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function connect_errors()
|
||||
io.stderr:write("connection refused: ")
|
||||
printf("connection refused: ")
|
||||
local c, e = socket.connect("localhost", 1);
|
||||
assert(not c and e)
|
||||
print("ok: ", e)
|
||||
io.stderr:write("host not found: ")
|
||||
pass("ok")
|
||||
printf("host not found: ")
|
||||
local c, e = socket.connect("host.is.invalid", 1);
|
||||
assert(not c and e, e)
|
||||
print("ok: ", e)
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -447,7 +453,7 @@ function rebind_test()
|
|||
r, e = s:bind("localhost", p)
|
||||
assert(not r, "managed to rebind!")
|
||||
assert(e)
|
||||
print("ok: ", e)
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -469,14 +475,14 @@ function getstats_test()
|
|||
assert(s == t, "sent count failed" .. tostring(s)
|
||||
.. "/" .. tostring(t))
|
||||
end
|
||||
print("ok")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------
|
||||
function test_nonblocking(size)
|
||||
reconnect()
|
||||
print("Testing " .. 2*size .. " bytes")
|
||||
printf("testing " .. 2*size .. " bytes: ")
|
||||
remote(string.format([[
|
||||
data:send(string.rep("a", %d))
|
||||
socket.sleep(0.5)
|
||||
|
@ -508,7 +514,7 @@ remote(string.format([[
|
|||
data:settimeout(-1)
|
||||
local back = data:receive(2*size)
|
||||
assert(back == str, "'" .. back .. "' vs '" .. str .. "'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -516,7 +522,7 @@ function test_readafterclose()
|
|||
local back, partial, err
|
||||
local str = 'little string'
|
||||
reconnect()
|
||||
pass("trying repeated '*a' pattern")
|
||||
printf("trying repeated '*a' pattern")
|
||||
remote (string.format ([[
|
||||
data:send('%s')
|
||||
data:close()
|
||||
|
@ -526,9 +532,9 @@ function test_readafterclose()
|
|||
assert(back == str, "unexpected data read")
|
||||
back, err, partial = data:receive("*a")
|
||||
assert(back == nil and err == "closed", "should have returned 'closed'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
reconnect()
|
||||
pass("trying active close before '*a'")
|
||||
printf("trying active close before '*a'")
|
||||
remote (string.format ([[
|
||||
data:close()
|
||||
data = nil
|
||||
|
@ -536,9 +542,9 @@ function test_readafterclose()
|
|||
data:close()
|
||||
back, err, partial = data:receive("*a")
|
||||
assert(back == nil and err == "closed", "should have returned 'closed'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
reconnect()
|
||||
pass("trying active close before '*l'")
|
||||
printf("trying active close before '*l'")
|
||||
remote (string.format ([[
|
||||
data:close()
|
||||
data = nil
|
||||
|
@ -546,9 +552,9 @@ function test_readafterclose()
|
|||
data:close()
|
||||
back, err, partial = data:receive()
|
||||
assert(back == nil and err == "closed", "should have returned 'closed'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
reconnect()
|
||||
pass("trying active close before raw 1")
|
||||
printf("trying active close before raw 1")
|
||||
remote (string.format ([[
|
||||
data:close()
|
||||
data = nil
|
||||
|
@ -556,9 +562,9 @@ function test_readafterclose()
|
|||
data:close()
|
||||
back, err, partial = data:receive(1)
|
||||
assert(back == nil and err == "closed", "should have returned 'closed'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
reconnect()
|
||||
pass("trying active close before raw 0")
|
||||
printf("trying active close before raw 0")
|
||||
remote (string.format ([[
|
||||
data:close()
|
||||
data = nil
|
||||
|
@ -566,7 +572,7 @@ function test_readafterclose()
|
|||
data:close()
|
||||
back, err, partial = data:receive(0)
|
||||
assert(back == nil and err == "closed", "should have returned 'closed'")
|
||||
print("ok")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -581,13 +587,29 @@ function test_writeafterclose()
|
|||
while not err do
|
||||
sent, err, errsent, time = data:send(str)
|
||||
end
|
||||
print(sent, err, errsent, time)
|
||||
print("ok")
|
||||
assert(err == "closed", "should have returned 'closed'")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
--test_writeafterclose()
|
||||
|
||||
function test_partialrecv()
|
||||
local str = 'little string'
|
||||
reconnect()
|
||||
remote([[
|
||||
data:send("7890")
|
||||
]])
|
||||
data:settimeout(1)
|
||||
back, err = data:receive(10, "123456")
|
||||
assert(back == "1234567890", "failed on exact mixed length")
|
||||
back, err = data:receive(8, "87654321")
|
||||
assert(back == "87654321", "failed on exact length")
|
||||
back, err = data:receive(4, "87654321")
|
||||
assert(back == "87654321", "failed on smaller length")
|
||||
pass("ok")
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
test("method registration")
|
||||
test_methods(socket.tcp(), {
|
||||
"accept",
|
||||
|
@ -629,12 +651,18 @@ test_methods(socket.udp(), {
|
|||
"settimeout"
|
||||
})
|
||||
|
||||
test("partial receive")
|
||||
test_partialrecv()
|
||||
|
||||
test("select function")
|
||||
test_selectbugs()
|
||||
|
||||
test("testing read after close")
|
||||
test("read after close")
|
||||
test_readafterclose()
|
||||
|
||||
test("write after close")
|
||||
test_writeafterclose()
|
||||
|
||||
test("connect function")
|
||||
connect_timeout()
|
||||
empty_connect()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue