aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2002-12-03 07:20:34 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2002-12-03 07:20:34 +0000
commit7da19138e37c4e0123860f1fecbceb80c3d2627d (patch)
tree8453f003a9ba212807d9c9590c2f2b850d323f0f
parentd7e80592a69c076991ed4f4cc15d5390e14d1f0b (diff)
downloadluasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.gz
luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.bz2
luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.zip
Faltam testes de ftp e smtp. O resto passa.
-rw-r--r--src/buffer.c4
-rw-r--r--src/http.lua6
-rw-r--r--src/select.c2
-rw-r--r--src/timeout.c6
-rw-r--r--src/url.lua6
-rw-r--r--test/httptest.lua88
-rw-r--r--test/testclnt.lua169
-rw-r--r--test/testsrvr.lua6
-rw-r--r--test/urltest.lua2
9 files changed, 141 insertions, 148 deletions
diff --git a/src/buffer.c b/src/buffer.c
index a9a9782..4260f20 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -74,7 +74,7 @@ int buf_send(lua_State *L, p_buf buf)
74 } 74 }
75 priv_pusherror(L, err); 75 priv_pusherror(L, err);
76 lua_pushnumber(L, total); 76 lua_pushnumber(L, total);
77#ifdef _DEBUG 77#ifdef LUASOCKET_DEBUG
78 /* push time elapsed during operation as the last return value */ 78 /* push time elapsed during operation as the last return value */
79 lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); 79 lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0);
80#endif 80#endif
@@ -139,7 +139,7 @@ int buf_receive(lua_State *L, p_buf buf)
139 for ( ; arg <= top; arg++) lua_pushnil(L); 139 for ( ; arg <= top; arg++) lua_pushnil(L);
140 /* last return is an error code */ 140 /* last return is an error code */
141 priv_pusherror(L, err); 141 priv_pusherror(L, err);
142#ifdef _DEBUG 142#ifdef LUASOCKET_DEBUG
143 /* push time elapsed during operation as the last return value */ 143 /* push time elapsed during operation as the last return value */
144 lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0); 144 lua_pushnumber(L, tm_getelapsed(&base->base_tm)/1000.0);
145#endif 145#endif
diff --git a/src/http.lua b/src/http.lua
index dce3ac8..9543d59 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -8,7 +8,7 @@
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9 9
10local Public, Private = {}, {} 10local Public, Private = {}, {}
11HTTP = Public 11http = Public
12 12
13----------------------------------------------------------------------------- 13-----------------------------------------------------------------------------
14-- Program constants 14-- Program constants
@@ -195,7 +195,7 @@ end
195function Private.receivebody_bylength(sock, length, receive_cb) 195function Private.receivebody_bylength(sock, length, receive_cb)
196 local uerr, go 196 local uerr, go
197 while length > 0 do 197 while length > 0 do
198 local size = min(Public.BLOCKSIZE, length) 198 local size = math.min(Public.BLOCKSIZE, length)
199 local chunk, err = sock:receive(size) 199 local chunk, err = sock:receive(size)
200 if err then 200 if err then
201 go, uerr = receive_cb(nil, err) 201 go, uerr = receive_cb(nil, err)
@@ -542,7 +542,7 @@ function Public.request_cb(request, response)
542 scheme = "http" 542 scheme = "http"
543 }) 543 })
544 if parsed.scheme ~= "http" then 544 if parsed.scheme ~= "http" then
545 response.error = format("unknown scheme '%s'", parsed.scheme) 545 response.error = string.format("unknown scheme '%s'", parsed.scheme)
546 return response 546 return response
547 end 547 end
548 -- explicit authentication info overrides that given by the URL 548 -- explicit authentication info overrides that given by the URL
diff --git a/src/select.c b/src/select.c
index 1aaa7fe..5c08730 100644
--- a/src/select.c
+++ b/src/select.c
@@ -31,8 +31,6 @@ void select_open(lua_State *L)
31{ 31{
32 /* push select auxiliar lua function and register 32 /* push select auxiliar lua function and register
33 * select_lua_select with it as an upvalue */ 33 * select_lua_select with it as an upvalue */
34#ifdef LUASOCKET_DEBUG
35#endif
36 luaL_loadfile(L, "lsselect.lua"); 34 luaL_loadfile(L, "lsselect.lua");
37 lua_call(L, 0, 1); 35 lua_call(L, 0, 1);
38 lua_pushcclosure(L, select_lua_select, 1); 36 lua_pushcclosure(L, select_lua_select, 1);
diff --git a/src/timeout.c b/src/timeout.c
index fdbc47a..940ddca 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -20,7 +20,7 @@
20/*=========================================================================*\ 20/*=========================================================================*\
21* Internal function prototypes 21* Internal function prototypes
22\*=========================================================================*/ 22\*=========================================================================*/
23#ifdef _DEBUG 23#ifdef LUASOCKET_DEBUG
24static int tm_lua_time(lua_State *L); 24static int tm_lua_time(lua_State *L);
25static int tm_lua_sleep(lua_State *L); 25static int tm_lua_sleep(lua_State *L);
26#endif 26#endif
@@ -123,7 +123,7 @@ int tm_gettime(void)
123void tm_open(lua_State *L) 123void tm_open(lua_State *L)
124{ 124{
125 (void) L; 125 (void) L;
126#ifdef _DEBUG 126#ifdef LUASOCKET_DEBUG
127 lua_pushcfunction(L, tm_lua_time); 127 lua_pushcfunction(L, tm_lua_time);
128 priv_newglobal(L, "_time"); 128 priv_newglobal(L, "_time");
129 lua_pushcfunction(L, tm_lua_sleep); 129 lua_pushcfunction(L, tm_lua_sleep);
@@ -137,7 +137,7 @@ void tm_open(lua_State *L)
137/*-------------------------------------------------------------------------*\ 137/*-------------------------------------------------------------------------*\
138* Returns the time the system has been up, in secconds. 138* Returns the time the system has been up, in secconds.
139\*-------------------------------------------------------------------------*/ 139\*-------------------------------------------------------------------------*/
140#ifdef _DEBUG 140#ifdef LUASOCKET_DEBUG
141static int tm_lua_time(lua_State *L) 141static int tm_lua_time(lua_State *L)
142{ 142{
143 lua_pushnumber(L, tm_gettime()/1000.0); 143 lua_pushnumber(L, tm_gettime()/1000.0);
diff --git a/src/url.lua b/src/url.lua
index 0ecec3c..4d2bfa7 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -143,8 +143,8 @@ function Public.parse_path(path)
143 for i = 1, table.getn(parsed) do 143 for i = 1, table.getn(parsed) do
144 parsed[i] = Code.unescape(parsed[i]) 144 parsed[i] = Code.unescape(parsed[i])
145 end 145 end
146 if stringsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end 146 if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
147 if stringsub(path, -1, -1) == "/" then parsed.is_directory = 1 end 147 if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
148 return parsed 148 return parsed
149end 149end
150 150
@@ -214,7 +214,7 @@ end
214-- corresponding absolute path 214-- corresponding absolute path
215----------------------------------------------------------------------------- 215-----------------------------------------------------------------------------
216function Private.absolute_path(base_path, relative_path) 216function Private.absolute_path(base_path, relative_path)
217 if stringsub(relative_path, 1, 1) == "/" then return relative_path end 217 if string.sub(relative_path, 1, 1) == "/" then return relative_path end
218 local path = string.gsub(base_path, "[^/]*$", "") 218 local path = string.gsub(base_path, "[^/]*$", "")
219 path = path .. relative_path 219 path = path .. relative_path
220 path = string.gsub(path, "([^/]*%./)", function (s) 220 path = string.gsub(path, "([^/]*%./)", function (s)
diff --git a/test/httptest.lua b/test/httptest.lua
index 85c8bd8..2941390 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -7,21 +7,21 @@
7dofile("noglobals.lua") 7dofile("noglobals.lua")
8 8
9local similar = function(s1, s2) 9local similar = function(s1, s2)
10 return strlower(gsub(s1 or "", "%s", "")) == 10 return string.lower(string.gsub(s1 or "", "%s", "")) ==
11 strlower(gsub(s2 or "", "%s", "")) 11 string.lower(string.gsub(s2 or "", "%s", ""))
12end 12end
13 13
14local fail = function(s) 14local fail = function(s)
15 s = s or "failed!" 15 s = s or "failed!"
16 print(s) 16 print(s)
17 exit() 17 os.exit()
18end 18end
19 19
20local readfile = function(name) 20local readfile = function(name)
21 local f = readfrom(name) 21 local f = io.open(name, "r")
22 if not f then return nil end 22 if not f then return nil end
23 local s = read("*a") 23 local s = f:read("*a")
24 readfrom() 24 f:close()
25 return s 25 return s
26end 26end
27 27
@@ -31,7 +31,7 @@ local check = function (v, e)
31end 31end
32 32
33local check_request = function(request, expect, ignore) 33local check_request = function(request, expect, ignore)
34 local response = HTTP.request(request) 34 local response = http.request(request)
35 for i,v in response do 35 for i,v in response do
36 if not ignore[i] then 36 if not ignore[i] then
37 if v ~= expect[i] then %fail(i .. " differs!") end 37 if v ~= expect[i] then %fail(i .. " differs!") end
@@ -45,30 +45,28 @@ local check_request = function(request, expect, ignore)
45 print("ok") 45 print("ok")
46end 46end
47 47
48dofile("../src/modules/http.lua")
49
50local request, response, ignore, expect, index, prefix, cgiprefix 48local request, response, ignore, expect, index, prefix, cgiprefix
51 49
52local t = _time() 50local t = socket._time()
53 51
54HOST = HOST or "localhost" 52HOST = HOST or "localhost"
55prefix = prefix or "/luasocket-test" 53prefix = prefix or "/luasocket"
56cgiprefix = cgiprefix or "/luasocket-test-cgi" 54cgiprefix = cgiprefix or "/luasocket/cgi"
57index = readfile("index.html") 55index = readfile("test/index.html")
58 56
59write("testing request uri correctness: ") 57io.write("testing request uri correctness: ")
60local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" 58local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
61local back = HTTP.get("http://" .. HOST .. forth) 59local back = http.get("http://" .. HOST .. forth)
62if similar(back, forth) then print("ok") 60if similar(back, forth) then print("ok")
63else fail("failed!") end 61else fail("failed!") end
64 62
65write("testing query string correctness: ") 63io.write("testing query string correctness: ")
66forth = "this+is+the+query+string" 64forth = "this+is+the+query+string"
67back = HTTP.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth) 65back = http.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth)
68if similar(back, forth) then print("ok") 66if similar(back, forth) then print("ok")
69else fail("failed!") end 67else fail("failed!") end
70 68
71write("testing document retrieval: ") 69io.write("testing document retrieval: ")
72request = { 70request = {
73 url = "http://" .. HOST .. prefix .. "/index.html" 71 url = "http://" .. HOST .. prefix .. "/index.html"
74} 72}
@@ -82,7 +80,7 @@ ignore = {
82} 80}
83check_request(request, expect, ignore) 81check_request(request, expect, ignore)
84 82
85write("testing HTTP redirection: ") 83io.write("testing http redirection: ")
86request = { 84request = {
87 url = "http://" .. HOST .. prefix 85 url = "http://" .. HOST .. prefix
88} 86}
@@ -97,7 +95,7 @@ ignore = {
97check_request(request, expect, ignore) 95check_request(request, expect, ignore)
98 96
99 97
100write("testing automatic auth failure: ") 98io.write("testing automatic auth failure: ")
101request = { 99request = {
102 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html" 100 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html"
103} 101}
@@ -111,7 +109,7 @@ ignore = {
111} 109}
112check_request(request, expect, ignore) 110check_request(request, expect, ignore)
113 111
114write("testing HTTP redirection failure: ") 112io.write("testing http redirection failure: ")
115request = { 113request = {
116 url = "http://" .. HOST .. prefix, 114 url = "http://" .. HOST .. prefix,
117 stay = 1 115 stay = 1
@@ -126,29 +124,29 @@ ignore = {
126} 124}
127check_request(request, expect, ignore) 125check_request(request, expect, ignore)
128 126
129write("testing host not found: ") 127io.write("testing host not found: ")
130request = { 128request = {
131 url = "http://wronghost/does/not/exist" 129 url = "http://wronghost/does/not/exist"
132} 130}
133local c, e = connect("wronghost", 80) 131local c, e = socket.connect("wronghost", 80)
134expect = { 132expect = {
135 error = e 133 error = e
136} 134}
137ignore = {} 135ignore = {}
138check_request(request, expect, ignore) 136check_request(request, expect, ignore)
139 137
140write("testing invalid url: ") 138io.write("testing invalid url: ")
141request = { 139request = {
142 url = HOST .. prefix 140 url = HOST .. prefix
143} 141}
144local c, e = connect("", 80) 142local c, e = socket.connect("", 80)
145expect = { 143expect = {
146 error = e 144 error = e
147} 145}
148ignore = {} 146ignore = {}
149check_request(request, expect, ignore) 147check_request(request, expect, ignore)
150 148
151write("testing document not found: ") 149io.write("testing document not found: ")
152request = { 150request = {
153 url = "http://" .. HOST .. "/wrongdocument.html" 151 url = "http://" .. HOST .. "/wrongdocument.html"
154} 152}
@@ -162,7 +160,7 @@ ignore = {
162} 160}
163check_request(request, expect, ignore) 161check_request(request, expect, ignore)
164 162
165write("testing auth failure: ") 163io.write("testing auth failure: ")
166request = { 164request = {
167 url = "http://" .. HOST .. prefix .. "/auth/index.html" 165 url = "http://" .. HOST .. prefix .. "/auth/index.html"
168} 166}
@@ -176,7 +174,7 @@ ignore = {
176} 174}
177check_request(request, expect, ignore) 175check_request(request, expect, ignore)
178 176
179write("testing manual basic auth: ") 177io.write("testing manual basic auth: ")
180request = { 178request = {
181 url = "http://" .. HOST .. prefix .. "/auth/index.html", 179 url = "http://" .. HOST .. prefix .. "/auth/index.html",
182 headers = { 180 headers = {
@@ -193,7 +191,7 @@ ignore = {
193} 191}
194check_request(request, expect, ignore) 192check_request(request, expect, ignore)
195 193
196write("testing automatic basic auth: ") 194io.write("testing automatic basic auth: ")
197request = { 195request = {
198 url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html" 196 url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html"
199} 197}
@@ -207,7 +205,7 @@ ignore = {
207} 205}
208check_request(request, expect, ignore) 206check_request(request, expect, ignore)
209 207
210write("testing auth info overriding: ") 208io.write("testing auth info overriding: ")
211request = { 209request = {
212 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", 210 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
213 user = "luasocket", 211 user = "luasocket",
@@ -223,7 +221,7 @@ ignore = {
223} 221}
224check_request(request, expect, ignore) 222check_request(request, expect, ignore)
225 223
226write("testing cgi output retrieval (probably chunked...): ") 224io.write("testing cgi output retrieval (probably chunked...): ")
227request = { 225request = {
228 url = "http://" .. HOST .. cgiprefix .. "/cat-index-html" 226 url = "http://" .. HOST .. cgiprefix .. "/cat-index-html"
229} 227}
@@ -237,7 +235,7 @@ ignore = {
237} 235}
238check_request(request, expect, ignore) 236check_request(request, expect, ignore)
239 237
240write("testing redirect loop: ") 238io.write("testing redirect loop: ")
241request = { 239request = {
242 url = "http://" .. HOST .. cgiprefix .. "/redirect-loop" 240 url = "http://" .. HOST .. cgiprefix .. "/redirect-loop"
243} 241}
@@ -251,7 +249,7 @@ ignore = {
251} 249}
252check_request(request, expect, ignore) 250check_request(request, expect, ignore)
253 251
254write("testing post method: ") 252io.write("testing post method: ")
255request = { 253request = {
256 url = "http://" .. HOST .. cgiprefix .. "/cat", 254 url = "http://" .. HOST .. cgiprefix .. "/cat",
257 method = "POST", 255 method = "POST",
@@ -267,7 +265,7 @@ ignore = {
267} 265}
268check_request(request, expect, ignore) 266check_request(request, expect, ignore)
269 267
270write("testing wrong scheme: ") 268io.write("testing wrong scheme: ")
271request = { 269request = {
272 url = "wrong://" .. HOST .. cgiprefix .. "/cat", 270 url = "wrong://" .. HOST .. cgiprefix .. "/cat",
273 method = "GET" 271 method = "GET"
@@ -280,31 +278,31 @@ ignore = {
280check_request(request, expect, ignore) 278check_request(request, expect, ignore)
281 279
282local body 280local body
283write("testing simple get function: ") 281io.write("testing simple get function: ")
284body = HTTP.get("http://" .. HOST .. prefix .. "/index.html") 282body = http.get("http://" .. HOST .. prefix .. "/index.html")
285check(body == index) 283check(body == index)
286 284
287write("testing simple get function with table args: ") 285io.write("testing simple get function with table args: ")
288body = HTTP.get { 286body = http.get {
289 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", 287 url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
290 user = "luasocket", 288 user = "luasocket",
291 password = "password" 289 password = "password"
292} 290}
293check(body == index) 291check(body == index)
294 292
295write("testing simple post function: ") 293io.write("testing simple post function: ")
296body = HTTP.post("http://" .. HOST .. cgiprefix .. "/cat", index) 294body = http.post("http://" .. HOST .. cgiprefix .. "/cat", index)
297check(body == index) 295check(body == index)
298 296
299write("testing simple post function with table args: ") 297io.write("testing simple post function with table args: ")
300body = HTTP.post { 298body = http.post {
301 url = "http://" .. HOST .. cgiprefix .. "/cat", 299 url = "http://" .. HOST .. cgiprefix .. "/cat",
302 body = index 300 body = index
303} 301}
304check(body == index) 302check(body == index)
305 303
306write("testing HEAD method: ") 304io.write("testing HEAD method: ")
307response = HTTP.request { 305response = http.request {
308 method = "HEAD", 306 method = "HEAD",
309 url = "http://www.tecgraf.puc-rio.br/~diego/" 307 url = "http://www.tecgraf.puc-rio.br/~diego/"
310} 308}
@@ -312,4 +310,4 @@ check(response and response.headers)
312 310
313print("passed all tests") 311print("passed all tests")
314 312
315print(format("done in %.2fs", _time() - t)) 313print(string.format("done in %.2fs", socket._time() - t))
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 73c10de..15f1dd8 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -2,32 +2,32 @@ HOST = HOST or "localhost"
2PORT = PORT or "8080" 2PORT = PORT or "8080"
3 3
4function pass(...) 4function pass(...)
5 local s = call(format, arg) 5 local s = string.format(unpack(arg))
6 write(s, "\n") 6 io.write(s, "\n")
7end 7end
8 8
9function fail(...) 9function fail(...)
10 local s = call(format, arg) 10 local s = string.format(unpack(arg))
11 write("ERROR: ", s, "!\n") 11 io.write("ERROR: ", s, "!\n")
12 exit() 12 os.exit()
13end 13end
14 14
15function warn(...) 15function warn(...)
16 local s = call(format, arg) 16 local s = format(unpack(arg))
17 write("WARNING: ", s, "\n") 17 io.write("WARNING: ", s, "\n")
18end 18end
19 19
20function remote(...) 20function remote(...)
21 local s = call(format, arg) 21 local s = string.format(unpack(arg))
22 s = gsub(s, "\n", ";") 22 s = string.gsub(s, "\n", ";")
23 s = gsub(s, "%s+", " ") 23 s = string.gsub(s, "%s+", " ")
24 s = gsub(s, "^%s*", "") 24 s = string.gsub(s, "^%s*", "")
25 control:send(s, "\n") 25 control:send(s, "\n")
26 control:receive() 26 control:receive()
27end 27end
28 28
29function test(test) 29function test(test)
30 write("----------------------------------------------\n", 30 io.write("----------------------------------------------\n",
31 "testing: ", test, "\n", 31 "testing: ", test, "\n",
32 "----------------------------------------------\n") 32 "----------------------------------------------\n")
33end 33end
@@ -66,51 +66,69 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone)
66 end 66 end
67end 67end
68 68
69write("----------------------------------------------\n", 69io.write("----------------------------------------------\n",
70"LuaSocket Test Procedures\n", 70"LuaSocket Test Procedures\n",
71"----------------------------------------------\n") 71"----------------------------------------------\n")
72 72
73if not _time or not _sleep then fail("not compiled with _DEBUG") end 73if not socket._time or not socket._sleep then
74 fail("not compiled with _DEBUG")
75end
74 76
75start = _time() 77start = socket._time()
76 78
77function tcpreconnect() 79function tcpreconnect()
78 write("attempting data connection... ") 80 io.write("attempting data connection... ")
79 if data then data:close() end 81 if data then data:close() end
80 remote [[ 82 remote [[
81 if data then data:close() data = nil end 83 if data then data:close() data = nil end
82 data = server:accept() 84 data = server:accept()
83 ]] 85 ]]
84 data, error = connect(HOST, PORT) 86 data, err = socket.connect(HOST, PORT)
85 if not data then fail(error) 87 if not data then fail(err)
86 else pass("connected!") end 88 else pass("connected!") end
87end 89end
88reconnect = tcpreconnect 90reconnect = tcpreconnect
89 91
90pass("attempting control connection...") 92pass("attempting control connection...")
91control, error = connect(HOST, PORT) 93control, err = socket.connect(HOST, PORT)
92if error then fail(error) 94if err then fail(err)
93else pass("connected!") end 95else pass("connected!") end
94 96
95------------------------------------------------------------------------ 97------------------------------------------------------------------------
96test("bugs") 98test("bugs")
97 99
98write("empty host connect: ") 100io.write("empty host connect: ")
99function empty_connect() 101function empty_connect()
100 if data then data:close() data = nil end 102 if data then data:close() data = nil end
101 remote [[ 103 remote [[
102 if data then data:close() data = nil end 104 if data then data:close() data = nil end
103 data = server:accept() 105 data = server:accept()
104 ]] 106 ]]
105 data, err = connect("", PORT) 107 data, err = socket.connect("", PORT)
106 if not data then 108 if not data then
107 pass("ok") 109 pass("ok")
108 data = connect(HOST, PORT) 110 data = socket.connect(HOST, PORT)
109 else fail("should not have connected!") end 111 else fail("should not have connected!") end
110end 112end
111 113
112empty_connect() 114empty_connect()
113 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
114------------------------------------------------------------------------ 132------------------------------------------------------------------------
115test("method registration") 133test("method registration")
116 134
@@ -133,7 +151,7 @@ test_methods(control, {
133}) 151})
134 152
135if udpsocket then 153if udpsocket then
136 test_methods(udpsocket(), { 154 test_methods(socket.udp(), {
137 "close", 155 "close",
138 "timeout", 156 "timeout",
139 "send", 157 "send",
@@ -147,50 +165,27 @@ if udpsocket then
147 }) 165 })
148end 166end
149 167
150test_methods(bind("*", 0), { 168test_methods(socket.bind("*", 0), {
151 "close", 169 "close",
152 "timeout", 170 "timeout",
153 "accept" 171 "accept"
154}) 172})
155 173
156if pipe then
157 local p1, p2 = pipe()
158 test_methods(p1, {
159 "close",
160 "timeout",
161 "send",
162 "receive"
163 })
164 test_methods(p2, {
165 "close",
166 "timeout",
167 "send",
168 "receive"
169 })
170end
171
172if filesocket then
173 test_methods(filesocket(0), {
174 "close",
175 "timeout",
176 "send",
177 "receive"
178 })
179end
180
181------------------------------------------------------------------------ 174------------------------------------------------------------------------
182test("select function") 175test("select function")
183function test_selectbugs() 176function test_selectbugs()
184 local r, s, e = select(nil, nil, 0.1) 177 local r, s, e = socket.select(nil, nil, 0.1)
185 assert(type(r) == "table" and type(s) == "table" and e == "timeout") 178 assert(type(r) == "table" and type(s) == "table" and e == "timeout")
186 pass("both nil: ok") 179 pass("both nil: ok")
187 local udp = udpsocket() 180 local udp = socket.udp()
188 udp:close() 181 udp:close()
189 r, s, e = select({ data }, { data }, 0.1) 182 r, s, e = socket.select({ udp }, { udp }, 0.1)
190 assert(type(r) == "table" and type(s) == "table" and e == "timeout") 183 assert(type(r) == "table" and type(s) == "table" and e == "timeout")
191 pass("closed sockets: ok") 184 pass("closed sockets: ok")
192 e = call(select, {"wrong", 1, 0.1}, "x", nil) 185 e = pcall(socket.select, "wrong", 1, 0.1)
193 assert(e == nil) 186 assert(e == false)
187 e = pcall(socket.select, {}, 1, 0.1)
188 assert(e == false)
194 pass("invalid input: ok") 189 pass("invalid input: ok")
195end 190end
196 191
@@ -202,8 +197,8 @@ reconnect()
202 197
203function test_asciiline(len) 198function test_asciiline(len)
204 local str, str10, back, err 199 local str, str10, back, err
205 str = strrep("x", mod(len, 10)) 200 str = string.rep("x", math.mod(len, 10))
206 str10 = strrep("aZb.c#dAe?", floor(len/10)) 201 str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
207 str = str .. str10 202 str = str .. str10
208 pass(len .. " byte(s) line") 203 pass(len .. " byte(s) line")
209remote "str = data:receive()" 204remote "str = data:receive()"
@@ -229,8 +224,9 @@ reconnect()
229 224
230function test_rawline(len) 225function test_rawline(len)
231 local str, str10, back, err 226 local str, str10, back, err
232 str = strrep(strchar(47), mod(len, 10)) 227 str = string.rep(string.char(47), math.mod(len, 10))
233 str10 = strrep(strchar(120,21,77,4,5,0,7,36,44,100), floor(len/10)) 228 str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
229 math.floor(len/10))
234 str = str .. str10 230 str = str .. str10
235 pass(len .. " byte(s) line") 231 pass(len .. " byte(s) line")
236remote "str = data:receive()" 232remote "str = data:receive()"
@@ -260,12 +256,12 @@ test("raw transfer")
260reconnect() 256reconnect()
261 257
262function test_raw(len) 258function test_raw(len)
263 local half = floor(len/2) 259 local half = math.floor(len/2)
264 local s1, s2, back, err 260 local s1, s2, back, err
265 s1 = strrep("x", half) 261 s1 = string.rep("x", half)
266 s2 = strrep("y", len-half) 262 s2 = string.rep("y", len-half)
267 pass(len .. " byte(s) block") 263 pass(len .. " byte(s) block")
268remote (format("str = data:receive(%d)", len)) 264remote (string.format("str = data:receive(%d)", len))
269 err = data:send(s1) 265 err = data:send(s1)
270 if err then fail(err) end 266 if err then fail(err) end
271 err = data:send(s2) 267 err = data:send(s2)
@@ -312,17 +308,18 @@ test("mixed patterns")
312reconnect() 308reconnect()
313 309
314function test_mixed(len) 310function test_mixed(len)
315 local inter = floor(len/3) 311 local inter = math.floor(len/3)
316 local p1 = "unix " .. strrep("x", inter) .. "line\n" 312 local p1 = "unix " .. string.rep("x", inter) .. "line\n"
317 local p2 = "dos " .. strrep("y", inter) .. "line\r\n" 313 local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
318 local p3 = "raw " .. strrep("z", inter) .. "bytes" 314 local p3 = "raw " .. string.rep("z", inter) .. "bytes"
319 local bp1, bp2, bp3 315 local bp1, bp2, bp3
320 pass(len .. " byte(s) patterns") 316 pass(len .. " byte(s) patterns")
321remote (format("str = data:receive(%d)", strlen(p1)+strlen(p2)+strlen(p3))) 317remote (string.format("str = data:receive(%d)",
318 string.len(p1)+string.len(p2)+string.len(p3)))
322 err = data:send(p1, p2, p3) 319 err = data:send(p1, p2, p3)
323 if err then fail(err) end 320 if err then fail(err) end
324remote "data:send(str)" 321remote "data:send(str)"
325 bp1, bp2, bp3, err = data:receive("*lu", "*l", strlen(p3)) 322 bp1, bp2, bp3, err = data:receive("*lu", "*l", string.len(p3))
326 if err then fail(err) end 323 if err then fail(err) end
327 if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then 324 if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 then
328 pass("patterns match") 325 pass("patterns match")
@@ -349,7 +346,7 @@ function test_closed()
349 local str = 'little string' 346 local str = 'little string'
350 reconnect() 347 reconnect()
351 pass("trying read detection") 348 pass("trying read detection")
352 remote (format ([[ 349 remote (string.format ([[
353 data:send('%s') 350 data:send('%s')
354 data:close() 351 data:close()
355 data = nil 352 data = nil
@@ -366,7 +363,7 @@ function test_closed()
366 data:close() 363 data:close()
367 data = nil 364 data = nil
368 ]] 365 ]]
369 err, total = data:send(strrep("ugauga", 100000)) 366 err, total = data:send(string.rep("ugauga", 100000))
370 if not err then 367 if not err then
371pass("failed: output buffer is at least %d bytes long!", total) 368pass("failed: output buffer is at least %d bytes long!", total)
372 elseif err ~= "closed" then 369 elseif err ~= "closed" then
@@ -384,19 +381,19 @@ function test_blockingtimeoutreceive(len, tm, sl)
384 local str, err, total 381 local str, err, total
385 reconnect() 382 reconnect()
386 pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) 383 pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl)
387 remote (format ([[ 384 remote (string.format ([[
388 data:timeout(%d) 385 data:timeout(%d)
389 str = strrep('a', %d) 386 str = string.rep('a', %d)
390 data:send(str) 387 data:send(str)
391 print('server: sleeping for %ds') 388 print('server: sleeping for %ds')
392 _sleep(%d) 389 socket._sleep(%d)
393 print('server: woke up') 390 print('server: woke up')
394 data:send(str) 391 data:send(str)
395 ]], 2*tm, len, sl, sl)) 392 ]], 2*tm, len, sl, sl))
396 data:timeout(tm, "return") 393 data:timeout(tm, "return")
397 str, err, elapsed = data:receive(2*len) 394 str, err, elapsed = data:receive(2*len)
398 check_timeout(tm, sl, elapsed, err, "receive", "return", 395 check_timeout(tm, sl, elapsed, err, "receive", "return",
399 strlen(str) == 2*len) 396 string.len(str) == 2*len)
400end 397end
401test_blockingtimeoutreceive(800091, 1, 3) 398test_blockingtimeoutreceive(800091, 1, 3)
402test_blockingtimeoutreceive(800091, 2, 3) 399test_blockingtimeoutreceive(800091, 2, 3)
@@ -409,16 +406,16 @@ function test_returntimeoutsend(len, tm, sl)
409 local str, err, total 406 local str, err, total
410 reconnect() 407 reconnect()
411 pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl) 408 pass("%d bytes, %ds return timeout, %ds pause", len, tm, sl)
412 remote (format ([[ 409 remote (string.format ([[
413 data:timeout(%d) 410 data:timeout(%d)
414 str = data:receive(%d) 411 str = data:receive(%d)
415 print('server: sleeping for %ds') 412 print('server: sleeping for %ds')
416 _sleep(%d) 413 socket._sleep(%d)
417 print('server: woke up') 414 print('server: woke up')
418 str = data:receive(%d) 415 str = data:receive(%d)
419 ]], 2*tm, len, sl, sl, len)) 416 ]], 2*tm, len, sl, sl, len))
420 data:timeout(tm, "return") 417 data:timeout(tm, "return")
421 str = strrep("a", 2*len) 418 str = string.rep("a", 2*len)
422 err, total, elapsed = data:send(str) 419 err, total, elapsed = data:send(str)
423 check_timeout(tm, sl, elapsed, err, "send", "return", 420 check_timeout(tm, sl, elapsed, err, "send", "return",
424 total == 2*len) 421 total == 2*len)
@@ -435,19 +432,19 @@ function test_blockingtimeoutreceive(len, tm, sl)
435 local str, err, total 432 local str, err, total
436 reconnect() 433 reconnect()
437 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) 434 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
438 remote (format ([[ 435 remote (string.format ([[
439 data:timeout(%d) 436 data:timeout(%d)
440 str = strrep('a', %d) 437 str = string.rep('a', %d)
441 data:send(str) 438 data:send(str)
442 print('server: sleeping for %ds') 439 print('server: sleeping for %ds')
443 _sleep(%d) 440 socket._sleep(%d)
444 print('server: woke up') 441 print('server: woke up')
445 data:send(str) 442 data:send(str)
446 ]], 2*tm, len, sl, sl)) 443 ]], 2*tm, len, sl, sl))
447 data:timeout(tm) 444 data:timeout(tm)
448 str, err, elapsed = data:receive(2*len) 445 str, err, elapsed = data:receive(2*len)
449 check_timeout(tm, sl, elapsed, err, "receive", "blocking", 446 check_timeout(tm, sl, elapsed, err, "receive", "blocking",
450 strlen(str) == 2*len) 447 string.len(str) == 2*len)
451end 448end
452test_blockingtimeoutreceive(800091, 1, 3) 449test_blockingtimeoutreceive(800091, 1, 3)
453test_blockingtimeoutreceive(800091, 2, 3) 450test_blockingtimeoutreceive(800091, 2, 3)
@@ -461,16 +458,16 @@ function test_blockingtimeoutsend(len, tm, sl)
461 local str, err, total 458 local str, err, total
462 reconnect() 459 reconnect()
463 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) 460 pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
464 remote (format ([[ 461 remote (string.format ([[
465 data:timeout(%d) 462 data:timeout(%d)
466 str = data:receive(%d) 463 str = data:receive(%d)
467 print('server: sleeping for %ds') 464 print('server: sleeping for %ds')
468 _sleep(%d) 465 socket._sleep(%d)
469 print('server: woke up') 466 print('server: woke up')
470 str = data:receive(%d) 467 str = data:receive(%d)
471 ]], 2*tm, len, sl, sl, len)) 468 ]], 2*tm, len, sl, sl, len))
472 data:timeout(tm) 469 data:timeout(tm)
473 str = strrep("a", 2*len) 470 str = string.rep("a", 2*len)
474 err, total, elapsed = data:send(str) 471 err, total, elapsed = data:send(str)
475 check_timeout(tm, sl, elapsed, err, "send", "blocking", 472 check_timeout(tm, sl, elapsed, err, "send", "blocking",
476 total == 2*len) 473 total == 2*len)
@@ -481,4 +478,4 @@ test_blockingtimeoutsend(800091, 3, 2)
481test_blockingtimeoutsend(800091, 3, 1) 478test_blockingtimeoutsend(800091, 3, 1)
482 479
483------------------------------------------------------------------------ 480------------------------------------------------------------------------
484test(format("done in %.2fs", _time() - start)) 481test(string.format("done in %.2fs", socket._time() - start))
diff --git a/test/testsrvr.lua b/test/testsrvr.lua
index 227e341..141c058 100644
--- a/test/testsrvr.lua
+++ b/test/testsrvr.lua
@@ -1,8 +1,8 @@
1HOST = HOST or "localhost" 1HOST = HOST or "localhost"
2PORT = PORT or "8080" 2PORT = PORT or "8080"
3 3
4server, error = bind(HOST, PORT) 4server, error = socket.bind(HOST, PORT)
5if not server then print("server: " .. tostring(error)) exit() end 5if not server then print("server: " .. tostring(error)) os.exit() end
6while 1 do 6while 1 do
7 print("server: waiting for client connection..."); 7 print("server: waiting for client connection...");
8 control = server:accept() 8 control = server:accept()
@@ -19,6 +19,6 @@ while 1 do
19 print("server: closing connection...") 19 print("server: closing connection...")
20 break 20 break
21 end 21 end
22 dostring(command) 22 (loadstring(command))()
23 end 23 end
24end 24end
diff --git a/test/urltest.lua b/test/urltest.lua
index da7475a..8ca36fe 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -30,7 +30,7 @@ end
30 30
31local check_parse_path = function(path, expect) 31local check_parse_path = function(path, expect)
32 local parsed = URL.parse_path(path) 32 local parsed = URL.parse_path(path)
33 for i = 1, max(getn(parsed), getn(expect)) do 33 for i = 1, math.max(table.getn(parsed), table.getn(expect)) do
34 if parsed[i] ~= expect[i] then 34 if parsed[i] ~= expect[i] then
35 print(path) 35 print(path)
36 write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])), 36 write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])),