diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-12-03 07:20:34 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-12-03 07:20:34 +0000 |
| commit | 7da19138e37c4e0123860f1fecbceb80c3d2627d (patch) | |
| tree | 8453f003a9ba212807d9c9590c2f2b850d323f0f /test/testclnt.lua | |
| parent | d7e80592a69c076991ed4f4cc15d5390e14d1f0b (diff) | |
| download | luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.gz luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.bz2 luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.zip | |
Faltam testes de ftp e smtp. O resto passa.
Diffstat (limited to 'test/testclnt.lua')
| -rw-r--r-- | test/testclnt.lua | 169 |
1 files changed, 83 insertions, 86 deletions
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" | |||
| 2 | PORT = PORT or "8080" | 2 | PORT = PORT or "8080" |
| 3 | 3 | ||
| 4 | function pass(...) | 4 | function pass(...) |
| 5 | local s = call(format, arg) | 5 | local s = string.format(unpack(arg)) |
| 6 | write(s, "\n") | 6 | io.write(s, "\n") |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | function fail(...) | 9 | function fail(...) |
| 10 | local s = call(format, arg) | 10 | local s = string.format(unpack(arg)) |
| 11 | write("ERROR: ", s, "!\n") | 11 | io.write("ERROR: ", s, "!\n") |
| 12 | exit() | 12 | os.exit() |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | function warn(...) | 15 | function warn(...) |
| 16 | local s = call(format, arg) | 16 | local s = format(unpack(arg)) |
| 17 | write("WARNING: ", s, "\n") | 17 | io.write("WARNING: ", s, "\n") |
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | function remote(...) | 20 | function remote(...) |
| 21 | local s = call(format, arg) | 21 | local s = string.format(unpack(arg)) |
| 22 | s = gsub(s, "\n", ";") | 22 | s = string.gsub(s, "\n", ";") |
| 23 | s = gsub(s, "%s+", " ") | 23 | s = string.gsub(s, "%s+", " ") |
| 24 | s = gsub(s, "^%s*", "") | 24 | s = string.gsub(s, "^%s*", "") |
| 25 | control:send(s, "\n") | 25 | control:send(s, "\n") |
| 26 | control:receive() | 26 | control:receive() |
| 27 | end | 27 | end |
| 28 | 28 | ||
| 29 | function test(test) | 29 | function test(test) |
| 30 | write("----------------------------------------------\n", | 30 | io.write("----------------------------------------------\n", |
| 31 | "testing: ", test, "\n", | 31 | "testing: ", test, "\n", |
| 32 | "----------------------------------------------\n") | 32 | "----------------------------------------------\n") |
| 33 | end | 33 | end |
| @@ -66,51 +66,69 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | |||
| 66 | end | 66 | end |
| 67 | end | 67 | end |
| 68 | 68 | ||
| 69 | write("----------------------------------------------\n", | 69 | io.write("----------------------------------------------\n", |
| 70 | "LuaSocket Test Procedures\n", | 70 | "LuaSocket Test Procedures\n", |
| 71 | "----------------------------------------------\n") | 71 | "----------------------------------------------\n") |
| 72 | 72 | ||
| 73 | if not _time or not _sleep then fail("not compiled with _DEBUG") end | 73 | if not socket._time or not socket._sleep then |
| 74 | fail("not compiled with _DEBUG") | ||
| 75 | end | ||
| 74 | 76 | ||
| 75 | start = _time() | 77 | start = socket._time() |
| 76 | 78 | ||
| 77 | function tcpreconnect() | 79 | function tcpreconnect() |
| 78 | write("attempting data connection... ") | 80 | io.write("attempting data connection... ") |
| 79 | if data then data:close() end | 81 | if data then data:close() end |
| 80 | remote [[ | 82 | remote [[ |
| 81 | if data then data:close() data = nil end | 83 | if data then data:close() data = nil end |
| 82 | data = server:accept() | 84 | data = server:accept() |
| 83 | ]] | 85 | ]] |
| 84 | data, error = connect(HOST, PORT) | 86 | data, err = socket.connect(HOST, PORT) |
| 85 | if not data then fail(error) | 87 | if not data then fail(err) |
| 86 | else pass("connected!") end | 88 | else pass("connected!") end |
| 87 | end | 89 | end |
| 88 | reconnect = tcpreconnect | 90 | reconnect = tcpreconnect |
| 89 | 91 | ||
| 90 | pass("attempting control connection...") | 92 | pass("attempting control connection...") |
| 91 | control, error = connect(HOST, PORT) | 93 | control, err = socket.connect(HOST, PORT) |
| 92 | if error then fail(error) | 94 | if err then fail(err) |
| 93 | else pass("connected!") end | 95 | else pass("connected!") end |
| 94 | 96 | ||
| 95 | ------------------------------------------------------------------------ | 97 | ------------------------------------------------------------------------ |
| 96 | test("bugs") | 98 | test("bugs") |
| 97 | 99 | ||
| 98 | write("empty host connect: ") | 100 | io.write("empty host connect: ") |
| 99 | function empty_connect() | 101 | function empty_connect() |
| 100 | if data then data:close() data = nil end | 102 | if data then data:close() data = nil end |
| 101 | remote [[ | 103 | remote [[ |
| 102 | if data then data:close() data = nil end | 104 | if data then data:close() data = nil end |
| 103 | data = server:accept() | 105 | data = server:accept() |
| 104 | ]] | 106 | ]] |
| 105 | data, err = connect("", PORT) | 107 | data, err = socket.connect("", PORT) |
| 106 | if not data then | 108 | if not data then |
| 107 | pass("ok") | 109 | pass("ok") |
| 108 | data = connect(HOST, PORT) | 110 | data = socket.connect(HOST, PORT) |
| 109 | else fail("should not have connected!") end | 111 | else fail("should not have connected!") end |
| 110 | end | 112 | end |
| 111 | 113 | ||
| 112 | empty_connect() | 114 | empty_connect() |
| 113 | 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 | |||
| 114 | ------------------------------------------------------------------------ | 132 | ------------------------------------------------------------------------ |
| 115 | test("method registration") | 133 | test("method registration") |
| 116 | 134 | ||
| @@ -133,7 +151,7 @@ test_methods(control, { | |||
| 133 | }) | 151 | }) |
| 134 | 152 | ||
| 135 | if udpsocket then | 153 | if udpsocket then |
| 136 | test_methods(udpsocket(), { | 154 | test_methods(socket.udp(), { |
| 137 | "close", | 155 | "close", |
| 138 | "timeout", | 156 | "timeout", |
| 139 | "send", | 157 | "send", |
| @@ -147,50 +165,27 @@ if udpsocket then | |||
| 147 | }) | 165 | }) |
| 148 | end | 166 | end |
| 149 | 167 | ||
| 150 | test_methods(bind("*", 0), { | 168 | test_methods(socket.bind("*", 0), { |
| 151 | "close", | 169 | "close", |
| 152 | "timeout", | 170 | "timeout", |
| 153 | "accept" | 171 | "accept" |
| 154 | }) | 172 | }) |
| 155 | 173 | ||
| 156 | if pipe then | ||
| 157 | local p1, p2 = pipe() | ||
| 158 | test_methods(p1, { | ||
| 159 | "close", | ||
| 160 | "timeout", | ||
| 161 | "send", | ||
| 162 | "receive" | ||
| 163 | }) | ||
| 164 | test_methods(p2, { | ||
| 165 | "close", | ||
| 166 | "timeout", | ||
| 167 | "send", | ||
| 168 | "receive" | ||
| 169 | }) | ||
| 170 | end | ||
| 171 | |||
| 172 | if filesocket then | ||
| 173 | test_methods(filesocket(0), { | ||
| 174 | "close", | ||
| 175 | "timeout", | ||
| 176 | "send", | ||
| 177 | "receive" | ||
| 178 | }) | ||
| 179 | end | ||
| 180 | |||
| 181 | ------------------------------------------------------------------------ | 174 | ------------------------------------------------------------------------ |
| 182 | test("select function") | 175 | test("select function") |
| 183 | function test_selectbugs() | 176 | function test_selectbugs() |
| 184 | local r, s, e = select(nil, nil, 0.1) | 177 | local r, s, e = socket.select(nil, nil, 0.1) |
| 185 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | 178 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") |
| 186 | pass("both nil: ok") | 179 | pass("both nil: ok") |
| 187 | local udp = udpsocket() | 180 | local udp = socket.udp() |
| 188 | udp:close() | 181 | udp:close() |
| 189 | r, s, e = select({ data }, { data }, 0.1) | 182 | r, s, e = socket.select({ udp }, { udp }, 0.1) |
| 190 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") | 183 | assert(type(r) == "table" and type(s) == "table" and e == "timeout") |
| 191 | pass("closed sockets: ok") | 184 | pass("closed sockets: ok") |
| 192 | e = call(select, {"wrong", 1, 0.1}, "x", nil) | 185 | e = pcall(socket.select, "wrong", 1, 0.1) |
| 193 | assert(e == nil) | 186 | assert(e == false) |
| 187 | e = pcall(socket.select, {}, 1, 0.1) | ||
| 188 | assert(e == false) | ||
| 194 | pass("invalid input: ok") | 189 | pass("invalid input: ok") |
| 195 | end | 190 | end |
| 196 | 191 | ||
| @@ -202,8 +197,8 @@ reconnect() | |||
| 202 | 197 | ||
| 203 | function test_asciiline(len) | 198 | function test_asciiline(len) |
| 204 | local str, str10, back, err | 199 | local str, str10, back, err |
| 205 | str = strrep("x", mod(len, 10)) | 200 | str = string.rep("x", math.mod(len, 10)) |
| 206 | str10 = strrep("aZb.c#dAe?", floor(len/10)) | 201 | str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) |
| 207 | str = str .. str10 | 202 | str = str .. str10 |
| 208 | pass(len .. " byte(s) line") | 203 | pass(len .. " byte(s) line") |
| 209 | remote "str = data:receive()" | 204 | remote "str = data:receive()" |
| @@ -229,8 +224,9 @@ reconnect() | |||
| 229 | 224 | ||
| 230 | function test_rawline(len) | 225 | function test_rawline(len) |
| 231 | local str, str10, back, err | 226 | local str, str10, back, err |
| 232 | str = strrep(strchar(47), mod(len, 10)) | 227 | str = string.rep(string.char(47), math.mod(len, 10)) |
| 233 | str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) | 228 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), |
| 229 | math.floor(len/10)) | ||
| 234 | str = str .. str10 | 230 | str = str .. str10 |
| 235 | pass(len .. " byte(s) line") | 231 | pass(len .. " byte(s) line") |
| 236 | remote "str = data:receive()" | 232 | remote "str = data:receive()" |
| @@ -260,12 +256,12 @@ test("raw transfer") | |||
| 260 | reconnect() | 256 | reconnect() |
| 261 | 257 | ||
| 262 | function test_raw(len) | 258 | function test_raw(len) |
| 263 | local half = floor(len/2) | 259 | local half = math.floor(len/2) |
| 264 | local s1, s2, back, err | 260 | local s1, s2, back, err |
| 265 | s1 = strrep("x", half) | 261 | s1 = string.rep("x", half) |
| 266 | s2 = strrep("y", len-half) | 262 | s2 = string.rep("y", len-half) |
| 267 | pass(len .. " byte(s) block") | 263 | pass(len .. " byte(s) block") |
| 268 | remote (format("str = data:receive(%d)", len)) | 264 | remote (string.format("str = data:receive(%d)", len)) |
| 269 | err = data:send(s1) | 265 | err = data:send(s1) |
| 270 | if err then fail(err) end | 266 | if err then fail(err) end |
| 271 | err = data:send(s2) | 267 | err = data:send(s2) |
| @@ -312,17 +308,18 @@ test("mixed patterns") | |||
| 312 | reconnect() | 308 | reconnect() |
| 313 | 309 | ||
| 314 | function test_mixed(len) | 310 | function test_mixed(len) |
| 315 | local inter = floor(len/3) | 311 | local inter = math.floor(len/3) |
| 316 | local p1 = "unix " .. strrep("x", inter) .. "line\n" | 312 | local p1 = "unix " .. string.rep("x", inter) .. "line\n" |
| 317 | local p2 = "dos " .. strrep("y", inter) .. "line\r\n" | 313 | local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" |
| 318 | local p3 = "raw " .. strrep("z", inter) .. "bytes" | 314 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" |
| 319 | local bp1, bp2, bp3 | 315 | local bp1, bp2, bp3 |
| 320 | pass(len .. " byte(s) patterns") | 316 | pass(len .. " byte(s) patterns") |
| 321 | remote (format("str = data:receive(%d)", strlen(p1)+strlen(p2)+strlen(p3))) | 317 | remote (string.format("str = data:receive(%d)", |
| 318 | string.len(p1)+string.len(p2)+string.len(p3))) | ||
| 322 | err = data:send(p1, p2, p3) | 319 | err = data:send(p1, p2, p3) |
| 323 | if err then fail(err) end | 320 | if err then fail(err) end |
| 324 | remote "data:send(str)" | 321 | remote "data:send(str)" |
| 325 | bp1, bp2, bp3, err = data:receive("*lu", "*l", strlen(p3)) | 322 | bp1, bp2, bp3, err = data:receive("*lu", "*l", string.len(p3)) |
| 326 | if err then fail(err) end | 323 | if err then fail(err) end |
| 327 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then | 324 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then |
| 328 | pass("patterns match") | 325 | pass("patterns match") |
| @@ -349,7 +346,7 @@ function test_closed() | |||
| 349 | local str = 'little string' | 346 | local str = 'little string' |
| 350 | reconnect() | 347 | reconnect() |
| 351 | pass("trying read detection") | 348 | pass("trying read detection") |
| 352 | remote (format ([[ | 349 | remote (string.format ([[ |
| 353 | data:send('%s') | 350 | data:send('%s') |
| 354 | data:close() | 351 | data:close() |
| 355 | data = nil | 352 | data = nil |
| @@ -366,7 +363,7 @@ function test_closed() | |||
| 366 | data:close() | 363 | data:close() |
| 367 | data = nil | 364 | data = nil |
| 368 | ]] | 365 | ]] |
| 369 | err, total = data:send(strrep("ugauga", 100000)) | 366 | err, total = data:send(string.rep("ugauga", 100000)) |
| 370 | if not err then | 367 | if not err then |
| 371 | pass("failed: output buffer is at least %d bytes long!", total) | 368 | pass("failed: output buffer is at least %d bytes long!", total) |
| 372 | elseif err ~= "closed" then | 369 | elseif err ~= "closed" then |
| @@ -384,19 +381,19 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
| 384 | local str, err, total | 381 | local str, err, total |
| 385 | reconnect() | 382 | reconnect() |
| 386 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) | 383 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) |
| 387 | remote (format ([[ | 384 | remote (string.format ([[ |
| 388 | data:timeout(%d) | 385 | data:timeout(%d) |
| 389 | str = strrep('a', %d) | 386 | str = string.rep('a', %d) |
| 390 | data:send(str) | 387 | data:send(str) |
| 391 | print('server: sleeping for %ds') | 388 | print('server: sleeping for %ds') |
| 392 | _sleep(%d) | 389 | socket._sleep(%d) |
| 393 | print('server: woke up') | 390 | print('server: woke up') |
| 394 | data:send(str) | 391 | data:send(str) |
| 395 | ]], 2*tm, len, sl, sl)) | 392 | ]], 2*tm, len, sl, sl)) |
| 396 | data:timeout(tm, "return") | 393 | data:timeout(tm, "return") |
| 397 | str, err, elapsed = data:receive(2*len) | 394 | str, err, elapsed = data:receive(2*len) |
| 398 | check_timeout(tm, sl, elapsed, err, "receive", "return", | 395 | check_timeout(tm, sl, elapsed, err, "receive", "return", |
| 399 | strlen(str) == 2*len) | 396 | string.len(str) == 2*len) |
| 400 | end | 397 | end |
| 401 | test_blockingtimeoutreceive(800091, 1, 3) | 398 | test_blockingtimeoutreceive(800091, 1, 3) |
| 402 | test_blockingtimeoutreceive(800091, 2, 3) | 399 | test_blockingtimeoutreceive(800091, 2, 3) |
| @@ -409,16 +406,16 @@ function test_returntimeoutsend(len, tm, sl) | |||
| 409 | local str, err, total | 406 | local str, err, total |
| 410 | reconnect() | 407 | reconnect() |
| 411 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) | 408 | pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) |
| 412 | remote (format ([[ | 409 | remote (string.format ([[ |
| 413 | data:timeout(%d) | 410 | data:timeout(%d) |
| 414 | str = data:receive(%d) | 411 | str = data:receive(%d) |
| 415 | print('server: sleeping for %ds') | 412 | print('server: sleeping for %ds') |
| 416 | _sleep(%d) | 413 | socket._sleep(%d) |
| 417 | print('server: woke up') | 414 | print('server: woke up') |
| 418 | str = data:receive(%d) | 415 | str = data:receive(%d) |
| 419 | ]], 2*tm, len, sl, sl, len)) | 416 | ]], 2*tm, len, sl, sl, len)) |
| 420 | data:timeout(tm, "return") | 417 | data:timeout(tm, "return") |
| 421 | str = strrep("a", 2*len) | 418 | str = string.rep("a", 2*len) |
| 422 | err, total, elapsed = data:send(str) | 419 | err, total, elapsed = data:send(str) |
| 423 | check_timeout(tm, sl, elapsed, err, "send", "return", | 420 | check_timeout(tm, sl, elapsed, err, "send", "return", |
| 424 | total == 2*len) | 421 | total == 2*len) |
| @@ -435,19 +432,19 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
| 435 | local str, err, total | 432 | local str, err, total |
| 436 | reconnect() | 433 | reconnect() |
| 437 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | 434 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) |
| 438 | remote (format ([[ | 435 | remote (string.format ([[ |
| 439 | data:timeout(%d) | 436 | data:timeout(%d) |
| 440 | str = strrep('a', %d) | 437 | str = string.rep('a', %d) |
| 441 | data:send(str) | 438 | data:send(str) |
| 442 | print('server: sleeping for %ds') | 439 | print('server: sleeping for %ds') |
| 443 | _sleep(%d) | 440 | socket._sleep(%d) |
| 444 | print('server: woke up') | 441 | print('server: woke up') |
| 445 | data:send(str) | 442 | data:send(str) |
| 446 | ]], 2*tm, len, sl, sl)) | 443 | ]], 2*tm, len, sl, sl)) |
| 447 | data:timeout(tm) | 444 | data:timeout(tm) |
| 448 | str, err, elapsed = data:receive(2*len) | 445 | str, err, elapsed = data:receive(2*len) |
| 449 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", | 446 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", |
| 450 | strlen(str) == 2*len) | 447 | string.len(str) == 2*len) |
| 451 | end | 448 | end |
| 452 | test_blockingtimeoutreceive(800091, 1, 3) | 449 | test_blockingtimeoutreceive(800091, 1, 3) |
| 453 | test_blockingtimeoutreceive(800091, 2, 3) | 450 | test_blockingtimeoutreceive(800091, 2, 3) |
| @@ -461,16 +458,16 @@ function test_blockingtimeoutsend(len, tm, sl) | |||
| 461 | local str, err, total | 458 | local str, err, total |
| 462 | reconnect() | 459 | reconnect() |
| 463 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | 460 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) |
| 464 | remote (format ([[ | 461 | remote (string.format ([[ |
| 465 | data:timeout(%d) | 462 | data:timeout(%d) |
| 466 | str = data:receive(%d) | 463 | str = data:receive(%d) |
| 467 | print('server: sleeping for %ds') | 464 | print('server: sleeping for %ds') |
| 468 | _sleep(%d) | 465 | socket._sleep(%d) |
| 469 | print('server: woke up') | 466 | print('server: woke up') |
| 470 | str = data:receive(%d) | 467 | str = data:receive(%d) |
| 471 | ]], 2*tm, len, sl, sl, len)) | 468 | ]], 2*tm, len, sl, sl, len)) |
| 472 | data:timeout(tm) | 469 | data:timeout(tm) |
| 473 | str = strrep("a", 2*len) | 470 | str = string.rep("a", 2*len) |
| 474 | err, total, elapsed = data:send(str) | 471 | err, total, elapsed = data:send(str) |
| 475 | check_timeout(tm, sl, elapsed, err, "send", "blocking", | 472 | check_timeout(tm, sl, elapsed, err, "send", "blocking", |
| 476 | total == 2*len) | 473 | total == 2*len) |
| @@ -481,4 +478,4 @@ test_blockingtimeoutsend(800091, 3, 2) | |||
| 481 | test_blockingtimeoutsend(800091, 3, 1) | 478 | test_blockingtimeoutsend(800091, 3, 1) |
| 482 | 479 | ||
| 483 | ------------------------------------------------------------------------ | 480 | ------------------------------------------------------------------------ |
| 484 | test(format("done in %.2fs", _time() - start)) | 481 | test(string.format("done in %.2fs", socket._time() - start)) |
