Debian Codegraph @

texk/web2c/luatexdir/luasocket/test/ftptest.lua

raw symbols SCIP
1 local socket = require("socket")
2 local ftp = require("socket.ftp")
3 local url = require("socket.url")
4 local ltn12 = require("ltn12")
5
6 -- use dscl to create user "luasocket" with password "password"
7 -- with home in /Users/diego/luasocket/test/ftp
8 -- with group com.apple.access_ftp
9 -- with shell set to /sbin/nologin
10 -- set /etc/ftpchroot to chroot luasocket
11 -- must set group com.apple.access_ftp on user _ftp (for anonymous access)
12 -- copy index.html to /var/empty/pub (home of user ftp)
13 -- start ftp server with
14 -- sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
15 -- copy index.html to /Users/diego/luasocket/test/ftp
16 -- stop with
17 -- sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist
18
19 -- override protection to make sure we see all errors
20 --socket.protect = function(s) return s end
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 -- a function that returns a directory listing
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 -- function that removes a remote file
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 -- read index with CRLF convention
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))