diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2000-12-29 22:15:09 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2000-12-29 22:15:09 +0000 |
| commit | 17c4d1c30544f0ed638879835f179ada96249868 (patch) | |
| tree | 46ca8042ba8fda147f56af61e26ec2ceec41f614 /test | |
| parent | 6f9d15b66027cef58441549f8ac0603ca42da0ac (diff) | |
| download | luasocket-17c4d1c30544f0ed638879835f179ada96249868.tar.gz luasocket-17c4d1c30544f0ed638879835f179ada96249868.tar.bz2 luasocket-17c4d1c30544f0ed638879835f179ada96249868.zip | |
Initial revision
Diffstat (limited to 'test')
| -rw-r--r-- | test/testclnt.lua | 359 | ||||
| -rw-r--r-- | test/testsrvr.lua | 90 |
2 files changed, 449 insertions, 0 deletions
diff --git a/test/testclnt.lua b/test/testclnt.lua new file mode 100644 index 0000000..c1c22bd --- /dev/null +++ b/test/testclnt.lua | |||
| @@ -0,0 +1,359 @@ | |||
| 1 | ----------------------------------------------------------------------------- | ||
| 2 | -- LuaSocket automated test module | ||
| 3 | -- client.lua | ||
| 4 | -- This is the client module. It connects with the server module and executes | ||
| 5 | -- all tests. | ||
| 6 | ----------------------------------------------------------------------------- | ||
| 7 | |||
| 8 | ----------------------------------------------------------------------------- | ||
| 9 | -- Prints a header to separate the test phases | ||
| 10 | -- Input | ||
| 11 | -- test: test phase name | ||
| 12 | ----------------------------------------------------------------------------- | ||
| 13 | function new_test(test) | ||
| 14 | write("----------------------------------------------\n", | ||
| 15 | test, "\n", | ||
| 16 | "----------------------------------------------\n") | ||
| 17 | end | ||
| 18 | |||
| 19 | ----------------------------------------------------------------------------- | ||
| 20 | -- Read command definitions and stablish control connection | ||
| 21 | ----------------------------------------------------------------------------- | ||
| 22 | new_test("initializing...") | ||
| 23 | dofile("command.lua") | ||
| 24 | test_debug_mode() | ||
| 25 | while control == nil do | ||
| 26 | print("client: trying control connection...") | ||
| 27 | control, err = connect(HOST, PORT) | ||
| 28 | if control then | ||
| 29 | print("client: control connection stablished!") | ||
| 30 | else | ||
| 31 | sleep(2) | ||
| 32 | end | ||
| 33 | end | ||
| 34 | |||
| 35 | ----------------------------------------------------------------------------- | ||
| 36 | -- Make sure server is ready for data transmission | ||
| 37 | ----------------------------------------------------------------------------- | ||
| 38 | function sync() | ||
| 39 | send_command(SYNC) | ||
| 40 | get_command() | ||
| 41 | end | ||
| 42 | |||
| 43 | ----------------------------------------------------------------------------- | ||
| 44 | -- Close and reopen data connection, to get rid of any unread blocks | ||
| 45 | ----------------------------------------------------------------------------- | ||
| 46 | function reconnect() | ||
| 47 | if data then | ||
| 48 | data:close() | ||
| 49 | send_command(CLOSE) | ||
| 50 | data = nil | ||
| 51 | end | ||
| 52 | while data == nil do | ||
| 53 | send_command(CONNECT) | ||
| 54 | data = connect(HOST, PORT) | ||
| 55 | if not data then | ||
| 56 | print("client: waiting for data connection.") | ||
| 57 | sleep(1) | ||
| 58 | end | ||
| 59 | end | ||
| 60 | sync() | ||
| 61 | end | ||
| 62 | |||
| 63 | ----------------------------------------------------------------------------- | ||
| 64 | -- Tests the command connection | ||
| 65 | ----------------------------------------------------------------------------- | ||
| 66 | function test_command(cmd, par) | ||
| 67 | local cmd_back, par_back | ||
| 68 | reconnect() | ||
| 69 | send_command(COMMAND) | ||
| 70 | write("testing command ") | ||
| 71 | print_command(cmd, par) | ||
| 72 | send_command(cmd, par) | ||
| 73 | cmd_back, par_back = get_command() | ||
| 74 | if cmd_back ~= cmd or par_back ~= par then | ||
| 75 | fail(cmd) | ||
| 76 | else | ||
| 77 | pass() | ||
| 78 | end | ||
| 79 | end | ||
| 80 | |||
| 81 | ----------------------------------------------------------------------------- | ||
| 82 | -- Tests ASCII line transmission | ||
| 83 | -- Input | ||
| 84 | -- len: length of line to be tested | ||
| 85 | ----------------------------------------------------------------------------- | ||
| 86 | function test_asciiline(len) | ||
| 87 | local str, str10, back, err | ||
| 88 | reconnect() | ||
| 89 | send_command(ECHO_LINE) | ||
| 90 | str = strrep("x", mod(len, 10)) | ||
| 91 | str10 = strrep("aZb.c#dAe?", floor(len/10)) | ||
| 92 | str = str .. str10 | ||
| 93 | write("testing ", len, " byte(s) line\n") | ||
| 94 | err = data:send(str, "\n") | ||
| 95 | if err then fail(err) end | ||
| 96 | back, err = data:receive() | ||
| 97 | if err then fail(err) end | ||
| 98 | if back == str then pass("lines match") | ||
| 99 | else fail("lines don't match") end | ||
| 100 | end | ||
| 101 | |||
| 102 | ----------------------------------------------------------------------------- | ||
| 103 | -- Tests closed connection detection | ||
| 104 | ----------------------------------------------------------------------------- | ||
| 105 | function test_closed() | ||
| 106 | local str = "This is our little test line" | ||
| 107 | local len = strlen(str) | ||
| 108 | local back, err, total | ||
| 109 | reconnect() | ||
| 110 | print("testing close while reading line") | ||
| 111 | send_command(ECHO_BLOCK, len) | ||
| 112 | data:send(str) | ||
| 113 | send_command(CLOSE) | ||
| 114 | -- try to get a line | ||
| 115 | back, err = data:receive() | ||
| 116 | if not err then fail("shold have gotten 'closed'.") | ||
| 117 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | ||
| 118 | elseif str ~= back then fail("didn't receive what i should 'closed'.") | ||
| 119 | else pass("rightfull 'closed' received") end | ||
| 120 | reconnect() | ||
| 121 | print("testing close while reading block") | ||
| 122 | send_command(ECHO_BLOCK, len) | ||
| 123 | data:send(str) | ||
| 124 | send_command(CLOSE) | ||
| 125 | -- try to get a line | ||
| 126 | back, err = data:receive(2*len) | ||
| 127 | if not err then fail("shold have gotten 'closed'.") | ||
| 128 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | ||
| 129 | elseif str ~= back then fail("didn't receive what I should.") | ||
| 130 | else pass("rightfull 'closed' received") end | ||
| 131 | end | ||
| 132 | |||
| 133 | ----------------------------------------------------------------------------- | ||
| 134 | -- Tests binary line transmission | ||
| 135 | -- Input | ||
| 136 | -- len: length of line to be tested | ||
| 137 | ----------------------------------------------------------------------------- | ||
| 138 | function test_rawline(len) | ||
| 139 | local str, str10, back, err | ||
| 140 | reconnect() | ||
| 141 | send_command(ECHO_LINE) | ||
| 142 | str = strrep(strchar(47), mod(len, 10)) | ||
| 143 | str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) | ||
| 144 | str = str .. str10 | ||
| 145 | write("testing ", len, " byte(s) line\n") | ||
| 146 | err = data:send(str, "\n") | ||
| 147 | if err then fail(err) end | ||
| 148 | back, err = data:receive() | ||
| 149 | if err then fail(err) end | ||
| 150 | if back == str then pass("lines match") | ||
| 151 | else fail("lines don't match") end | ||
| 152 | end | ||
| 153 | |||
| 154 | ----------------------------------------------------------------------------- | ||
| 155 | -- Tests block transmission | ||
| 156 | -- Input | ||
| 157 | -- len: length of block to be tested | ||
| 158 | ----------------------------------------------------------------------------- | ||
| 159 | function test_block(len) | ||
| 160 | local half = floor(len/2) | ||
| 161 | local s1, s2, back, err | ||
| 162 | reconnect() | ||
| 163 | send_command(ECHO_BLOCK, len) | ||
| 164 | write("testing ", len, " byte(s) block\n") | ||
| 165 | s1 = strrep("x", half) | ||
| 166 | err = data:send(s1) | ||
| 167 | if err then fail(err) end | ||
| 168 | sleep(1) | ||
| 169 | s2 = strrep("y", len-half) | ||
| 170 | err = data:send(s2) | ||
| 171 | if err then fail(err) end | ||
| 172 | back, err = data:receive(len) | ||
| 173 | if err then fail(err) end | ||
| 174 | if back == s1..s2 then pass("blocks match") | ||
| 175 | else fail("blocks don't match") end | ||
| 176 | end | ||
| 177 | |||
| 178 | ----------------------------------------------------------------------------- | ||
| 179 | -- Tests if return-timeout was respected | ||
| 180 | -- delta: time elapsed during transfer | ||
| 181 | -- t: timeout value | ||
| 182 | -- err: error code returned by I/O operation | ||
| 183 | ----------------------------------------------------------------------------- | ||
| 184 | function returntimed_out(delta, t, err) | ||
| 185 | if err == "timeout" then | ||
| 186 | if delta + 0.1 >= t then | ||
| 187 | pass("got right timeout") | ||
| 188 | return 1 | ||
| 189 | else | ||
| 190 | fail("shouldn't have gotten timeout") | ||
| 191 | end | ||
| 192 | elseif delta > t then | ||
| 193 | fail("should have gotten timeout") | ||
| 194 | end | ||
| 195 | end | ||
| 196 | |||
| 197 | ----------------------------------------------------------------------------- | ||
| 198 | -- Tests if return-timeout was respected | ||
| 199 | -- delta: time elapsed during transfer | ||
| 200 | -- t: timeout value | ||
| 201 | -- err: error code returned by I/O operation | ||
| 202 | -- o: operation being executed | ||
| 203 | ----------------------------------------------------------------------------- | ||
| 204 | function blockedtimed_out(t, s, err, o) | ||
| 205 | if err == "timeout" then | ||
| 206 | if s >= t then | ||
| 207 | pass("got right forced timeout") | ||
| 208 | return 1 | ||
| 209 | else | ||
| 210 | pass("got natural cause timeout (may be wrong)") | ||
| 211 | return 1 | ||
| 212 | end | ||
| 213 | elseif s > t then | ||
| 214 | if o == "send" then | ||
| 215 | pass("must have been buffered (may be wrong)") | ||
| 216 | else | ||
| 217 | fail("should have gotten timeout") | ||
| 218 | end | ||
| 219 | end | ||
| 220 | end | ||
| 221 | |||
| 222 | ----------------------------------------------------------------------------- | ||
| 223 | -- Tests blocked-timeout conformance | ||
| 224 | -- Input | ||
| 225 | -- len: length of block to be tested | ||
| 226 | -- t: timeout value | ||
| 227 | -- s: server sleep between transfers | ||
| 228 | ----------------------------------------------------------------------------- | ||
| 229 | function test_blockedtimeout(len, t, s) | ||
| 230 | local str, err, back, total | ||
| 231 | reconnect() | ||
| 232 | send_command(RECEIVE_BLOCK, len) | ||
| 233 | send_command(SLEEP, s) | ||
| 234 | send_command(RECEIVE_BLOCK, len) | ||
| 235 | write("testing ", len, " bytes, ", t, | ||
| 236 | "s block timeout, ", s, "s sleep\n") | ||
| 237 | data:timeout(t) | ||
| 238 | str = strrep("a", 2*len) | ||
| 239 | err, total = data:send(str) | ||
| 240 | if blockedtimed_out(t, s, err, "send") then return end | ||
| 241 | if err then fail(err) end | ||
| 242 | send_command(SEND_BLOCK) | ||
| 243 | send_command(SLEEP, s) | ||
| 244 | send_command(SEND_BLOCK) | ||
| 245 | back, err = data:receive(2*len) | ||
| 246 | if blockedtimed_out(t, s, err, "receive") then return end | ||
| 247 | if err then fail(err) end | ||
| 248 | if back == str then pass("blocks match") | ||
| 249 | else fail("blocks don't match") end | ||
| 250 | end | ||
| 251 | |||
| 252 | ----------------------------------------------------------------------------- | ||
| 253 | -- Tests return-timeout conformance | ||
| 254 | -- Input | ||
| 255 | -- len: length of block to be tested | ||
| 256 | -- t: timeout value | ||
| 257 | -- s: server sleep between transfers | ||
| 258 | ----------------------------------------------------------------------------- | ||
| 259 | function test_returntimeout(len, t, s) | ||
| 260 | local str, err, back, delta, total | ||
| 261 | reconnect() | ||
| 262 | send_command(RECEIVE_BLOCK, len) | ||
| 263 | send_command(SLEEP, s) | ||
| 264 | send_command(RECEIVE_BLOCK, len) | ||
| 265 | write("testing ", len, " bytes, ", t, | ||
| 266 | "s return timeout, ", s, "s sleep\n") | ||
| 267 | data:timeout(t, "return") | ||
| 268 | str = strrep("a", 2*len) | ||
| 269 | err, total, delta = data:send(str) | ||
| 270 | print("delta: " .. delta) | ||
| 271 | if returntimed_out(delta, t, err) then return end | ||
| 272 | if err then fail(err) end | ||
| 273 | send_command(SEND_BLOCK) | ||
| 274 | send_command(SLEEP, s) | ||
| 275 | send_command(SEND_BLOCK) | ||
| 276 | back, err, delta = data:receive(2*len) | ||
| 277 | print("delta: " .. delta) | ||
| 278 | if returntimed_out(delta, t, err) then return end | ||
| 279 | if err then fail(err) end | ||
| 280 | if back == str then pass("blocks match") | ||
| 281 | else fail("blocks don't match") end | ||
| 282 | end | ||
| 283 | |||
| 284 | ----------------------------------------------------------------------------- | ||
| 285 | -- Execute all tests | ||
| 286 | ----------------------------------------------------------------------------- | ||
| 287 | new_test("control connection test") | ||
| 288 | test_command(EXIT) | ||
| 289 | test_command(CONNECT) | ||
| 290 | test_command(CLOSE) | ||
| 291 | test_command(ECHO_BLOCK, 12234) | ||
| 292 | test_command(SLEEP, 1111) | ||
| 293 | test_command(ECHO_LINE) | ||
| 294 | |||
| 295 | new_test("connection close test") | ||
| 296 | test_closed() | ||
| 297 | |||
| 298 | new_test("binary string test") | ||
| 299 | test_rawline(1) | ||
| 300 | test_rawline(17) | ||
| 301 | test_rawline(200) | ||
| 302 | test_rawline(3000) | ||
| 303 | test_rawline(8000) | ||
| 304 | test_rawline(40000) | ||
| 305 | |||
| 306 | new_test("blocking transfer test") | ||
| 307 | test_block(1) | ||
| 308 | test_block(17) | ||
| 309 | test_block(200) | ||
| 310 | test_block(3000) | ||
| 311 | test_block(80000) | ||
| 312 | test_block(800000) | ||
| 313 | |||
| 314 | new_test("non-blocking transfer test") | ||
| 315 | -- the value is not important, we only want | ||
| 316 | -- to test non-blockin I/O anyways | ||
| 317 | data:timeout(200) | ||
| 318 | test_block(1) | ||
| 319 | test_block(17) | ||
| 320 | test_block(200) | ||
| 321 | test_block(3000) | ||
| 322 | test_block(80000) | ||
| 323 | test_block(800000) | ||
| 324 | test_block(8000000) | ||
| 325 | |||
| 326 | new_test("character string test") | ||
| 327 | test_asciiline(1) | ||
| 328 | test_asciiline(17) | ||
| 329 | test_asciiline(200) | ||
| 330 | test_asciiline(3000) | ||
| 331 | test_asciiline(8000) | ||
| 332 | test_asciiline(40000) | ||
| 333 | |||
| 334 | new_test("return timeout test") | ||
| 335 | test_returntimeout(80, .5, 1) | ||
| 336 | test_returntimeout(80, 1, 0.5) | ||
| 337 | test_returntimeout(8000, .5, 0) | ||
| 338 | test_returntimeout(80000, .5, 0) | ||
| 339 | test_returntimeout(800000, .5, 0) | ||
| 340 | |||
| 341 | new_test("blocked timeout test") | ||
| 342 | test_blockedtimeout(80, .5, 1) | ||
| 343 | test_blockedtimeout(80, 1, 1) | ||
| 344 | test_blockedtimeout(80, 1.5, 1) | ||
| 345 | test_blockedtimeout(800, 1, 0) | ||
| 346 | test_blockedtimeout(8000, 1, 0) | ||
| 347 | test_blockedtimeout(80000, 1, 0) | ||
| 348 | test_blockedtimeout(800000, 1, 0) | ||
| 349 | |||
| 350 | ----------------------------------------------------------------------------- | ||
| 351 | -- Close connection and exit server. We are done. | ||
| 352 | ----------------------------------------------------------------------------- | ||
| 353 | new_test("the library has passed all tests") | ||
| 354 | print("client: closing connection with server") | ||
| 355 | send_command(CLOSE) | ||
| 356 | send_command(EXIT) | ||
| 357 | control:close() | ||
| 358 | print("client: exiting...") | ||
| 359 | exit() | ||
diff --git a/test/testsrvr.lua b/test/testsrvr.lua new file mode 100644 index 0000000..99ecd2a --- /dev/null +++ b/test/testsrvr.lua | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | ----------------------------------------------------------------------------- | ||
| 2 | -- LuaSocket automated test module | ||
| 3 | -- server.lua | ||
| 4 | -- This is the server module. It's completely controled by the client module | ||
| 5 | -- by the use of a control connection. | ||
| 6 | ----------------------------------------------------------------------------- | ||
| 7 | |||
| 8 | ----------------------------------------------------------------------------- | ||
| 9 | -- Read command definitions | ||
| 10 | ----------------------------------------------------------------------------- | ||
| 11 | dofile("command.lua") | ||
| 12 | test_debug_mode() | ||
| 13 | |||
| 14 | ----------------------------------------------------------------------------- | ||
| 15 | -- Bind to address and wait for control connection | ||
| 16 | ----------------------------------------------------------------------------- | ||
| 17 | server, err = bind(HOST, PORT) | ||
| 18 | if not server then | ||
| 19 | print(err) | ||
| 20 | exit(1) | ||
| 21 | end | ||
| 22 | print("server: waiting for control connection...") | ||
| 23 | control = server:accept() | ||
| 24 | print("server: control connection stablished!") | ||
| 25 | |||
| 26 | ----------------------------------------------------------------------------- | ||
| 27 | -- Executes a command, detecting any possible failures | ||
| 28 | -- Input | ||
| 29 | -- cmd: command to be executed | ||
| 30 | -- par: command parameters, if needed | ||
| 31 | ----------------------------------------------------------------------------- | ||
| 32 | function execute_command(cmd, par) | ||
| 33 | if cmd == CONNECT then | ||
| 34 | print("server: waiting for data connection...") | ||
| 35 | data = server:accept() | ||
| 36 | if not data then | ||
| 37 | fail("server: unable to start data connection!") | ||
| 38 | else | ||
| 39 | print("server: data connection stablished!") | ||
| 40 | end | ||
| 41 | elseif cmd == CLOSE then | ||
| 42 | print("server: closing connection with client...") | ||
| 43 | if data then | ||
| 44 | data:close() | ||
| 45 | data = nil | ||
| 46 | end | ||
| 47 | elseif cmd == ECHO_LINE then | ||
| 48 | str, err = data:receive() | ||
| 49 | if err then fail("server: " .. err) end | ||
| 50 | err = data:send(str, "\n") | ||
| 51 | if err then fail("server: " .. err) end | ||
| 52 | elseif cmd == ECHO_BLOCK then | ||
| 53 | str, err = data:receive(par) | ||
| 54 | if err then fail("server: " .. err) end | ||
| 55 | err = data:send(str) | ||
| 56 | if err then fail("server: " .. err) end | ||
| 57 | elseif cmd == RECEIVE_BLOCK then | ||
| 58 | str, err = data:receive(par) | ||
| 59 | elseif cmd == SEND_BLOCK then | ||
| 60 | err = data:send(str) | ||
| 61 | elseif cmd == ECHO_TIMEOUT then | ||
| 62 | str, err = data:receive(par) | ||
| 63 | if err then fail("server: " .. err) end | ||
| 64 | err = data:send(str) | ||
| 65 | if err then fail("server: " .. err) end | ||
| 66 | elseif cmd == COMMAND then | ||
| 67 | cmd, par = get_command() | ||
| 68 | send_command(cmd, par) | ||
| 69 | elseif cmd == EXIT then | ||
| 70 | print("server: exiting...") | ||
| 71 | exit(0) | ||
| 72 | elseif cmd == SYNC then | ||
| 73 | print("server: synchronizing...") | ||
| 74 | send_command(SYNC) | ||
| 75 | elseif cmd == SLEEP then | ||
| 76 | print("server: sleeping for " .. par .. " seconds...") | ||
| 77 | sleep(par) | ||
| 78 | print("server: woke up!") | ||
| 79 | end | ||
| 80 | end | ||
| 81 | |||
| 82 | ----------------------------------------------------------------------------- | ||
| 83 | -- Loop forever, accepting and executing commands | ||
| 84 | ----------------------------------------------------------------------------- | ||
| 85 | while 1 do | ||
| 86 | cmd, par = get_command() | ||
| 87 | if not cmd then fail("server: " .. par) end | ||
| 88 | print_command(cmd, par) | ||
| 89 | execute_command(cmd, par) | ||
| 90 | end | ||
