Debian Codegraph @

texk/web2c/luatexdir/luasocket/test/smtptest.lua

raw symbols SCIP
1 local sent = {}
2
3 local from = "diego@localhost"
4 local server = "localhost"
5 local rcpt = "luasocket@localhost"
6
7 local files = {
8 "/var/spool/mail/luasocket",
9 "/var/spool/mail/luasock1",
10 "/var/spool/mail/luasock2",
11 "/var/spool/mail/luasock3",
12 }
13
14 local t = socket.time()
15 local err
16
17 dofile("mbox.lua")
18 local parse = mbox.parse
19 dofile("testsupport.lua")
20
21 local total = function()
22 local t = 0
23 for i = 1, #sent do
24 t = t + sent[i].count
25 end
26 return t
27 end
28
29 local similar = function(s1, s2)
30 return
31 string.lower(string.gsub(s1, "%s", "")) ==
32 string.lower(string.gsub(s2, "%s", ""))
33 end
34
35 local fail = function(s)
36 s = s or "failed!"
37 print(s)
38 os.exit()
39 end
40
41 local readfile = function(name)
42 local f = io.open(name, "r")
43 if not f then
44 fail("unable to open file!")
45 return nil
46 end
47 local s = f:read("*a")
48 f:close()
49 return s
50 end
51
52 local empty = function()
53 for i,v in ipairs(files) do
54 local f = io.open(v, "w")
55 if not f then
56 fail("unable to open file!")
57 end
58 f:close()
59 end
60 end
61
62 local get = function()
63 local s = ""
64 for i,v in ipairs(files) do
65 s = s .. "\n" .. readfile(v)
66 end
67 return s
68 end
69
70 local check_headers = function(sent, got)
71 sent = sent or {}
72 got = got or {}
73 for i,v in pairs(sent) do
74 if not similar(v, got[i]) then fail("header " .. v .. "failed!") end
75 end
76 end
77
78 local check_body = function(sent, got)
79 sent = sent or ""
80 got = got or ""
81 if not similar(sent, got) then fail("bodies differ!") end
82 end
83
84 local check = function(sent, m)
85 io.write("checking ", m.headers.title, ": ")
86 for i = 1, #sent do
87 local s = sent[i]
88 if s.title == m.headers.title and s.count > 0 then
89 check_headers(s.headers, m.headers)
90 check_body(s.body, m.body)
91 s.count = s.count - 1
92 print("ok")
93 return
94 end
95 end
96 fail("not found")
97 end
98
99 local insert = function(sent, message)
100 if type(message.rcpt) == "table" then
101 message.count = #message.rcpt
102 else message.count = 1 end
103 message.headers = message.headers or {}
104 message.headers.title = message.title
105 table.insert(sent, message)
106 end
107
108 local mark = function()
109 local time = socket.time()
110 return { time = time }
111 end
112
113 local wait = function(sentinel, n)
114 local to
115 io.write("waiting for ", n, " messages: ")
116 while 1 do
117 local mbox = parse(get())
118 if n == #mbox then break end
119 if socket.time() - sentinel.time > 50 then
120 to = 1
121 break
122 end
123 socket.sleep(1)
124 io.write(".")
125 io.stdout:flush()
126 end
127 if to then fail("timeout")
128 else print("ok") end
129 end
130
131 local stuffed_body = [[
132 This message body needs to be
133 stuffed because it has a dot
134 .
135 by itself on a line.
136 Otherwise the mailer would
137 think that the dot
138 .
139 is the end of the message
140 and the remaining text would cause
141 a lot of trouble.
142 ]]
143
144 insert(sent, {
145 from = from,
146 rcpt = {
147 "luasocket@localhost",
148 "[email protected]",
149 "[email protected]"
150 },
151 body = "multiple rcpt body",
152 title = "multiple rcpt",
153 })
154
155 insert(sent, {
156 from = from,
157 rcpt = {
158 "luasock2@localhost",
159 "luasock3",
160 "luasock1"
161 },
162 headers = {
163 header1 = "header 1",
164 header2 = "header 2",
165 header3 = "header 3",
166 header4 = "header 4",
167 header5 = "header 5",
168 header6 = "header 6",
169 },
170 body = stuffed_body,
171 title = "complex message",
172 })
173
174 insert(sent, {
175 from = from,
176 rcpt = rcpt,
177 server = server,
178 body = "simple message body",
179 title = "simple message"
180 })
181
182 insert(sent, {
183 from = from,
184 rcpt = rcpt,
185 server = server,
186 body = stuffed_body,
187 title = "stuffed message body"
188 })
189
190 insert(sent, {
191 from = from,
192 rcpt = rcpt,
193 headers = {
194 header1 = "header 1",
195 header2 = "header 2",
196 header3 = "header 3",
197 header4 = "header 4",
198 header5 = "header 5",
199 header6 = "header 6",
200 },
201 title = "multiple headers"
202 })
203
204 insert(sent, {
205 from = from,
206 rcpt = rcpt,
207 title = "minimum message"
208 })
209
210 io.write("testing host not found: ")
211 local c, e = socket.connect("wrong.host", 25)
212 local ret, err = socket.smtp.mail{
213 from = from,
214 rcpt = rcpt,
215 server = "wrong.host"
216 }
217 if ret or e ~= err then fail("wrong error message")
218 else print("ok") end
219
220 io.write("testing invalid from: ")
221 local ret, err = socket.smtp.mail{
222 from = ' " " (( _ * ',
223 rcpt = rcpt,
224 }
225 if ret or not err then fail("wrong error message")
226 else print(err) end
227
228 io.write("testing no rcpt: ")
229 local ret, err = socket.smtp.mail{
230 from = from,
231 }
232 if ret or not err then fail("wrong error message")
233 else print(err) end
234
235 io.write("clearing mailbox: ")
236 empty()
237 print("ok")
238
239 io.write("sending messages: ")
240 for i = 1, #sent do
241 ret, err = socket.smtp.mail(sent[i])
242 if not ret then fail(err) end
243 io.write("+")
244 io.stdout:flush()
245 end
246 print("ok")
247
248 wait(mark(), total())
249
250 io.write("parsing mailbox: ")
251 local mbox = parse(get())
252 print(#mbox .. " messages found!")
253
254 for i = 1, #mbox do
255 check(sent, mbox[i])
256 end
257
258 print("passed all tests")
259 print(string.format("done in %.2fs", socket.time() - t))