aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/testclnt.lua359
-rw-r--r--test/testsrvr.lua90
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-----------------------------------------------------------------------------
13function new_test(test)
14 write("----------------------------------------------\n",
15 test, "\n",
16 "----------------------------------------------\n")
17end
18
19-----------------------------------------------------------------------------
20-- Read command definitions and stablish control connection
21-----------------------------------------------------------------------------
22new_test("initializing...")
23dofile("command.lua")
24test_debug_mode()
25while 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
33end
34
35-----------------------------------------------------------------------------
36-- Make sure server is ready for data transmission
37-----------------------------------------------------------------------------
38function sync()
39 send_command(SYNC)
40 get_command()
41end
42
43-----------------------------------------------------------------------------
44-- Close and reopen data connection, to get rid of any unread blocks
45-----------------------------------------------------------------------------
46function 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()
61end
62
63-----------------------------------------------------------------------------
64-- Tests the command connection
65-----------------------------------------------------------------------------
66function 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
79end
80
81-----------------------------------------------------------------------------
82-- Tests ASCII line transmission
83-- Input
84-- len: length of line to be tested
85-----------------------------------------------------------------------------
86function 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
100end
101
102-----------------------------------------------------------------------------
103-- Tests closed connection detection
104-----------------------------------------------------------------------------
105function 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
131end
132
133-----------------------------------------------------------------------------
134-- Tests binary line transmission
135-- Input
136-- len: length of line to be tested
137-----------------------------------------------------------------------------
138function 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
152end
153
154-----------------------------------------------------------------------------
155-- Tests block transmission
156-- Input
157-- len: length of block to be tested
158-----------------------------------------------------------------------------
159function 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
176end
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-----------------------------------------------------------------------------
184function 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
195end
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-----------------------------------------------------------------------------
204function 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
220end
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-----------------------------------------------------------------------------
229function 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
250end
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-----------------------------------------------------------------------------
259function 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
282end
283
284-----------------------------------------------------------------------------
285-- Execute all tests
286-----------------------------------------------------------------------------
287new_test("control connection test")
288test_command(EXIT)
289test_command(CONNECT)
290test_command(CLOSE)
291test_command(ECHO_BLOCK, 12234)
292test_command(SLEEP, 1111)
293test_command(ECHO_LINE)
294
295new_test("connection close test")
296test_closed()
297
298new_test("binary string test")
299test_rawline(1)
300test_rawline(17)
301test_rawline(200)
302test_rawline(3000)
303test_rawline(8000)
304test_rawline(40000)
305
306new_test("blocking transfer test")
307test_block(1)
308test_block(17)
309test_block(200)
310test_block(3000)
311test_block(80000)
312test_block(800000)
313
314new_test("non-blocking transfer test")
315-- the value is not important, we only want
316-- to test non-blockin I/O anyways
317data:timeout(200)
318test_block(1)
319test_block(17)
320test_block(200)
321test_block(3000)
322test_block(80000)
323test_block(800000)
324test_block(8000000)
325
326new_test("character string test")
327test_asciiline(1)
328test_asciiline(17)
329test_asciiline(200)
330test_asciiline(3000)
331test_asciiline(8000)
332test_asciiline(40000)
333
334new_test("return timeout test")
335test_returntimeout(80, .5, 1)
336test_returntimeout(80, 1, 0.5)
337test_returntimeout(8000, .5, 0)
338test_returntimeout(80000, .5, 0)
339test_returntimeout(800000, .5, 0)
340
341new_test("blocked timeout test")
342test_blockedtimeout(80, .5, 1)
343test_blockedtimeout(80, 1, 1)
344test_blockedtimeout(80, 1.5, 1)
345test_blockedtimeout(800, 1, 0)
346test_blockedtimeout(8000, 1, 0)
347test_blockedtimeout(80000, 1, 0)
348test_blockedtimeout(800000, 1, 0)
349
350-----------------------------------------------------------------------------
351-- Close connection and exit server. We are done.
352-----------------------------------------------------------------------------
353new_test("the library has passed all tests")
354print("client: closing connection with server")
355send_command(CLOSE)
356send_command(EXIT)
357control:close()
358print("client: exiting...")
359exit()
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-----------------------------------------------------------------------------
11dofile("command.lua")
12test_debug_mode()
13
14-----------------------------------------------------------------------------
15-- Bind to address and wait for control connection
16-----------------------------------------------------------------------------
17server, err = bind(HOST, PORT)
18if not server then
19 print(err)
20 exit(1)
21end
22print("server: waiting for control connection...")
23control = server:accept()
24print("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-----------------------------------------------------------------------------
32function 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
80end
81
82-----------------------------------------------------------------------------
83-- Loop forever, accepting and executing commands
84-----------------------------------------------------------------------------
85while 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)
90end