diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-05-25 01:54:13 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-05-25 01:54:13 +0000 |
| commit | 0f6c8d50a99997ac7829864b1c93362b50f1bbf3 (patch) | |
| tree | d0cefe3a05484e65b7b7e79d8cae4a1d2e6d19fb /test | |
| parent | c1ef3e7103cc652d2004ef1ddc9409b946207f33 (diff) | |
| download | luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.gz luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.tar.bz2 luasocket-0f6c8d50a99997ac7829864b1c93362b50f1bbf3.zip | |
Porting to LUA 5.0 final
Diffstat (limited to 'test')
| -rw-r--r-- | test/ftptest.lua | 6 | ||||
| -rw-r--r-- | test/httptest.lua | 15 | ||||
| -rw-r--r-- | test/smtptest.lua | 10 | ||||
| -rw-r--r-- | test/testclnt.lua | 372 | ||||
| -rw-r--r-- | test/testsrvr.lua | 3 | ||||
| -rw-r--r-- | test/tftptest.lua | 7 |
6 files changed, 201 insertions, 212 deletions
diff --git a/test/ftptest.lua b/test/ftptest.lua index ee3af91..6ba61a4 100644 --- a/test/ftptest.lua +++ b/test/ftptest.lua | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | dofile("noglobals.lua") | ||
| 2 | |||
| 3 | local similar = function(s1, s2) | 1 | local similar = function(s1, s2) |
| 4 | return | 2 | return |
| 5 | string.lower(string.gsub(s1, "%s", "")) == | 3 | string.lower(string.gsub(s1, "%s", "")) == |
| @@ -34,7 +32,7 @@ end | |||
| 34 | 32 | ||
| 35 | local index, err, saved, back, expected | 33 | local index, err, saved, back, expected |
| 36 | 34 | ||
| 37 | local t = socket._time() | 35 | local t = socket.time() |
| 38 | 36 | ||
| 39 | index = readfile("test/index.html") | 37 | index = readfile("test/index.html") |
| 40 | 38 | ||
| @@ -112,4 +110,4 @@ back, err = socket.ftp.get("ftp://localhost/index.wrong.html;type=a") | |||
| 112 | check(err, err) | 110 | check(err, err) |
| 113 | 111 | ||
| 114 | print("passed all tests") | 112 | print("passed all tests") |
| 115 | print(string.format("done in %.2fs", socket._time() - t)) | 113 | print(string.format("done in %.2fs", socket.time() - t)) |
diff --git a/test/httptest.lua b/test/httptest.lua index 1eb4b6a..030974c 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
| @@ -3,9 +3,6 @@ | |||
| 3 | -- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi | 3 | -- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi |
| 4 | -- to /luasocket-test-cgi | 4 | -- to /luasocket-test-cgi |
| 5 | -- needs AllowOverride AuthConfig on /home/c/diego/tec/luasocket/test/auth | 5 | -- needs AllowOverride AuthConfig on /home/c/diego/tec/luasocket/test/auth |
| 6 | |||
| 7 | dofile("noglobals.lua") | ||
| 8 | |||
| 9 | local similar = function(s1, s2) | 6 | local similar = function(s1, s2) |
| 10 | return string.lower(string.gsub(s1 or "", "%s", "")) == | 7 | return string.lower(string.gsub(s1 or "", "%s", "")) == |
| 11 | string.lower(string.gsub(s2 or "", "%s", "")) | 8 | string.lower(string.gsub(s2 or "", "%s", "")) |
| @@ -27,27 +24,27 @@ end | |||
| 27 | 24 | ||
| 28 | local check = function (v, e) | 25 | local check = function (v, e) |
| 29 | if v then print("ok") | 26 | if v then print("ok") |
| 30 | else %fail(e) end | 27 | else fail(e) end |
| 31 | end | 28 | end |
| 32 | 29 | ||
| 33 | local check_request = function(request, expect, ignore) | 30 | local check_request = function(request, expect, ignore) |
| 34 | local response = socket.http.request(request) | 31 | local response = socket.http.request(request) |
| 35 | for i,v in response do | 32 | for i,v in response do |
| 36 | if not ignore[i] then | 33 | if not ignore[i] then |
| 37 | if v ~= expect[i] then %fail(i .. " differs!") end | 34 | if v ~= expect[i] then fail(i .. " differs!") end |
| 38 | end | 35 | end |
| 39 | end | 36 | end |
| 40 | for i,v in expect do | 37 | for i,v in expect do |
| 41 | if not ignore[i] then | 38 | if not ignore[i] then |
| 42 | if v ~= response[i] then %fail(i .. " differs!") end | 39 | if v ~= response[i] then fail(i .. " differs!") end |
| 43 | end | 40 | end |
| 44 | end | 41 | end |
| 45 | print("ok") | 42 | print("ok") |
| 46 | end | 43 | end |
| 47 | 44 | ||
| 48 | local request, response, ignore, expect, index, prefix, cgiprefix | 45 | local host, request, response, ignore, expect, index, prefix, cgiprefix |
| 49 | 46 | ||
| 50 | local t = socket._time() | 47 | local t = socket.time() |
| 51 | 48 | ||
| 52 | host = host or "localhost" | 49 | host = host or "localhost" |
| 53 | prefix = prefix or "/luasocket" | 50 | prefix = prefix or "/luasocket" |
| @@ -310,4 +307,4 @@ check(response and response.headers) | |||
| 310 | 307 | ||
| 311 | print("passed all tests") | 308 | print("passed all tests") |
| 312 | 309 | ||
| 313 | print(string.format("done in %.2fs", socket._time() - t)) | 310 | print(string.format("done in %.2fs", socket.time() - t)) |
diff --git a/test/smtptest.lua b/test/smtptest.lua index 27ba400..09bf634 100644 --- a/test/smtptest.lua +++ b/test/smtptest.lua | |||
| @@ -11,7 +11,7 @@ local files = { | |||
| 11 | "/var/spool/mail/luasock3", | 11 | "/var/spool/mail/luasock3", |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | local t = socket._time() | 14 | local t = socket.time() |
| 15 | local err | 15 | local err |
| 16 | 16 | ||
| 17 | dofile("mbox.lua") | 17 | dofile("mbox.lua") |
| @@ -106,7 +106,7 @@ local insert = function(sent, message) | |||
| 106 | end | 106 | end |
| 107 | 107 | ||
| 108 | local mark = function() | 108 | local mark = function() |
| 109 | local time = socket._time() | 109 | local time = socket.time() |
| 110 | return { time = time } | 110 | return { time = time } |
| 111 | end | 111 | end |
| 112 | 112 | ||
| @@ -116,11 +116,11 @@ local wait = function(sentinel, n) | |||
| 116 | while 1 do | 116 | while 1 do |
| 117 | local mbox = parse(get()) | 117 | local mbox = parse(get()) |
| 118 | if n == table.getn(mbox) then break end | 118 | if n == table.getn(mbox) then break end |
| 119 | if socket._time() - sentinel.time > 50 then | 119 | if socket.time() - sentinel.time > 50 then |
| 120 | to = 1 | 120 | to = 1 |
| 121 | break | 121 | break |
| 122 | end | 122 | end |
| 123 | socket._sleep(1) | 123 | socket.sleep(1) |
| 124 | io.write(".") | 124 | io.write(".") |
| 125 | io.stdout:flush() | 125 | io.stdout:flush() |
| 126 | end | 126 | end |
| @@ -256,4 +256,4 @@ for i = 1, table.getn(mbox) do | |||
| 256 | end | 256 | end |
| 257 | 257 | ||
| 258 | print("passed all tests") | 258 | print("passed all tests") |
| 259 | print(string.format("done in %.2fs", socket._time() - t)) | 259 | print(string.format("done in %.2fs", socket.time() - t)) |
diff --git a/test/testclnt.lua b/test/testclnt.lua index 3e80a36..b2b4b18 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
| @@ -43,7 +43,7 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | |||
| 43 | else pass("proper timeout") end | 43 | else pass("proper timeout") end |
| 44 | end | 44 | end |
| 45 | else | 45 | else |
| 46 | if mode == "return" then | 46 | if mode == "total" then |
| 47 | if elapsed > tm then | 47 | if elapsed > tm then |
| 48 | if err ~= "timeout" then fail("should have timed out") | 48 | if err ~= "timeout" then fail("should have timed out") |
| 49 | else pass("proper timeout") end | 49 | else pass("proper timeout") end |
| @@ -66,17 +66,17 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | |||
| 66 | end | 66 | end |
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | if not socket.debug then | ||
| 70 | fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") | ||
| 71 | end | ||
| 72 | |||
| 69 | io.write("----------------------------------------------\n", | 73 | io.write("----------------------------------------------\n", |
| 70 | "LuaSocket Test Procedures\n", | 74 | "LuaSocket Test Procedures\n", |
| 71 | "----------------------------------------------\n") | 75 | "----------------------------------------------\n") |
| 72 | 76 | ||
| 73 | if not socket._time or not socket._sleep then | 77 | start = socket.time() |
| 74 | fail("not compiled with _DEBUG") | ||
| 75 | end | ||
| 76 | |||
| 77 | start = socket._time() | ||
| 78 | 78 | ||
| 79 | function tcpreconnect() | 79 | function reconnect() |
| 80 | io.write("attempting data connection... ") | 80 | io.write("attempting data connection... ") |
| 81 | if data then data:close() end | 81 | if data then data:close() end |
| 82 | remote [[ | 82 | remote [[ |
| @@ -87,7 +87,6 @@ function tcpreconnect() | |||
| 87 | if not data then fail(err) | 87 | if not data then fail(err) |
| 88 | else pass("connected!") end | 88 | else pass("connected!") end |
| 89 | end | 89 | end |
| 90 | reconnect = tcpreconnect | ||
| 91 | 90 | ||
| 92 | pass("attempting control connection...") | 91 | pass("attempting control connection...") |
| 93 | control, err = socket.connect(host, port) | 92 | control, err = socket.connect(host, port) |
| @@ -95,101 +94,78 @@ if err then fail(err) | |||
| 95 | else pass("connected!") end | 94 | else pass("connected!") end |
| 96 | 95 | ||
| 97 | ------------------------------------------------------------------------ | 96 | ------------------------------------------------------------------------ |
| 98 | test("bugs") | ||
| 99 | |||
| 100 | io.write("empty host connect: ") | ||
| 101 | function empty_connect() | ||
| 102 | if data then data:close() data = nil end | ||
| 103 | remote [[ | ||
| 104 | if data then data:close() data = nil end | ||
| 105 | data = server:accept() | ||
| 106 | ]] | ||
| 107 | data, err = socket.connect("", port) | ||
| 108 | if not data then | ||
| 109 | pass("ok") | ||
| 110 | data = socket.connect(host, port) | ||
| 111 | else fail("should not have connected!") end | ||
| 112 | end | ||
| 113 | |||
| 114 | empty_connect() | ||
| 115 | |||
| 116 | io.write("active close: ") | ||
| 117 | function active_close() | ||
| 118 | reconnect() | ||
| 119 | if socket._isclosed(data) then fail("should not be closed") end | ||
| 120 | data:close() | ||
| 121 | if not socket._isclosed(data) then fail("should be closed") end | ||
| 122 | data = nil | ||
| 123 | local udp = socket.udp() | ||
| 124 | if socket._isclosed(udp) then fail("should not be closed") end | ||
| 125 | udp:close() | ||
| 126 | if not socket._isclosed(udp) then fail("should be closed") end | ||
| 127 | pass("ok") | ||
| 128 | end | ||
| 129 | |||
| 130 | active_close() | ||
| 131 | |||
| 132 | ------------------------------------------------------------------------ | ||
| 133 | test("method registration") | 97 | test("method registration") |
| 134 | 98 | ||
| 135 | function test_methods(sock, methods) | 99 | function test_methods(sock, methods) |
| 136 | for _, v in methods do | 100 | for _, v in methods do |
| 137 | if type(sock[v]) ~= "function" then | 101 | if type(sock[v]) ~= "function" then |
| 138 | fail(type(sock) .. " method " .. v .. "not registered") | 102 | fail(sock.class .. " method '" .. v .. "' not registered") |
| 139 | end | 103 | end |
| 140 | end | 104 | end |
| 141 | pass(type(sock) .. " methods are ok") | 105 | pass(sock.class .. " methods are ok") |
| 142 | end | 106 | end |
| 143 | 107 | ||
| 144 | test_methods(control, { | 108 | test_methods(socket.tcp(), { |
| 145 | "close", | 109 | "connect", |
| 146 | "timeout", | 110 | "send", |
| 147 | "send", | 111 | "receive", |
| 148 | "receive", | 112 | "bind", |
| 113 | "accept", | ||
| 114 | "setpeername", | ||
| 115 | "setsockname", | ||
| 149 | "getpeername", | 116 | "getpeername", |
| 150 | "getsockname" | 117 | "getsockname", |
| 118 | "timeout", | ||
| 119 | "close", | ||
| 151 | }) | 120 | }) |
| 152 | 121 | ||
| 153 | if udpsocket then | 122 | test_methods(socket.udp(), { |
| 154 | test_methods(socket.udp(), { | 123 | "getpeername", |
| 155 | "close", | 124 | "getsockname", |
| 156 | "timeout", | 125 | "setsockname", |
| 157 | "send", | 126 | "setpeername", |
| 158 | "sendto", | 127 | "send", |
| 159 | "receive", | 128 | "sendto", |
| 160 | "receivefrom", | 129 | "receive", |
| 161 | "getpeername", | 130 | "receivefrom", |
| 162 | "getsockname", | ||
| 163 | "setsockname", | ||
| 164 | "setpeername" | ||
| 165 | }) | ||
| 166 | end | ||
| 167 | |||
| 168 | test_methods(socket.bind("*", 0), { | ||
| 169 | "close", | ||
| 170 | "timeout", | 131 | "timeout", |
| 171 | "accept" | 132 | "close", |
| 172 | }) | 133 | }) |
| 173 | 134 | ||
| 174 | ------------------------------------------------------------------------ | 135 | ------------------------------------------------------------------------ |
| 175 | test("select function") | 136 | test("mixed patterns") |
| 176 | function test_selectbugs() | 137 | |
| 177 | local r, s, e = socket.select(nil, nil, 0.1) | 138 | function test_mixed(len) |
| 178 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | 139 | reconnect() |
| 179 | pass("both nil: ok") | 140 | local inter = math.ceil(len/4) |
| 180 | local udp = socket.udp() | 141 | local p1 = "unix " .. string.rep("x", inter) .. "line\n" |
| 181 | udp:close() | 142 | local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" |
| 182 | r, s, e = socket.select({ udp }, { udp }, 0.1) | 143 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" |
| 183 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | 144 | local p4 = "end" .. string.rep("w", inter) .. "bytes" |
| 184 | pass("closed sockets: ok") | 145 | local bp1, bp2, bp3, bp4 |
| 185 | e = pcall(socket.select, "wrong", 1, 0.1) | 146 | pass(len .. " byte(s) patterns") |
| 186 | assert(e == false) | 147 | remote (string.format("str = data:receive(%d)", |
| 187 | e = pcall(socket.select, {}, 1, 0.1) | 148 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) |
| 188 | assert(e == false) | 149 | sent, err = data:send(p1, p2, p3, p4) |
| 189 | pass("invalid input: ok") | 150 | if err then fail(err) end |
| 151 | remote "data:send(str); data:close()" | ||
| 152 | bp1, bp2, bp3, bp4, err = data:receive("*l", "*l", string.len(p3), "*a") | ||
| 153 | if err then fail(err) end | ||
| 154 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then | ||
| 155 | pass("patterns match") | ||
| 156 | else fail("patterns don't match") end | ||
| 190 | end | 157 | end |
| 191 | 158 | ||
| 192 | test_selectbugs() | 159 | |
| 160 | test_mixed(1) | ||
| 161 | test_mixed(17) | ||
| 162 | test_mixed(200) | ||
| 163 | test_mixed(4091) | ||
| 164 | test_mixed(80199) | ||
| 165 | test_mixed(4091) | ||
| 166 | test_mixed(200) | ||
| 167 | test_mixed(17) | ||
| 168 | test_mixed(1) | ||
| 193 | 169 | ||
| 194 | ------------------------------------------------------------------------ | 170 | ------------------------------------------------------------------------ |
| 195 | test("character line") | 171 | test("character line") |
| @@ -202,7 +178,7 @@ function test_asciiline(len) | |||
| 202 | str = str .. str10 | 178 | str = str .. str10 |
| 203 | pass(len .. " byte(s) line") | 179 | pass(len .. " byte(s) line") |
| 204 | remote "str = data:receive()" | 180 | remote "str = data:receive()" |
| 205 | err = data:send(str, "\n") | 181 | sent, err = data:send(str, "\n") |
| 206 | if err then fail(err) end | 182 | if err then fail(err) end |
| 207 | remote "data:send(str, '\\n')" | 183 | remote "data:send(str, '\\n')" |
| 208 | back, err = data:receive() | 184 | back, err = data:receive() |
| @@ -230,7 +206,7 @@ function test_rawline(len) | |||
| 230 | str = str .. str10 | 206 | str = str .. str10 |
| 231 | pass(len .. " byte(s) line") | 207 | pass(len .. " byte(s) line") |
| 232 | remote "str = data:receive()" | 208 | remote "str = data:receive()" |
| 233 | err = data:send(str, "\n") | 209 | sent, err = data:send(str, "\n") |
| 234 | if err then fail(err) end | 210 | if err then fail(err) end |
| 235 | remote "data:send(str, '\\n')" | 211 | remote "data:send(str, '\\n')" |
| 236 | back, err = data:receive() | 212 | back, err = data:receive() |
| @@ -262,9 +238,9 @@ function test_raw(len) | |||
| 262 | s2 = string.rep("y", len-half) | 238 | s2 = string.rep("y", len-half) |
| 263 | pass(len .. " byte(s) block") | 239 | pass(len .. " byte(s) block") |
| 264 | remote (string.format("str = data:receive(%d)", len)) | 240 | remote (string.format("str = data:receive(%d)", len)) |
| 265 | err = data:send(s1) | 241 | sent, err = data:send(s1) |
| 266 | if err then fail(err) end | 242 | if err then fail(err) end |
| 267 | err = data:send(s2) | 243 | sent, err = data:send(s2) |
| 268 | if err then fail(err) end | 244 | if err then fail(err) end |
| 269 | remote "data:send(str)" | 245 | remote "data:send(str)" |
| 270 | back, err = data:receive(len) | 246 | back, err = data:receive(len) |
| @@ -304,127 +280,54 @@ test_raw(17) | |||
| 304 | test_raw(1) | 280 | test_raw(1) |
| 305 | 281 | ||
| 306 | ------------------------------------------------------------------------ | 282 | ------------------------------------------------------------------------ |
| 307 | test("mixed patterns") | 283 | test("total timeout on receive") |
| 308 | reconnect() | 284 | function test_totaltimeoutreceive(len, tm, sl) |
| 309 | |||
| 310 | function test_mixed(len) | ||
| 311 | local inter = math.floor(len/3) | ||
| 312 | local p1 = "unix " .. string.rep("x", inter) .. "line\n" | ||
| 313 | local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" | ||
| 314 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" | ||
| 315 | local bp1, bp2, bp3 | ||
| 316 | pass(len .. " byte(s) patterns") | ||
| 317 | remote (string.format("str = data:receive(%d)", | ||
| 318 | string.len(p1)+string.len(p2)+string.len(p3))) | ||
| 319 | err = data:send(p1, p2, p3) | ||
| 320 | if err then fail(err) end | ||
| 321 | remote "data:send(str)" | ||
| 322 | bp1, bp2, bp3, err = data:receive("*lu", "*l", string.len(p3)) | ||
| 323 | if err then fail(err) end | ||
| 324 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then | ||
| 325 | pass("patterns match") | ||
| 326 | else fail("patterns don't match") end | ||
| 327 | end | ||
| 328 | |||
| 329 | test_mixed(1) | ||
| 330 | test_mixed(17) | ||
| 331 | test_mixed(200) | ||
| 332 | test_mixed(4091) | ||
| 333 | test_mixed(80199) | ||
| 334 | test_mixed(800000) | ||
| 335 | test_mixed(80199) | ||
| 336 | test_mixed(4091) | ||
| 337 | test_mixed(200) | ||
| 338 | test_mixed(17) | ||
| 339 | test_mixed(1) | ||
| 340 | |||
| 341 | ------------------------------------------------------------------------ | ||
| 342 | test("closed connection detection") | ||
| 343 | |||
| 344 | function test_closed() | ||
| 345 | local back, err | ||
| 346 | local str = 'little string' | ||
| 347 | reconnect() | ||
| 348 | pass("trying read detection") | ||
| 349 | remote (string.format ([[ | ||
| 350 | data:send('%s') | ||
| 351 | data:close() | ||
| 352 | data = nil | ||
| 353 | ]], str)) | ||
| 354 | -- try to get a line | ||
| 355 | back, err = data:receive() | ||
| 356 | if not err then fail("shold have gotten 'closed'.") | ||
| 357 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | ||
| 358 | elseif str ~= back then fail("didn't receive partial result.") | ||
| 359 | else pass("graceful 'closed' received") end | ||
| 360 | reconnect() | ||
| 361 | pass("trying write detection") | ||
| 362 | remote [[ | ||
| 363 | data:close() | ||
| 364 | data = nil | ||
| 365 | ]] | ||
| 366 | err, total = data:send(string.rep("ugauga", 100000)) | ||
| 367 | if not err then | ||
| 368 | pass("failed: output buffer is at least %d bytes long!", total) | ||
| 369 | elseif err ~= "closed" then | ||
| 370 | fail("got '"..err.."' instead of 'closed'.") | ||
| 371 | else | ||
| 372 | pass("graceful 'closed' received after %d bytes were sent", total) | ||
| 373 | end | ||
| 374 | end | ||
| 375 | |||
| 376 | test_closed() | ||
| 377 | |||
| 378 | ------------------------------------------------------------------------ | ||
| 379 | test("return timeout on receive") | ||
| 380 | function test_blockingtimeoutreceive(len, tm, sl) | ||
| 381 | local str, err, total | 285 | local str, err, total |
| 382 | reconnect() | 286 | reconnect() |
| 383 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) | 287 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) |
| 384 | remote (string.format ([[ | 288 | remote (string.format ([[ |
| 385 | data:timeout(%d) | 289 | data:timeout(%d) |
| 386 | str = string.rep('a', %d) | 290 | str = string.rep('a', %d) |
| 387 | data:send(str) | 291 | data:send(str) |
| 388 | print('server: sleeping for %ds') | 292 | print('server: sleeping for %ds') |
| 389 | socket._sleep(%d) | 293 | socket.sleep(%d) |
| 390 | print('server: woke up') | 294 | print('server: woke up') |
| 391 | data:send(str) | 295 | data:send(str) |
| 392 | ]], 2*tm, len, sl, sl)) | 296 | ]], 2*tm, len, sl, sl)) |
| 393 | data:timeout(tm, "return") | 297 | data:timeout(tm, "total") |
| 394 | str, err, elapsed = data:receive(2*len) | 298 | str, err, elapsed = data:receive(2*len) |
| 395 | check_timeout(tm, sl, elapsed, err, "receive", "return", | 299 | check_timeout(tm, sl, elapsed, err, "receive", "total", |
| 396 | string.len(str) == 2*len) | 300 | string.len(str) == 2*len) |
| 397 | end | 301 | end |
| 398 | test_blockingtimeoutreceive(800091, 1, 3) | 302 | test_totaltimeoutreceive(800091, 1, 3) |
| 399 | test_blockingtimeoutreceive(800091, 2, 3) | 303 | test_totaltimeoutreceive(800091, 2, 3) |
| 400 | test_blockingtimeoutreceive(800091, 3, 2) | 304 | test_totaltimeoutreceive(800091, 3, 2) |
| 401 | test_blockingtimeoutreceive(800091, 3, 1) | 305 | test_totaltimeoutreceive(800091, 3, 1) |
| 402 | 306 | ||
| 403 | ------------------------------------------------------------------------ | 307 | ------------------------------------------------------------------------ |
| 404 | test("return timeout on send") | 308 | test("total timeout on send") |
| 405 | function test_returntimeoutsend(len, tm, sl) | 309 | function test_totaltimeoutsend(len, tm, sl) |
| 406 | local str, err, total | 310 | local str, err, total |
| 407 | reconnect() | 311 | reconnect() |
| 408 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) | 312 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) |
| 409 | remote (string.format ([[ | 313 | remote (string.format ([[ |
| 410 | data:timeout(%d) | 314 | data:timeout(%d) |
| 411 | str = data:receive(%d) | 315 | str = data:receive(%d) |
| 412 | print('server: sleeping for %ds') | 316 | print('server: sleeping for %ds') |
| 413 | socket._sleep(%d) | 317 | socket.sleep(%d) |
| 414 | print('server: woke up') | 318 | print('server: woke up') |
| 415 | str = data:receive(%d) | 319 | str = data:receive(%d) |
| 416 | ]], 2*tm, len, sl, sl, len)) | 320 | ]], 2*tm, len, sl, sl, len)) |
| 417 | data:timeout(tm, "return") | 321 | data:timeout(tm, "total") |
| 418 | str = string.rep("a", 2*len) | 322 | str = string.rep("a", 2*len) |
| 419 | err, total, elapsed = data:send(str) | 323 | total, err, elapsed = data:send(str) |
| 420 | check_timeout(tm, sl, elapsed, err, "send", "return", | 324 | check_timeout(tm, sl, elapsed, err, "send", "total", |
| 421 | total == 2*len) | 325 | total == 2*len) |
| 422 | end | 326 | end |
| 423 | test_returntimeoutsend(800091, 1, 3) | 327 | test_totaltimeoutsend(800091, 1, 3) |
| 424 | test_returntimeoutsend(800091, 2, 3) | 328 | test_totaltimeoutsend(800091, 2, 3) |
| 425 | test_returntimeoutsend(800091, 3, 2) | 329 | test_totaltimeoutsend(800091, 3, 2) |
| 426 | test_returntimeoutsend(800091, 3, 1) | 330 | test_totaltimeoutsend(800091, 3, 1) |
| 427 | |||
| 428 | 331 | ||
| 429 | ------------------------------------------------------------------------ | 332 | ------------------------------------------------------------------------ |
| 430 | test("blocking timeout on receive") | 333 | test("blocking timeout on receive") |
| @@ -437,7 +340,7 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
| 437 | str = string.rep('a', %d) | 340 | str = string.rep('a', %d) |
| 438 | data:send(str) | 341 | data:send(str) |
| 439 | print('server: sleeping for %ds') | 342 | print('server: sleeping for %ds') |
| 440 | socket._sleep(%d) | 343 | socket.sleep(%d) |
| 441 | print('server: woke up') | 344 | print('server: woke up') |
| 442 | data:send(str) | 345 | data:send(str) |
| 443 | ]], 2*tm, len, sl, sl)) | 346 | ]], 2*tm, len, sl, sl)) |
| @@ -451,7 +354,6 @@ test_blockingtimeoutreceive(800091, 2, 3) | |||
| 451 | test_blockingtimeoutreceive(800091, 3, 2) | 354 | test_blockingtimeoutreceive(800091, 3, 2) |
| 452 | test_blockingtimeoutreceive(800091, 3, 1) | 355 | test_blockingtimeoutreceive(800091, 3, 1) |
| 453 | 356 | ||
| 454 | |||
| 455 | ------------------------------------------------------------------------ | 357 | ------------------------------------------------------------------------ |
| 456 | test("blocking timeout on send") | 358 | test("blocking timeout on send") |
| 457 | function test_blockingtimeoutsend(len, tm, sl) | 359 | function test_blockingtimeoutsend(len, tm, sl) |
| @@ -462,13 +364,13 @@ function test_blockingtimeoutsend(len, tm, sl) | |||
| 462 | data:timeout(%d) | 364 | data:timeout(%d) |
| 463 | str = data:receive(%d) | 365 | str = data:receive(%d) |
| 464 | print('server: sleeping for %ds') | 366 | print('server: sleeping for %ds') |
| 465 | socket._sleep(%d) | 367 | socket.sleep(%d) |
| 466 | print('server: woke up') | 368 | print('server: woke up') |
| 467 | str = data:receive(%d) | 369 | str = data:receive(%d) |
| 468 | ]], 2*tm, len, sl, sl, len)) | 370 | ]], 2*tm, len, sl, sl, len)) |
| 469 | data:timeout(tm) | 371 | data:timeout(tm) |
| 470 | str = string.rep("a", 2*len) | 372 | str = string.rep("a", 2*len) |
| 471 | err, total, elapsed = data:send(str) | 373 | total, err, elapsed = data:send(str) |
| 472 | check_timeout(tm, sl, elapsed, err, "send", "blocking", | 374 | check_timeout(tm, sl, elapsed, err, "send", "blocking", |
| 473 | total == 2*len) | 375 | total == 2*len) |
| 474 | end | 376 | end |
| @@ -478,4 +380,98 @@ test_blockingtimeoutsend(800091, 3, 2) | |||
| 478 | test_blockingtimeoutsend(800091, 3, 1) | 380 | test_blockingtimeoutsend(800091, 3, 1) |
| 479 | 381 | ||
| 480 | ------------------------------------------------------------------------ | 382 | ------------------------------------------------------------------------ |
| 481 | test(string.format("done in %.2fs", socket._time() - start)) | 383 | test("bugs") |
| 384 | |||
| 385 | io.write("empty host connect: ") | ||
| 386 | function empty_connect() | ||
| 387 | if data then data:close() data = nil end | ||
| 388 | remote [[ | ||
| 389 | if data then data:close() data = nil end | ||
| 390 | data = server:accept() | ||
| 391 | ]] | ||
| 392 | data, err = socket.connect("", port) | ||
| 393 | if not data then | ||
| 394 | pass("ok") | ||
| 395 | data = socket.connect(host, port) | ||
| 396 | else fail("should not have connected!") end | ||
| 397 | end | ||
| 398 | |||
| 399 | empty_connect() | ||
| 400 | |||
| 401 | -- io.write("active close: ") | ||
| 402 | function active_close() | ||
| 403 | reconnect() | ||
| 404 | if socket._isclosed(data) then fail("should not be closed") end | ||
| 405 | data:close() | ||
| 406 | if not socket._isclosed(data) then fail("should be closed") end | ||
| 407 | data = nil | ||
| 408 | local udp = socket.udp() | ||
| 409 | if socket._isclosed(udp) then fail("should not be closed") end | ||
| 410 | udp:close() | ||
| 411 | if not socket._isclosed(udp) then fail("should be closed") end | ||
| 412 | pass("ok") | ||
| 413 | end | ||
| 414 | |||
| 415 | -- active_close() | ||
| 416 | |||
| 417 | ------------------------------------------------------------------------ | ||
| 418 | test("closed connection detection") | ||
| 419 | |||
| 420 | function test_closed() | ||
| 421 | local back, err | ||
| 422 | local str = 'little string' | ||
| 423 | reconnect() | ||
| 424 | pass("trying read detection") | ||
| 425 | remote (string.format ([[ | ||
| 426 | data:send('%s') | ||
| 427 | data:close() | ||
| 428 | data = nil | ||
| 429 | ]], str)) | ||
| 430 | -- try to get a line | ||
| 431 | back, err = data:receive() | ||
| 432 | if not err then fail("shold have gotten 'closed'.") | ||
| 433 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | ||
| 434 | elseif str ~= back then fail("didn't receive partial result.") | ||
| 435 | else pass("graceful 'closed' received") end | ||
| 436 | reconnect() | ||
| 437 | pass("trying write detection") | ||
| 438 | remote [[ | ||
| 439 | data:close() | ||
| 440 | data = nil | ||
| 441 | ]] | ||
| 442 | total, err = data:send(string.rep("ugauga", 100000)) | ||
| 443 | if not err then | ||
| 444 | pass("failed: output buffer is at least %d bytes long!", total) | ||
| 445 | elseif err ~= "closed" then | ||
| 446 | fail("got '"..err.."' instead of 'closed'.") | ||
| 447 | else | ||
| 448 | pass("graceful 'closed' received after %d bytes were sent", total) | ||
| 449 | end | ||
| 450 | end | ||
| 451 | |||
| 452 | test_closed() | ||
| 453 | |||
| 454 | ------------------------------------------------------------------------ | ||
| 455 | test("select function") | ||
| 456 | function test_selectbugs() | ||
| 457 | local r, s, e = socket.select(nil, nil, 0.1) | ||
| 458 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | ||
| 459 | pass("both nil: ok") | ||
| 460 | local udp = socket.udp() | ||
| 461 | udp:close() | ||
| 462 | r, s, e = socket.select({ udp }, { udp }, 0.1) | ||
| 463 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | ||
| 464 | pass("closed sockets: ok") | ||
| 465 | e = pcall(socket.select, "wrong", 1, 0.1) | ||
| 466 | assert(e == false) | ||
| 467 | e = pcall(socket.select, {}, 1, 0.1) | ||
| 468 | assert(e == false) | ||
| 469 | pass("invalid input: ok") | ||
| 470 | end | ||
| 471 | |||
| 472 | -- test_selectbugs() | ||
| 473 | |||
| 474 | |||
| 475 | |||
| 476 | |||
| 477 | test(string.format("done in %.2fs", socket.time() - start)) | ||
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index fb77ea5..3c40840 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
| @@ -13,12 +13,13 @@ while 1 do | |||
| 13 | print("server: closing connection...") | 13 | print("server: closing connection...") |
| 14 | break | 14 | break |
| 15 | end | 15 | end |
| 16 | error = control:send("\n") | 16 | sent, error = control:send("\n") |
| 17 | if error then | 17 | if error then |
| 18 | control:close() | 18 | control:close() |
| 19 | print("server: closing connection...") | 19 | print("server: closing connection...") |
| 20 | break | 20 | break |
| 21 | end | 21 | end |
| 22 | print(command); | ||
| 22 | (loadstring(command))() | 23 | (loadstring(command))() |
| 23 | end | 24 | end |
| 24 | end | 25 | end |
diff --git a/test/tftptest.lua b/test/tftptest.lua index a435ad4..a478ed8 100644 --- a/test/tftptest.lua +++ b/test/tftptest.lua | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | -- load tftpclnt.lua | 1 | -- load tftpclnt.lua |
| 2 | dofile("tftpclnt.lua") | 2 | dofile("tftp.lua") |
| 3 | 3 | ||
| 4 | -- needs tftp server running on localhost, with root pointing to | 4 | -- needs tftp server running on localhost, with root pointing to |
| 5 | -- a directory with index.html in it | 5 | -- a directory with index.html in it |
| @@ -13,11 +13,8 @@ function readfile(file) | |||
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | host = host or "localhost" | 15 | host = host or "localhost" |
| 16 | print("downloading") | 16 | retrieved, err = socket.tftp.get("tftp://" .. host .."/index.html") |
| 17 | err = tftp_get(host, 69, "index.html", "index.got") | ||
| 18 | assert(not err, err) | 17 | assert(not err, err) |
| 19 | original = readfile("test/index.html") | 18 | original = readfile("test/index.html") |
| 20 | retrieved = readfile("index.got") | ||
| 21 | os.remove("index.got") | ||
| 22 | assert(original == retrieved, "files differ!") | 19 | assert(original == retrieved, "files differ!") |
| 23 | print("passed") | 20 | print("passed") |
