Faltam testes de ftp e smtp. O resto passa.

This commit is contained in:
Diego Nehab 2002-12-03 07:20:34 +00:00
parent d7e80592a6
commit 7da19138e3
9 changed files with 141 additions and 148 deletions

View File

@ -74,7 +74,7 @@ int buf_send(lua_State *L, p_buf buf)
} }
priv_pusherror(L, err); priv_pusherror(L, err);
lua_pushnumber(L, total); lua_pushnumber(L, total);
#ifdef _DEBUG #ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */ /* push time elapsed during operation as the last return value */
lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0);
#endif #endif
@ -139,7 +139,7 @@ int buf_receive(lua_State *L, p_buf buf)
for ( ; arg <= top; arg++) lua_pushnil(L); for ( ; arg <= top; arg++) lua_pushnil(L);
/* last return is an error code */ /* last return is an error code */
priv_pusherror(L, err); priv_pusherror(L, err);
#ifdef _DEBUG #ifdef LUASOCKET_DEBUG
/* push time elapsed during operation as the last return value */ /* push time elapsed during operation as the last return value */
lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0);
#endif #endif

View File

@ -8,7 +8,7 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local Public, Private = {}, {} local Public, Private = {}, {}
HTTP = Public http = Public
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
@ -195,7 +195,7 @@ end
function Private.receivebody_bylength(sock, length, receive_cb) function Private.receivebody_bylength(sock, length, receive_cb)
local uerr, go local uerr, go
while length > 0 do while length > 0 do
local size = min(Public.BLOCKSIZE, length) local size = math.min(Public.BLOCKSIZE, length)
local chunk, err = sock:receive(size) local chunk, err = sock:receive(size)
if err then if err then
go, uerr = receive_cb(nil, err) go, uerr = receive_cb(nil, err)
@ -542,7 +542,7 @@ function Public.request_cb(request, response)
scheme = "http" scheme = "http"
}) })
if parsed.scheme ~= "http" then if parsed.scheme ~= "http" then
response.error = format("unknown scheme '%s'", parsed.scheme) response.error = string.format("unknown scheme '%s'", parsed.scheme)
return response return response
end end
-- explicit authentication info overrides that given by the URL -- explicit authentication info overrides that given by the URL

View File

