aboutsummaryrefslogtreecommitdiff
path: root/test/testclnt.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/testclnt.lua')
-rw-r--r--test/testclnt.lua122
1 files changed, 75 insertions, 47 deletions
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 1f03b10..4c2f211 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -3,15 +3,20 @@ local socket = require"socket"
3host = host or "localhost" 3host = host or "localhost"
4port = port or "8383" 4port = port or "8383"
5 5
6function pass(...) 6function printf(...)
7 local s = string.format(unpack(arg)) 7 local s = string.format(unpack(arg))
8 io.stderr:write(s, "\n") 8 io.stderr:write(s)
9end
10
11function pass(...)
12 printf(...)
13 io.stderr:write("\n")
9end 14end
10 15
11function fail(...) 16function fail(...)
12 local s = string.format(unpack(arg)) 17 io.stderr:write("ERROR: ")
13 io.stderr:write("ERROR: ", s, "!\n") 18 printf(...)
14socket.sleep(3) 19 io.stderr:write("!\n")
15 os.exit() 20 os.exit()
16end 21end
17 22
@@ -80,7 +85,6 @@ io.stderr:write("----------------------------------------------\n",
80start = socket.gettime() 85start = socket.gettime()
81 86
82function reconnect() 87function reconnect()
83 io.stderr:write("attempting data connection... ")
84 if data then data:close() end 88 if data then data:close() end
85 remote [[ 89 remote [[
86 if data then data:close() data = nil end 90 if data then data:close() data = nil end
@@ -88,12 +92,11 @@ function reconnect()
88 data:setoption("tcp-nodelay", true) 92 data:setoption("tcp-nodelay", true)
89 ]] 93 ]]
90 data, err = socket.connect(host, port) 94 data, err = socket.connect(host, port)
91 if not data then fail(err) 95 if not data then fail(err) end
92 else pass("connected!") end
93 data:setoption("tcp-nodelay", true) 96 data:setoption("tcp-nodelay", true)
94end 97end
95 98
96pass("attempting control connection...") 99printf("attempting control connection...")
97control, err = socket.connect(host, port) 100control, err = socket.connect(host, port)
98if err then fail(err) 101if err then fail(err)
99else pass("connected!") end 102else pass("connected!") end
@@ -112,6 +115,7 @@ end
112------------------------------------------------------------------------ 115------------------------------------------------------------------------
113function test_mixed(len) 116function test_mixed(len)
114 reconnect() 117 reconnect()
118 io.stderr:write("length " .. len .. ": ")
115 local inter = math.ceil(len/4) 119 local inter = math.ceil(len/4)
116 local p1 = "unix " .. string.rep("x", inter) .. "line\n" 120 local p1 = "unix " .. string.rep("x", inter) .. "line\n"
117 local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" 121 local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
@@ -139,6 +143,7 @@ end
139------------------------------------------------------------------------ 143------------------------------------------------------------------------
140function test_asciiline(len) 144function test_asciiline(len)
141 reconnect() 145 reconnect()
146 io.stderr:write("length " .. len .. ": ")
142 local str, str10, back, err 147 local str, str10, back, err
143 str = string.rep("x", math.mod(len, 10)) 148 str = string.rep("x", math.mod(len, 10))
144 str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) 149 str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
@@ -156,6 +161,7 @@ end
156------------------------------------------------------------------------ 161------------------------------------------------------------------------
157function test_rawline(len) 162function test_rawline(len)
158 reconnect() 163 reconnect()
164 io.stderr:write("length " .. len .. ": ")
159 local str, str10, back, err 165 local str, str10, back, err
160 str = string.rep(string.char(47), math.mod(len, 10)) 166 str = string.rep(string.char(47), math.mod(len, 10))
161 str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), 167 str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
@@ -174,6 +180,7 @@ end
174------------------------------------------------------------------------ 180------------------------------------------------------------------------
175function test_raw(len) 181function test_raw(len)
176 reconnect() 182 reconnect()
183 io.stderr:write("length " .. len .. ": ")
177 local half = math.floor(len/2) 184 local half = math.floor(len/2)
178 local s1, s2, back, err 185 local s1, s2, back, err
179 s1 = string.rep("x", half) 186 s1 = string.rep("x", half)
@@ -194,7 +201,7 @@ end
194function test_totaltimeoutreceive(len, tm, sl) 201function test_totaltimeoutreceive(len, tm, sl)
195 reconnect() 202 reconnect()
196 local str, err, partial 203 local str, err, partial
197 pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) 204 printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
198 remote (string.format ([[ 205 remote (string.format ([[
199 data:settimeout(%d) 206 data:settimeout(%d)
200 str = string.rep('a', %d) 207 str = string.rep('a', %d)
@@ -215,7 +222,7 @@ end
215function test_totaltimeoutsend(len, tm, sl) 222function test_totaltimeoutsend(len, tm, sl)
216 reconnect() 223 reconnect()
217 local str, err, total 224 local str, err, total
218 pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) 225 printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
219 remote (string.format ([[ 226 remote (string.format ([[
220 data:settimeout(%d) 227 data:settimeout(%d)
221 str = data:receive(%d) 228 str = data:receive(%d)
@@ -235,7 +242,7 @@ end
235function test_blockingtimeoutreceive(len, tm, sl) 242function test_blockingtimeoutreceive(len, tm, sl)
236 reconnect() 243 reconnect()
237 local str, err, partial 244 local str, err, partial
238 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) 245 printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
239 remote (string.format ([[ 246 remote (string.format ([[
240 data:settimeout(%d) 247 data:settimeout(%d)
241 str = string.rep('a', %d) 248 str = string.rep('a', %d)
@@ -255,7 +262,7 @@ end
255function test_blockingtimeoutsend(len, tm, sl) 262function test_blockingtimeoutsend(len, tm, sl)
256 reconnect() 263 reconnect()
257 local str, err, total 264 local str, err, total
258 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) 265 printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
259 remote (string.format ([[ 266 remote (string.format ([[
260 data:settimeout(%d) 267 data:settimeout(%d)
261 str = data:receive(%d) 268 str = data:receive(%d)
@@ -273,6 +280,7 @@ end
273 280
274------------------------------------------------------------------------ 281------------------------------------------------------------------------
275function empty_connect() 282function empty_connect()
283 printf("empty connect: ")
276 reconnect() 284 reconnect()
277 if data then data:close() data = nil end 285 if data then data:close() data = nil end
278 remote [[ 286 remote [[
@@ -284,7 +292,7 @@ function empty_connect()
284 pass("ok") 292 pass("ok")
285 data = socket.connect(host, port) 293 data = socket.connect(host, port)
286 else 294 else
287 pass("gethostbyname returns localhost on empty string...") 295 pass("gethostbyname returns localhost on empty string...")
288 end 296 end
289end 297end
290 298
@@ -311,7 +319,7 @@ function test_closed()
311 local back, partial, err 319 local back, partial, err
312 local str = 'little string' 320 local str = 'little string'
313 reconnect() 321 reconnect()
314 pass("trying read detection") 322 printf("trying read detection: ")
315 remote (string.format ([[ 323 remote (string.format ([[
316 data:send('%s') 324 data:send('%s')
317 data:close() 325 data:close()
@@ -324,7 +332,7 @@ function test_closed()
324 elseif str ~= partial then fail("didn't receive partial result.") 332 elseif str ~= partial then fail("didn't receive partial result.")
325 else pass("graceful 'closed' received") end 333 else pass("graceful 'closed' received") end
326 reconnect() 334 reconnect()
327 pass("trying write detection") 335 printf("trying write detection: ")
328 remote [[ 336 remote [[
329 data:close() 337 data:close()
330 data = nil 338 data = nil
@@ -342,7 +350,6 @@ end
342------------------------------------------------------------------------ 350------------------------------------------------------------------------
343function test_selectbugs() 351function test_selectbugs()
344 local r, s, e = socket.select(nil, nil, 0.1) 352 local r, s, e = socket.select(nil, nil, 0.1)
345print(r, s, e)
346 assert(type(r) == "table" and type(s) == "table" and 353 assert(type(r) == "table" and type(s) == "table" and
347 (e == "timeout" or e == "error")) 354 (e == "timeout" or e == "error"))
348 pass("both nil: ok") 355 pass("both nil: ok")
@@ -374,7 +381,7 @@ end
374 381
375------------------------------------------------------------------------ 382------------------------------------------------------------------------
376function accept_timeout() 383function accept_timeout()
377 io.stderr:write("accept with timeout (if it hangs, it failed): ") 384 printf("accept with timeout (if it hangs, it failed): ")
378 local s, e = socket.bind("*", 0, 0) 385 local s, e = socket.bind("*", 0, 0)
379 assert(s, e) 386 assert(s, e)
380 local t = socket.gettime() 387 local t = socket.gettime()
@@ -390,23 +397,22 @@ end
390 397
391------------------------------------------------------------------------ 398------------------------------------------------------------------------
392function connect_timeout() 399function connect_timeout()
393 io.stderr:write("connect with timeout (if it hangs, it failed!): ") 400 printf("connect with timeout (if it hangs, it failed!): ")
394 local t = socket.gettime() 401 local t = socket.gettime()
395 local c, e = socket.tcp() 402 local c, e = socket.tcp()
396 assert(c, e) 403 assert(c, e)
397 c:settimeout(0.1) 404 c:settimeout(0.1)
398 local t = socket.gettime() 405 local t = socket.gettime()
399 local r, e = c:connect("10.0.0.1", 81) 406 local r, e = c:connect("10.0.0.1", 81)
400print(r, e)
401 assert(not r, "should not connect") 407 assert(not r, "should not connect")
402 assert(socket.gettime() - t < 2, "took too long to give up.") 408 assert(socket.gettime() - t < 2, "took too long to give up.")
403 c:close() 409 c:close()
404 print("ok") 410 pass("ok")
405end 411end
406 412
407------------------------------------------------------------------------ 413------------------------------------------------------------------------
408function accept_errors() 414function accept_errors()
409 io.stderr:write("not listening: ") 415 printf("not listening: ")
410 local d, e = socket.bind("*", 0) 416 local d, e = socket.bind("*", 0)
411 assert(d, e); 417 assert(d, e);
412 local c, e = socket.tcp(); 418 local c, e = socket.tcp();
@@ -415,26 +421,26 @@ function accept_errors()
415 d:settimeout(2) 421 d:settimeout(2)
416 local r, e = d:accept() 422 local r, e = d:accept()
417 assert(not r and e) 423 assert(not r and e)
418 print("ok: ", e) 424 pass("ok")
419 io.stderr:write("not supported: ") 425 printf("not supported: ")
420 local c, e = socket.udp() 426 local c, e = socket.udp()
421 assert(c, e); 427 assert(c, e);
422 d:setfd(c:getfd()) 428 d:setfd(c:getfd())
423 local r, e = d:accept() 429 local r, e = d:accept()
424 assert(not r and e) 430 assert(not r and e)
425 print("ok: ", e) 431 pass("ok")
426end 432end
427 433
428------------------------------------------------------------------------ 434------------------------------------------------------------------------
429function connect_errors() 435function connect_errors()
430 io.stderr:write("connection refused: ") 436 printf("connection refused: ")
431 local c, e = socket.connect("localhost", 1); 437 local c, e = socket.connect("localhost", 1);
432 assert(not c and e) 438 assert(not c and e)
433 print("ok: ", e) 439 pass("ok")
434 io.stderr:write("host not found: ") 440 printf("host not found: ")
435 local c, e = socket.connect("host.is.invalid", 1); 441 local c, e = socket.connect("host.is.invalid", 1);
436 assert(not c and e, e) 442 assert(not c and e, e)
437 print("ok: ", e) 443 pass("ok")
438end 444end
439 445
440------------------------------------------------------------------------ 446------------------------------------------------------------------------
@@ -447,7 +453,7 @@ function rebind_test()
447 r, e = s:bind("localhost", p) 453 r, e = s:bind("localhost", p)
448 assert(not r, "managed to rebind!") 454 assert(not r, "managed to rebind!")
449 assert(e) 455 assert(e)
450 print("ok: ", e) 456 pass("ok")
451end 457end
452 458
453------------------------------------------------------------------------ 459------------------------------------------------------------------------
@@ -469,14 +475,14 @@ function getstats_test()
469 assert(s == t, "sent count failed" .. tostring(s) 475 assert(s == t, "sent count failed" .. tostring(s)
470 .. "/" .. tostring(t)) 476 .. "/" .. tostring(t))
471 end 477 end
472 print("ok") 478 pass("ok")
473end 479end
474 480
475 481
476------------------------------------------------------------------------ 482------------------------------------------------------------------------
477function test_nonblocking(size) 483function test_nonblocking(size)
478 reconnect() 484 reconnect()
479print("Testing " .. 2*size .. " bytes") 485 printf("testing " .. 2*size .. " bytes: ")
480remote(string.format([[ 486remote(string.format([[
481 data:send(string.rep("a", %d)) 487 data:send(string.rep("a", %d))
482 socket.sleep(0.5) 488 socket.sleep(0.5)
@@ -508,7 +514,7 @@ remote(string.format([[
508 data:settimeout(-1) 514 data:settimeout(-1)
509 local back = data:receive(2*size) 515 local back = data:receive(2*size)
510 assert(back == str, "'" .. back .. "' vs '" .. str .. "'") 516 assert(back == str, "'" .. back .. "' vs '" .. str .. "'")
511 print("ok") 517 pass("ok")
512end 518end
513 519
514------------------------------------------------------------------------ 520------------------------------------------------------------------------
@@ -516,7 +522,7 @@ function test_readafterclose()
516 local back, partial, err 522 local back, partial, err
517 local str = 'little string' 523 local str = 'little string'
518 reconnect() 524 reconnect()
519 pass("trying repeated '*a' pattern") 525 printf("trying repeated '*a' pattern")
520 remote (string.format ([[ 526 remote (string.format ([[
521 data:send('%s') 527 data:send('%s')
522 data:close() 528 data:close()
@@ -526,9 +532,9 @@ function test_readafterclose()
526 assert(back == str, "unexpected data read") 532 assert(back == str, "unexpected data read")
527 back, err, partial = data:receive("*a") 533 back, err, partial = data:receive("*a")
528 assert(back == nil and err == "closed", "should have returned 'closed'") 534 assert(back == nil and err == "closed", "should have returned 'closed'")
529 print("ok") 535 pass("ok")
530 reconnect() 536 reconnect()
531 pass("trying active close before '*a'") 537 printf("trying active close before '*a'")
532 remote (string.format ([[ 538 remote (string.format ([[
533 data:close() 539 data:close()
534 data = nil 540 data = nil
@@ -536,9 +542,9 @@ function test_readafterclose()
536 data:close() 542 data:close()
537 back, err, partial = data:receive("*a") 543 back, err, partial = data:receive("*a")
538 assert(back == nil and err == "closed", "should have returned 'closed'") 544 assert(back == nil and err == "closed", "should have returned 'closed'")
539 print("ok") 545 pass("ok")
540 reconnect() 546 reconnect()
541 pass("trying active close before '*l'") 547 printf("trying active close before '*l'")
542 remote (string.format ([[ 548 remote (string.format ([[
543 data:close() 549 data:close()
544 data = nil 550 data = nil
@@ -546,9 +552,9 @@ function test_readafterclose()
546 data:close() 552 data:close()
547 back, err, partial = data:receive() 553 back, err, partial = data:receive()
548 assert(back == nil and err == "closed", "should have returned 'closed'") 554 assert(back == nil and err == "closed", "should have returned 'closed'")
549 print("ok") 555 pass("ok")
550 reconnect() 556 reconnect()
551 pass("trying active close before raw 1") 557 printf("trying active close before raw 1")
552 remote (string.format ([[ 558 remote (string.format ([[
553 data:close() 559 data:close()
554 data = nil 560 data = nil
@@ -556,9 +562,9 @@ function test_readafterclose()
556 data:close() 562 data:close()
557 back, err, partial = data:receive(1) 563 back, err, partial = data:receive(1)
558 assert(back == nil and err == "closed", "should have returned 'closed'") 564 assert(back == nil and err == "closed", "should have returned 'closed'")
559 print("ok") 565 pass("ok")
560 reconnect() 566 reconnect()
561 pass("trying active close before raw 0") 567 printf("trying active close before raw 0")
562 remote (string.format ([[ 568 remote (string.format ([[
563 data:close() 569 data:close()
564 data = nil 570 data = nil
@@ -566,7 +572,7 @@ function test_readafterclose()
566 data:close() 572 data:close()
567 back, err, partial = data:receive(0) 573 back, err, partial = data:receive(0)
568 assert(back == nil and err == "closed", "should have returned 'closed'") 574 assert(back == nil and err == "closed", "should have returned 'closed'")
569 print("ok") 575 pass("ok")
570end 576end
571 577
572------------------------------------------------------------------------ 578------------------------------------------------------------------------
@@ -581,13 +587,29 @@ function test_writeafterclose()
581 while not err do 587 while not err do
582 sent, err, errsent, time = data:send(str) 588 sent, err, errsent, time = data:send(str)
583 end 589 end
584 print(sent, err, errsent, time) 590 assert(err == "closed", "should have returned 'closed'")
585 print("ok") 591 pass("ok")
586end 592end
587 593
588------------------------------------------------------------------------ 594------------------------------------------------------------------------
589--test_writeafterclose()
590 595
596function test_partialrecv()
597 local str = 'little string'
598 reconnect()
599remote([[
600 data:send("7890")
601]])
602 data:settimeout(1)
603 back, err = data:receive(10, "123456")
604 assert(back == "1234567890", "failed on exact mixed length")
605 back, err = data:receive(8, "87654321")
606 assert(back == "87654321", "failed on exact length")
607 back, err = data:receive(4, "87654321")
608 assert(back == "87654321", "failed on smaller length")
609 pass("ok")
610end
611
612------------------------------------------------------------------------
591test("method registration") 613test("method registration")
592test_methods(socket.tcp(), { 614test_methods(socket.tcp(), {
593 "accept", 615 "accept",
@@ -629,12 +651,18 @@ test_methods(socket.udp(), {
629 "settimeout" 651 "settimeout"
630}) 652})
631 653
654test("partial receive")
655test_partialrecv()
656
632test("select function") 657test("select function")
633test_selectbugs() 658test_selectbugs()
634 659
635test("testing read after close") 660test("read after close")
636test_readafterclose() 661test_readafterclose()
637 662
663test("write after close")
664test_writeafterclose()
665
638test("connect function") 666test("connect function")
639connect_timeout() 667connect_timeout()
640empty_connect() 668empty_connect()