aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-05-25 01:54:13 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-05-25 01:54:13 +0000
commit0f6c8d50a99997ac7829864b1c93362b50f1bbf3 (patch)
treed0cefe3a05484e65b7b7e79d8cae4a1d2e6d19fb /test
parentc1ef3e7103cc652d2004ef1ddc9409b946207f33 (diff)
downloadluasocket-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.lua6
-rw-r--r--test/httptest.lua15
-rw-r--r--test/smtptest.lua10
-rw-r--r--test/testclnt.lua372
-rw-r--r--test/testsrvr.lua3
-rw-r--r--test/tftptest.lua7
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 @@
1dofile("noglobals.lua")
2
3local similar = function(s1, s2) 1local 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
35local index, err, saved, back, expected 33local index, err, saved, back, expected
36 34
37local t = socket._time() 35local t = socket.time()
38 36
39index = readfile("test/index.html") 37index = readfile("test/index.html")
40 38
@@ -112,4 +110,4 @@ back, err = socket.ftp.get("ftp://localhost/index.wrong.html;type=a")
112check(err, err) 110check(err, err)
113 111
114print("passed all tests") 112print("passed all tests")
115print(string.format("done in %.2fs", socket._time() - t)) 113print(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
7dofile("noglobals.lua")
8
9local similar = function(s1, s2) 6local 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
28local check = function (v, e) 25local 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
31end 28end
32 29
33local check_request = function(request, expect, ignore) 30local 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")
46end 43end
47 44
48local request, response, ignore, expect, index, prefix, cgiprefix 45local host, request, response, ignore, expect, index, prefix, cgiprefix
49 46
50local t = socket._time() 47local t = socket.time()
51 48
52host = host or "localhost" 49host = host or "localhost"
53prefix = prefix or "/luasocket" 50prefix = prefix or "/luasocket"
@@ -310,4 +307,4 @@ check(response and response.headers)
310 307
311print("passed all tests") 308print("passed all tests")
312 309
313print(string.format("done in %.2fs", socket._time() - t)) 310print(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
14local t = socket._time() 14local t = socket.time()
15local err 15local err
16 16
17dofile("mbox.lua") 17dofile("mbox.lua")
@@ -106,7 +106,7 @@ local insert = function(sent, message)
106end 106end
107 107
108local mark = function() 108local mark = function()
109 local time = socket._time() 109 local time = socket.time()
110 return { time = time } 110 return { time = time }
111end 111end
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
256end 256end
257 257
258print("passed all tests") 258print("passed all tests")
259print(string.format("done in %.2fs", socket._time() - t)) 259print(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
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))
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
24end 25end
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
2dofile("tftpclnt.lua") 2dofile("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)
13end 13end
14 14
15host = host or "localhost" 15host = host or "localhost"
16print("downloading") 16retrieved, err = socket.tftp.get("tftp://" .. host .."/index.html")
17err = tftp_get(host, 69, "index.html", "index.got")
18assert(not err, err) 17assert(not err, err)
19original = readfile("test/index.html") 18original = readfile("test/index.html")
20retrieved = readfile("index.got")
21os.remove("index.got")
22assert(original == retrieved, "files differ!") 19assert(original == retrieved, "files differ!")
23print("passed") 20print("passed")