aboutsummaryrefslogtreecommitdiff
path: root/test/ftptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/ftptest.lua')
-rw-r--r--test/ftptest.lua155
1 files changed, 81 insertions, 74 deletions
diff --git a/test/ftptest.lua b/test/ftptest.lua
index f578c82..ef1bf0f 100644
--- a/test/ftptest.lua
+++ b/test/ftptest.lua
@@ -1,103 +1,110 @@
1local similar = function(s1, s2) 1local socket = require("socket")
2 return 2local ftp = require("socket.ftp")
3 string.lower(string.gsub(s1, "%s", "")) == 3local url = require("socket.url")
4 string.lower(string.gsub(s2, "%s", "")) 4local ltn12 = require("ltn12")
5end
6 5
7local readfile = function(name) 6-- override protection to make sure we see all errors
8 local f = io.open(name, "r") 7--socket.protect = function(s) return s end
9 if not f then return nil end
10 local s = f:read("*a")
11 f:close()
12 return s
13end
14 8
15local capture = function(cmd) 9dofile("testsupport.lua")
16 local f = io.popen(cmd)
17 if not f then return nil end
18 local s = f:read("*a")
19 f:close()
20 return s
21end
22 10
23local check = function(v, e, o) 11local host, port, index_file, index, back, err, ret
24 e = e or "failed!"
25 o = o or "ok"
26 if v then print(o)
27 else print(e) os.exit() end
28end
29 12
30-- needs an account luasocket:password 13local t = socket.gettime()
31-- and some directories and files in ~ftp
32 14
33local index, err, saved, back, expected 15host = host or "diego.student.princeton.edu"
16index_file = "test/index.html"
34 17
35local t = socket.time()
36 18
37index = readfile("test/index.html") 19-- a function that returns a directory listing
20local function nlst(u)
21 local t = {}
22 local p = url.parse(u)
23 p.command = "nlst"
24 p.sink = ltn12.sink.table(t)
25 local r, e = ftp.get(p)
26 return r and table.concat(t), e
27end
28
29-- function that removes a remote file
30local function dele(u)
31 local p = url.parse(u)
32 p.command = "dele"
33 p.argument = string.gsub(p.path, "^/", "")
34 if p.argumet == "" then p.argument = nil end
35 p.check = 250
36 return ftp.command(p)
37end
38
39-- read index with CRLF convention
40index = readfile(index_file)
38 41
39io.write("testing wrong scheme: ") 42io.write("testing wrong scheme: ")
40back, err = socket.ftp.get("wrong://banana.com/lixo") 43back, err = ftp.get("wrong://banana.com/lixo")
41check(not back and err == "unknown scheme 'wrong'", err) 44assert(not back and err == "wrong scheme 'wrong'", err)
45print("ok")
42 46
43io.write("testing invalid url: ") 47io.write("testing invalid url: ")
44back, err = socket.ftp.get("localhost/dir1/index.html;type=i") 48back, err = ftp.get("localhost/dir1/index.html;type=i")
45local c, e = socket.connect("", 21) 49assert(not back and err)
46check(not back and err == e, err) 50print("ok")
47
48io.write("testing anonymous file upload: ")
49os.remove("/var/ftp/pub/index.up.html")
50ret, err = socket.ftp.put("ftp://localhost/pub/index.up.html;type=i", index)
51saved = readfile("/var/ftp/pub/index.up.html")
52check(ret and not err and saved == index, err)
53 51
54io.write("testing anonymous file download: ") 52io.write("testing anonymous file download: ")
55back, err = socket.ftp.get("ftp://localhost/pub/index.up.html;type=i") 53back, err = socket.ftp.get("ftp://" .. host .. "/pub/index.html;type=i")
56check(not err and back == index, err) 54assert(not err and back == index, err)
57 55print("ok")
58io.write("testing no directory changes: ") 56
59back, err = socket.ftp.get("ftp://localhost/index.html;type=i") 57io.write("erasing before upload: ")
60check(not err and back == index, err) 58ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html")
61 59if not ret then print(err)
62io.write("testing multiple directory changes: ") 60else print("ok") end
63back, err = socket.ftp.get("ftp://localhost/pub/dir1/dir2/dir3/dir4/dir5/index.html;type=i") 61
64check(not err and back == index, err) 62io.write("testing upload: ")
65 63ret, err = ftp.put("ftp://luasocket:password@" .. host .. "/index.up.html;type=i", index)
66io.write("testing authenticated upload: ") 64assert(ret and not err, err)
67os.remove("/home/luasocket/index.up.html") 65print("ok")
68ret, err = socket.ftp.put("ftp://luasocket:password@localhost/index.up.html;type=i", index) 66
69saved = readfile("/home/luasocket/index.up.html") 67io.write("downloading uploaded file: ")
70check(ret and not err and saved == index, err) 68back, err = ftp.get("ftp://luasocket:password@" .. host .. "/index.up.html;type=i")
71 69assert(ret and not err and index == back, err)
72io.write("testing authenticated download: ") 70print("ok")
73back, err = socket.ftp.get("ftp://luasocket:password@localhost/index.up.html;type=i") 71
74check(not err and back == index, err) 72io.write("erasing after upload/download: ")
73ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html")
74assert(ret and not err, err)
75print("ok")
75 76
76io.write("testing weird-character translation: ") 77io.write("testing weird-character translation: ")
77back, err = socket.ftp.get("ftp://luasocket:password@localhost/%2fvar/ftp/pub/index.html;type=i") 78back, err = ftp.get("ftp://luasocket:password@" .. host .. "/%23%3f;type=i")
78check(not err and back == index, err) 79assert(not err and back == index, err)
80print("ok")
79 81
80io.write("testing parameter overriding: ") 82io.write("testing parameter overriding: ")
81back, err = socket.ftp.get { 83local back = {}
82 url = "//stupid:mistake@localhost/index.html", 84ret, err = ftp.get{
85 url = "//stupid:mistake@" .. host .. "/index.html",
83 user = "luasocket", 86 user = "luasocket",
84 password = "password", 87 password = "password",
85 type = "i" 88 type = "i",
89 sink = ltn12.sink.table(back)
86} 90}
87check(not err and back == index, err) 91assert(ret and not err and table.concat(back) == index, err)
92print("ok")
88 93
89io.write("testing upload denial: ") 94io.write("testing upload denial: ")
90ret, err = socket.ftp.put("ftp://localhost/index.up.html;type=a", index) 95ret, err = ftp.put("ftp://" .. host .. "/index.up.html;type=a", index)
91check(err, err) 96assert(not ret and err, "should have failed")
97print(err)
92 98
93io.write("testing authentication failure: ") 99io.write("testing authentication failure: ")
94ret, err = socket.ftp.put("ftp://luasocket:wrong@localhost/index.html;type=a", index) 100ret, err = ftp.get("ftp://luasocket:wrong@".. host .. "/index.html;type=a")
101assert(not ret and err, "should have failed")
95print(err) 102print(err)
96check(not ret and err, err)
97 103
98io.write("testing wrong file: ") 104io.write("testing wrong file: ")
99back, err = socket.ftp.get("ftp://localhost/index.wrong.html;type=a") 105back, err = ftp.get("ftp://".. host .. "/index.wrong.html;type=a")
100check(err, err) 106assert(not back and err, "should have failed")
107print(err)
101 108
102print("passed all tests") 109print("passed all tests")
103print(string.format("done in %.2fs", socket.time() - t)) 110print(string.format("done in %.2fs", socket.gettime() - t))