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