| 1 |
local socket = require("socket") |
| 2 |
local ftp = require("socket.ftp") |
| 3 |
local url = require("socket.url") |
| 4 |
local ltn12 = require("ltn12") |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
dofile("testsupport.lua") |
| 23 |
|
| 24 |
local host = host or "localhost" |
| 25 |
local port, index_file, index, back, err, ret |
| 26 |
|
| 27 |
local t = socket.gettime() |
| 28 |
|
| 29 |
index_file = "index.html" |
| 30 |
|
| 31 |
|
| 32 |
local function nlst(u) |
| 33 |
local t = {} |
| 34 |
local p = url.parse(u) |
| 35 |
p.command = "nlst" |
| 36 |
p.sink = ltn12.sink.table(t) |
| 37 |
local r, e = ftp.get(p) |
| 38 |
return r and table.concat(t), e |
| 39 |
end |
| 40 |
|
| 41 |
|
| 42 |
local function dele(u) |
| 43 |
local p = url.parse(u) |
| 44 |
p.command = "dele" |
| 45 |
p.argument = string.gsub(p.path, "^/", "") |
| 46 |
if p.argumet == "" then p.argument = nil end |
| 47 |
p.check = 250 |
| 48 |
return ftp.command(p) |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
index = readfile(index_file) |
| 53 |
|
| 54 |
io.write("testing wrong scheme: ") |
| 55 |
back, err = ftp.get("wrong://banana.com/lixo") |
| 56 |
assert(not back and err == "wrong scheme 'wrong'", err) |
| 57 |
print("ok") |
| 58 |
|
| 59 |
io.write("testing invalid url: ") |
| 60 |
back, err = ftp.get("localhost/dir1/index.html;type=i") |
| 61 |
assert(not back and err) |
| 62 |
print("ok") |
| 63 |
|
| 64 |
io.write("testing anonymous file download: ") |
| 65 |
back, err = socket.ftp.get("ftp://" .. host .. "/pub/index.html;type=i") |
| 66 |
assert(not err and back == index, err) |
| 67 |
print("ok") |
| 68 |
|
| 69 |
io.write("erasing before upload: ") |
| 70 |
ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") |
| 71 |
if not ret then print(err) |
| 72 |
else print("ok") end |
| 73 |
|
| 74 |
io.write("testing upload: ") |
| 75 |
ret, err = ftp.put("ftp://luasocket:password@" .. host .. "/index.up.html;type=i", index) |
| 76 |
assert(ret and not err, err) |
| 77 |
print("ok") |
| 78 |
|
| 79 |
io.write("downloading uploaded file: ") |
| 80 |
back, err = ftp.get("ftp://luasocket:password@" .. host .. "/index.up.html;type=i") |
| 81 |
assert(ret and not err and index == back, err) |
| 82 |
print("ok") |
| 83 |
|
| 84 |
io.write("erasing after upload/download: ") |
| 85 |
ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html") |
| 86 |
assert(ret and not err, err) |
| 87 |
print("ok") |
| 88 |
|
| 89 |
io.write("testing weird-character translation: ") |
| 90 |
back, err = ftp.get("ftp://luasocket:password@" .. host .. "/%23%3f;type=i") |
| 91 |
assert(not err and back == index, err) |
| 92 |
print("ok") |
| 93 |
|
| 94 |
io.write("testing parameter overriding: ") |
| 95 |
local back = {} |
| 96 |
ret, err = ftp.get{ |
| 97 |
url = "//stupid:mistake@" .. host .. "/index.html", |
| 98 |
user = "luasocket", |
| 99 |
password = "password", |
| 100 |
type = "i", |
| 101 |
sink = ltn12.sink.table(back) |
| 102 |
} |
| 103 |
assert(ret and not err and table.concat(back) == index, err) |
| 104 |
print("ok") |
| 105 |
|
| 106 |
io.write("testing upload denial: ") |
| 107 |
ret, err = ftp.put("ftp://" .. host .. "/index.up.html;type=a", index) |
| 108 |
assert(not ret and err, "should have failed") |
| 109 |
print(err) |
| 110 |
|
| 111 |
io.write("testing authentication failure: ") |
| 112 |
ret, err = ftp.get("ftp://luasocket:wrong@".. host .. "/index.html;type=a") |
| 113 |
assert(not ret and err, "should have failed") |
| 114 |
print(err) |
| 115 |
|
| 116 |
io.write("testing wrong file: ") |
| 117 |
back, err = ftp.get("ftp://".. host .. "/index.wrong.html;type=a") |
| 118 |
assert(not back and err, "should have failed") |
| 119 |
print(err) |
| 120 |
|
| 121 |
print("passed all tests") |
| 122 |
print(string.format("done in %.2fs", socket.gettime() - t)) |