local socket = require("socket")
local ftp = require("socket.ftp")
local url = require("socket.url")
local ltn12 = require("ltn12")

-- override protection to make sure we see all errors
--socket.protect = function(s) return s end

dofile("testsupport.lua")

local host, port, index_file, index, back, err, ret

local t = socket.gettime()

host = host or "localhost"
index_file = "index.html"


-- 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: ")
back, err = ftp.get("wrong://banana.com/lixo")
assert(not back and err == "wrong scheme 'wrong'", err)
print("ok")

io.write("testing invalid url: ")
back, err = ftp.get("localhost/dir1/index.html;type=i")
assert(not back and err)
print("ok")

io.write("testing anonymous file download: ")
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: ")
ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
if not ret then print(err) 
else print("ok") end

io.write("testing upload: ")
ret, err = ftp.put("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i", index)
assert(ret and not err, err)
print("ok")

io.write("downloading uploaded file: ")
back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/index.up.html;type=i")
assert(ret and not err and index == back, err)
print("ok")

io.write("erasing after upload/download: ")
ret, err = dele("ftp://luasocket:pedrovian@" .. host .. "/index.up.html")
assert(ret and not err, err) 
print("ok")

io.write("testing weird-character translation: ")
back, err = ftp.get("ftp://luasocket:pedrovian@" .. host .. "/%23%3f;type=i")
assert(not err and back == index, err)
print("ok")

io.write("testing parameter overriding: ")
local back = {}
ret, err = ftp.get{
    url = "//stupid:mistake@" .. host .. "/index.html",
    user = "luasocket",
    password = "pedrovian",
    type = "i",
    sink = ltn12.sink.table(back)
}
assert(ret and not err and table.concat(back) == index, err)
print("ok")

io.write("testing upload denial: ")
ret, err = ftp.put("ftp://" .. host .. "/index.up.html;type=a", index)
assert(not ret and err, "should have failed")
print(err)

io.write("testing authentication failure: ")
ret, err = ftp.get("ftp://luasocket:wrong@".. host .. "/index.html;type=a")
assert(not ret and err, "should have failed")
print(err)

io.write("testing wrong file: ")
back, err = ftp.get("ftp://".. host .. "/index.wrong.html;type=a")
assert(not back and err, "should have failed")
print(err)

print("passed all tests")
print(string.format("done in %.2fs", socket.gettime() - t))
