diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
| commit | 53857360bb1ca9cd2080b69d930763ae59db9b06 (patch) | |
| tree | 6c1bc6d6462695cf9048801b2244f7fd0cd21ad5 /test/smtptest.lua | |
| parent | 7da19138e37c4e0123860f1fecbceb80c3d2627d (diff) | |
| download | luasocket-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.lua | 161 |
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 @@ | |||
| 1 | local sent = {} | 1 | local sent = {} |
| 2 | 2 | ||
| 3 | local from = "luasock@tecgraf.puc-rio.br" | 3 | local from = "diego@localhost" |
| 4 | local server = "mail.tecgraf.puc-rio.br" | 4 | local server = "localhost" |
| 5 | local rcpt = "luasock@tecgraf.puc-rio.br" | 5 | local rcpt = "luasocket@localhost" |
| 6 | 6 | ||
| 7 | local name = "/var/spool/mail/luasock" | 7 | local files = { |
| 8 | "/var/spool/mail/luasocket", | ||
| 9 | "/var/spool/mail/luasock1", | ||
| 10 | "/var/spool/mail/luasock2", | ||
| 11 | "/var/spool/mail/luasock3", | ||
| 12 | } | ||
| 8 | 13 | ||
| 9 | local t = _time() | 14 | local t = socket._time() |
| 10 | local err | 15 | local err |
| 11 | 16 | ||
| 12 | dofile("parsembox.lua") | 17 | dofile("mbox.lua") |
| 13 | local parse = parse | 18 | local parse = mbox.parse |
| 14 | dofile("noglobals.lua") | 19 | dofile("noglobals.lua") |
| 15 | 20 | ||
| 16 | local total = function() | 21 | local 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 |
| 22 | end | 27 | end |
| 23 | 28 | ||
| 24 | local similar = function(s1, s2) | 29 | local similar = function(s1, s2) |
| 25 | return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", "")) | 30 | return |
| 26 | end | 31 | string.lower(string.gsub(s1, "%s", "")) == |
| 27 | 32 | string.lower(string.gsub(s2, "%s", "")) | |
| 28 | local 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 | ||
| 34 | end | ||
| 35 | |||
| 36 | local capture = function(cmd) | ||
| 37 | readfrom("| " .. cmd) | ||
| 38 | local s = read("*a") | ||
| 39 | readfrom() | ||
| 40 | return s | ||
| 41 | end | 33 | end |
| 42 | 34 | ||
| 43 | local fail = function(s) | 35 | local fail = function(s) |
| 44 | s = s or "failed!" | 36 | s = s or "failed!" |
| 45 | print(s) | 37 | print(s) |
| 46 | exit() | 38 | os.exit() |
| 47 | end | 39 | end |
| 48 | 40 | ||
| 49 | local empty = function() | 41 | local 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 | ||
| 52 | end | 50 | end |
| 53 | 51 | ||
| 54 | local get = function() | 52 | local 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 | ||
| 56 | end | 60 | end |
| 57 | 61 | ||
| 58 | local list = function() | 62 | local 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 | ||
| 60 | end | 68 | end |
| 61 | 69 | ||
| 62 | local check_headers = function(sent, got) | 70 | local 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 |
| 68 | end | 76 | end |
| 69 | 77 | ||
| 70 | local check_body = function(sent, got) | 78 | local 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 |
| 74 | end | 82 | end |
| 75 | 83 | ||
| 76 | local check = function(sent, m) | 84 | local 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") |
| 89 | end | 97 | end |
| 90 | 98 | ||
| 91 | local insert = function(sent, message) | 99 | local 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) |
| 98 | end | 106 | end |
| 99 | 107 | ||
| 100 | local mark = function() | 108 | local mark = function() |
| 101 | local time = _time() | 109 | local time = socket._time() |
| 102 | return { time = time } | 110 | return { time = time } |
| 103 | end | 111 | end |
| 104 | 112 | ||
| 105 | local wait = function(sentinel, n) | 113 | local 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 |
| 121 | end | 129 | end |
| 122 | 130 | ||
| @@ -129,16 +137,16 @@ Otherwise the mailer would | |||
| 129 | think that the dot | 137 | think that the dot |
| 130 | . | 138 | . |
| 131 | is the end of the message | 139 | is the end of the message |
| 132 | and the remaining will cause | 140 | and the remaining text would cause |
| 133 | a lot of trouble. | 141 | a lot of trouble. |
| 134 | ]] | 142 | ]] |
| 135 | 143 | ||
| 136 | insert(sent, { | 144 | insert(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, { | |||
| 147 | insert(sent, { | 155 | insert(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 | ||
| 202 | write("testing host not found: ") | 210 | io.write("testing host not found: ") |
| 203 | local c, e = connect("wrong.host", 25) | 211 | local c, e = socket.connect("wrong.host", 25) |
| 204 | local err = SMTP.mail{ | 212 | local 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{ | |||
| 209 | if e ~= err then fail("wrong error message") | 217 | if e ~= err then fail("wrong error message") |
| 210 | else print("ok") end | 218 | else print("ok") end |
| 211 | 219 | ||
| 212 | write("testing invalid from: ") | 220 | io.write("testing invalid from: ") |
| 213 | local err = SMTP.mail{ | 221 | local err = socket.smtp.mail{ |
| 214 | from = ' " " (( _ * ', | 222 | from = ' " " (( _ * ', |
| 215 | rcpt = rcpt, | 223 | rcpt = rcpt, |
| 216 | } | 224 | } |
| 217 | if not err then fail("wrong error message") | 225 | if not err then fail("wrong error message") |
| 218 | else print(err) end | 226 | else print(err) end |
| 219 | 227 | ||
| 220 | write("testing no rcpt: ") | 228 | io.write("testing no rcpt: ") |
| 221 | local err = SMTP.mail{ | 229 | local err = socket.smtp.mail{ |
| 222 | from = from, | 230 | from = from, |
| 223 | } | 231 | } |
| 224 | if not err then fail("wrong error message") | 232 | if not err then fail("wrong error message") |
| 225 | else print(err) end | 233 | else print(err) end |
| 226 | 234 | ||
| 227 | write("clearing mailbox: ") | 235 | io.write("clearing mailbox: ") |
| 228 | empty() | 236 | empty() |
| 229 | print("ok") | 237 | print("ok") |
| 230 | 238 | ||
| 231 | write("sending messages: ") | 239 | io.write("sending messages: ") |
| 232 | for i = 1, getn(sent) do | 240 | for 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() |
| 237 | end | 245 | end |
| 238 | print("ok") | 246 | print("ok") |
| 239 | 247 | ||
| 240 | wait(mark(), total()) | 248 | wait(mark(), total()) |
| 241 | 249 | ||
| 242 | write("parsing mailbox: ") | 250 | io.write("parsing mailbox: ") |
| 243 | local mbox = parse.mbox(get()) | 251 | local mbox = parse(get()) |
| 244 | print(getn(mbox) .. " messages found!") | 252 | print(table.getn(mbox) .. " messages found!") |
| 245 | 253 | ||
| 246 | for i = 1, getn(mbox) do | 254 | for i = 1, table.getn(mbox) do |
| 247 | check(sent, mbox[i]) | 255 | check(sent, mbox[i]) |
| 248 | end | 256 | end |
| 249 | 257 | ||
| 250 | |||
| 251 | print("passed all tests") | 258 | print("passed all tests") |
| 252 | print(format("done in %.2fs", _time() - t)) | 259 | print(string.format("done in %.2fs", socket._time() - t)) |