@ -31,8 +31,6 @@ void select_open(lua_State *L)
{ {
/* push select auxiliar lua function and register /* push select auxiliar lua function and register
* select_lua_select with it as an upvalue */ * select_lua_select with it as an upvalue */
#ifdef LUASOCKET_DEBUG
#endif
luaL_loadfile(L, "lsselect.lua"); luaL_loadfile(L, "lsselect.lua");
lua_call(L, 0, 1); lua_call(L, 0, 1);
lua_pushcclosure(L, select_lua_select, 1); lua_pushcclosure(L, select_lua_select, 1);

View File

@ -20,7 +20,7 @@
/*=========================================================================*\ /*=========================================================================*\
* Internal function prototypes * Internal function prototypes
\*=========================================================================*/ \*=========================================================================*/
#ifdef _DEBUG #ifdef LUASOCKET_DEBUG
static int tm_lua_time(lua_State *L); static int tm_lua_time(lua_State *L);
static int tm_lua_sleep(lua_State *L); static int tm_lua_sleep(lua_State *L);
#endif #endif
@ -123,7 +123,7 @@ int tm_gettime(void)
void tm_open(lua_State *L) void tm_open(lua_State *L)
{ {
(void) L; (void) L;
#ifdef _DEBUG #ifdef LUASOCKET_DEBUG
lua_pushcfunction(L, tm_lua_time); lua_pushcfunction(L, tm_lua_time);
priv_newglobal(L, "_time"); priv_newglobal(L, "_time");
lua_pushcfunction(L, tm_lua_sleep); lua_pushcfunction(L, tm_lua_sleep);
@ -137,7 +137,7 @@ void tm_open(lua_State *L)
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Returns the time the system has been up, in secconds. * Returns the time the system has been up, in secconds.
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
#ifdef _DEBUG #ifdef LUASOCKET_DEBUG
static int tm_lua_time(lua_State *L) static int tm_lua_time(lua_State *L)
{ {
lua_pushnumber(L, tm_gettime()/1000.0); lua_pushnumber(L, tm_gettime()/1000.0);

View File

@ -143,8 +143,8 @@ function Public.parse_path(path)
for i = 1, table.getn(parsed) do for i = 1, table.getn(parsed) do
parsed[i] = Code.unescape(parsed[i]) parsed[i] = Code.unescape(parsed[i])
end end
if stringsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
if stringsub(path, -1, -1) == "/" then parsed.is_directory = 1 end if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
return parsed return parsed
end end
@ -214,7 +214,7 @@ end
-- corresponding absolute path -- corresponding absolute path
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
function Private.absolute_path(base_path, relative_path) function Private.absolute_path(base_path, relative_path)
if stringsub(relative_path, 1, 1) == "/" then return relative_path end if string.sub(relative_path, 1, 1) == "/" then return relative_path end
local path = string.gsub(base_path, "[^/]*$", "") local path = string.gsub(base_path, "[^/]*$", "")
path = path .. relative_path path = path .. relative_path
path = string.gsub(path, "([^/]*%./)", function (s) path = string.gsub(path, "([^/]*%./)", function (s)

View File

@ -7,21 +7,21 @@
dofile("noglobals.lua") dofile("noglobals.lua")
local similar = function(s1, s2) local similar = function(s1, s2)
return strlower(gsub(s1 or "", "%s", "")) == return string.lower(string.gsub(s1 or "", "%s", "")) ==
strlower(gsub(s2 or "", "%s", "")) string.lower(string.gsub(s2 or "", "%s", ""))
end end
local fail = function(s) local fail = function(s)
s = s or "failed!" s = s or "failed!"
print(s) print(s)
exit() os.exit()
end end
local readfile = function(name) local readfile = function(name)
local f = readfrom(name) local f = io.open(name, "r")
if not f then return nil end if not f then return nil end
local s = read("*a") local s = f:read("*a")
readfrom() f:close()
return s return s
end end
@ -31,7 +31,7 @@ local check = function (v, e)
end end
local check_request = function(request, expect, ignore) local check_request = function(request, expect, ignore)
local response = HTTP.request(request) local response = http.request(request)
for i,v in response do for i,v in response do
if not ignore[i] then if not ignore[i] then
if v ~= expect[i] then %fail(i .. " differs!") end if v ~= expect[i] then %fail(i .. " differs!") end
@ -45,30 +45,28 @@ local check_request = function(request, expect, ignore)
print("ok") print("ok")
end end
dofile("../src/modules/http.lua")
local request, response, ignore, expect, index, prefix, cgiprefix local request, response, ignore, expect, index, prefix, cgiprefix
local t = _time() local t = socket._time()
HOST = HOST or "localhost" HOST = HOST or "localhost"
prefix = prefix or "/luasocket-test" prefix = prefix or "/luasocket"
cgiprefix = cgiprefix or "/luasocket-test-cgi" cgiprefix = cgiprefix or "/luasocket/cgi"
index = readfile("index.html") index = readfile("test/index.html")
write("testing request uri correctness: ") io.write("testing request uri correctness: ")
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
local back = HTTP.get("http://" .. HOST .. forth) local back = http.get("http://" .. HOST .. forth)
if similar(back, forth) then print("ok") if similar(back, forth) then print("ok")
else fail("failed!") end else fail("failed!") end
write("testing query string correctness: ") io.write("testing query string correctness: ")
forth = "this+is+the+query+string" forth = "this+is+the+query+string"
back = HTTP.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth) back = http.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth)
if similar(back, forth) then print("ok") if similar(back, forth) then print("ok")
else fail("failed!") end else fail("failed!") end
write("testing document retrieval: ") io.write("testing document retrieval: ")
request = { request = {
url = "http://" .. HOST .. prefix .. "/index.html" url = "http://" .. HOST .. prefix .. "/index.html"
} }
@ -82,7 +80,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing HTTP redirection: ") io.write("testing http redirection: ")
request = { request = {
url = "http://" .. HOST .. prefix url = "http://" .. HOST .. prefix
} }
@ -97,7 +95,7 @@ ignore = {
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing automatic auth failure: ") io.write("testing automatic auth failure: ")
request = { request = {
url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html" url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html"
} }
@ -111,7 +109,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing HTTP redirection failure: ") io.write("testing http redirection failure: ")
request = { request = {
url = "http://" .. HOST .. prefix, url = "http://" .. HOST .. prefix,
stay = 1 stay = 1
@ -126,29 +124,29 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing host not found: ") io.write("testing host not found: ")
request = { request = {
url = "http://wronghost/does/not/exist" url = "http://wronghost/does/not/exist"
} }
local c, e = connect("wronghost", 80) local c, e = socket.connect("wronghost", 80)
expect = { expect = {
error = e error = e
} }
ignore = {} ignore = {}
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing invalid url: ") io.write("testing invalid url: ")
request = { request = {
url = HOST .. prefix url = HOST .. prefix
} }
local c, e = connect("", 80) local c, e = socket.connect("", 80)
expect = { expect = {
error = e error = e
} }
ignore = {} ignore = {}
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing document not found: ") io.write("testing document not found: ")
request = { request = {
url = "http://" .. HOST .. "/wrongdocument.html" url = "http://" .. HOST .. "/wrongdocument.html"
} }
@ -162,7 +160,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing auth failure: ") io.write("testing auth failure: ")
request = { request = {
url = "http://" .. HOST .. prefix .. "/auth/index.html" url = "http://" .. HOST .. prefix .. "/auth/index.html"
} }
@ -176,7 +174,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing manual basic auth: ") io.write("testing manual basic auth: ")
request = { request = {
url = "http://" .. HOST .. prefix .. "/auth/index.html", url = "http://" .. HOST .. prefix .. "/auth/index.html",
headers = { headers = {
@ -193,7 +191,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing automatic basic auth: ") io.write("testing automatic basic auth: ")
request = { request = {
url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html" url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html"
} }
@ -207,7 +205,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing auth info overriding: ") io.write("testing auth info overriding: ")
request = { request = {
url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
user = "luasocket", user = "luasocket",
@ -223,7 +221,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing cgi output retrieval (probably chunked...): ") io.write("testing cgi output retrieval (probably chunked...): ")
request = { request = {
url = "http://" .. HOST .. cgiprefix .. "/cat-index-html" url = "http://" .. HOST .. cgiprefix .. "/cat-index-html"
} }
@ -237,7 +235,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing redirect loop: ") io.write("testing redirect loop: ")
request = { request = {
url = "http://" .. HOST .. cgiprefix .. "/redirect-loop" url = "http://" .. HOST .. cgiprefix .. "/redirect-loop"
} }
@ -251,7 +249,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing post method: ") io.write("testing post method: ")
request = { request = {
url = "http://" .. HOST .. cgiprefix .. "/cat", url = "http://" .. HOST .. cgiprefix .. "/cat",
method = "POST", method = "POST",
@ -267,7 +265,7 @@ ignore = {
} }
check_request(request, expect, ignore) check_request(request, expect, ignore)
write("testing wrong scheme: ") io.write("testing wrong scheme: ")
request = { request = {
url = "wrong://" .. HOST .. cgiprefix .. "/cat", url = "wrong://" .. HOST .. cgiprefix .. "/cat",
method = "GET" method = "GET"
@ -280,31 +278,31 @@ ignore = {
check_request(request, expect, ignore) check_request(request, expect, ignore)
local body local body
write("testing simple get function: ") io.write("testing simple get function: ")
body = HTTP.get("http://" .. HOST .. prefix .. "/index.html") body = http.get("http://" .. HOST .. prefix .. "/index.html")
check(body == index) check(body == index)
write("testing simple get function with table args: ") io.write("testing simple get function with table args: ")
body = HTTP.get { body = http.get {
url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
user = "luasocket", user = "luasocket",
password = "password" password = "password"
} }
check(body == index) check(body == index)
write("testing simple post function: ") io.write("testing simple post function: ")
body = HTTP.post("http://" .. HOST .. cgiprefix .. "/cat", index) body = http.post("http://" .. HOST .. cgiprefix .. "/cat", index)
check(body == index) check(body == index)
write("testing simple post function with table args: ") io.write("testing simple post function with table args: ")
body = HTTP.post { body = http.post {
url = "http://" .. HOST .. cgiprefix .. "/cat", url = "http://" .. HOST .. cgiprefix .. "/cat",
body = index body = index
} }
check(body == index) check(body == index)
write("testing HEAD method: ") io.write("testing HEAD method: ")
response = HTTP.request { response = http.request {
method = "HEAD", method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego/" url = "http://www.tecgraf.puc-rio.br/~diego/"
} }
@ -312,4 +310,4 @@ check(response and response.headers)
print("passed all tests") print("passed all tests")
print(format("done in %.2fs", _time() - t)) print(string.format("done in %.2fs", socket._time() - t))

View File

@ -2,32 +2,32 @@ HOST = HOST or "localhost"
PORT = PORT or "8080" PORT = PORT or "8080"
function pass(...) function pass(...)
local s = call(format, arg) local s = string.format(unpack(arg))
write(s, "\n") io.write(s, "\n")
end end
function fail(...) function fail(...)
local s = call(format, arg) local s = string.format(unpack(arg))
write("ERROR: ", s, "!\n") io.write("ERROR: ", s, "!\n")
exit() os.exit()
end end
function warn(...) function warn(...)
local s = call(format, arg) local s = format(unpack(arg))
write("WARNING: ", s, "\n") io.write("WARNING: ", s, "\n")
end end
function remote(...) function remote(...)
local s = call(format, arg) local s = string.format(unpack(arg))
s = gsub(s, "\n", ";") s = string.gsub(s, "\n", ";")
s = gsub(s, "%s+", " ") s = string.gsub(s, "%s+", " ")
s = gsub(s, "^%s*", "") s = string.gsub(s, "^%s*", "")
control:send(s, "\n") control:send(s, "\n")
control:receive() control:receive()
end end
function test(test) function test(test)
write("----------------------------------------------\n", io.write("----------------------------------------------\n",
"testing: ", test, "\n", "testing: ", test, "\n",
"----------------------------------------------\n") "----------------------------------------------\n")
end end
@ -66,51 +66,69 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone)
end end
end end
write("----------------------------------------------\n", io.write("----------------------------------------------\n",
"LuaSocket Test Procedures\n", "LuaSocket Test Procedures\n",
"----------------------------------------------\n") "----------------------------------------------\n")
if not _time or not _sleep then fail("not compiled with _DEBUG") end if not socket._time or not socket._sleep then
fail("not compiled with _DEBUG")
end
start = _time() start = socket._time()
function tcpreconnect() function tcpreconnect()
write("attempting data connection... ") io.write("attempting data connection... ")
if data then data:close() end if data then data:close() end
remote [[ remote [[
if data then data:close() data = nil end if data then data:close() data = nil end
data = server:accept() data = server:accept()
]] ]]
data, error = connect(HOST, PORT) data, err = socket.connect(HOST, PORT)
if not data then fail(error) if not data then fail(err)
else pass("connected!") end else pass("connected!") end
end end
reconnect = tcpreconnect reconnect = tcpreconnect
pass("attempting control connection...") pass("attempting control connection...")
control, error = connect(HOST, PORT) control, err = socket.connect(HOST, PORT)
if error then fail(error) if err then fail(err)
else pass("connected!") end else pass("connected!") end
------------------------------------------------------------------------ ------------------------------------------------------------------------
test("bugs") test("bugs")
write("empty host connect: ") io.write("empty host connect: ")
function empty_connect() function empty_connect()
if data then data:close() data = nil end if data then data:close() data = nil end
remote [[ remote [[
if data then data:close() data = nil end if data then data:close() data = nil end
data = server:accept() data = server:accept()
]] ]]
data, err = connect("", PORT) data, err = socket.connect("", PORT)
if not data then if not data then
pass("ok") pass("ok")
data = connect(HOST, PORT) data = socket.connect(HOST, PORT)
else fail("should not have connected!") end else fail("should not have connected!") end
end end
empty_connect() empty_connect()
io.write("active close: ")
function active_close()
reconnect()
if socket._isclosed(data) then fail("should not be closed") end
data:close()
if not socket._isclosed(data) then fail("should be closed") end
data = nil
local udp = socket.udp()
if socket._isclosed(udp) then fail("should not be closed") end
udp:close()
if not socket._isclosed(udp) then fail("should be closed") end
pass("ok")
end
active_close()
------------------------------------------------------------------------ ------------------------------------------------------------------------
test("method registration") test("method registration")
@ -133,7 +151,7 @@ test_methods(control, {
}) })
if udpsocket then if udpsocket then
test_methods(udpsocket(), { test_methods(socket.udp(), {
"close", "close",
"timeout", "timeout",
"send", "send",
@ -147,50 +165,27 @@ if udpsocket then
}) })
end end
test_methods(bind("*", 0), { test_methods(socket.bind("*", 0), {
"close", "close",
"timeout", "timeout",
"accept" "accept"
}) })
if pipe then
local p1, p2 = pipe()
test_methods(p1, {
"close",
"timeout",
"send",
"receive"
})
test_methods(p2, {
"close",
"timeout",
"send",
"receive"
})
end
if filesocket then
test_methods(filesocket(0), {
"close",
"timeout",
"send",
"receive"
})
end
------------------------------------------------------------------------ ------------------------------------------------------------------------
test("select function") test("select function")
function test_selectbugs() function test_selectbugs()
local r, s, e = select(nil, nil, 0.1) local r, s, e = socket.select(nil, nil, 0.1)
assert(type(r) == "table" and type(s) == "table" and e == "timeout") assert(type(r) == "table" and type(s) == "table" and e == "timeout")
pass("both nil: ok") pass("both nil: ok")
local udp = udpsocket() local udp = socket.udp()
udp:close() udp:close()
r, s, e = select({ data }, { data }, 0.1) r, s, e = socket.select({ udp }, { udp }, 0.1)
assert(type(r) == "table" and type(s) == "table" and e == "timeout") assert(type(r) == "table" and type(s) == "table" and e == "timeout")
pass("closed sockets: ok") pass("closed sockets: ok")
e = call(select, {"wrong", 1, 0.1}, "x", nil) e = pcall(socket.select, "wrong", 1, 0.1)
assert(e == nil) assert(e == false)
e = pcall(socket.select, {}, 1, 0.1)
assert(e == false)
pass("invalid input: ok") pass("invalid input: ok")
end end
@ -202,8 +197,8 @@ reconnect()
function test_asciiline(len) function test_asciiline(len)
local str, str10, back, err local str, str10, back, err
str = strrep("x", mod(len, 10)) str = string.rep("x", math.mod(len, 10))
str10 = strrep("aZb.c#dAe?", floor(len/10)) str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
str = str .. str10 str = str .. str10
pass(len .. " byte(s) line") pass(len .. " byte(s) line")
remote "str = data:receive()" remote "str = data:receive()"
@ -229,8 +224,9 @@ reconnect()
function test_rawline(len) function test_rawline(len)
local str, str10, back, err local str, str10, back, err
str = strrep(strchar(47), mod(len, 10)) str = string.rep(string.char(47), math.mod(len, 10))
str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
math.floor(len/10))
str = str .. str10 str = str .. str10
pass(len .. " byte(s) line") pass(len .. " byte(s) line")
remote "str = data:receive()" remote "str = data:receive()"
@ -260,12 +256,12 @@ test("raw transfer")
reconnect() reconnect()
function test_raw(len) function test_raw(len)
local half = floor(len/2) local half = math.floor(len/2)
local s1, s2, back, err local s1, s2, back, err
s1 = strrep("x", half) s1 = string.rep("x", half)
s2 = strrep("y", len-half) s2 = string.rep("y", len-half)
pass(len .. " byte(s) block") pass(len .. " byte(s) block")
remote (format("str = data:receive(%d)", len)) remote (string.format("str = data:receive(%d)", len))
err = data:send(s1) err = data:send(s1)
if err then fail(err) end if err then fail(err) end
err = data:send(s2) err = data:send(s2)
@ -312,17 +308,18 @@ test("mixed patterns")
reconnect() reconnect()
function test_mixed(len) function test_mixed(len)
local inter = floor(len/3) local inter = math.floor(len/3)
local p1 = "unix " .. strrep("x", inter) .. "line\n" local p1 = "unix " .. string.rep("x", inter) .. "line\n"
local p2 = "dos " .. strrep("y", inter) .. "line\r\n" local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
local p3 = "raw " .. strrep("z", inter) .. "bytes" local p3 = "raw " .. string.rep("z", inter) .. "bytes"
local bp1, bp2, bp3 local bp1, bp2, bp3
pass(len .. " byte(s) patterns") pass(len .. " byte(s) patterns")
remote (format("str = data:receive(%d)", strlen(p1)+strlen(p2)+strlen(p3))) remote (string.format("str = data:receive(%d)",
string.len(p1)+string.len(p2)+string.len(p3)))
err = data:send(p1, p2, p3) err = data:send(p1, p2, p3)
if err then fail(err) end if err then fail(err) end
remote "data:send(str)" remote "data:send(str)"
bp1, bp2, bp3, err = data:receive("*lu", "*l", strlen(p3)) bp1, bp2, bp3, err = data:receive("*lu", "*l", string.len(p3))
if err then fail(err) end if err then fail(err) end
if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then
pass("patterns match") pass("patterns match")
@ -349,7 +346,7 @@ function test_closed()
local str = 'little string' local str = 'little string'
reconnect() reconnect()
pass("trying read detection") pass("trying read detection")
remote (format ([[ remote (string.format ([[
data:send('%s') data:send('%s')
data:close() data:close()
data = nil data = nil
@ -366,7 +363,7 @@ function test_closed()
data:close() data:close()
data = nil data = nil
]] ]]
err, total = data:send(strrep("ugauga", 100000)) err, total = 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) pass("failed: output buffer is at least %d bytes long!", total)
elseif err ~= "closed" then elseif err ~= "closed" then
@ -384,19 +381,19 @@ function test_blockingtimeoutreceive(len, tm, sl)
local str, err, total local str, err, total
reconnect() reconnect()
pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl)
remote (format ([[ remote (string.format ([[
data:timeout(%d) data:timeout(%d)
str = strrep('a', %d) str = string.rep('a', %d)
data:send(str) data:send(str)
print('server: sleeping for %ds') print('server: sleeping for %ds')
_sleep(%d) socket._sleep(%d)
print('server: woke up') print('server: woke up')
data:send(str) data:send(str)
]], 2*tm, len, sl, sl)) ]], 2*tm, len, sl, sl))
data:timeout(tm, "return") data:timeout(tm, "return")
str, err, elapsed = data:receive(2*len) str, err, elapsed = data:receive(2*len)
check_timeout(tm, sl, elapsed, err, "receive", "return", check_timeout(tm, sl, elapsed, err, "receive", "return",
strlen(str) == 2*len) string.len(str) == 2*len)
end end
test_blockingtimeoutreceive(800091, 1, 3) test_blockingtimeoutreceive(800091, 1, 3)
test_blockingtimeoutreceive(800091, 2, 3) test_blockingtimeoutreceive(800091, 2, 3)
@ -409,16 +406,16 @@ function test_returntimeoutsend(len, tm, sl)
local str, err, total local str, err, total
reconnect() reconnect()
pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl)
remote (format ([[ remote (string.format ([[
data:timeout(%d) data:timeout(%d)
str = data:receive(%d) str = data:receive(%d)
print('server: sleeping for %ds') print('server: sleeping for %ds')
_sleep(%d) socket._sleep(%d)
print('server: woke up') print('server: woke up')
str = data:receive(%d) str = data:receive(%d)
]], 2*tm, len, sl, sl, len)) ]], 2*tm, len, sl, sl, len))
data:timeout(tm, "return") data:timeout(tm, "return")
str = strrep("a", 2*len) str = string.rep("a", 2*len)
err, total, elapsed = data:send(str) err, total, elapsed = data:send(str)
check_timeout(tm, sl, elapsed, err, "send", "return", check_timeout(tm, sl, elapsed, err, "send", "return",
total == 2*len) total == 2*len)
@ -435,19 +432,19 @@ function test_blockingtimeoutreceive(len, tm, sl)
local str, err, total local str, err, total
reconnect() reconnect()
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
remote (format ([[ remote (string.format ([[
data:timeout(%d) data:timeout(%d)
str = strrep('a', %d) str = string.rep('a', %d)
data:send(str) data:send(str)
print('server: sleeping for %ds') print('server: sleeping for %ds')
_sleep(%d) socket._sleep(%d)
print('server: woke up') print('server: woke up')
data:send(str) data:send(str)
]], 2*tm, len, sl, sl)) ]], 2*tm, len, sl, sl))
data:timeout(tm) data:timeout(tm)
str, err, elapsed = data:receive(2*len) str, err, elapsed = data:receive(2*len)
check_timeout(tm, sl, elapsed, err, "receive", "blocking", check_timeout(tm, sl, elapsed, err, "receive", "blocking",
strlen(str) == 2*len) string.len(str) == 2*len)
end end
test_blockingtimeoutreceive(800091, 1, 3) test_blockingtimeoutreceive(800091, 1, 3)
test_blockingtimeoutreceive(800091, 2, 3) test_blockingtimeoutreceive(800091, 2, 3)
@ -461,16 +458,16 @@ function test_blockingtimeoutsend(len, tm, sl)
local str, err, total local str, err, total
reconnect() reconnect()
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
remote (format ([[ remote (string.format ([[
data:timeout(%d) data:timeout(%d)
str = data:receive(%d) str = data:receive(%d)
print('server: sleeping for %ds') print('server: sleeping for %ds')
_sleep(%d) socket._sleep(%d)
print('server: woke up') print('server: woke up')
str = data:receive(%d) str = data:receive(%d)
]], 2*tm, len, sl, sl, len)) ]], 2*tm, len, sl, sl, len))
data:timeout(tm) data:timeout(tm)
str = strrep("a", 2*len) str = string.rep("a", 2*len)
err, total, elapsed = data:send(str) err, total, elapsed = data:send(str)
check_timeout(tm, sl, elapsed, err, "send", "blocking", check_timeout(tm, sl, elapsed, err, "send", "blocking",
total == 2*len) total == 2*len)
@ -481,4 +478,4 @@ test_blockingtimeoutsend(800091, 3, 2)
test_blockingtimeoutsend(800091, 3, 1) test_blockingtimeoutsend(800091, 3, 1)
------------------------------------------------------------------------ ------------------------------------------------------------------------
test(format("done in %.2fs", _time() - start)) test(string.format("done in %.2fs", socket._time() - start))

View File

@ -1,8 +1,8 @@
HOST = HOST or "localhost" HOST = HOST or "localhost"
PORT = PORT or "8080" PORT = PORT or "8080"
server, error = bind(HOST, PORT) server, error = socket.bind(HOST, PORT)
if not server then print("server: " .. tostring(error)) exit() end if not server then print("server: " .. tostring(error)) os.exit() end
while 1 do while 1 do
print("server: waiting for client connection..."); print("server: waiting for client connection...");
control = server:accept() control = server:accept()
@ -19,6 +19,6 @@ while 1 do
print("server: closing connection...") print("server: closing connection...")
break break
end end
dostring(command) (loadstring(command))()
end end
end end

View File

@ -30,7 +30,7 @@ end
local check_parse_path = function(path, expect) local check_parse_path = function(path, expect)
local parsed = URL.parse_path(path) local parsed = URL.parse_path(path)
for i = 1, max(getn(parsed), getn(expect)) do for i = 1, math.max(table.getn(parsed), table.getn(expect)) do
if parsed[i] ~= expect[i] then if parsed[i] ~= expect[i] then
print(path) print(path)
write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])), write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])),