From 7da19138e37c4e0123860f1fecbceb80c3d2627d Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 3 Dec 2002 07:20:34 +0000 Subject: [PATCH] Faltam testes de ftp e smtp. O resto passa. --- src/buffer.c | 4 +- src/http.lua | 6 +- src/select.c | 2 - src/timeout.c | 6 +- src/url.lua | 6 +- test/httptest.lua | 88 ++++++++++++------------ test/testclnt.lua | 169 +++++++++++++++++++++++----------------------- test/testsrvr.lua | 6 +- test/urltest.lua | 2 +- 9 files changed, 141 insertions(+), 148 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index a9a9782..4260f20 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -74,7 +74,7 @@ int buf_send(lua_State *L, p_buf buf) } priv_pusherror(L, err); lua_pushnumber(L, total); -#ifdef _DEBUG +#ifdef LUASOCKET_DEBUG /* push time elapsed during operation as the last return value */ lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); #endif @@ -139,7 +139,7 @@ int buf_receive(lua_State *L, p_buf buf) for ( ; arg <= top; arg++) lua_pushnil(L); /* last return is an error code */ priv_pusherror(L, err); -#ifdef _DEBUG +#ifdef LUASOCKET_DEBUG /* push time elapsed during operation as the last return value */ lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); #endif diff --git a/src/http.lua b/src/http.lua index dce3ac8..9543d59 100644 --- a/src/http.lua +++ b/src/http.lua @@ -8,7 +8,7 @@ ----------------------------------------------------------------------------- local Public, Private = {}, {} -HTTP = Public +http = Public ----------------------------------------------------------------------------- -- Program constants @@ -195,7 +195,7 @@ end function Private.receivebody_bylength(sock, length, receive_cb) local uerr, go while length > 0 do - local size = min(Public.BLOCKSIZE, length) + local size = math.min(Public.BLOCKSIZE, length) local chunk, err = sock:receive(size) if err then go, uerr = receive_cb(nil, err) @@ -542,7 +542,7 @@ function Public.request_cb(request, response) scheme = "http" }) if parsed.scheme ~= "http" then - response.error = format("unknown scheme '%s'", parsed.scheme) + response.error = string.format("unknown scheme '%s'", parsed.scheme) return response end -- explicit authentication info overrides that given by the URL diff --git a/src/select.c b/src/select.c index 1aaa7fe..5c08730 100644 --- a/src/select.c +++ b/src/select.c @@ -31,8 +31,6 @@ void select_open(lua_State *L) { /* push select auxiliar lua function and register * select_lua_select with it as an upvalue */ -#ifdef LUASOCKET_DEBUG -#endif luaL_loadfile(L, "lsselect.lua"); lua_call(L, 0, 1); lua_pushcclosure(L, select_lua_select, 1); diff --git a/src/timeout.c b/src/timeout.c index fdbc47a..940ddca 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -20,7 +20,7 @@ /*=========================================================================*\ * Internal function prototypes \*=========================================================================*/ -#ifdef _DEBUG +#ifdef LUASOCKET_DEBUG static int tm_lua_time(lua_State *L); static int tm_lua_sleep(lua_State *L); #endif @@ -123,7 +123,7 @@ int tm_gettime(void) void tm_open(lua_State *L) { (void) L; -#ifdef _DEBUG +#ifdef LUASOCKET_DEBUG lua_pushcfunction(L, tm_lua_time); priv_newglobal(L, "_time"); 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. \*-------------------------------------------------------------------------*/ -#ifdef _DEBUG +#ifdef LUASOCKET_DEBUG static int tm_lua_time(lua_State *L) { lua_pushnumber(L, tm_gettime()/1000.0); diff --git a/src/url.lua b/src/url.lua index 0ecec3c..4d2bfa7 100644 --- a/src/url.lua +++ b/src/url.lua @@ -143,8 +143,8 @@ function Public.parse_path(path) for i = 1, table.getn(parsed) do parsed[i] = Code.unescape(parsed[i]) end - if stringsub(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_absolute = 1 end + if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end return parsed end @@ -214,7 +214,7 @@ end -- corresponding absolute 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, "[^/]*$", "") path = path .. relative_path path = string.gsub(path, "([^/]*%./)", function (s) diff --git a/test/httptest.lua b/test/httptest.lua index 85c8bd8..2941390 100644 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -7,21 +7,21 @@ dofile("noglobals.lua") local similar = function(s1, s2) - return strlower(gsub(s1 or "", "%s", "")) == - strlower(gsub(s2 or "", "%s", "")) + return string.lower(string.gsub(s1 or "", "%s", "")) == + string.lower(string.gsub(s2 or "", "%s", "")) end local fail = function(s) s = s or "failed!" print(s) - exit() + os.exit() end local readfile = function(name) - local f = readfrom(name) + local f = io.open(name, "r") if not f then return nil end - local s = read("*a") - readfrom() + local s = f:read("*a") + f:close() return s end @@ -31,7 +31,7 @@ local check = function (v, e) end local check_request = function(request, expect, ignore) - local response = HTTP.request(request) + local response = http.request(request) for i,v in response do if not ignore[i] then if v ~= expect[i] then %fail(i .. " differs!") end @@ -45,30 +45,28 @@ local check_request = function(request, expect, ignore) print("ok") end -dofile("../src/modules/http.lua") - local request, response, ignore, expect, index, prefix, cgiprefix -local t = _time() +local t = socket._time() HOST = HOST or "localhost" -prefix = prefix or "/luasocket-test" -cgiprefix = cgiprefix or "/luasocket-test-cgi" -index = readfile("index.html") +prefix = prefix or "/luasocket" +cgiprefix = cgiprefix or "/luasocket/cgi" +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 back = HTTP.get("http://" .. HOST .. forth) +local back = http.get("http://" .. HOST .. forth) if similar(back, forth) then print("ok") else fail("failed!") end -write("testing query string correctness: ") +io.write("testing query string correctness: ") 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") else fail("failed!") end -write("testing document retrieval: ") +io.write("testing document retrieval: ") request = { url = "http://" .. HOST .. prefix .. "/index.html" } @@ -82,7 +80,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing HTTP redirection: ") +io.write("testing http redirection: ") request = { url = "http://" .. HOST .. prefix } @@ -97,7 +95,7 @@ ignore = { check_request(request, expect, ignore) -write("testing automatic auth failure: ") +io.write("testing automatic auth failure: ") request = { url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html" } @@ -111,7 +109,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing HTTP redirection failure: ") +io.write("testing http redirection failure: ") request = { url = "http://" .. HOST .. prefix, stay = 1 @@ -126,29 +124,29 @@ ignore = { } check_request(request, expect, ignore) -write("testing host not found: ") +io.write("testing host not found: ") request = { url = "http://wronghost/does/not/exist" } -local c, e = connect("wronghost", 80) +local c, e = socket.connect("wronghost", 80) expect = { error = e } ignore = {} check_request(request, expect, ignore) -write("testing invalid url: ") +io.write("testing invalid url: ") request = { url = HOST .. prefix } -local c, e = connect("", 80) +local c, e = socket.connect("", 80) expect = { error = e } ignore = {} check_request(request, expect, ignore) -write("testing document not found: ") +io.write("testing document not found: ") request = { url = "http://" .. HOST .. "/wrongdocument.html" } @@ -162,7 +160,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing auth failure: ") +io.write("testing auth failure: ") request = { url = "http://" .. HOST .. prefix .. "/auth/index.html" } @@ -176,7 +174,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing manual basic auth: ") +io.write("testing manual basic auth: ") request = { url = "http://" .. HOST .. prefix .. "/auth/index.html", headers = { @@ -193,7 +191,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing automatic basic auth: ") +io.write("testing automatic basic auth: ") request = { url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html" } @@ -207,7 +205,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing auth info overriding: ") +io.write("testing auth info overriding: ") request = { url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", user = "luasocket", @@ -223,7 +221,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing cgi output retrieval (probably chunked...): ") +io.write("testing cgi output retrieval (probably chunked...): ") request = { url = "http://" .. HOST .. cgiprefix .. "/cat-index-html" } @@ -237,7 +235,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing redirect loop: ") +io.write("testing redirect loop: ") request = { url = "http://" .. HOST .. cgiprefix .. "/redirect-loop" } @@ -251,7 +249,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing post method: ") +io.write("testing post method: ") request = { url = "http://" .. HOST .. cgiprefix .. "/cat", method = "POST", @@ -267,7 +265,7 @@ ignore = { } check_request(request, expect, ignore) -write("testing wrong scheme: ") +io.write("testing wrong scheme: ") request = { url = "wrong://" .. HOST .. cgiprefix .. "/cat", method = "GET" @@ -280,31 +278,31 @@ ignore = { check_request(request, expect, ignore) local body -write("testing simple get function: ") -body = HTTP.get("http://" .. HOST .. prefix .. "/index.html") +io.write("testing simple get function: ") +body = http.get("http://" .. HOST .. prefix .. "/index.html") check(body == index) -write("testing simple get function with table args: ") -body = HTTP.get { +io.write("testing simple get function with table args: ") +body = http.get { url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", user = "luasocket", password = "password" } check(body == index) -write("testing simple post function: ") -body = HTTP.post("http://" .. HOST .. cgiprefix .. "/cat", index) +io.write("testing simple post function: ") +body = http.post("http://" .. HOST .. cgiprefix .. "/cat", index) check(body == index) -write("testing simple post function with table args: ") -body = HTTP.post { +io.write("testing simple post function with table args: ") +body = http.post { url = "http://" .. HOST .. cgiprefix .. "/cat", body = index } check(body == index) -write("testing HEAD method: ") -response = HTTP.request { +io.write("testing HEAD method: ") +response = http.request { method = "HEAD", url = "http://www.tecgraf.puc-rio.br/~diego/" } @@ -312,4 +310,4 @@ check(response and response.headers) print("passed all tests") -print(format("done in %.2fs", _time() - t)) +print(string.format("done in %.2fs", socket._time() - t)) diff --git a/test/testclnt.lua b/test/testclnt.lua index 73c10de..15f1dd8 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua @@ -2,32 +2,32 @@ HOST = HOST or "localhost" PORT = PORT or "8080" function pass(...) - local s = call(format, arg) - write(s, "\n") + local s = string.format(unpack(arg)) + io.write(s, "\n") end function fail(...) - local s = call(format, arg) - write("ERROR: ", s, "!\n") - exit() + local s = string.format(unpack(arg)) + io.write("ERROR: ", s, "!\n") + os.exit() end function warn(...) - local s = call(format, arg) - write("WARNING: ", s, "\n") + local s = format(unpack(arg)) + io.write("WARNING: ", s, "\n") end function remote(...) - local s = call(format, arg) - s = gsub(s, "\n", ";") - s = gsub(s, "%s+", " ") - s = gsub(s, "^%s*", "") + local s = string.format(unpack(arg)) + s = string.gsub(s, "\n", ";") + s = string.gsub(s, "%s+", " ") + s = string.gsub(s, "^%s*", "") control:send(s, "\n") control:receive() end function test(test) - write("----------------------------------------------\n", + io.write("----------------------------------------------\n", "testing: ", test, "\n", "----------------------------------------------\n") end @@ -66,51 +66,69 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) end end -write("----------------------------------------------\n", +io.write("----------------------------------------------\n", "LuaSocket Test Procedures\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() - write("attempting data connection... ") + io.write("attempting data connection... ") if data then data:close() end remote [[ if data then data:close() data = nil end data = server:accept() ]] - data, error = connect(HOST, PORT) - if not data then fail(error) + data, err = socket.connect(HOST, PORT) + if not data then fail(err) else pass("connected!") end end reconnect = tcpreconnect pass("attempting control connection...") -control, error = connect(HOST, PORT) -if error then fail(error) +control, err = socket.connect(HOST, PORT) +if err then fail(err) else pass("connected!") end ------------------------------------------------------------------------ test("bugs") -write("empty host connect: ") +io.write("empty host connect: ") function empty_connect() if data then data:close() data = nil end remote [[ if data then data:close() data = nil end data = server:accept() ]] - data, err = connect("", PORT) + data, err = socket.connect("", PORT) if not data then pass("ok") - data = connect(HOST, PORT) + data = socket.connect(HOST, PORT) else fail("should not have connected!") end end 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") @@ -133,7 +151,7 @@ test_methods(control, { }) if udpsocket then - test_methods(udpsocket(), { + test_methods(socket.udp(), { "close", "timeout", "send", @@ -147,50 +165,27 @@ if udpsocket then }) end -test_methods(bind("*", 0), { +test_methods(socket.bind("*", 0), { "close", "timeout", "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") 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") pass("both nil: ok") - local udp = udpsocket() + local udp = socket.udp() 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") pass("closed sockets: ok") - e = call(select, {"wrong", 1, 0.1}, "x", nil) - assert(e == nil) + e = pcall(socket.select, "wrong", 1, 0.1) + assert(e == false) + e = pcall(socket.select, {}, 1, 0.1) + assert(e == false) pass("invalid input: ok") end @@ -202,8 +197,8 @@ reconnect() function test_asciiline(len) local str, str10, back, err - str = strrep("x", mod(len, 10)) - str10 = strrep("aZb.c#dAe?", floor(len/10)) + str = string.rep("x", math.mod(len, 10)) + str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) str = str .. str10 pass(len .. " byte(s) line") remote "str = data:receive()" @@ -229,8 +224,9 @@ reconnect() function test_rawline(len) local str, str10, back, err - str = strrep(strchar(47), mod(len, 10)) - str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) + 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), + math.floor(len/10)) str = str .. str10 pass(len .. " byte(s) line") remote "str = data:receive()" @@ -260,12 +256,12 @@ test("raw transfer") reconnect() function test_raw(len) - local half = floor(len/2) + local half = math.floor(len/2) local s1, s2, back, err - s1 = strrep("x", half) - s2 = strrep("y", len-half) + s1 = string.rep("x", half) + s2 = string.rep("y", len-half) pass(len .. " byte(s) block") -remote (format("str = data:receive(%d)", len)) +remote (string.format("str = data:receive(%d)", len)) err = data:send(s1) if err then fail(err) end err = data:send(s2) @@ -312,17 +308,18 @@ test("mixed patterns") reconnect() function test_mixed(len) - local inter = floor(len/3) - local p1 = "unix " .. strrep("x", inter) .. "line\n" - local p2 = "dos " .. strrep("y", inter) .. "line\r\n" - local p3 = "raw " .. strrep("z", inter) .. "bytes" + local inter = math.floor(len/3) + local p1 = "unix " .. string.rep("x", inter) .. "line\n" + local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" + local p3 = "raw " .. string.rep("z", inter) .. "bytes" local bp1, bp2, bp3 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) if err then fail(err) end 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 bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then pass("patterns match") @@ -349,7 +346,7 @@ function test_closed() local str = 'little string' reconnect() pass("trying read detection") - remote (format ([[ + remote (string.format ([[ data:send('%s') data:close() data = nil @@ -366,7 +363,7 @@ function test_closed() data:close() data = nil ]] - err, total = data:send(strrep("ugauga", 100000)) + err, total = data:send(string.rep("ugauga", 100000)) if not err then pass("failed: output buffer is at least %d bytes long!", total) elseif err ~= "closed" then @@ -384,19 +381,19 @@ function test_blockingtimeoutreceive(len, tm, sl) local str, err, total reconnect() pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) - remote (format ([[ + remote (string.format ([[ data:timeout(%d) - str = strrep('a', %d) + str = string.rep('a', %d) data:send(str) print('server: sleeping for %ds') - _sleep(%d) + socket._sleep(%d) print('server: woke up') data:send(str) ]], 2*tm, len, sl, sl)) data:timeout(tm, "return") str, err, elapsed = data:receive(2*len) check_timeout(tm, sl, elapsed, err, "receive", "return", - strlen(str) == 2*len) + string.len(str) == 2*len) end test_blockingtimeoutreceive(800091, 1, 3) test_blockingtimeoutreceive(800091, 2, 3) @@ -409,16 +406,16 @@ function test_returntimeoutsend(len, tm, sl) local str, err, total reconnect() pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) - remote (format ([[ + remote (string.format ([[ data:timeout(%d) str = data:receive(%d) print('server: sleeping for %ds') - _sleep(%d) + socket._sleep(%d) print('server: woke up') str = data:receive(%d) ]], 2*tm, len, sl, sl, len)) data:timeout(tm, "return") - str = strrep("a", 2*len) + str = string.rep("a", 2*len) err, total, elapsed = data:send(str) check_timeout(tm, sl, elapsed, err, "send", "return", total == 2*len) @@ -435,19 +432,19 @@ function test_blockingtimeoutreceive(len, tm, sl) local str, err, total reconnect() pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) - remote (format ([[ + remote (string.format ([[ data:timeout(%d) - str = strrep('a', %d) + str = string.rep('a', %d) data:send(str) print('server: sleeping for %ds') - _sleep(%d) + socket._sleep(%d) print('server: woke up') data:send(str) ]], 2*tm, len, sl, sl)) data:timeout(tm) str, err, elapsed = data:receive(2*len) check_timeout(tm, sl, elapsed, err, "receive", "blocking", - strlen(str) == 2*len) + string.len(str) == 2*len) end test_blockingtimeoutreceive(800091, 1, 3) test_blockingtimeoutreceive(800091, 2, 3) @@ -461,16 +458,16 @@ function test_blockingtimeoutsend(len, tm, sl) local str, err, total reconnect() pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) - remote (format ([[ + remote (string.format ([[ data:timeout(%d) str = data:receive(%d) print('server: sleeping for %ds') - _sleep(%d) + socket._sleep(%d) print('server: woke up') str = data:receive(%d) ]], 2*tm, len, sl, sl, len)) data:timeout(tm) - str = strrep("a", 2*len) + str = string.rep("a", 2*len) err, total, elapsed = data:send(str) check_timeout(tm, sl, elapsed, err, "send", "blocking", total == 2*len) @@ -481,4 +478,4 @@ test_blockingtimeoutsend(800091, 3, 2) test_blockingtimeoutsend(800091, 3, 1) ------------------------------------------------------------------------ -test(format("done in %.2fs", _time() - start)) +test(string.format("done in %.2fs", socket._time() - start)) diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 227e341..141c058 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua @@ -1,8 +1,8 @@ HOST = HOST or "localhost" PORT = PORT or "8080" -server, error = bind(HOST, PORT) -if not server then print("server: " .. tostring(error)) exit() end +server, error = socket.bind(HOST, PORT) +if not server then print("server: " .. tostring(error)) os.exit() end while 1 do print("server: waiting for client connection..."); control = server:accept() @@ -19,6 +19,6 @@ while 1 do print("server: closing connection...") break end - dostring(command) + (loadstring(command))() end end diff --git a/test/urltest.lua b/test/urltest.lua index da7475a..8ca36fe 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -30,7 +30,7 @@ end local check_parse_path = function(path, expect) 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 print(path) write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])),