diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua new file mode 100644 index 0000000..94e03d3 --- /dev/null +++ b/samples/daytimeclnt.lua @@ -0,0 +1,14 @@ +host = host or "127.0.0.1" +port = port or 13 +if arg then + host = arg[1] or host + port = arg[2] or port +end +host = toip(host) +udp = udpsocket() +print("Using host '" ..host.. "' and port " ..port.. "...") +err = sendto(udp, "anything", host, port) +if err then print(err) exit() end +dgram, err = receive(udp) +if not dgram then print(err) exit() end +write(dgram) diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua new file mode 100644 index 0000000..d1c56c7 --- /dev/null +++ b/samples/echoclnt.lua @@ -0,0 +1,21 @@ +host = host or "localhost" +port = port or 7 +if arg then + host = arg[1] or host + port = arg[2] or port +end +host = toip(host) +udp, err = udpsocket() +if not udp then print(err) exit() end +err = setpeername(udp, host, port) +if err then print(err) exit() end +print("Using host '" ..host.. "' and port " ..port.. "...") +while 1 do + line = read() + if not line then exit() end + err = send(udp, line) + if err then print(err) exit() end + dgram, err = receive(udp) + if not dgram then print(err) exit() end + print(dgram) +end diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua new file mode 100644 index 0000000..fe7da06 --- /dev/null +++ b/samples/echosrvr.lua @@ -0,0 +1,22 @@ +host = host or "127.0.0.1" +port = port or 7 +if arg then + host = arg[1] or host + port = arg[2] or port +end +print("Binding to host '" ..host.. "' and port " ..port.. "...") +udp, err = udpsocket() +if not udp then print(err) exit() end +err = setsockname(udp, host, port) +if err then print(err) exit() end +timeout(udp, 5) +ip, port = getsockname(udp) +print("Waiting packets on " .. ip .. ":" .. port .. "...") +while 1 do + dgram, ip, port = receivefrom(udp) + if not dgram then print(ip) + else + print("Echoing from " .. ip .. ":" .. port) + sendto(udp, dgram, ip, port) + end +end diff --git a/test/ftptest.lua b/test/ftptest.lua new file mode 100644 index 0000000..85dc16b --- /dev/null +++ b/test/ftptest.lua @@ -0,0 +1,33 @@ +assert(dofile("../lua/ftp.lua")) +assert(dofile("auxiliar.lua")) + +pdir = "/home/i/diego/public/html/luasocket/test/" +ldir = "/home/luasocket/" +adir = "/home/ftp/test/" + +-- needs an accound luasocket:password +-- and a copy of /home/i/diego/public/html/luasocket/test in ~ftp/test + +print("testing authenticated upload") +bf = readfile(pdir .. "index.html") +e = ftp_put("ftp://luasocket:password@localhost/index.html", bf, "b") +assert(not e, e) +assert(compare(ldir .. "index.html", bf), "files differ") +remove(ldir .. "index.html") + +print("testing authenticated download") +f, e = ftp_get("ftp://luasocket:password@localhost/test/index.html", "b") +assert(f, e) +assert(compare(pdir .. "index.html", f), "files differ") + +print("testing anonymous download") +f, e = ftp_get("ftp://localhost/test/index.html", "b") +assert(f, e) +assert(compare(adir .. "index.html", f), "files differ") + +print("testing directory listing") +f, e = ftp_get("ftp://localhost/test/") +assert(f, e) +assert(f == "index.html\r\n", "files differ") + +print("passed all tests") diff --git a/test/httptest.lua b/test/httptest.lua new file mode 100644 index 0000000..b88475d --- /dev/null +++ b/test/httptest.lua @@ -0,0 +1,85 @@ +-- load http +assert(dofile("../lua/http.lua")) +assert(dofile("../lua/base64.lua")) +assert(dofile("auxiliar.lua")) + +-- needs Alias from /home/i/diego/public/html/luasocket/test to +-- /luasocket-test +-- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi-bin +-- to /luasocket-cgi-bin/ + +function join(s, e) + return tostring(s) .. ":" .. tostring(e) +end + +function status(s) + local code + _,_, code = strfind(s, "(%d%d%d)") + return tonumber(code) +end + +pdir = pdir or "/home/i/diego/public/html/luasocket/test/" +host = host or "localhost" + +print("testing document retrieval") +url = "http://" .. host .. "/luasocket-test/index.html" +f, m, s, e = http_get(url) +assert(f and m and s and not e, join(s, e)) +assert(compare(pdir .. "index.html", f), "documents differ") + +print("testing HTTP redirection") +url = "http://" .. host .. "/luasocket-test" +f, m, s, e = http_get(url) +assert(f and m and s and not e, join(s, e)) +assert(compare(pdir .. "index.html", f), "documents differ") + +print("testing cgi output retrieval (probably chunked...)") +url = "http://" .. host .. "/luasocket-cgi-bin/cat-index-html" +f, m, s, e = http_get(url) +assert(f and m and s and not e, join(s, e)) +assert(compare(pdir .. "index.html", f), "documents differ") + +print("testing post method") +url = "http://" .. host .. "/luasocket-cgi-bin/cat" +rf = strrep("!@#$!@#%", 80000) +f, m, s, e = http_post(url, rf) +assert(f and m and s and not e) +assert(rf == f, "files differ") + +print("testing automatic auth failure") +url = "http://really:wrong@" .. host .. "/luasocket-test/auth/index.html" +f, m, s, e = http_get(url) +assert(f and m and s and not e and status(s) == 401) + +write("testing host not found: ") +url = "http://wronghost/luasocket-test/index.html" +f, m, s, e = http_get(url) +assert(not f and not m and not s and e) +print(e) + +write("testing auth failure: ") +url = "http://" .. host .. "/luasocket-test/auth/index.html" +f, m, s, e = http_get(url) +assert(f and m and s and not e and status(s) == 401) +print(s) + +write("testing document not found: ") +url = "http://" .. host .. "/luasocket-test/wrongdocument.html" +f, m, s, e = http_get(url) +assert(f and m and s and not e and status(s) == 404) +print(s) + +print("testing manual auth") +url = "http://" .. host .. "/luasocket-test/auth/index.html" +h = {authorization = "Basic " .. base64("luasocket:password")} +f, m, s, e = http_get(url, h) +assert(f and m and s and not e, join(s, e)) +assert(compare(pdir .. "auth/index.html", f), "documents differ") + +print("testing automatic auth") +url = "http://luasocket:password@" .. host .. "/luasocket-test/auth/index.html" +f, m, s, e = http_get(url) +assert(f and m and s and not e, join(s, e)) +assert(compare(pdir .. "auth/index.html", f), "documents differ") + +print("passed all tests") diff --git a/test/tftptest.lua b/test/tftptest.lua new file mode 100644 index 0000000..7fb8253 --- /dev/null +++ b/test/tftptest.lua @@ -0,0 +1,16 @@ +-- load tftpclng.lua +assert(dofile("../examples/tftpclnt.lua")) +assert(dofile("auxiliar.lua")) + +-- needs tftp server running on localhost, with root pointing to +-- /home/i/diego/public/html/luasocket/test + +host = host or "localhost" +print("downloading") +err = tftp_get(host, 69, "test/index.html") +assert(not err, err) +original = readfile("/home/i/diego/public/html/luasocket/test/index.html") +retrieved = readfile("index.html") +remove("index.html") +assert(original == retrieved, "files differ!") +print("passed")