aboutsummaryrefslogtreecommitdiff
path: root/test/smtptest.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-20 00:24:44 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-20 00:24:44 +0000
commit53857360bb1ca9cd2080b69d930763ae59db9b06 (patch)
tree6c1bc6d6462695cf9048801b2244f7fd0cd21ad5 /test/smtptest.lua
parent7da19138e37c4e0123860f1fecbceb80c3d2627d (diff)
downloadluasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.gz
luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.bz2
luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.zip
Finish port to Lua 5. Everything is working fine.
Still doesn't work in Windows.
Diffstat (limited to 'test/smtptest.lua')
-rw-r--r--test/smtptest.lua161
1 files changed, 84 insertions, 77 deletions
diff --git a/test/smtptest.lua b/test/smtptest.lua
index 6b01134..1bba27f 100644
--- a/test/smtptest.lua
+++ b/test/smtptest.lua
@@ -1,122 +1,130 @@
1local sent = {} 1local sent = {}
2 2
3local from = "luasock@tecgraf.puc-rio.br" 3local from = "diego@localhost"
4local server = "mail.tecgraf.puc-rio.br" 4local server = "localhost"
5local rcpt = "luasock@tecgraf.puc-rio.br" 5local rcpt = "luasocket@localhost"
6 6
7local name = "/var/spool/mail/luasock" 7local files = {
8 "/var/spool/mail/luasocket",
9 "/var/spool/mail/luasock1",
10 "/var/spool/mail/luasock2",
11 "/var/spool/mail/luasock3",
12}
8 13
9local t = _time() 14local t = socket._time()
10local err 15local err
11 16
12dofile("parsembox.lua") 17dofile("mbox.lua")
13local parse = parse 18local parse = mbox.parse
14dofile("noglobals.lua") 19dofile("noglobals.lua")
15 20
16local total = function() 21local total = function()
17 local t = 0 22 local t = 0
18 for i = 1, getn(%sent) do 23 for i = 1, table.getn(sent) do
19 t = t + %sent[i].count 24 t = t + sent[i].count
20 end 25 end
21 return t 26 return t
22end 27end
23 28
24local similar = function(s1, s2) 29local similar = function(s1, s2)
25 return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", "")) 30 return
26end 31 string.lower(string.gsub(s1, "%s", "")) ==
27 32 string.lower(string.gsub(s2, "%s", ""))
28local readfile = function(name)
29 local f = readfrom(name)
30 if not f then return nil end
31 local s = read("*a")
32 readfrom()
33 return s
34end
35
36local capture = function(cmd)
37 readfrom("| " .. cmd)
38 local s = read("*a")
39 readfrom()
40 return s
41end 33end
42 34
43local fail = function(s) 35local fail = function(s)
44 s = s or "failed!" 36 s = s or "failed!"
45 print(s) 37 print(s)
46 exit() 38 os.exit()
47end 39end
48 40
49local empty = function() 41local readfile = function(name)
50 local f = openfile(%name, "w") 42 local f = io.open(name, "r")
51 closefile(f) 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
52end 50end
53 51
54local get = function() 52local empty = function()
55 return %readfile(%name) 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
56end 60end
57 61
58local list = function() 62local get = function()
59 return %capture("ls -l " .. %name) 63 s = ""
64 for i,v in ipairs(files) do
65 s = s .. "\n" .. readfile(v)
66 end
67 return s
60end 68end
61 69
62local check_headers = function(sent, got) 70local check_headers = function(sent, got)
63 sent = sent or {} 71 sent = sent or {}
64 got = got or {} 72 got = got or {}
65 for i,v in sent do 73 for i,v in sent do
66 if not %similar(v, got[i]) then %fail("header " .. v .. "failed!") end 74 if not similar(v, got[i]) then fail("header " .. v .. "failed!") end
67 end 75 end
68end 76end
69 77
70local check_body = function(sent, got) 78local check_body = function(sent, got)
71 sent = sent or "" 79 sent = sent or ""
72 got = got or "" 80 got = got or ""
73 if not %similar(sent, got) then %fail("bodies differ!") end 81 if not similar(sent, got) then fail("bodies differ!") end
74end 82end
75 83
76local check = function(sent, m) 84local check = function(sent, m)
77 write("checking ", m.headers.title, ": ") 85 io.write("checking ", m.headers.title, ": ")
78 for i = 1, getn(sent) do 86 for i = 1, table.getn(sent) do
79 local s = sent[i] 87 local s = sent[i]
80 if s.title == m.headers.title and s.count > 0 then 88 if s.title == m.headers.title and s.count > 0 then
81 %check_headers(s.headers, m.headers) 89 check_headers(s.headers, m.headers)
82 %check_body(s.body, m.body) 90 check_body(s.body, m.body)
83 s.count = s.count - 1 91 s.count = s.count - 1
84 print("ok") 92 print("ok")
85 return 93 return
86 end 94 end
87 end 95 end
88 %fail("not found") 96 fail("not found")
89end 97end
90 98
91local insert = function(sent, message) 99local insert = function(sent, message)
92 if type(message.rcpt) == "table" then 100 if type(message.rcpt) == "table" then
93 message.count = getn(message.rcpt) 101 message.count = table.getn(message.rcpt)
94 else message.count = 1 end 102 else message.count = 1 end
95 message.headers = message.headers or {} 103 message.headers = message.headers or {}
96 message.headers.title = message.title 104 message.headers.title = message.title
97 tinsert(sent, message) 105 table.insert(sent, message)
98end 106end
99 107
100local mark = function() 108local mark = function()
101 local time = _time() 109 local time = socket._time()
102 return { time = time } 110 return { time = time }
103end 111end
104 112
105local wait = function(sentinel, n) 113local wait = function(sentinel, n)
106 local to 114 local to
107 write("waiting for ", n, " messages: ") 115 io.write("waiting for ", n, " messages: ")
108 while 1 do 116 while 1 do
109 local mbox = %parse.mbox(%get()) 117 local mbox = parse(get())
110 if n == getn(mbox) then break end 118 if n == table.getn(mbox) then break end
111 if _time() - sentinel.time > 50 then 119 if socket._time() - sentinel.time > 50 then
112 to = 1 120 to = 1
113 break 121 break
114 end 122 end
115 _sleep(1) 123 socket._sleep(1)
116 write(".") 124 io.write(".")
117 flush(_STDOUT) 125 io.stdout:flush()
118 end 126 end
119 if to then %fail("timeout") 127 if to then fail("timeout")
120 else print("ok") end 128 else print("ok") end
121end 129end
122 130
@@ -129,16 +137,16 @@ Otherwise the mailer would
129think that the dot 137think that the dot
130. 138.
131is the end of the message 139is the end of the message
132and the remaining will cause 140and the remaining text would cause
133a lot of trouble. 141a lot of trouble.
134]] 142]]
135 143
136insert(sent, { 144insert(sent, {
137 from = from, 145 from = from,
138 rcpt = { 146 rcpt = {
139 "luasock2@tecgraf.puc-rio.br", 147 "luasocket@localhost",
140 "luasock", 148 "luasock3@dell-diego.cs.princeton.edu",
141 "luasock1" 149 "luasock1@dell-diego.cs.princeton.edu"
142 }, 150 },
143 body = "multiple rcpt body", 151 body = "multiple rcpt body",
144 title = "multiple rcpt", 152 title = "multiple rcpt",
@@ -147,8 +155,8 @@ insert(sent, {
147insert(sent, { 155insert(sent, {
148 from = from, 156 from = from,
149 rcpt = { 157 rcpt = {
150 "luasock2@tecgraf.puc-rio.br", 158 "luasock2@localhost",
151 "luasock", 159 "luasock3",
152 "luasock1" 160 "luasock1"
153 }, 161 },
154 headers = { 162 headers = {
@@ -199,9 +207,9 @@ insert(sent, {
199 title = "minimum message" 207 title = "minimum message"
200}) 208})
201 209
202write("testing host not found: ") 210io.write("testing host not found: ")
203local c, e = connect("wrong.host", 25) 211local c, e = socket.connect("wrong.host", 25)
204local err = SMTP.mail{ 212local err = socket.smtp.mail{
205 from = from, 213 from = from,
206 rcpt = rcpt, 214 rcpt = rcpt,
207 server = "wrong.host" 215 server = "wrong.host"
@@ -209,44 +217,43 @@ local err = SMTP.mail{
209if e ~= err then fail("wrong error message") 217if e ~= err then fail("wrong error message")
210else print("ok") end 218else print("ok") end
211 219
212write("testing invalid from: ") 220io.write("testing invalid from: ")
213local err = SMTP.mail{ 221local err = socket.smtp.mail{
214 from = ' " " (( _ * ', 222 from = ' " " (( _ * ',
215 rcpt = rcpt, 223 rcpt = rcpt,
216} 224}
217if not err then fail("wrong error message") 225if not err then fail("wrong error message")
218else print(err) end 226else print(err) end
219 227
220write("testing no rcpt: ") 228io.write("testing no rcpt: ")
221local err = SMTP.mail{ 229local err = socket.smtp.mail{
222 from = from, 230 from = from,
223} 231}
224if not err then fail("wrong error message") 232if not err then fail("wrong error message")
225else print(err) end 233else print(err) end
226 234
227write("clearing mailbox: ") 235io.write("clearing mailbox: ")
228empty() 236empty()
229print("ok") 237print("ok")
230 238
231write("sending messages: ") 239io.write("sending messages: ")
232for i = 1, getn(sent) do 240for i = 1, table.getn(sent) do
233 err = SMTP.mail(sent[i]) 241 err = socket.smtp.mail(sent[i])
234 if err then fail(err) end 242 if err then fail(err) end
235 write("+") 243 io.write("+")
236 flush(_STDOUT) 244 io.stdout:flush()
237end 245end
238print("ok") 246print("ok")
239 247
240wait(mark(), total()) 248wait(mark(), total())
241 249
242write("parsing mailbox: ") 250io.write("parsing mailbox: ")
243local mbox = parse.mbox(get()) 251local mbox = parse(get())
244print(getn(mbox) .. " messages found!") 252print(table.getn(mbox) .. " messages found!")
245 253
246for i = 1, getn(mbox) do 254for i = 1, table.getn(mbox) do
247 check(sent, mbox[i]) 255 check(sent, mbox[i])
248end 256end
249 257
250
251print("passed all tests") 258print("passed all tests")
252print(format("done in %.2fs", _time() - t)) 259print(string.format("done in %.2fs", socket._time() - t))