diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-02-08 10:01:01 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-02-08 10:01:01 +0000 |
| commit | 8d4e240f6ae50d9b22ddc44f5e207018935da907 (patch) | |
| tree | d8ca9a51dc35534592f700e42740feac20242ede /test | |
| parent | 5d32848674b723521e87836eafa24f5ae8f80a89 (diff) | |
| download | luasocket-8d4e240f6ae50d9b22ddc44f5e207018935da907.tar.gz luasocket-8d4e240f6ae50d9b22ddc44f5e207018935da907.tar.bz2 luasocket-8d4e240f6ae50d9b22ddc44f5e207018935da907.zip | |
Forward server working on Mac OS X...
Diffstat (limited to 'test')
| -rw-r--r-- | test/dicttest.lua | 2 | ||||
| -rw-r--r-- | test/httptest.lua | 2 | ||||
| -rw-r--r-- | test/testclnt.lua | 16 | ||||
| -rw-r--r-- | test/testsrvr.lua | 1 | ||||
| -rw-r--r-- | test/utestclnt.lua | 644 | ||||
| -rw-r--r-- | test/utestsrvr.lua | 17 |
6 files changed, 671 insertions, 11 deletions
diff --git a/test/dicttest.lua b/test/dicttest.lua index a37ec8d..7ac7811 100644 --- a/test/dicttest.lua +++ b/test/dicttest.lua | |||
| @@ -1,3 +1,3 @@ | |||
| 1 | local dict = require"socket.dict" | 1 | local dict = require"socket.dict" |
| 2 | 2 | ||
| 3 | for i,v in dict.get("dict://dell-diego/d:banana") do print(v) end | 3 | for i,v in dict.get("dict://localhost/d:teste") do print(v) end |
diff --git a/test/httptest.lua b/test/httptest.lua index 8862ceb..2335fcb 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
| @@ -23,7 +23,7 @@ http.TIMEOUT = 10 | |||
| 23 | local t = socket.gettime() | 23 | local t = socket.gettime() |
| 24 | 24 | ||
| 25 | host = host or "diego.student.princeton.edu" | 25 | host = host or "diego.student.princeton.edu" |
| 26 | proxy = proxy or "http://dell-diego:3128" | 26 | proxy = proxy or "http://localhost:3128" |
| 27 | prefix = prefix or "/luasocket-test" | 27 | prefix = prefix or "/luasocket-test" |
| 28 | cgiprefix = cgiprefix or "/luasocket-test-cgi" | 28 | cgiprefix = cgiprefix or "/luasocket-test-cgi" |
| 29 | index_file = "test/index.html" | 29 | index_file = "test/index.html" |
diff --git a/test/testclnt.lua b/test/testclnt.lua index c2c782c..e3f2b94 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
| @@ -465,16 +465,14 @@ print("Testing " .. 2*size .. " bytes") | |||
| 465 | remote(string.format([[ | 465 | remote(string.format([[ |
| 466 | data:send(string.rep("a", %d)) | 466 | data:send(string.rep("a", %d)) |
| 467 | socket.sleep(0.5) | 467 | socket.sleep(0.5) |
| 468 | data:send(string.rep("b", %d)) | 468 | data:send(string.rep("b", %d) .. "\n") |
| 469 | ]], size, size)) | 469 | ]], size, size)) |
| 470 | local err = "timeout" | 470 | local err = "timeout" |
| 471 | local part = "" | 471 | local part = "" |
| 472 | local str | 472 | local str |
| 473 | data:settimeout(0) | 473 | data:settimeout(0) |
| 474 | while 1 do | 474 | while 1 do |
| 475 | local needed = 2*size - string.len(part) | 475 | str, err, part = data:receive("*l", part) |
| 476 | assert(needed > 0, "weird") | ||
| 477 | str, err, part = data:receive(needed, part) | ||
| 478 | if err ~= "timeout" then break end | 476 | if err ~= "timeout" then break end |
| 479 | end | 477 | end |
| 480 | assert(str == (string.rep("a", size) .. string.rep("b", size))) | 478 | assert(str == (string.rep("a", size) .. string.rep("b", size))) |
| @@ -482,15 +480,14 @@ remote(string.format([[ | |||
| 482 | remote(string.format([[ | 480 | remote(string.format([[ |
| 483 | str = data:receive(%d) | 481 | str = data:receive(%d) |
| 484 | socket.sleep(0.5) | 482 | socket.sleep(0.5) |
| 485 | str = data:receive(%d, str) | 483 | str = data:receive(2*%d, str) |
| 486 | data:send(str) | 484 | data:send(str) |
| 487 | ]], size, size)) | 485 | ]], size, size)) |
| 488 | data:settimeout(0) | 486 | data:settimeout(0) |
| 489 | local sofar = 1 | 487 | local start = 0 |
| 490 | while 1 do | 488 | while 1 do |
| 491 | _, err, part = data:send(str, sofar) | 489 | ret, err, start = data:send(str, start+1) |
| 492 | if err ~= "timeout" then break end | 490 | if err ~= "timeout" then break end |
| 493 | sofar = sofar + part | ||
| 494 | end | 491 | end |
| 495 | data:send("\n") | 492 | data:send("\n") |
| 496 | data:settimeout(-1) | 493 | data:settimeout(-1) |
| @@ -501,6 +498,7 @@ end | |||
| 501 | 498 | ||
| 502 | ------------------------------------------------------------------------ | 499 | ------------------------------------------------------------------------ |
| 503 | 500 | ||
| 501 | |||
| 504 | test("method registration") | 502 | test("method registration") |
| 505 | test_methods(socket.tcp(), { | 503 | test_methods(socket.tcp(), { |
| 506 | "accept", | 504 | "accept", |
| @@ -622,7 +620,7 @@ test_nonblocking(17) | |||
| 622 | test_nonblocking(200) | 620 | test_nonblocking(200) |
| 623 | test_nonblocking(4091) | 621 | test_nonblocking(4091) |
| 624 | test_nonblocking(80199) | 622 | test_nonblocking(80199) |
| 625 | test_nonblocking(8000000) | 623 | test_nonblocking(800000) |
| 626 | test_nonblocking(80199) | 624 | test_nonblocking(80199) |
| 627 | test_nonblocking(4091) | 625 | test_nonblocking(4091) |
| 628 | test_nonblocking(200) | 626 | test_nonblocking(200) |
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 2408e83..f1972c2 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
| @@ -9,6 +9,7 @@ while 1 do | |||
| 9 | while 1 do | 9 | while 1 do |
| 10 | command = assert(control:receive()); | 10 | command = assert(control:receive()); |
| 11 | assert(control:send(ack)); | 11 | assert(control:send(ack)); |
| 12 | print(command); | ||
| 12 | (loadstring(command))(); | 13 | (loadstring(command))(); |
| 13 | end | 14 | end |
| 14 | end | 15 | end |
diff --git a/test/utestclnt.lua b/test/utestclnt.lua new file mode 100644 index 0000000..f002c6e --- /dev/null +++ b/test/utestclnt.lua | |||
| @@ -0,0 +1,644 @@ | |||
| 1 | require"socket" | ||
| 2 | local socket = require"socket.unix" | ||
| 3 | |||
| 4 | host = "luasocket" | ||
| 5 | |||
| 6 | function pass(...) | ||
| 7 | local s = string.format(unpack(arg)) | ||
| 8 | io.stderr:write(s, "\n") | ||
| 9 | end | ||
| 10 | |||
| 11 | function fail(...) | ||
| 12 | local s = string.format(unpack(arg)) | ||
| 13 | io.stderr:write("ERROR: ", s, "!\n") | ||
| 14 | socket.sleep(3) | ||
| 15 | os.exit() | ||
| 16 | end | ||
| 17 | |||
| 18 | function warn(...) | ||
| 19 | local s = string.format(unpack(arg)) | ||
| 20 | io.stderr:write("WARNING: ", s, "\n") | ||
| 21 | end | ||
| 22 | |||
| 23 | function remote(...) | ||
| 24 | local s = string.format(unpack(arg)) | ||
| 25 | s = string.gsub(s, "\n", ";") | ||
| 26 | s = string.gsub(s, "%s+", " ") | ||
| 27 | s = string.gsub(s, "^%s*", "") | ||
| 28 | control:send(s .. "\n") | ||
| 29 | control:receive() | ||
| 30 | end | ||
| 31 | |||
| 32 | function test(test) | ||
| 33 | io.stderr:write("----------------------------------------------\n", | ||
| 34 | "testing: ", test, "\n", | ||
| 35 | "----------------------------------------------\n") | ||
| 36 | end | ||
| 37 | |||
| 38 | function uconnect(path) | ||
| 39 | local u = assert(socket.unix()) | ||
| 40 | assert(u:connect(path)) | ||
| 41 | return u | ||
| 42 | end | ||
| 43 | |||
| 44 | function ubind(path) | ||
| 45 | local u = assert(socket.unix()) | ||
| 46 | assert(u:bind(path)) | ||
| 47 | assert(u:listen(5)) | ||
| 48 | return u | ||
| 49 | end | ||
| 50 | |||
| 51 | function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | ||
| 52 | if tm < sl then | ||
| 53 | if opp == "send" then | ||
| 54 | if not err then warn("must be buffered") | ||
| 55 | elseif err == "timeout" then pass("proper timeout") | ||
| 56 | else fail("unexpected error '%s'", err) end | ||
| 57 | else | ||
| 58 | if err ~= "timeout" then fail("should have timed out") | ||
| 59 | else pass("proper timeout") end | ||
| 60 | end | ||
| 61 | else | ||
| 62 | if mode == "total" then | ||
| 63 | if elapsed > tm then | ||
| 64 | if err ~= "timeout" then fail("should have timed out") | ||
| 65 | else pass("proper timeout") end | ||
| 66 | elseif elapsed < tm then | ||
| 67 | if err then fail(err) | ||
| 68 | else pass("ok") end | ||
| 69 | else | ||
| 70 | if alldone then | ||
| 71 | if err then fail("unexpected error '%s'", err) | ||
| 72 | else pass("ok") end | ||
| 73 | else | ||
| 74 | if err ~= "timeout" then fail(err) | ||
| 75 | else pass("proper timeoutk") end | ||
| 76 | end | ||
| 77 | end | ||
| 78 | else | ||
| 79 | if err then fail(err) | ||
| 80 | else pass("ok") end | ||
| 81 | end | ||
| 82 | end | ||
| 83 | end | ||
| 84 | |||
| 85 | if not socket.DEBUG then | ||
| 86 | fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") | ||
| 87 | end | ||
| 88 | |||
| 89 | io.stderr:write("----------------------------------------------\n", | ||
| 90 | "LuaSocket Test Procedures\n", | ||
| 91 | "----------------------------------------------\n") | ||
| 92 | |||
| 93 | start = socket.gettime() | ||
| 94 | |||
| 95 | function reconnect() | ||
| 96 | io.stderr:write("attempting data connection... ") | ||
| 97 | if data then data:close() end | ||
| 98 | remote [[ | ||
| 99 | i = i or 1 | ||
| 100 | if data then data:close() data = nil end | ||
| 101 | print("accepting") | ||
| 102 | data = server:accept() | ||
| 103 | i = i + 1 | ||
| 104 | print("done " .. i) | ||
| 105 | ]] | ||
| 106 | data, err = uconnect(host, port) | ||
| 107 | if not data then fail(err) | ||
| 108 | else pass("connected!") end | ||
| 109 | end | ||
| 110 | |||
| 111 | pass("attempting control connection...") | ||
| 112 | control, err = uconnect(host, port) | ||
| 113 | if err then fail(err) | ||
| 114 | else pass("connected!") end | ||
| 115 | |||
| 116 | ------------------------------------------------------------------------ | ||
| 117 | function test_methods(sock, methods) | ||
| 118 | for _, v in methods do | ||
| 119 | if type(sock[v]) ~= "function" then | ||
| 120 | fail(sock.class .. " method '" .. v .. "' not registered") | ||
| 121 | end | ||
| 122 | end | ||
| 123 | pass(sock.class .. " methods are ok") | ||
| 124 | end | ||
| 125 | |||
| 126 | ------------------------------------------------------------------------ | ||
| 127 | function test_mixed(len) | ||
| 128 | reconnect() | ||
| 129 | local inter = math.ceil(len/4) | ||
| 130 | local p1 = "unix " .. string.rep("x", inter) .. "line\n" | ||
| 131 | local p2 = "dos " .. string.rep("y", inter) .. "line\r\n" | ||
| 132 | local p3 = "raw " .. string.rep("z", inter) .. "bytes" | ||
| 133 | local p4 = "end" .. string.rep("w", inter) .. "bytes" | ||
| 134 | local bp1, bp2, bp3, bp4 | ||
| 135 | remote (string.format("str = data:receive(%d)", | ||
| 136 | string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) | ||
| 137 | sent, err = data:send(p1..p2..p3..p4) | ||
| 138 | if err then fail(err) end | ||
| 139 | remote "data:send(str); data:close()" | ||
| 140 | bp1, err = data:receive() | ||
| 141 | if err then fail(err) end | ||
| 142 | bp2, err = data:receive() | ||
| 143 | if err then fail(err) end | ||
| 144 | bp3, err = data:receive(string.len(p3)) | ||
| 145 | if err then fail(err) end | ||
| 146 | bp4, err = data:receive("*a") | ||
| 147 | if err then fail(err) end | ||
| 148 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then | ||
| 149 | pass("patterns match") | ||
| 150 | else fail("patterns don't match") end | ||
| 151 | end | ||
| 152 | |||
| 153 | ------------------------------------------------------------------------ | ||
| 154 | function test_asciiline(len) | ||
| 155 | reconnect() | ||
| 156 | local str, str10, back, err | ||
| 157 | str = string.rep("x", math.mod(len, 10)) | ||
| 158 | str10 = string.rep("aZb.c#dAe?", math.floor(len/10)) | ||
| 159 | str = str .. str10 | ||
| 160 | remote "str = data:receive()" | ||
| 161 | sent, err = data:send(str.."\n") | ||
| 162 | if err then fail(err) end | ||
| 163 | remote "data:send(str ..'\\n')" | ||
| 164 | back, err = data:receive() | ||
| 165 | if err then fail(err) end | ||
| 166 | if back == str then pass("lines match") | ||
| 167 | else fail("lines don't match") end | ||
| 168 | end | ||
| 169 | |||
| 170 | ------------------------------------------------------------------------ | ||
| 171 | function test_rawline(len) | ||
| 172 | reconnect() | ||
| 173 | local str, str10, back, err | ||
| 174 | str = string.rep(string.char(47), math.mod(len, 10)) | ||
| 175 | str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), | ||
| 176 | math.floor(len/10)) | ||
| 177 | str = str .. str10 | ||
| 178 | remote "str = data:receive()" | ||
| 179 | sent, err = data:send(str.."\n") | ||
| 180 | if err then fail(err) end | ||
| 181 | remote "data:send(str..'\\n')" | ||
| 182 | back, err = data:receive() | ||
| 183 | if err then fail(err) end | ||
| 184 | if back == str then pass("lines match") | ||
| 185 | else fail("lines don't match") end | ||
| 186 | end | ||
| 187 | |||
| 188 | ------------------------------------------------------------------------ | ||
| 189 | function test_raw(len) | ||
| 190 | reconnect() | ||
| 191 | local half = math.floor(len/2) | ||
| 192 | local s1, s2, back, err | ||
| 193 | s1 = string.rep("x", half) | ||
| 194 | s2 = string.rep("y", len-half) | ||
| 195 | remote (string.format("str = data:receive(%d)", len)) | ||
| 196 | sent, err = data:send(s1) | ||
| 197 | if err then fail(err) end | ||
| 198 | sent, err = data:send(s2) | ||
| 199 | if err then fail(err) end | ||
| 200 | remote "data:send(str)" | ||
| 201 | back, err = data:receive(len) | ||
| 202 | if err then fail(err) end | ||
| 203 | if back == s1..s2 then pass("blocks match") | ||
| 204 | else fail("blocks don't match") end | ||
| 205 | end | ||
| 206 | |||
| 207 | ------------------------------------------------------------------------ | ||
| 208 | function test_totaltimeoutreceive(len, tm, sl) | ||
| 209 | reconnect() | ||
| 210 | local str, err, partial | ||
| 211 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) | ||
| 212 | remote (string.format ([[ | ||
| 213 | data:settimeout(%d) | ||
| 214 | str = string.rep('a', %d) | ||
| 215 | data:send(str) | ||
| 216 | print('server: sleeping for %ds') | ||
| 217 | socket.sleep(%d) | ||
| 218 | print('server: woke up') | ||
| 219 | data:send(str) | ||
| 220 | ]], 2*tm, len, sl, sl)) | ||
| 221 | data:settimeout(tm, "total") | ||
| 222 | local t = socket.gettime() | ||
| 223 | str, err, partial, elapsed = data:receive(2*len) | ||
| 224 | check_timeout(tm, sl, elapsed, err, "receive", "total", | ||
| 225 | string.len(str or partial) == 2*len) | ||
| 226 | end | ||
| 227 | |||
| 228 | ------------------------------------------------------------------------ | ||
| 229 | function test_totaltimeoutsend(len, tm, sl) | ||
| 230 | reconnect() | ||
| 231 | local str, err, total | ||
| 232 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) | ||
| 233 | remote (string.format ([[ | ||
| 234 | data:settimeout(%d) | ||
| 235 | str = data:receive(%d) | ||
| 236 | print('server: sleeping for %ds') | ||
| 237 | socket.sleep(%d) | ||
| 238 | print('server: woke up') | ||
| 239 | str = data:receive(%d) | ||
| 240 | ]], 2*tm, len, sl, sl, len)) | ||
| 241 | data:settimeout(tm, "total") | ||
| 242 | str = string.rep("a", 2*len) | ||
| 243 | total, err, partial, elapsed = data:send(str) | ||
| 244 | check_timeout(tm, sl, elapsed, err, "send", "total", | ||
| 245 | total == 2*len) | ||
| 246 | end | ||
| 247 | |||
| 248 | ------------------------------------------------------------------------ | ||
| 249 | function test_blockingtimeoutreceive(len, tm, sl) | ||
| 250 | reconnect() | ||
| 251 | local str, err, partial | ||
| 252 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | ||
| 253 | remote (string.format ([[ | ||
| 254 | data:settimeout(%d) | ||
| 255 | str = string.rep('a', %d) | ||
| 256 | data:send(str) | ||
| 257 | print('server: sleeping for %ds') | ||
| 258 | socket.sleep(%d) | ||
| 259 | print('server: woke up') | ||
| 260 | data:send(str) | ||
| 261 | ]], 2*tm, len, sl, sl)) | ||
| 262 | data:settimeout(tm) | ||
| 263 | str, err, partial, elapsed = data:receive(2*len) | ||
| 264 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", | ||
| 265 | string.len(str or partial) == 2*len) | ||
| 266 | end | ||
| 267 | |||
| 268 | ------------------------------------------------------------------------ | ||
| 269 | function test_blockingtimeoutsend(len, tm, sl) | ||
| 270 | reconnect() | ||
| 271 | local str, err, total | ||
| 272 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | ||
| 273 | remote (string.format ([[ | ||
| 274 | data:settimeout(%d) | ||
| 275 | str = data:receive(%d) | ||
| 276 | print('server: sleeping for %ds') | ||
| 277 | socket.sleep(%d) | ||
| 278 | print('server: woke up') | ||
| 279 | str = data:receive(%d) | ||
| 280 | ]], 2*tm, len, sl, sl, len)) | ||
| 281 | data:settimeout(tm) | ||
| 282 | str = string.rep("a", 2*len) | ||
| 283 | total, err, partial, elapsed = data:send(str) | ||
| 284 | check_timeout(tm, sl, elapsed, err, "send", "blocking", | ||
| 285 | total == 2*len) | ||
| 286 | end | ||
| 287 | |||
| 288 | ------------------------------------------------------------------------ | ||
| 289 | function empty_connect() | ||
| 290 | reconnect() | ||
| 291 | if data then data:close() data = nil end | ||
| 292 | remote [[ | ||
| 293 | if data then data:close() data = nil end | ||
| 294 | data = server:accept() | ||
| 295 | ]] | ||
| 296 | data, err = socket.connect("", port) | ||
| 297 | if not data then | ||
| 298 | pass("ok") | ||
| 299 | data = socket.connect(host, port) | ||
| 300 | else | ||
| 301 | pass("gethostbyname returns localhost on empty string...") | ||
| 302 | end | ||
| 303 | end | ||
| 304 | |||
| 305 | ------------------------------------------------------------------------ | ||
| 306 | function isclosed(c) | ||
| 307 | return c:getfd() == -1 or c:getfd() == (2^32-1) | ||
| 308 | end | ||
| 309 | |||
| 310 | function active_close() | ||
| 311 | reconnect() | ||
| 312 | if isclosed(data) then fail("should not be closed") end | ||
| 313 | data:close() | ||
| 314 | if not isclosed(data) then fail("should be closed") end | ||
| 315 | data = nil | ||
| 316 | local udp = socket.udp() | ||
| 317 | if isclosed(udp) then fail("should not be closed") end | ||
| 318 | udp:close() | ||
| 319 | if not isclosed(udp) then fail("should be closed") end | ||
| 320 | pass("ok") | ||
| 321 | end | ||
| 322 | |||
| 323 | ------------------------------------------------------------------------ | ||
| 324 | function test_closed() | ||
| 325 | local back, partial, err | ||
| 326 | local str = 'little string' | ||
| 327 | reconnect() | ||
| 328 | pass("trying read detection") | ||
| 329 | remote (string.format ([[ | ||
| 330 | data:send('%s') | ||
| 331 | data:close() | ||
| 332 | data = nil | ||
| 333 | ]], str)) | ||
| 334 | -- try to get a line | ||
| 335 | back, err, partial = data:receive() | ||
| 336 | if not err then fail("should have gotten 'closed'.") | ||
| 337 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | ||
| 338 | elseif str ~= partial then fail("didn't receive partial result.") | ||
| 339 | else pass("graceful 'closed' received") end | ||
| 340 | reconnect() | ||
| 341 | pass("trying write detection") | ||
| 342 | remote [[ | ||
| 343 | data:close() | ||
| 344 | data = nil | ||
| 345 | ]] | ||
| 346 | total, err, partial = data:send(string.rep("ugauga", 100000)) | ||
| 347 | if not err then | ||
| 348 | pass("failed: output buffer is at least %d bytes long!", total) | ||
| 349 | elseif err ~= "closed" then | ||
| 350 | fail("got '"..err.."' instead of 'closed'.") | ||
| 351 | else | ||
| 352 | pass("graceful 'closed' received after %d bytes were sent", partial) | ||
| 353 | end | ||
| 354 | end | ||
| 355 | |||
| 356 | ------------------------------------------------------------------------ | ||
| 357 | function test_selectbugs() | ||
| 358 | local r, s, e = socket.select(nil, nil, 0.1) | ||
| 359 | assert(type(r) == "table" and type(s) == "table" and | ||
| 360 | (e == "timeout" or e == "error")) | ||
| 361 | pass("both nil: ok") | ||
| 362 | local udp = socket.udp() | ||
| 363 | udp:close() | ||
| 364 | r, s, e = socket.select({ udp }, { udp }, 0.1) | ||
| 365 | assert(type(r) == "table" and type(s) == "table" and | ||
| 366 | (e == "timeout" or e == "error")) | ||
| 367 | pass("closed sockets: ok") | ||
| 368 | e = pcall(socket.select, "wrong", 1, 0.1) | ||
| 369 | assert(e == false) | ||
| 370 | e = pcall(socket.select, {}, 1, 0.1) | ||
| 371 | assert(e == false) | ||
| 372 | pass("invalid input: ok") | ||
| 373 | end | ||
| 374 | |||
| 375 | ------------------------------------------------------------------------ | ||
| 376 | function accept_timeout() | ||
| 377 | io.stderr:write("accept with timeout (if it hangs, it failed): ") | ||
| 378 | local s, e = socket.bind("*", 0, 0) | ||
| 379 | assert(s, e) | ||
| 380 | local t = socket.gettime() | ||
| 381 | s:settimeout(1) | ||
| 382 | local c, e = s:accept() | ||
| 383 | assert(not c, "should not accept") | ||
| 384 | assert(e == "timeout", string.format("wrong error message (%s)", e)) | ||
| 385 | t = socket.gettime() - t | ||
| 386 | assert(t < 2, string.format("took to long to give up (%gs)", t)) | ||
| 387 | s:close() | ||
| 388 | pass("good") | ||
| 389 | end | ||
| 390 | |||
| 391 | ------------------------------------------------------------------------ | ||
| 392 | function connect_timeout() | ||
| 393 | io.stderr:write("connect with timeout (if it hangs, it failed!): ") | ||
| 394 | local t = socket.gettime() | ||
| 395 | local c, e = socket.tcp() | ||
| 396 | assert(c, e) | ||
| 397 | c:settimeout(0.1) | ||
| 398 | local t = socket.gettime() | ||
| 399 | local r, e = c:connect("127.0.0.2", 80) | ||
| 400 | assert(not r, "should not connect") | ||
| 401 | assert(socket.gettime() - t < 2, "took too long to give up.") | ||
| 402 | c:close() | ||
| 403 | print("ok") | ||
| 404 | end | ||
| 405 | |||
| 406 | ------------------------------------------------------------------------ | ||
| 407 | function accept_errors() | ||
| 408 | io.stderr:write("not listening: ") | ||
| 409 | local d, e = socket.bind("*", 0) | ||
| 410 | assert(d, e); | ||
| 411 | local c, e = socket.tcp(); | ||
| 412 | assert(c, e); | ||
| 413 | d:setfd(c:getfd()) | ||
| 414 | d:settimeout(2) | ||
| 415 | local r, e = d:accept() | ||
| 416 | assert(not r and e) | ||
| 417 | print("ok: ", e) | ||
| 418 | io.stderr:write("not supported: ") | ||
| 419 | local c, e = socket.udp() | ||
| 420 | assert(c, e); | ||
| 421 | d:setfd(c:getfd()) | ||
| 422 | local r, e = d:accept() | ||
| 423 | assert(not r and e) | ||
| 424 | print("ok: ", e) | ||
| 425 | end | ||
| 426 | |||
| 427 | ------------------------------------------------------------------------ | ||
| 428 | function connect_errors() | ||
| 429 | io.stderr:write("connection refused: ") | ||
| 430 | local c, e = socket.connect("localhost", 1); | ||
| 431 | assert(not c and e) | ||
| 432 | print("ok: ", e) | ||
| 433 | io.stderr:write("host not found: ") | ||
| 434 | local c, e = socket.connect("host.is.invalid", 1); | ||
| 435 | assert(not c and e, e) | ||
| 436 | print("ok: ", e) | ||
| 437 | end | ||
| 438 | |||
| 439 | ------------------------------------------------------------------------ | ||
| 440 | function rebind_test() | ||
| 441 | local c = socket.bind("localhost", 0) | ||
| 442 | local i, p = c:getsockname() | ||
| 443 | local s, e = socket.tcp() | ||
| 444 | assert(s, e) | ||
| 445 | s:setoption("reuseaddr", false) | ||
| 446 | r, e = s:bind("localhost", p) | ||
| 447 | assert(not r, "managed to rebind!") | ||
| 448 | assert(e) | ||
| 449 | print("ok: ", e) | ||
| 450 | end | ||
| 451 | |||
| 452 | ------------------------------------------------------------------------ | ||
| 453 | function getstats_test() | ||
| 454 | reconnect() | ||
| 455 | local t = 0 | ||
| 456 | for i = 1, 25 do | ||
| 457 | local c = math.random(1, 100) | ||
| 458 | remote (string.format ([[ | ||
| 459 | str = data:receive(%d) | ||
| 460 | data:send(str) | ||
| 461 | ]], c)) | ||
| 462 | data:send(string.rep("a", c)) | ||
| 463 | data:receive(c) | ||
| 464 | t = t + c | ||
| 465 | local r, s, a = data:getstats() | ||
| 466 | assert(r == t, "received count failed" .. tostring(r) | ||
| 467 | .. "/" .. tostring(t)) | ||
| 468 | assert(s == t, "sent count failed" .. tostring(s) | ||
| 469 | .. "/" .. tostring(t)) | ||
| 470 | end | ||
| 471 | print("ok") | ||
| 472 | end | ||
| 473 | |||
| 474 | |||
| 475 | ------------------------------------------------------------------------ | ||
| 476 | function test_nonblocking(size) | ||
| 477 | reconnect() | ||
| 478 | print("Testing " .. 2*size .. " bytes") | ||
| 479 | remote(string.format([[ | ||
| 480 | data:send(string.rep("a", %d)) | ||
| 481 | socket.sleep(0.5) | ||
| 482 | data:send(string.rep("b", %d) .. "\n") | ||
| 483 | ]], size, size)) | ||
| 484 | local err = "timeout" | ||
| 485 | local part = "" | ||
| 486 | local str | ||
| 487 | data:settimeout(0) | ||
| 488 | while 1 do | ||
| 489 | str, err, part = data:receive("*l", part) | ||
| 490 | if err ~= "timeout" then break end | ||
| 491 | end | ||
| 492 | assert(str == (string.rep("a", size) .. string.rep("b", size))) | ||
| 493 | reconnect() | ||
| 494 | remote(string.format([[ | ||
| 495 | str = data:receive(%d) | ||
| 496 | socket.sleep(0.5) | ||
| 497 | str = data:receive(%d, str) | ||
| 498 | data:send(str) | ||
| 499 | ]], size, size)) | ||
| 500 | data:settimeout(0) | ||
| 501 | local start = 0 | ||
| 502 | while 1 do | ||
| 503 | ret, err, start = data:send(str, start+1) | ||
| 504 | if err ~= "timeout" then break end | ||
| 505 | end | ||
| 506 | data:send("\n") | ||
| 507 | data:settimeout(-1) | ||
| 508 | local back = data:receive(2*size) | ||
| 509 | assert(back == str, "'" .. back .. "' vs '" .. str .. "'") | ||
| 510 | print("ok") | ||
| 511 | end | ||
| 512 | |||
| 513 | ------------------------------------------------------------------------ | ||
| 514 | |||
| 515 | test("method registration") | ||
| 516 | test_methods(socket.unix(), { | ||
| 517 | "accept", | ||
| 518 | "bind", | ||
| 519 | "close", | ||
| 520 | "connect", | ||
| 521 | "dirty", | ||
| 522 | "getfd", | ||
| 523 | "getstats", | ||
| 524 | "setstats", | ||
| 525 | "listen", | ||
| 526 | "receive", | ||
| 527 | "send", | ||
| 528 | "setfd", | ||
| 529 | "setoption", | ||
| 530 | "setpeername", | ||
| 531 | "setsockname", | ||
| 532 | "settimeout", | ||
| 533 | "shutdown", | ||
| 534 | }) | ||
| 535 | |||
| 536 | test("connect function") | ||
| 537 | --connect_timeout() | ||
| 538 | --empty_connect() | ||
| 539 | --connect_errors() | ||
| 540 | |||
| 541 | --test("rebinding: ") | ||
| 542 | --rebind_test() | ||
| 543 | |||
| 544 | test("active close: ") | ||
| 545 | active_close() | ||
| 546 | |||
| 547 | test("closed connection detection: ") | ||
| 548 | test_closed() | ||
| 549 | |||
| 550 | test("accept function: ") | ||
| 551 | accept_timeout() | ||
| 552 | accept_errors() | ||
| 553 | |||
| 554 | test("getstats test") | ||
| 555 | getstats_test() | ||
| 556 | |||
| 557 | test("character line") | ||
| 558 | test_asciiline(1) | ||
| 559 | test_asciiline(17) | ||
| 560 | test_asciiline(200) | ||
| 561 | test_asciiline(4091) | ||
| 562 | test_asciiline(80199) | ||
| 563 | test_asciiline(8000000) | ||
| 564 | test_asciiline(80199) | ||
| 565 | test_asciiline(4091) | ||
| 566 | test_asciiline(200) | ||
| 567 | test_asciiline(17) | ||
| 568 | test_asciiline(1) | ||
| 569 | |||
| 570 | test("mixed patterns") | ||
| 571 | test_mixed(1) | ||
| 572 | test_mixed(17) | ||
| 573 | test_mixed(200) | ||
| 574 | test_mixed(4091) | ||
| 575 | test_mixed(801990) | ||
| 576 | test_mixed(4091) | ||
| 577 | test_mixed(200) | ||
| 578 | test_mixed(17) | ||
| 579 | test_mixed(1) | ||
| 580 | |||
| 581 | test("binary line") | ||
| 582 | test_rawline(1) | ||
| 583 | test_rawline(17) | ||
| 584 | test_rawline(200) | ||
| 585 | test_rawline(4091) | ||
| 586 | test_rawline(80199) | ||
| 587 | test_rawline(8000000) | ||
| 588 | test_rawline(80199) | ||
| 589 | test_rawline(4091) | ||
| 590 | test_rawline(200) | ||
| 591 | test_rawline(17) | ||
| 592 | test_rawline(1) | ||
| 593 | |||
| 594 | test("raw transfer") | ||
| 595 | test_raw(1) | ||
| 596 | test_raw(17) | ||
| 597 | test_raw(200) | ||
| 598 | test_raw(4091) | ||
| 599 | test_raw(80199) | ||
| 600 | test_raw(8000000) | ||
| 601 | test_raw(80199) | ||
| 602 | test_raw(4091) | ||
| 603 | test_raw(200) | ||
| 604 | test_raw(17) | ||
| 605 | test_raw(1) | ||
| 606 | |||
| 607 | test("non-blocking transfer") | ||
| 608 | test_nonblocking(1) | ||
| 609 | test_nonblocking(17) | ||
| 610 | test_nonblocking(200) | ||
| 611 | test_nonblocking(4091) | ||
| 612 | test_nonblocking(80199) | ||
| 613 | test_nonblocking(8000000) | ||
| 614 | test_nonblocking(80199) | ||
| 615 | test_nonblocking(4091) | ||
| 616 | test_nonblocking(200) | ||
| 617 | test_nonblocking(17) | ||
| 618 | test_nonblocking(1) | ||
| 619 | |||
| 620 | test("total timeout on send") | ||
| 621 | test_totaltimeoutsend(800091, 1, 3) | ||
| 622 | test_totaltimeoutsend(800091, 2, 3) | ||
| 623 | test_totaltimeoutsend(800091, 5, 2) | ||
| 624 | test_totaltimeoutsend(800091, 3, 1) | ||
| 625 | |||
| 626 | test("total timeout on receive") | ||
| 627 | test_totaltimeoutreceive(800091, 1, 3) | ||
| 628 | test_totaltimeoutreceive(800091, 2, 3) | ||
| 629 | test_totaltimeoutreceive(800091, 3, 2) | ||
| 630 | test_totaltimeoutreceive(800091, 3, 1) | ||
| 631 | |||
| 632 | test("blocking timeout on send") | ||
| 633 | test_blockingtimeoutsend(800091, 1, 3) | ||
| 634 | test_blockingtimeoutsend(800091, 2, 3) | ||
| 635 | test_blockingtimeoutsend(800091, 3, 2) | ||
| 636 | test_blockingtimeoutsend(800091, 3, 1) | ||
| 637 | |||
| 638 | test("blocking timeout on receive") | ||
| 639 | test_blockingtimeoutreceive(800091, 1, 3) | ||
| 640 | test_blockingtimeoutreceive(800091, 2, 3) | ||
| 641 | test_blockingtimeoutreceive(800091, 3, 2) | ||
| 642 | test_blockingtimeoutreceive(800091, 3, 1) | ||
| 643 | |||
| 644 | test(string.format("done in %.2fs", socket.gettime() - start)) | ||
diff --git a/test/utestsrvr.lua b/test/utestsrvr.lua new file mode 100644 index 0000000..f7be196 --- /dev/null +++ b/test/utestsrvr.lua | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | require("socket"); | ||
| 2 | os.remove("/tmp/luasocket") | ||
| 3 | socket = require("socket.unix"); | ||
| 4 | host = "luasocket"; | ||
| 5 | server = socket.unix() | ||
| 6 | print(server:bind(host)) | ||
| 7 | print(server:listen(5)) | ||
| 8 | ack = "\n"; | ||
| 9 | while 1 do | ||
| 10 | print("server: waiting for client connection..."); | ||
| 11 | control = assert(server:accept()); | ||
| 12 | while 1 do | ||
| 13 | command = assert(control:receive()); | ||
| 14 | assert(control:send(ack)); | ||
| 15 | (loadstring(command))(); | ||
| 16 | end | ||
| 17 | end | ||
