aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2001-09-25 21:35:17 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2001-09-25 21:35:17 +0000
commit6dd115015c6f2514a2f180aaf474a66364939c31 (patch)
tree70440522e4aa720c2813258b6923afd6a25e41b0
parentd36460a249261680a4a920f05767b7f7cf2868ba (diff)
downloadluasocket-6dd115015c6f2514a2f180aaf474a66364939c31.tar.gz
luasocket-6dd115015c6f2514a2f180aaf474a66364939c31.tar.bz2
luasocket-6dd115015c6f2514a2f180aaf474a66364939c31.zip
Initial revision
Diffstat (limited to '')
-rw-r--r--test/smtptest.lua306
-rw-r--r--test/urltest.lua388
2 files changed, 694 insertions, 0 deletions
diff --git a/test/smtptest.lua b/test/smtptest.lua
new file mode 100644
index 0000000..fcd238b
--- /dev/null
+++ b/test/smtptest.lua
@@ -0,0 +1,306 @@
1local sent = {}
2
3local from = "luasock@tecgraf.puc-rio.br"
4local server = "mail.tecgraf.puc-rio.br"
5local rcpt = "luasock@tecgraf.puc-rio.br"
6
7local parse = {}
8
9local name = "/var/spool/mail/luasock"
10
11local t = _time()
12local err
13
14function mysetglobal (varname, oldvalue, newvalue)
15 print("changing " .. varname)
16 %rawset(%globals(), varname, newvalue)
17end
18function mygetglobal (varname, newvalue)
19 print("checking " .. varname)
20 return %rawget(%globals(), varname)
21end
22settagmethod(tag(nil), "setglobal", mysetglobal)
23settagmethod(tag(nil), "getglobal", mygetglobal)
24
25assert(dofile("../lua/smtp.lua"))
26assert(dofile("../lua/cl-compat.lua"))
27
28local total = function()
29 local t = 0
30 for i = 1, getn(%sent) do
31 t = t + %sent[i].count
32 end
33 return t
34end
35
36local similar = function(s1, s2)
37 return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
38end
39
40function parse.headers(headers_s)
41 local headers = {}
42 headers_s = "\n" .. headers_s .. "$$$:\n"
43 local i, j = 1, 1
44 local name, value, _
45 while 1 do
46 j = strfind(headers_s, "\n%S-:", i+1)
47 if not j then break end
48 _, _, name, value = strfind(strsub(headers_s, i+1, j-1), "(%S-): (.*)")
49 value = gsub(value, "\r\n", "\n")
50 value = gsub(value, "\n%s*", " ")
51 name = strlower(name)
52 if headers[name] then headers[name] = headers[name] .. ", " .. value
53 else headers[name] = value end
54 i, j = j, i
55 end
56 headers["$$$"] = nil
57 return headers
58end
59
60function parse.message(message_s)
61 message_s = gsub(message_s, "^.-\n", "")
62 local _, headers_s, body
63 _, _, headers_s, body = strfind(message_s, "^(.-\n)\n(.*)")
64 headers_s = headers_s or ""
65 body = body or ""
66 return { headers = %parse.headers(headers_s), body = body }
67end
68
69function parse.mbox(mbox_s)
70 local mbox = {}
71 mbox_s = "\n" .. mbox_s .. "\nFrom "
72 local i, j = 1, 1
73 while 1 do
74 j = strfind(mbox_s, "\nFrom ", i + 1)
75 if not j then break end
76 tinsert(mbox, %parse.message(strsub(mbox_s, i + 1, j - 1)))
77 i, j = j, i
78 end
79 return mbox
80end
81
82local readfile = function(name)
83 local f = readfrom(name)
84 if not f then return nil end
85 local s = read("*a")
86 readfrom()
87 return s
88end
89
90local capture = function(cmd)
91 readfrom("| " .. cmd)
92 local s = read("*a")
93 readfrom()
94 return s
95end
96
97local fail = function(s)
98 s = s or "failed!"
99 print(s)
100 exit()
101end
102
103local empty = function()
104 local f = openfile(%name, "w")
105 closefile(f)
106end
107
108local get = function()
109 return %readfile(%name)
110end
111
112local list = function()
113 return %capture("ls -l " .. %name)
114end
115
116local check_headers = function(sent, got)
117 sent = sent or {}
118 got = got or {}
119 for i,v in sent do
120 if not %similar(v, got[i]) then %fail("header " .. v .. "failed!") end
121 end
122end
123
124local check_body = function(sent, got)
125 sent = sent or ""
126 got = got or ""
127 if not %similar(sent, got) then %fail("bodies differ!") end
128end
129
130local check = function(sent, m)
131 write("checking ", m.headers.title, ": ")
132 for i = 1, getn(sent) do
133 local s = sent[i]
134 if s.title == m.headers.title and s.count > 0 then
135 %check_headers(s.headers, m.headers)
136 %check_body(s.body, m.body)
137 s.count = s.count - 1
138 print("ok")
139 return
140 end
141 end
142 %fail("not found")
143end
144
145local insert = function(sent, message)
146 if type(message.rcpt) == "table" then
147 message.count = getn(message.rcpt)
148 else message.count = 1 end
149 message.headers = message.headers or {}
150 message.headers.title = message.title
151 tinsert(sent, message)
152end
153
154local mark = function()
155 local time = _time()
156 return { time = time }
157end
158
159local wait = function(sentinel, n)
160 local to
161 write("waiting for ", n, " messages: ")
162 while 1 do
163 local mbox = %parse.mbox(%get())
164 if n == getn(mbox) then break end
165 if _time() - sentinel.time > 50 then
166 to = 1
167 break
168 end
169 _sleep(1)
170 write(".")
171 flush(_STDOUT)
172 end
173 if to then %fail("timeout")
174 else print("ok") end
175end
176
177local stuffed_body = [[
178This message body needs to be
179stuffed because it has a dot
180.
181by itself on a line.
182Otherwise the mailer would
183think that the dot
184.
185is the end of the message
186and the remaining will cause
187a lot of trouble.
188]]
189
190insert(sent, {
191 from = from,
192 rcpt = {
193 "luasock2@tecgraf.puc-rio.br",
194 "luasock",
195 "luasock1"
196 },
197 body = "multiple rcpt body",
198 title = "multiple rcpt",
199})
200
201insert(sent, {
202 from = from,
203 rcpt = {
204 "luasock2@tecgraf.puc-rio.br",
205 "luasock",
206 "luasock1"
207 },
208 headers = {
209 header1 = "header 1",
210 header2 = "header 2",
211 header3 = "header 3",
212 header4 = "header 4",
213 header5 = "header 5",
214 header6 = "header 6",
215 },
216 body = stuffed_body,
217 title = "complex message",
218})
219
220insert(sent, {
221 from = from,
222 rcpt = rcpt,
223 server = server,
224 body = "simple message body",
225 title = "simple message"
226})
227
228insert(sent, {
229 from = from,
230 rcpt = rcpt,
231 server = server,
232 body = stuffed_body,
233 title = "stuffed message body"
234})
235
236insert(sent, {
237 from = from,
238 rcpt = rcpt,
239 headers = {
240 header1 = "header 1",
241 header2 = "header 2",
242 header3 = "header 3",
243 header4 = "header 4",
244 header5 = "header 5",
245 header6 = "header 6",
246 },
247 title = "multiple headers"
248})
249
250insert(sent, {
251 from = from,
252 rcpt = rcpt,
253 title = "minimum message"
254})
255
256write("testing host not found: ")
257local c, e = connect("wrong.host", 25)
258local err = SMTP.mail{
259 from = from,
260 rcpt = rcpt,
261 server = "wrong.host"
262}
263if e ~= err then fail("wrong error message")
264else print("ok") end
265
266write("testing invalid from: ")
267local err = SMTP.mail{
268 from = ' " " (( _ * ',
269 rcpt = rcpt,
270}
271if not err then fail("wrong error message")
272else print(err) end
273
274write("testing no rcpt: ")
275local err = SMTP.mail{
276 from = from,
277}
278if not err then fail("wrong error message")
279else print(err) end
280
281write("clearing mailbox: ")
282empty()
283print("ok")
284
285write("sending messages: ")
286for i = 1, getn(sent) do
287 err = SMTP.mail(sent[i])
288 if err then fail(err) end
289 write("+")
290 flush(_STDOUT)
291end
292print("ok")
293
294wait(mark(), total())
295
296write("parsing mailbox: ")
297local mbox = parse.mbox(get())
298print(getn(mbox) .. " messages found!")
299
300for i = 1, getn(mbox) do
301 check(sent, mbox[i])
302end
303
304
305print("passed all tests")
306print(format("done in %.2fs", _time() - t))
diff --git a/test/urltest.lua b/test/urltest.lua
new file mode 100644
index 0000000..20a4f7d
--- /dev/null
+++ b/test/urltest.lua
@@ -0,0 +1,388 @@
1function mysetglobal (varname, oldvalue, newvalue)
2 print("changing " .. varname)
3 %rawset(%globals(), varname, newvalue)
4end
5function mygetglobal (varname, newvalue)
6 print("checking " .. varname)
7 return %rawget(%globals(), varname)
8end
9settagmethod(tag(nil), "setglobal", mysetglobal)
10settagmethod(tag(nil), "getglobal", mygetglobal)
11
12assert(dofile "../lua/code.lua")
13assert(dofile "../lua/url.lua")
14
15local check_protect = function(parsed, path)
16 local built = URL.build_path(parsed)
17 if built ~= path then
18 print(built, path)
19 print("path composition failed.")
20 exit()
21 end
22end
23
24local check_invert = function(url)
25 local parsed = URL.parse_url(url)
26 parsed.path = URL.build_path(URL.parse_path(parsed.path))
27 local rebuilt = URL.build_url(parsed)
28 if rebuilt ~= url then
29 print(url, rebuilt)
30 print("original and rebuilt are different")
31 exit()
32 end
33end
34
35local check_parse_path = function(path, expect)
36 local parsed = URL.parse_path(path)
37 for i = 1, max(getn(parsed), getn(expect)) do
38 if parsed[i] ~= expect[i] then
39 print(path)
40 write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])),
41 "' but expected '", Code.hexa(tostring(expect[i])), "'\n")
42 exit()
43 end
44 end
45 if expect.is_directory ~= parsed.is_directory then
46 print(path)
47 print("is_directory mismatch")
48 exit()
49 end
50 if expect.is_absolute ~= parsed.is_absolute then
51 print(path)
52 print("is_absolute mismatch")
53 exit()
54 end
55 local built = URL.build_path(expect)
56 if built ~= path then
57 print(built, path)
58 print("path composition failed.")
59 exit()
60 end
61end
62
63local check_absolute_url = function(base, relative, absolute)
64 local res = URL.absolute_url(base, relative)
65 if res ~= absolute then
66 write("absolute: In test for '", relative, "' expected '",
67 absolute, "' but got '", res, "'\n")
68 exit()
69 end
70end
71
72local check_parse_url = function(gaba)
73 local url = gaba.url
74 gaba.url = nil
75 local parsed = URL.parse_url(url)
76 for i, v in gaba do
77 if v ~= parsed[i] then
78 write("parse: In test for '", url, "' expected ", i, " = '",
79 v, "' but got '", tostring(parsed[i]), "'\n")
80 for i,v in parsed do print(i,v) end
81 exit()
82 end
83 end
84 for i, v in parsed do
85 if v ~= gaba[i] then
86 write("parse: In test for '", url, "' expected ", i, " = '",
87 tostring(gaba[i]), "' but got '", v, "'\n")
88 for i,v in parsed do print(i,v) end
89 exit()
90 end
91 end
92end
93
94print("testing URL parsing")
95check_parse_url{
96 url = "scheme://userinfo@host:port/path;params?query#fragment",
97 scheme = "scheme",
98 authority = "userinfo@host:port",
99 host = "host",
100 port = "port",
101 userinfo = "userinfo",
102 user = "userinfo",
103 path = "/path",
104 params = "params",
105 query = "query",
106 fragment = "fragment"
107}
108
109check_parse_url{
110 url = "scheme://user:password@host:port/path;params?query#fragment",
111 scheme = "scheme",
112 authority = "user:password@host:port",
113 host = "host",
114 port = "port",
115 userinfo = "user:password",
116 user = "user",
117 password = "password",
118 path = "/path",
119 params = "params",
120 query = "query",
121 fragment = "fragment",
122}
123
124check_parse_url{
125 url = "scheme://userinfo@host:port/path;params?query#",
126 scheme = "scheme",
127 authority = "userinfo@host:port",
128 host = "host",
129 port = "port",
130 userinfo = "userinfo",
131 user = "userinfo",
132 path = "/path",
133 params = "params",
134 query = "query",
135 fragment = ""
136}
137
138check_parse_url{
139 url = "scheme://userinfo@host:port/path;params?#fragment",
140 scheme = "scheme",
141 authority = "userinfo@host:port",
142 host = "host",
143 port = "port",
144 userinfo = "userinfo",
145 user = "userinfo",
146 path = "/path",
147 params = "params",
148 query = "",
149 fragment = "fragment"
150}
151
152check_parse_url{
153 url = "scheme://userinfo@host:port/path;params#fragment",
154 scheme = "scheme",
155 authority = "userinfo@host:port",
156 host = "host",
157 port = "port",
158 userinfo = "userinfo",
159 user = "userinfo",
160 path = "/path",
161 params = "params",
162 fragment = "fragment"
163}
164
165check_parse_url{
166 url = "scheme://userinfo@host:port/path;?query#fragment",
167 scheme = "scheme",
168 authority = "userinfo@host:port",
169 host = "host",
170 port = "port",
171 userinfo = "userinfo",
172 user = "userinfo",
173 path = "/path",
174 params = "",
175 query = "query",
176 fragment = "fragment"
177}
178
179check_parse_url{
180 url = "scheme://userinfo@host:port/path?query#fragment",
181 scheme = "scheme",
182 authority = "userinfo@host:port",
183 host = "host",
184 port = "port",
185 userinfo = "userinfo",
186 user = "userinfo",
187 path = "/path",
188 query = "query",
189 fragment = "fragment"
190}
191
192check_parse_url{
193 url = "scheme://userinfo@host:port/;params?query#fragment",
194 scheme = "scheme",
195 authority = "userinfo@host:port",
196 host = "host",
197 port = "port",
198 userinfo = "userinfo",
199 user = "userinfo",
200 path = "/",
201 params = "params",
202 query = "query",
203 fragment = "fragment"
204}
205
206check_parse_url{
207 url = "scheme://userinfo@host:port",
208 scheme = "scheme",
209 authority = "userinfo@host:port",
210 host = "host",
211 port = "port",
212 userinfo = "userinfo",
213 user = "userinfo",
214}
215
216check_parse_url{
217 url = "//userinfo@host:port/path;params?query#fragment",
218 authority = "userinfo@host:port",
219 host = "host",
220 port = "port",
221 userinfo = "userinfo",
222 user = "userinfo",
223 path = "/path",
224 params = "params",
225 query = "query",
226 fragment = "fragment"
227}
228
229check_parse_url{
230 url = "//userinfo@host:port/path",
231 authority = "userinfo@host:port",
232 host = "host",
233 port = "port",
234 userinfo = "userinfo",
235 user = "userinfo",
236 path = "/path",
237}
238
239check_parse_url{
240 url = "//userinfo@host/path",
241 authority = "userinfo@host",
242 host = "host",
243 userinfo = "userinfo",
244 user = "userinfo",
245 path = "/path",
246}
247
248check_parse_url{
249 url = "//user:password@host/path",
250 authority = "user:password@host",
251 host = "host",
252 userinfo = "user:password",
253 password = "password",
254 user = "user",
255 path = "/path",
256}
257
258check_parse_url{
259 url = "//user:@host/path",
260 authority = "user:@host",
261 host = "host",
262 userinfo = "user:",
263 password = "",
264 user = "user",
265 path = "/path",
266}
267
268check_parse_url{
269 url = "//user@host:port/path",
270 authority = "user@host:port",
271 host = "host",
272 userinfo = "user",
273 user = "user",
274 port = "port",
275 path = "/path",
276}
277
278check_parse_url{
279 url = "//host:port/path",
280 authority = "host:port",
281 port = "port",
282 host = "host",
283 path = "/path",
284}
285
286check_parse_url{
287 url = "//host/path",
288 authority = "host",
289 host = "host",
290 path = "/path",
291}
292
293check_parse_url{
294 url = "//host",
295 authority = "host",
296 host = "host",
297}
298
299check_parse_url{
300 url = "/path",
301 path = "/path",
302}
303
304check_parse_url{
305 url = "path",
306 path = "path",
307}
308
309-- standard RFC tests
310print("testing absolute resolution")
311check_absolute_url("http://a/b/c/d;p?q#f", "g:h", "g:h")
312check_absolute_url("http://a/b/c/d;p?q#f", "g", "http://a/b/c/g")
313check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g")
314check_absolute_url("http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/")
315check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g")
316check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g")
317check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y")
318check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y")
319check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x")
320check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s")
321check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s")
322check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x")
323check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s")
324check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x")
325check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x")
326check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s")
327check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/")
328check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/")
329check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/")
330check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/")
331check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g")
332check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/")
333check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/")
334check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g")
335check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f")
336check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g")
337check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g")
338check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.")
339check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g")
340check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..")
341check_absolute_url("http://a/b/c/d;p?q#f", "..g", "http://a/b/c/..g")
342check_absolute_url("http://a/b/c/d;p?q#f", "./../g", "http://a/b/g")
343check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/")
344check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h")
345check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h")
346
347-- extra tests
348check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f")
349check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f")
350check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f")
351check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f")
352check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html",
353 "http://velox.telemar.com.br/dashboard/index.html")
354
355print("testing path parsing and composition")
356check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })
357check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 })
358check_parse_path("eu/tu/ele/nos/vos/eles/",
359 { "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1})
360check_parse_path("/", { is_absolute = 1, is_directory = 1})
361check_parse_path("", { })
362check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/",
363 { "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1})
364check_parse_path("eu/tu", { "eu", "tu" })
365
366print("testing path protection")
367check_protect({ "eu", "-_.!~*'():@&=+$,", "tu" }, "eu/-_.!~*'():@&=+$,/tu")
368check_protect({ "eu ", "~diego" }, "eu%20/~diego")
369check_protect({ "/eu>", "<diego?" }, "%2feu%3e/%3cdiego%3f")
370check_protect({ "\\eu]", "[diego`" }, "%5ceu%5d/%5bdiego%60")
371check_protect({ "{eu}", "|diego\127" }, "%7beu%7d/%7cdiego%7f")
372
373print("testing inversion")
374check_invert("http:")
375check_invert("a/b/c/d.html")
376check_invert("//net_loc")
377check_invert("http:a/b/d/c.html")
378check_invert("//net_loc/a/b/d/c.html")
379check_invert("http://net_loc/a/b/d/c.html")
380check_invert("//who:isit@net_loc")
381check_invert("http://he:man@boo.bar/a/b/c/i.html;type=moo?this=that#mark")
382check_invert("/b/c/d#fragment")
383check_invert("/b/c/d;param#fragment")
384check_invert("/b/c/d;param?query#fragment")
385check_invert("/b/c/d?query")
386check_invert("/b/c/d;param?query")
387
388print("the library passed all tests")