Finish port to Lua 5. Everything is working fine.

Still doesn't work in Windows.
This commit is contained in:
Diego Nehab 2003-03-20 00:24:44 +00:00
parent 7da19138e3
commit 53857360bb
11 changed files with 289 additions and 277 deletions

View file

@ -1,29 +1,32 @@
dofile("noglobals.lua")
local similar = function(s1, s2)
return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
end
local capture = function(cmd)
readfrom("| " .. cmd)
local s = read("*a")
readfrom()
return s
return
string.lower(string.gsub(s1, "%s", "")) ==
string.lower(string.gsub(s2, "%s", ""))
end
local readfile = function(name)
local f = readfrom(name)
if not f then return nil end
local s = read("*a")
readfrom()
return s
local f = io.open(name, "r")
if not f then return nil end
local s = f:read("*a")
f:close()
return s
end
local capture = function(cmd)
local f = io.popen(cmd)
if not f then return nil end
local s = f:read("*a")
f:close()
return s
end
local check = function(v, e, o)
e = e or "failed!"
o = o or "ok"
if v then print(o)
else print(e) exit() end
else print(e) os.exit() end
end
-- needs an account luasocket:password
@ -31,81 +34,82 @@ end
local index, err, saved, back, expected
local t = _time()
local t = socket._time()
index = readfile("index.html")
index = readfile("test/index.html")
write("testing file upload: ")
remove("/var/ftp/dir1/index.up.html")
err = FTP.put("ftp://localhost/dir1/index.up.html;type=i", index)
saved = readfile("/var/ftp/dir1/index.up.html")
io.write("testing wrong scheme: ")
back, err = socket.ftp.get("wrong://banana.com/lixo")
check(not back and err == "unknown scheme 'wrong'", err)
io.write("testing invalid url: ")
back, err = socket.ftp.get("localhost/dir1/index.html;type=i")
local c, e = socket.connect("", 21)
check(not back and err == e, err)
io.write("testing anonymous file upload: ")
os.remove("/var/ftp/pub/index.up.html")
err = socket.ftp.put("ftp://localhost/pub/index.up.html;type=i", index)
saved = readfile("/var/ftp/pub/index.up.html")
check(not err and saved == index, err)
write("testing file download: ")
back, err = FTP.get("ftp://localhost/dir1/index.up.html;type=i")
io.write("testing anonymous file download: ")
back, err = socket.ftp.get("ftp://localhost/pub/index.up.html;type=i")
check(not err and back == index, err)
write("testing no directory changes: ")
back, err = FTP.get("ftp://localhost/index.html;type=i")
io.write("testing no directory changes: ")
back, err = socket.ftp.get("ftp://localhost/index.html;type=i")
check(not err and back == index, err)
write("testing multiple directory changes: ")
back, err = FTP.get("ftp://localhost/dir1/dir2/dir3/dir4/dir5/dir6/index.html;type=i")
io.write("testing multiple directory changes: ")
back, err = socket.ftp.get("ftp://localhost/pub/dir1/dir2/dir3/dir4/dir5/index.html;type=i")
check(not err and back == index, err)
write("testing authenticated upload: ")
remove("/home/luasocket/index.up.html")
err = FTP.put("ftp://luasocket:password@localhost/index.up.html;type=i", index)
io.write("testing authenticated upload: ")
os.remove("/home/luasocket/index.up.html")
err = socket.ftp.put("ftp://luasocket:password@localhost/index.up.html;type=i", index)
saved = readfile("/home/luasocket/index.up.html")
check(not err and saved == index, err)
write("testing authenticated download: ")
back, err = FTP.get("ftp://luasocket:password@localhost/index.up.html;type=i")
io.write("testing authenticated download: ")
back, err = socket.ftp.get("ftp://luasocket:password@localhost/index.up.html;type=i")
check(not err and back == index, err)
write("testing weird-character translation: ")
back, err = FTP.get("ftp://luasocket:password@localhost/%2fvar/ftp/dir1/index.html;type=i")
io.write("testing weird-character translation: ")
back, err = socket.ftp.get("ftp://luasocket:password@localhost/%2fvar/ftp/pub/index.html;type=i")
check(not err and back == index, err)
write("testing parameter overriding: ")
back, err = FTP.get {
url = "//stupid:mistake@localhost/dir1/index.html",
io.write("testing parameter overriding: ")
back, err = socket.ftp.get {
url = "//stupid:mistake@localhost/index.html",
user = "luasocket",
password = "password",
type = "i"
}
check(not err and back == index, err)
write("testing wrong scheme: ")
back, err = FTP.get("wrong://banana.com/lixo")
check(not back and err == "unknown scheme 'wrong'", err)
write("testing invalid url: ")
back, err = FTP.get("localhost/dir1/index.html;type=i")
local c, e = connect("", 21)
check(not back and err == e, err)
write("testing directory listing: ")
expected = capture("ls -F /var/ftp/dir1 | grep -v /")
back, err = FTP.get("ftp://localhost/dir1;type=d")
check(similar(back, expected))
write("testing home directory listing: ")
io.write("testing home directory listing: ")
expected = capture("ls -F /var/ftp | grep -v /")
back, err = FTP.get("ftp://localhost/")
back, err = socket.ftp.get("ftp://localhost/")
check(back and similar(back, expected), nil, err)
write("testing upload denial: ")
err = FTP.put("ftp://localhost/index.up.html;type=a", index)
io.write("testing directory listing: ")
expected = capture("ls -F /var/ftp/pub | grep -v /")
back, err = socket.ftp.get("ftp://localhost/pub;type=d")
check(similar(back, expected))
io.write("testing upload denial: ")
err = socket.ftp.put("ftp://localhost/index.up.html;type=a", index)
check(err, err)
write("testing authentication failure: ")
err = FTP.put("ftp://luasocket:wrong@localhost/index.html;type=a", index)
io.write("testing authentication failure: ")
err = socket.ftp.put("ftp://luasocket:wrong@localhost/index.html;type=a", index)
print(err)
check(err, err)
write("testing wrong file: ")
back, err = FTP.get("ftp://localhost/index.wrong.html;type=a")
io.write("testing wrong file: ")
back, err = socket.ftp.get("ftp://localhost/index.wrong.html;type=a")
check(err, err)
print("passed all tests")
print(format("done in %.2fs", _time() - t))
print(string.format("done in %.2fs", socket._time() - t))

View file

@ -31,7 +31,7 @@ local check = function (v, e)
end
local check_request = function(request, expect, ignore)
local response = http.request(request)
local response = socket.http.request(request)
for i,v in response do
if not ignore[i] then
if v ~= expect[i] then %fail(i .. " differs!") end
@ -56,13 +56,13 @@ index = readfile("test/index.html")
io.write("testing request uri correctness: ")
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
local back = http.get("http://" .. HOST .. forth)
local back = socket.http.get("http://" .. HOST .. forth)
if similar(back, forth) then print("ok")
else fail("failed!") end
io.write("testing query string correctness: ")
forth = "this+is+the+query+string"
back = http.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth)
back = socket.http.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth)
if similar(back, forth) then print("ok")
else fail("failed!") end
@ -178,7 +178,7 @@ io.write("testing manual basic auth: ")
request = {
url = "http://" .. HOST .. prefix .. "/auth/index.html",
headers = {
authorization = "Basic " .. Code.base64("luasocket:password")
authorization = "Basic " .. socket.code.base64("luasocket:password")
}
}
expect = {
@ -279,11 +279,11 @@ check_request(request, expect, ignore)
local body
io.write("testing simple get function: ")
body = http.get("http://" .. HOST .. prefix .. "/index.html")
body = socket.http.get("http://" .. HOST .. prefix .. "/index.html")
check(body == index)
io.write("testing simple get function with table args: ")
body = http.get {
body = socket.http.get {
url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
user = "luasocket",
password = "password"
@ -291,18 +291,18 @@ body = http.get {
check(body == index)
io.write("testing simple post function: ")
body = http.post("http://" .. HOST .. cgiprefix .. "/cat", index)
body = socket.http.post("http://" .. HOST .. cgiprefix .. "/cat", index)
check(body == index)
io.write("testing simple post function with table args: ")
body = http.post {
body = socket.http.post {
url = "http://" .. HOST .. cgiprefix .. "/cat",
body = index
}
check(body == index)
io.write("testing HEAD method: ")
response = http.request {
response = socket.http.request {
method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego/"
}

View file

@ -1,122 +1,130 @@
local sent = {}
local from = "luasock@tecgraf.puc-rio.br"
local server = "mail.tecgraf.puc-rio.br"
local rcpt = "luasock@tecgraf.puc-rio.br"
local from = "diego@localhost"
local server = "localhost"
local rcpt = "luasocket@localhost"
local name = "/var/spool/mail/luasock"
local files = {
"/var/spool/mail/luasocket",
"/var/spool/mail/luasock1",
"/var/spool/mail/luasock2",
"/var/spool/mail/luasock3",
}
local t = _time()
local t = socket._time()
local err
dofile("parsembox.lua")
local parse = parse
dofile("mbox.lua")
local parse = mbox.parse
dofile("noglobals.lua")
local total = function()
local t = 0
for i = 1, getn(%sent) do
t = t + %sent[i].count
for i = 1, table.getn(sent) do
t = t + sent[i].count
end
return t
end
local similar = function(s1, s2)
return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
end
local readfile = function(name)
local f = readfrom(name)
if not f then return nil end
local s = read("*a")
readfrom()
return s
end
local capture = function(cmd)
readfrom("| " .. cmd)
local s = read("*a")
readfrom()
return s
return
string.lower(string.gsub(s1, "%s", "")) ==
string.lower(string.gsub(s2, "%s", ""))
end
local fail = function(s)
s = s or "failed!"
print(s)
exit()
os.exit()
end
local readfile = function(name)
local f = io.open(name, "r")
if not f then
fail("unable to open file!")
return nil
end
local s = f:read("*a")
f:close()
return s
end
local empty = function()
local f = openfile(%name, "w")
closefile(f)
for i,v in ipairs(files) do
local f = io.open(v, "w")
if not f then
fail("unable to open file!")
end
f:close()
end
end
local get = function()
return %readfile(%name)
end
local list = function()
return %capture("ls -l " .. %name)
s = ""
for i,v in ipairs(files) do
s = s .. "\n" .. readfile(v)
end
return s
end
local check_headers = function(sent, got)
sent = sent or {}
got = got or {}
for i,v in sent do
if not %similar(v, got[i]) then %fail("header " .. v .. "failed!") end
if not similar(v, got[i]) then fail("header " .. v .. "failed!") end
end
end
local check_body = function(sent, got)
sent = sent or ""
got = got or ""
if not %similar(sent, got) then %fail("bodies differ!") end
if not similar(sent, got) then fail("bodies differ!") end
end
local check = function(sent, m)
write("checking ", m.headers.title, ": ")
for i = 1, getn(sent) do
io.write("checking ", m.headers.title, ": ")
for i = 1, table.getn(sent) do
local s = sent[i]
if s.title == m.headers.title and s.count > 0 then
%check_headers(s.headers, m.headers)
%check_body(s.body, m.body)
check_headers(s.headers, m.headers)
check_body(s.body, m.body)
s.count = s.count - 1
print("ok")
return
end
end
%fail("not found")
fail("not found")
end
local insert = function(sent, message)
if type(message.rcpt) == "table" then
message.count = getn(message.rcpt)
message.count = table.getn(message.rcpt)
else message.count = 1 end
message.headers = message.headers or {}
message.headers.title = message.title
tinsert(sent, message)
table.insert(sent, message)
end
local mark = function()
local time = _time()
local time = socket._time()
return { time = time }
end
local wait = function(sentinel, n)
local to
write("waiting for ", n, " messages: ")
io.write("waiting for ", n, " messages: ")
while 1 do
local mbox = %parse.mbox(%get())
if n == getn(mbox) then break end
if _time() - sentinel.time > 50 then
local mbox = parse(get())
if n == table.getn(mbox) then break end
if socket._time() - sentinel.time > 50 then
to = 1
break
end
_sleep(1)
write(".")
flush(_STDOUT)
socket._sleep(1)
io.write(".")
io.stdout:flush()
end
if to then %fail("timeout")
if to then fail("timeout")
else print("ok") end
end
@ -129,16 +137,16 @@ Otherwise the mailer would
think that the dot
.
is the end of the message
and the remaining will cause
and the remaining text would cause
a lot of trouble.
]]
insert(sent, {
from = from,
rcpt = {
"luasock2@tecgraf.puc-rio.br",
"luasock",
"luasock1"
"luasocket@localhost",
"luasock3@dell-diego.cs.princeton.edu",
"luasock1@dell-diego.cs.princeton.edu"
},
body = "multiple rcpt body",
title = "multiple rcpt",
@ -147,8 +155,8 @@ insert(sent, {
insert(sent, {
from = from,
rcpt = {
"luasock2@tecgraf.puc-rio.br",
"luasock",
"luasock2@localhost",
"luasock3",
"luasock1"
},
headers = {
@ -199,9 +207,9 @@ insert(sent, {
title = "minimum message"
})
write("testing host not found: ")
local c, e = connect("wrong.host", 25)
local err = SMTP.mail{
io.write("testing host not found: ")
local c, e = socket.connect("wrong.host", 25)
local err = socket.smtp.mail{
from = from,
rcpt = rcpt,
server = "wrong.host"
@ -209,44 +217,43 @@ local err = SMTP.mail{
if e ~= err then fail("wrong error message")
else print("ok") end
write("testing invalid from: ")
local err = SMTP.mail{
io.write("testing invalid from: ")
local err = socket.smtp.mail{
from = ' " " (( _ * ',
rcpt = rcpt,
}
if not err then fail("wrong error message")
else print(err) end
write("testing no rcpt: ")
local err = SMTP.mail{
io.write("testing no rcpt: ")
local err = socket.smtp.mail{
from = from,
}
if not err then fail("wrong error message")
else print(err) end
write("clearing mailbox: ")
io.write("clearing mailbox: ")
empty()
print("ok")
write("sending messages: ")
for i = 1, getn(sent) do
err = SMTP.mail(sent[i])
io.write("sending messages: ")
for i = 1, table.getn(sent) do
err = socket.smtp.mail(sent[i])
if err then fail(err) end
write("+")
flush(_STDOUT)
io.write("+")
io.stdout:flush()
end
print("ok")
wait(mark(), total())
write("parsing mailbox: ")
local mbox = parse.mbox(get())
print(getn(mbox) .. " messages found!")
io.write("parsing mailbox: ")
local mbox = parse(get())
print(table.getn(mbox) .. " messages found!")
for i = 1, getn(mbox) do
for i = 1, table.getn(mbox) do
check(sent, mbox[i])
end
print("passed all tests")
print(format("done in %.2fs", _time() - t))
print(string.format("done in %.2fs", socket._time() - t))

View file

@ -1,5 +1,8 @@
local check_build_url = function(parsed)
local built = URL.build_url(parsed)
local built = socket.url.build(parsed)
if built ~= parsed.url then
print("built is different from expected")
print(built)
@ -9,7 +12,7 @@ local check_build_url = function(parsed)
end
local check_protect = function(parsed, path, unsafe)
local built = URL.build_path(parsed, unsafe)
local built = socket.url.build_path(parsed, unsafe)
if built ~= path then
print(built, path)
print("path composition failed.")
@ -18,9 +21,9 @@ local check_protect = function(parsed, path, unsafe)
end
local check_invert = function(url)
local parsed = URL.parse_url(url)
parsed.path = URL.build_path(URL.parse_path(parsed.path))
local rebuilt = URL.build_url(parsed)
local parsed = socket.url.parse(url)
parsed.path = socket.url.build_path(socket.url.parse_path(parsed.path))
local rebuilt = socket.url.build(parsed)
if rebuilt ~= url then
print(url, rebuilt)
print("original and rebuilt are different")
@ -29,7 +32,7 @@ local check_invert = function(url)
end
local check_parse_path = function(path, expect)
local parsed = URL.parse_path(path)
local parsed = socket.url.parse_path(path)
for i = 1, math.max(table.getn(parsed), table.getn(expect)) do
if parsed[i] ~= expect[i] then
print(path)
@ -48,7 +51,7 @@ local check_parse_path = function(path, expect)
print("is_absolute mismatch")
exit()
end
local built = URL.build_path(expect)
local built = socket.url.build_path(expect)
if built ~= path then
print(built, path)
print("path composition failed.")
@ -57,7 +60,7 @@ local check_parse_path = function(path, expect)
end
local check_absolute_url = function(base, relative, absolute)
local res = URL.absolute_url(base, relative)
local res = socket.url.absolute(base, relative)
if res ~= absolute then
write("absolute: In test for '", relative, "' expected '",
absolute, "' but got '", res, "'\n")
@ -68,7 +71,7 @@ end
local check_parse_url = function(gaba)
local url = gaba.url
gaba.url = nil
local parsed = URL.parse_url(url)
local parsed = socket.url.parse(url)
for i, v in gaba do
if v ~= parsed[i] then
write("parse: In test for '", url, "' expected ", i, " = '",