diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-25 21:35:17 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-25 21:35:17 +0000 |
commit | 6dd115015c6f2514a2f180aaf474a66364939c31 (patch) | |
tree | 70440522e4aa720c2813258b6923afd6a25e41b0 /test/smtptest.lua | |
parent | d36460a249261680a4a920f05767b7f7cf2868ba (diff) | |
download | luasocket-6dd115015c6f2514a2f180aaf474a66364939c31.tar.gz luasocket-6dd115015c6f2514a2f180aaf474a66364939c31.tar.bz2 luasocket-6dd115015c6f2514a2f180aaf474a66364939c31.zip |
Initial revision
Diffstat (limited to 'test/smtptest.lua')
-rw-r--r-- | test/smtptest.lua | 306 |
1 files changed, 306 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 @@ | |||
1 | local sent = {} | ||
2 | |||
3 | local from = "luasock@tecgraf.puc-rio.br" | ||
4 | local server = "mail.tecgraf.puc-rio.br" | ||
5 | local rcpt = "luasock@tecgraf.puc-rio.br" | ||
6 | |||
7 | local parse = {} | ||
8 | |||
9 | local name = "/var/spool/mail/luasock" | ||
10 | |||
11 | local t = _time() | ||
12 | local err | ||
13 | |||
14 | function mysetglobal (varname, oldvalue, newvalue) | ||
15 | print("changing " .. varname) | ||
16 | %rawset(%globals(), varname, newvalue) | ||
17 | end | ||
18 | function mygetglobal (varname, newvalue) | ||
19 | print("checking " .. varname) | ||
20 | return %rawget(%globals(), varname) | ||
21 | end | ||
22 | settagmethod(tag(nil), "setglobal", mysetglobal) | ||
23 | settagmethod(tag(nil), "getglobal", mygetglobal) | ||
24 | |||
25 | assert(dofile("../lua/smtp.lua")) | ||
26 | assert(dofile("../lua/cl-compat.lua")) | ||
27 | |||
28 | local total = function() | ||
29 | local t = 0 | ||
30 | for i = 1, getn(%sent) do | ||
31 | t = t + %sent[i].count | ||
32 | end | ||
33 | return t | ||
34 | end | ||
35 | |||
36 | local similar = function(s1, s2) | ||
37 | return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", "")) | ||
38 | end | ||
39 | |||
40 | function 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 | ||
58 | end | ||
59 | |||
60 | function 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 } | ||
67 | end | ||
68 | |||
69 | function 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 | ||
80 | end | ||
81 | |||
82 | local 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 | ||
88 | end | ||
89 | |||
90 | local capture = function(cmd) | ||
91 | readfrom("| " .. cmd) | ||
92 | local s = read("*a") | ||
93 | readfrom() | ||
94 | return s | ||
95 | end | ||
96 | |||
97 | local fail = function(s) | ||
98 | s = s or "failed!" | ||
99 | print(s) | ||
100 | exit() | ||
101 | end | ||
102 | |||
103 | local empty = function() | ||
104 | local f = openfile(%name, "w") | ||
105 | closefile(f) | ||
106 | end | ||
107 | |||
108 | local get = function() | ||
109 | return %readfile(%name) | ||
110 | end | ||
111 | |||
112 | local list = function() | ||
113 | return %capture("ls -l " .. %name) | ||
114 | end | ||
115 | |||
116 | local 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 | ||
122 | end | ||
123 | |||
124 | local check_body = function(sent, got) | ||
125 | sent = sent or "" | ||
126 | got = got or "" | ||
127 | if not %similar(sent, got) then %fail("bodies differ!") end | ||
128 | end | ||
129 | |||
130 | local 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") | ||
143 | end | ||
144 | |||
145 | local 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) | ||
152 | end | ||
153 | |||
154 | local mark = function() | ||
155 | local time = _time() | ||
156 | return { time = time } | ||
157 | end | ||
158 | |||
159 | local 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 | ||
175 | end | ||
176 | |||
177 | local stuffed_body = [[ | ||
178 | This message body needs to be | ||
179 | stuffed because it has a dot | ||
180 | . | ||
181 | by itself on a line. | ||
182 | Otherwise the mailer would | ||
183 | think that the dot | ||
184 | . | ||
185 | is the end of the message | ||
186 | and the remaining will cause | ||
187 | a lot of trouble. | ||
188 | ]] | ||
189 | |||
190 | insert(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 | |||
201 | insert(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 | |||
220 | insert(sent, { | ||
221 | from = from, | ||
222 | rcpt = rcpt, | ||
223 | server = server, | ||
224 | body = "simple message body", | ||
225 | title = "simple message" | ||
226 | }) | ||
227 | |||
228 | insert(sent, { | ||
229 | from = from, | ||
230 | rcpt = rcpt, | ||
231 | server = server, | ||
232 | body = stuffed_body, | ||
233 | title = "stuffed message body" | ||
234 | }) | ||
235 | |||
236 | insert(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 | |||
250 | insert(sent, { | ||
251 | from = from, | ||
252 | rcpt = rcpt, | ||
253 | title = "minimum message" | ||
254 | }) | ||
255 | |||
256 | write("testing host not found: ") | ||
257 | local c, e = connect("wrong.host", 25) | ||
258 | local err = SMTP.mail{ | ||
259 | from = from, | ||
260 | rcpt = rcpt, | ||
261 | server = "wrong.host" | ||
262 | } | ||
263 | if e ~= err then fail("wrong error message") | ||
264 | else print("ok") end | ||
265 | |||
266 | write("testing invalid from: ") | ||
267 | local err = SMTP.mail{ | ||
268 | from = ' " " (( _ * ', | ||
269 | rcpt = rcpt, | ||
270 | } | ||
271 | if not err then fail("wrong error message") | ||
272 | else print(err) end | ||
273 | |||
274 | write("testing no rcpt: ") | ||
275 | local err = SMTP.mail{ | ||
276 | from = from, | ||
277 | } | ||
278 | if not err then fail("wrong error message") | ||
279 | else print(err) end | ||
280 | |||
281 | write("clearing mailbox: ") | ||
282 | empty() | ||
283 | print("ok") | ||
284 | |||
285 | write("sending messages: ") | ||
286 | for i = 1, getn(sent) do | ||
287 | err = SMTP.mail(sent[i]) | ||
288 | if err then fail(err) end | ||
289 | write("+") | ||
290 | flush(_STDOUT) | ||
291 | end | ||
292 | print("ok") | ||
293 | |||
294 | wait(mark(), total()) | ||
295 | |||
296 | write("parsing mailbox: ") | ||
297 | local mbox = parse.mbox(get()) | ||
298 | print(getn(mbox) .. " messages found!") | ||
299 | |||
300 | for i = 1, getn(mbox) do | ||
301 | check(sent, mbox[i]) | ||
302 | end | ||
303 | |||
304 | |||
305 | print("passed all tests") | ||
306 | print(format("done in %.2fs", _time() - t)) | ||