aboutsummaryrefslogtreecommitdiff
path: root/test/ftptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/ftptest.lua')
-rw-r--r--test/ftptest.lua118
1 files changed, 61 insertions, 57 deletions
diff --git a/test/ftptest.lua b/test/ftptest.lua
index 34cccf1..ee3af91 100644
--- a/test/ftptest.lua
+++ b/test/ftptest.lua
@@ -1,29 +1,32 @@
1dofile("noglobals.lua") 1dofile("noglobals.lua")
2 2
3local similar = function(s1, s2) 3local similar = function(s1, s2)
4 return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", "")) 4 return
5 string.lower(string.gsub(s1, "%s", "")) ==
6 string.lower(string.gsub(s2, "%s", ""))
5end 7end
6 8
7local capture = function(cmd) 9local readfile = function(name)
8 readfrom("| " .. cmd) 10 local f = io.open(name, "r")
9 local s = read("*a") 11 if not f then return nil end
10 readfrom() 12 local s = f:read("*a")
13 f:close()
11 return s 14 return s
12end 15end
13 16
14local readfile = function(name) 17local capture = function(cmd)
15 local f = readfrom(name) 18 local f = io.popen(cmd)
16 if not f then return nil end 19 if not f then return nil end
17 local s = read("*a") 20 local s = f:read("*a")
18 readfrom() 21 f:close()
19 return s 22 return s
20end 23end
21 24
22local check = function(v, e, o) 25local check = function(v, e, o)
23 e = e or "failed!" 26 e = e or "failed!"
24 o = o or "ok" 27 o = o or "ok"
25 if v then print(o) 28 if v then print(o)
26 else print(e) exit() end 29 else print(e) os.exit() end
27end 30end
28 31
29-- needs an account luasocket:password 32-- needs an account luasocket:password
@@ -31,81 +34,82 @@ end
31 34
32local index, err, saved, back, expected 35local index, err, saved, back, expected
33 36
34local t = _time() 37local t = socket._time()
35 38
36index = readfile("index.html") 39index = readfile("test/index.html")
40
41io.write("testing wrong scheme: ")
42back, err = socket.ftp.get("wrong://banana.com/lixo")
43check(not back and err == "unknown scheme 'wrong'", err)
44
45io.write("testing invalid url: ")
46back, err = socket.ftp.get("localhost/dir1/index.html;type=i")
47local c, e = socket.connect("", 21)
48check(not back and err == e, err)
37 49
38write("testing file upload: ") 50io.write("testing anonymous file upload: ")
39remove("/var/ftp/dir1/index.up.html") 51os.remove("/var/ftp/pub/index.up.html")
40err = FTP.put("ftp://localhost/dir1/index.up.html;type=i", index) 52err = socket.ftp.put("ftp://localhost/pub/index.up.html;type=i", index)
41saved = readfile("/var/ftp/dir1/index.up.html") 53saved = readfile("/var/ftp/pub/index.up.html")
42check(not err and saved == index, err) 54check(not err and saved == index, err)
43 55
44write("testing file download: ") 56io.write("testing anonymous file download: ")
45back, err = FTP.get("ftp://localhost/dir1/index.up.html;type=i") 57back, err = socket.ftp.get("ftp://localhost/pub/index.up.html;type=i")
46check(not err and back == index, err) 58check(not err and back == index, err)
47 59
48write("testing no directory changes: ") 60io.write("testing no directory changes: ")
49back, err = FTP.get("ftp://localhost/index.html;type=i") 61back, err = socket.ftp.get("ftp://localhost/index.html;type=i")
50check(not err and back == index, err) 62check(not err and back == index, err)
51 63
52write("testing multiple directory changes: ") 64io.write("testing multiple directory changes: ")
53back, err = FTP.get("ftp://localhost/dir1/dir2/dir3/dir4/dir5/dir6/index.html;type=i") 65back, err = socket.ftp.get("ftp://localhost/pub/dir1/dir2/dir3/dir4/dir5/index.html;type=i")
54check(not err and back == index, err) 66check(not err and back == index, err)
55 67
56write("testing authenticated upload: ") 68io.write("testing authenticated upload: ")
57remove("/home/luasocket/index.up.html") 69os.remove("/home/luasocket/index.up.html")
58err = FTP.put("ftp://luasocket:password@localhost/index.up.html;type=i", index) 70err = socket.ftp.put("ftp://luasocket:password@localhost/index.up.html;type=i", index)
59saved = readfile("/home/luasocket/index.up.html") 71saved = readfile("/home/luasocket/index.up.html")
60check(not err and saved == index, err) 72check(not err and saved == index, err)
61 73
62write("testing authenticated download: ") 74io.write("testing authenticated download: ")
63back, err = FTP.get("ftp://luasocket:password@localhost/index.up.html;type=i") 75back, err = socket.ftp.get("ftp://luasocket:password@localhost/index.up.html;type=i")
64check(not err and back == index, err) 76check(not err and back == index, err)
65 77
66write("testing weird-character translation: ") 78io.write("testing weird-character translation: ")
67back, err = FTP.get("ftp://luasocket:password@localhost/%2fvar/ftp/dir1/index.html;type=i") 79back, err = socket.ftp.get("ftp://luasocket:password@localhost/%2fvar/ftp/pub/index.html;type=i")
68check(not err and back == index, err) 80check(not err and back == index, err)
69 81
70write("testing parameter overriding: ") 82io.write("testing parameter overriding: ")
71back, err = FTP.get { 83back, err = socket.ftp.get {
72 url = "//stupid:mistake@localhost/dir1/index.html", 84 url = "//stupid:mistake@localhost/index.html",
73 user = "luasocket", 85 user = "luasocket",
74 password = "password", 86 password = "password",
75 type = "i" 87 type = "i"
76} 88}
77check(not err and back == index, err) 89check(not err and back == index, err)
78 90
79write("testing wrong scheme: ") 91io.write("testing home directory listing: ")
80back, err = FTP.get("wrong://banana.com/lixo")
81check(not back and err == "unknown scheme 'wrong'", err)
82
83write("testing invalid url: ")
84back, err = FTP.get("localhost/dir1/index.html;type=i")
85local c, e = connect("", 21)
86check(not back and err == e, err)
87
88write("testing directory listing: ")
89expected = capture("ls -F /var/ftp/dir1 | grep -v /")
90back, err = FTP.get("ftp://localhost/dir1;type=d")
91check(similar(back, expected))
92
93write("testing home directory listing: ")
94expected = capture("ls -F /var/ftp | grep -v /") 92expected = capture("ls -F /var/ftp | grep -v /")
95back, err = FTP.get("ftp://localhost/") 93back, err = socket.ftp.get("ftp://localhost/")
96check(back and similar(back, expected), nil, err) 94check(back and similar(back, expected), nil, err)
97 95
98write("testing upload denial: ") 96io.write("testing directory listing: ")
99err = FTP.put("ftp://localhost/index.up.html;type=a", index) 97expected = capture("ls -F /var/ftp/pub | grep -v /")
98back, err = socket.ftp.get("ftp://localhost/pub;type=d")
99check(similar(back, expected))
100
101io.write("testing upload denial: ")
102err = socket.ftp.put("ftp://localhost/index.up.html;type=a", index)
100check(err, err) 103check(err, err)
101 104
102write("testing authentication failure: ") 105io.write("testing authentication failure: ")
103err = FTP.put("ftp://luasocket:wrong@localhost/index.html;type=a", index) 106err = socket.ftp.put("ftp://luasocket:wrong@localhost/index.html;type=a", index)
107print(err)
104check(err, err) 108check(err, err)
105 109
106write("testing wrong file: ") 110io.write("testing wrong file: ")
107back, err = FTP.get("ftp://localhost/index.wrong.html;type=a") 111back, err = socket.ftp.get("ftp://localhost/index.wrong.html;type=a")
108check(err, err) 112check(err, err)
109 113
110print("passed all tests") 114print("passed all tests")
111print(format("done in %.2fs", _time() - t)) 115print(string.format("done in %.2fs", socket._time() - t))