luasocket/test/ftptest.lua

111 lines
3.1 KiB
Lua
Raw Normal View History

2004-11-27 08:58:04 +01:00
local socket = require("socket")
local ftp = require("socket.ftp")
local url = require("socket.url")
local ltn12 = require("ltn12")
2001-09-25 23:34:43 +02:00
2004-11-27 08:58:04 +01:00
-- override protection to make sure we see all errors
--socket.protect = function(s) return s end
2001-09-25 23:34:43 +02:00
2004-11-27 08:58:04 +01:00
dofile("testsupport.lua")
2001-09-25 23:34:43 +02:00
2004-11-27 08:58:04 +01:00
local host, port, index_file, index, back, err, ret
2001-09-25 23:34:43 +02:00
2004-11-27 08:58:04 +01:00
local t = socket.gettime()
2001-09-25 23:34:43 +02:00
host = host or "localhost"
index_file = "index.html"
2001-09-25 23:34:43 +02:00
2004-11-27 08:58:04 +01:00
-- a function that returns a directory listing
local function nlst(u)
local t = {}
local p = url.parse(u)
p.command = "nlst"
p.sink = ltn12.sink.table(t)
local r, e = ftp.get(p)
return r and table.concat(t), e
end
-- function that removes a remote file
local function dele(u)
local p = url.parse(u)
p.command = "dele"
p.argument = string.gsub(p.path, "^/", "")
if p.argumet == "" then p.argument = nil end
p.check = 250
return ftp.command(p)
end
-- read index with CRLF convention
index = readfile(index_file)
io.write("testing wrong scheme: ")
2004-11-27 08:58:04 +01:00
back, err = ftp.get("wrong://banana.com/lixo")
assert(not back and err == "wrong scheme 'wrong'", err)
print("ok")
io.write("testing invalid url: ")
2004-11-27 08:58:04 +01:00
back, err = ftp.get("localhost/dir1/index.html;type=i")
assert(not back and err)
print("ok")
2001-09-25 23:34:43 +02:00
io.write("testing anonymous file download: ")
2004-11-27 08:58:04 +01:00
back, err = socket.ftp.get("ftp://" .. host .. "/pub/index.html;type=i")
assert(not err and back == index, err)
print("ok")
io.write("erasing before upload: ")
2005-10-07 06:40:59 +02:00
ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
2004-11-27 08:58:04 +01:00
if not ret then print(err)
else print("ok") end
io.write("testing upload: ")
2005-10-07 06:40:59 +02:00
ret, err = ftp.put("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i", index)
2004-11-27 08:58:04 +01:00
assert(ret and not err, err)
print("ok")
io.write("downloading uploaded file: ")
2005-10-07 06:40:59 +02:00
back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i")
2004-11-27 08:58:04 +01:00
assert(ret and not err and index == back, err)
print("ok")
io.write("erasing after upload/download: ")
2005-10-07 06:40:59 +02:00
ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
2004-11-27 08:58:04 +01:00
assert(ret and not err, err)
print("ok")
2001-09-25 23:34:43 +02:00
io.write("testing weird-character translation: ")
2005-10-07 06:40:59 +02:00
back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/%23%3f;type=i")
2004-11-27 08:58:04 +01:00
assert(not err and back == index, err)
print("ok")
2001-09-25 23:34:43 +02:00
io.write("testing parameter overriding: ")
2004-11-27 08:58:04 +01:00
local back = {}
ret, err = ftp.get{
url = "//stupid:mistake@" .. host .. "/index.html",
2001-09-25 23:34:43 +02:00
user = "luasocket",
2005-10-07 06:40:59 +02:00
password = "pedrovian",
2004-11-27 08:58:04 +01:00
type = "i",
sink = ltn12.sink.table(back)
2001-09-25 23:34:43 +02:00
}
2004-11-27 08:58:04 +01:00
assert(ret and not err and table.concat(back) == index, err)
print("ok")
2001-09-25 23:34:43 +02:00
io.write("testing upload denial: ")
2004-11-27 08:58:04 +01:00
ret, err = ftp.put("ftp://" .. host .. "/index.up.html;type=a", index)
assert(not ret and err, "should have failed")
print(err)
2001-09-25 23:34:43 +02:00
io.write("testing authentication failure: ")
2004-11-27 08:58:04 +01:00
ret, err = ftp.get("ftp://luasocket:wrong@".. host .. "/index.html;type=a")
assert(not ret and err, "should have failed")
print(err)
2001-09-25 23:34:43 +02:00
io.write("testing wrong file: ")
2004-11-27 08:58:04 +01:00
back, err = ftp.get("ftp://".. host .. "/index.wrong.html;type=a")
assert(not back and err, "should have failed")
print(err)
2001-01-25 22:59:39 +01:00
print("passed all tests")
2004-11-27 08:58:04 +01:00
print(string.format("done in %.2fs", socket.gettime() - t))