ftptest.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. local socket = require("socket")
  2. local ftp = require("socket.ftp")
  3. local url = require("socket.url")
  4. local ltn12 = require("ltn12")
  5. -- use dscl to create user "luasocket" with password "password"
  6. -- with home in /Users/diego/luasocket/test/ftp
  7. -- with group com.apple.access_ftp
  8. -- with shell set to /sbin/nologin
  9. -- set /etc/ftpchroot to chroot luasocket
  10. -- must set group com.apple.access_ftp on user _ftp (for anonymous access)
  11. -- copy index.html to /var/empty/pub (home of user ftp)
  12. -- start ftp server with
  13. -- sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
  14. -- copy index.html to /Users/diego/luasocket/test/ftp
  15. -- stop with
  16. -- sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist
  17. -- override protection to make sure we see all errors
  18. --socket.protect = function(s) return s end
  19. dofile("testsupport.lua")
  20. local host = host or "localhost"
  21. local port, index_file, index, back, err, ret
  22. local t = socket.gettime()
  23. index_file = "index.html"
  24. -- a function that returns a directory listing
  25. local function nlst(u)
  26. local t = {}
  27. local p = url.parse(u)
  28. p.command = "nlst"
  29. p.sink = ltn12.sink.table(t)
  30. local r, e = ftp.get(p)
  31. return r and table.concat(t), e
  32. end
  33. -- function that removes a remote file
  34. local function dele(u)
  35. local p = url.parse(u)
  36. p.command = "dele"
  37. p.argument = string.gsub(p.path, "^/", "")
  38. if p.argumet == "" then p.argument = nil end
  39. p.check = 250
  40. return ftp.command(p)
  41. end
  42. -- read index with CRLF convention
  43. index = readfile(index_file)
  44. io.write("testing wrong scheme: ")
  45. back, err = ftp.get("wrong://banana.com/lixo")
  46. assert(not back and err == "wrong scheme 'wrong'", err)
  47. print("ok")
  48. io.write("testing invalid url: ")
  49. back, err = ftp.get("localhost/dir1/index.html;type=i")
  50. assert(not back and err)
  51. print("ok")
  52. io.write("testing anonymous file download: ")
  53. back, err = socket.ftp.get("ftp://" .. host .. "/pub/index.html;type=i")
  54. assert(not err and back == index, err)
  55. print("ok")
  56. io.write("erasing before upload: ")
  57. ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html")
  58. if not ret then print(err)
  59. else print("ok") end
  60. io.write("testing upload: ")
  61. ret, err = ftp.put("ftp://luasocket:password@" .. host .. "/index.up.html;type=i", index)
  62. assert(ret and not err, err)
  63. print("ok")
  64. io.write("downloading uploaded file: ")
  65. back, err = ftp.get("ftp://luasocket:password@" .. host .. "/index.up.html;type=i")
  66. assert(ret and not err and index == back, err)
  67. print("ok")
  68. io.write("erasing after upload/download: ")
  69. ret, err = dele("ftp://luasocket:password@" .. host .. "/index.up.html")
  70. assert(ret and not err, err)
  71. print("ok")
  72. io.write("testing weird-character translation: ")
  73. back, err = ftp.get("ftp://luasocket:password@" .. host .. "/%23%3f;type=i")
  74. assert(not err and back == index, err)
  75. print("ok")
  76. io.write("testing parameter overriding: ")
  77. local back = {}
  78. ret, err = ftp.get{
  79. url = "//stupid:mistake@" .. host .. "/index.html",
  80. user = "luasocket",
  81. password = "password",
  82. type = "i",
  83. sink = ltn12.sink.table(back)
  84. }
  85. assert(ret and not err and table.concat(back) == index, err)
  86. print("ok")
  87. io.write("testing upload denial: ")
  88. ret, err = ftp.put("ftp://" .. host .. "/index.up.html;type=a", index)
  89. assert(not ret and err, "should have failed")
  90. print(err)
  91. io.write("testing authentication failure: ")
  92. ret, err = ftp.get("ftp://luasocket:wrong@".. host .. "/index.html;type=a")
  93. assert(not ret and err, "should have failed")
  94. print(err)
  95. io.write("testing wrong file: ")
  96. back, err = ftp.get("ftp://".. host .. "/index.wrong.html;type=a")
  97. assert(not back and err, "should have failed")
  98. print(err)
  99. print("passed all tests")
  100. print(string.format("done in %.2fs", socket.gettime() - t))