diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/testclnt.lua | 89 |
1 files changed, 58 insertions, 31 deletions
diff --git a/test/testclnt.lua b/test/testclnt.lua index 6dccd0c..beb0157 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -117,7 +117,6 @@ function test_mixed(len) | |||
117 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" | 117 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" |
118 | local p4 = "end" .. string.rep("w", inter) .. "bytes" | 118 | local p4 = "end" .. string.rep("w", inter) .. "bytes" |
119 | local bp1, bp2, bp3, bp4 | 119 | local bp1, bp2, bp3, bp4 |
120 | pass(len .. " byte(s) patterns") | ||
121 | remote (string.format("str = data:receive(%d)", | 120 | remote (string.format("str = data:receive(%d)", |
122 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) | 121 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) |
123 | sent, err = data:send(p1, p2, p3, p4) | 122 | sent, err = data:send(p1, p2, p3, p4) |
@@ -137,7 +136,6 @@ function test_asciiline(len) | |||
137 | str = string.rep("x", math.mod(len, 10)) | 136 | str = string.rep("x", math.mod(len, 10)) |
138 | str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) | 137 | str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) |
139 | str = str .. str10 | 138 | str = str .. str10 |
140 | pass(len .. " byte(s) line") | ||
141 | remote "str = data:receive()" | 139 | remote "str = data:receive()" |
142 | sent, err = data:send(str, "\n") | 140 | sent, err = data:send(str, "\n") |
143 | if err then fail(err) end | 141 | if err then fail(err) end |
@@ -156,7 +154,6 @@ function test_rawline(len) | |||
156 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), | 154 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), |
157 | math.floor(len/10)) | 155 | math.floor(len/10)) |
158 | str = str .. str10 | 156 | str = str .. str10 |
159 | pass(len .. " byte(s) line") | ||
160 | remote "str = data:receive()" | 157 | remote "str = data:receive()" |
161 | sent, err = data:send(str, "\n") | 158 | sent, err = data:send(str, "\n") |
162 | if err then fail(err) end | 159 | if err then fail(err) end |
@@ -174,7 +171,6 @@ function test_raw(len) | |||
174 | local s1, s2, back, err | 171 | local s1, s2, back, err |
175 | s1 = string.rep("x", half) | 172 | s1 = string.rep("x", half) |
176 | s2 = string.rep("y", len-half) | 173 | s2 = string.rep("y", len-half) |
177 | pass(len .. " byte(s) block") | ||
178 | remote (string.format("str = data:receive(%d)", len)) | 174 | remote (string.format("str = data:receive(%d)", len)) |
179 | sent, err = data:send(s1) | 175 | sent, err = data:send(s1) |
180 | if err then fail(err) end | 176 | if err then fail(err) end |
@@ -271,12 +267,10 @@ end | |||
271 | function empty_connect() | 267 | function empty_connect() |
272 | reconnect() | 268 | reconnect() |
273 | if data then data:close() data = nil end | 269 | if data then data:close() data = nil end |
274 | print("before remote") | ||
275 | remote [[ | 270 | remote [[ |
276 | if data then data:close() data = nil end | 271 | if data then data:close() data = nil end |
277 | data = server:accept() | 272 | data = server:accept() |
278 | ]] | 273 | ]] |
279 | print("after remote") | ||
280 | data, err = socket.connect("", port) | 274 | data, err = socket.connect("", port) |
281 | if not data then | 275 | if not data then |
282 | pass("ok") | 276 | pass("ok") |
@@ -286,7 +280,7 @@ end | |||
286 | 280 | ||
287 | ------------------------------------------------------------------------ | 281 | ------------------------------------------------------------------------ |
288 | function isclosed(c) | 282 | function isclosed(c) |
289 | return c:fd() == -1 or c:fd() == (2^32-1) | 283 | return c:getfd() == -1 or c:getfd() == (2^32-1) |
290 | end | 284 | end |
291 | 285 | ||
292 | function active_close() | 286 | function active_close() |
@@ -354,13 +348,14 @@ end | |||
354 | 348 | ||
355 | ------------------------------------------------------------------------ | 349 | ------------------------------------------------------------------------ |
356 | function accept_timeout() | 350 | function accept_timeout() |
351 | io.write("accept with timeout (if it hangs, it failed): ") | ||
357 | local s, e = socket.bind("*", 0, 0) | 352 | local s, e = socket.bind("*", 0, 0) |
358 | assert(s, e) | 353 | assert(s, e) |
359 | local t = socket.time() | 354 | local t = socket.time() |
360 | s:settimeout(1) | 355 | s:settimeout(1) |
361 | local c, e = s:accept() | 356 | local c, e = s:accept() |
362 | assert(not c, "should not accept") | 357 | assert(not c, "should not accept") |
363 | assert(e == "timeout", "wrong error message") | 358 | assert(e == "timeout", string.format("wrong error message (%s)", e)) |
364 | t = socket.time() - t | 359 | t = socket.time() - t |
365 | assert(t < 2, string.format("took to long to give up (%gs)", t)) | 360 | assert(t < 2, string.format("took to long to give up (%gs)", t)) |
366 | s:close() | 361 | s:close() |
@@ -369,22 +364,51 @@ end | |||
369 | 364 | ||
370 | ------------------------------------------------------------------------ | 365 | ------------------------------------------------------------------------ |
371 | function connect_timeout() | 366 | function connect_timeout() |
367 | io.write("connect with timeout (if it hangs, it failed): ") | ||
372 | local t = socket.time() | 368 | local t = socket.time() |
373 | local c, e = socket.tcp() | 369 | local c, e = socket.tcp() |
374 | assert(c, e) | 370 | assert(c, e) |
375 | c:settimeout(0.1) | 371 | c:settimeout(0.1) |
376 | local r, e = c:connect("ibere.tecgraf.puc-rio.br", 80) | 372 | local r, e = c:connect("ibere.tecgraf.puc-rio.br", 80) |
377 | if r or e ~= "timeout" then | 373 | assert(not r, "should not connect") |
378 | print("wrong error message (this test is flaky anyways)") | 374 | assert(e == "timeout", e) |
379 | end | 375 | assert(socket.time() - t < 2, "took to long to give up") |
380 | if socket.time() - t > 1 then | ||
381 | print("took to long to give up") | ||
382 | end | ||
383 | print("whatever") | ||
384 | c:close() | 376 | c:close() |
385 | end | 377 | end |
386 | 378 | ||
387 | ------------------------------------------------------------------------ | 379 | ------------------------------------------------------------------------ |
380 | function accept_errors() | ||
381 | io.write("not listenning: ") | ||
382 | local d, e = socket.bind("*", 0) | ||
383 | assert(d, e); | ||
384 | local c, e = socket.tcp(); | ||
385 | assert(c, e); | ||
386 | d:setfd(c:getfd()) | ||
387 | local r, e = d:accept() | ||
388 | assert(not r and e == "not listening", e) | ||
389 | print("ok") | ||
390 | io.write("not supported: ") | ||
391 | local c, e = socket.udp() | ||
392 | assert(c, e); | ||
393 | d:setfd(c:getfd()) | ||
394 | local r, e = d:accept() | ||
395 | assert(not r and e == "not supported", e) | ||
396 | print("ok") | ||
397 | end | ||
398 | |||
399 | ------------------------------------------------------------------------ | ||
400 | function connect_errors() | ||
401 | io.write("connection refused: ") | ||
402 | local c, e = socket.connect("localhost", 1); | ||
403 | assert(not c and e == "connection refused", e) | ||
404 | print("ok") | ||
405 | io.write("host not found: ") | ||
406 | local c, e = socket.connect("not.exist.com", 1); | ||
407 | assert(not c and e == "host not found", e) | ||
408 | print("ok") | ||
409 | end | ||
410 | |||
411 | ------------------------------------------------------------------------ | ||
388 | function rebind_test() | 412 | function rebind_test() |
389 | local c = socket.bind("localhost", 0) | 413 | local c = socket.bind("localhost", 0) |
390 | local i, p = c:getsockname() | 414 | local i, p = c:getsockname() |
@@ -400,40 +424,44 @@ end | |||
400 | ------------------------------------------------------------------------ | 424 | ------------------------------------------------------------------------ |
401 | test("method registration") | 425 | test("method registration") |
402 | test_methods(socket.tcp(), { | 426 | test_methods(socket.tcp(), { |
403 | "connect", | 427 | "accept", |
404 | "send", | ||
405 | "receive", | ||
406 | "bind", | 428 | "bind", |
407 | "accept", | 429 | "close", |
408 | "setpeername", | 430 | "connect", |
409 | "setsockname", | ||
410 | "getpeername", | 431 | "getpeername", |
411 | "getsockname", | 432 | "getsockname", |
433 | "listen", | ||
434 | "receive", | ||
435 | "send", | ||
412 | "setoption", | 436 | "setoption", |
437 | "setpeername", | ||
438 | "setsockname", | ||
413 | "settimeout", | 439 | "settimeout", |
414 | "shutdown", | 440 | "shutdown", |
415 | "close", | ||
416 | }) | 441 | }) |
442 | |||
417 | test_methods(socket.udp(), { | 443 | test_methods(socket.udp(), { |
444 | "close", | ||
418 | "getpeername", | 445 | "getpeername", |
419 | "getsockname", | 446 | "getsockname", |
420 | "setsockname", | ||
421 | "setpeername", | ||
422 | "send", | ||
423 | "sendto", | ||
424 | "receive", | 447 | "receive", |
425 | "receivefrom", | 448 | "receivefrom", |
449 | "send", | ||
450 | "sendto", | ||
426 | "setoption", | 451 | "setoption", |
452 | "setpeername", | ||
453 | "setsockname", | ||
427 | "settimeout", | 454 | "settimeout", |
428 | "shutdown", | 455 | "shutdown", |
429 | "close", | ||
430 | }) | 456 | }) |
431 | 457 | ||
432 | test("select function") | 458 | test("select function") |
433 | test_selectbugs() | 459 | test_selectbugs() |
434 | 460 | ||
435 | test("empty host connect: ") | 461 | test("connect function") |
462 | connect_timeout() | ||
436 | empty_connect() | 463 | empty_connect() |
464 | connect_errors() | ||
437 | 465 | ||
438 | test("rebinding: ") | 466 | test("rebinding: ") |
439 | rebind_test() | 467 | rebind_test() |
@@ -444,11 +472,10 @@ active_close() | |||
444 | test("closed connection detection: ") | 472 | test("closed connection detection: ") |
445 | test_closed() | 473 | test_closed() |
446 | 474 | ||
447 | test("accept with timeout (if it hangs, it failed:)") | 475 | test("accept function: ") |
448 | accept_timeout() | 476 | accept_timeout() |
477 | accept_errors() | ||
449 | 478 | ||
450 | test("connect with timeout (if it hangs, it failed:)") | ||
451 | connect_timeout() | ||
452 | 479 | ||
453 | test("mixed patterns") | 480 | test("mixed patterns") |
454 | test_mixed(1) | 481 | test_mixed(1) |