aboutsummaryrefslogtreecommitdiff
path: root/test/testclnt.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/testclnt.lua')
-rw-r--r--test/testclnt.lua372
1 files changed, 184 insertions, 188 deletions
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
67end 67end
68 68
69if not socket.debug then
70 fail("Please define LUASOCKET_DEBUG and recompile LuaSocket")
71end
72
69io.write("----------------------------------------------\n", 73io.write("----------------------------------------------\n",
70"LuaSocket Test Procedures\n", 74"LuaSocket Test Procedures\n",
71"----------------------------------------------\n") 75"----------------------------------------------\n")
72 76
73if not socket._time or not socket._sleep then 77start = socket.time()
74 fail("not compiled with _DEBUG")
75end
76
77start = socket._time()
78 78
79function tcpreconnect() 79function 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
89end 89end
90reconnect = tcpreconnect
91 90
92pass("attempting control connection...") 91pass("attempting control connection...")
93control, err = socket.connect(host, port) 92control, err = socket.connect(host, port)
@@ -95,101 +94,78 @@ if err then fail(err)
95else pass("connected!") end 94else pass("connected!") end
96 95
97------------------------------------------------------------------------ 96------------------------------------------------------------------------
98test("bugs")
99
100io.write("empty host connect: ")
101function 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
112end
113
114empty_connect()
115
116io.write("active close: ")
117function 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")
128end
129
130active_close()
131
132------------------------------------------------------------------------
133test("method registration") 97test("method registration")
134 98
135function test_methods(sock, methods) 99function 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")
142end 106end
143 107
144test_methods(control, { 108test_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
153if udpsocket then 122test_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 })
166end
167
168test_methods(socket.bind("*", 0), {
169 "close",
170 "timeout", 131 "timeout",
171 "accept" 132 "close",
172}) 133})
173 134
174------------------------------------------------------------------------ 135------------------------------------------------------------------------
175test("select function") 136test("mixed patterns")
176function test_selectbugs() 137
177 local r, s, e = socket.select(nil, nil, 0.1) 138function 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) 147remote (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
151remote "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
190end 157end
191 158
192test_selectbugs() 159
160test_mixed(1)
161test_mixed(17)
162test_mixed(200)
163test_mixed(4091)
164test_mixed(80199)
165test_mixed(4091)
166test_mixed(200)
167test_mixed(17)
168test_mixed(1)
193 169
194------------------------------------------------------------------------ 170------------------------------------------------------------------------
195test("character line") 171test("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")
204remote "str = data:receive()" 180remote "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
207remote "data:send(str, '\\n')" 183remote "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")
232remote "str = data:receive()" 208remote "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
235remote "data:send(str, '\\n')" 211remote "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")
264remote (string.format("str = data:receive(%d)", len)) 240remote (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
269remote "data:send(str)" 245remote "data:send(str)"
270 back, err = data:receive(len) 246 back, err = data:receive(len)
@@ -304,127 +280,54 @@ test_raw(17)
304test_raw(1) 280test_raw(1)
305 281
306------------------------------------------------------------------------ 282------------------------------------------------------------------------
307test("mixed patterns") 283test("total timeout on receive")
308reconnect() 284function test_totaltimeoutreceive(len, tm, sl)
309
310function 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")
317remote (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
321remote "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
327end
328
329test_mixed(1)
330test_mixed(17)
331test_mixed(200)
332test_mixed(4091)
333test_mixed(80199)
334test_mixed(800000)
335test_mixed(80199)
336test_mixed(4091)
337test_mixed(200)
338test_mixed(17)
339test_mixed(1)
340
341------------------------------------------------------------------------
342test("closed connection detection")
343
344function 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
368pass("failed: output buffer is at least %d bytes long!", total)
369 elseif err ~= "closed" then
370fail("got '"..err.."' instead of 'closed'.")
371 else
372pass("graceful 'closed' received after %d bytes were sent", total)
373 end
374end
375
376test_closed()
377
378------------------------------------------------------------------------
379test("return timeout on receive")
380function 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)
397end 301end
398test_blockingtimeoutreceive(800091, 1, 3) 302test_totaltimeoutreceive(800091, 1, 3)
399test_blockingtimeoutreceive(800091, 2, 3) 303test_totaltimeoutreceive(800091, 2, 3)
400test_blockingtimeoutreceive(800091, 3, 2) 304test_totaltimeoutreceive(800091, 3, 2)
401test_blockingtimeoutreceive(800091, 3, 1) 305test_totaltimeoutreceive(800091, 3, 1)
402 306
403------------------------------------------------------------------------ 307------------------------------------------------------------------------
404test("return timeout on send") 308test("total timeout on send")
405function test_returntimeoutsend(len, tm, sl) 309function 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)
422end 326end
423test_returntimeoutsend(800091, 1, 3) 327test_totaltimeoutsend(800091, 1, 3)
424test_returntimeoutsend(800091, 2, 3) 328test_totaltimeoutsend(800091, 2, 3)
425test_returntimeoutsend(800091, 3, 2) 329test_totaltimeoutsend(800091, 3, 2)
426test_returntimeoutsend(800091, 3, 1) 330test_totaltimeoutsend(800091, 3, 1)
427
428 331
429------------------------------------------------------------------------ 332------------------------------------------------------------------------
430test("blocking timeout on receive") 333test("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)
451test_blockingtimeoutreceive(800091, 3, 2) 354test_blockingtimeoutreceive(800091, 3, 2)
452test_blockingtimeoutreceive(800091, 3, 1) 355test_blockingtimeoutreceive(800091, 3, 1)
453 356
454
455------------------------------------------------------------------------ 357------------------------------------------------------------------------
456test("blocking timeout on send") 358test("blocking timeout on send")
457function test_blockingtimeoutsend(len, tm, sl) 359function 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)
474end 376end
@@ -478,4 +380,98 @@ test_blockingtimeoutsend(800091, 3, 2)
478test_blockingtimeoutsend(800091, 3, 1) 380test_blockingtimeoutsend(800091, 3, 1)
479 381
480------------------------------------------------------------------------ 382------------------------------------------------------------------------
481test(string.format("done in %.2fs", socket._time() - start)) 383test("bugs")
384
385io.write("empty host connect: ")
386function 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
397end
398
399empty_connect()
400
401-- io.write("active close: ")
402function 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")
413end
414
415-- active_close()
416
417------------------------------------------------------------------------
418test("closed connection detection")
419
420function 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
444pass("failed: output buffer is at least %d bytes long!", total)
445 elseif err ~= "closed" then
446fail("got '"..err.."' instead of 'closed'.")
447 else
448pass("graceful 'closed' received after %d bytes were sent", total)
449 end
450end
451
452test_closed()
453
454------------------------------------------------------------------------
455test("select function")
456function 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")
470end
471
472-- test_selectbugs()
473
474
475
476
477test(string.format("done in %.2fs", socket.time() - start))