diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-01-25 21:59:39 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-01-25 21:59:39 +0000 |
| commit | 7096b8df82eebfe857e0043bc8a853353bd78480 (patch) | |
| tree | f5cdc12138e9526896ff5f8fe9e3ffaa0c47fc0c | |
| parent | 68f51243b38bf1e32d471e9cc9ddf12a25110e80 (diff) | |
| download | luasocket-7096b8df82eebfe857e0043bc8a853353bd78480.tar.gz luasocket-7096b8df82eebfe857e0043bc8a853353bd78480.tar.bz2 luasocket-7096b8df82eebfe857e0043bc8a853353bd78480.zip | |
Initial revision
| -rw-r--r-- | samples/daytimeclnt.lua | 14 | ||||
| -rw-r--r-- | samples/echoclnt.lua | 21 | ||||
| -rw-r--r-- | samples/echosrvr.lua | 22 | ||||
| -rw-r--r-- | test/ftptest.lua | 33 | ||||
| -rw-r--r-- | test/httptest.lua | 85 | ||||
| -rw-r--r-- | test/tftptest.lua | 16 |
6 files changed, 191 insertions, 0 deletions
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 @@ | |||
| 1 | host = host or "127.0.0.1" | ||
| 2 | port = port or 13 | ||
| 3 | if arg then | ||
| 4 | host = arg[1] or host | ||
| 5 | port = arg[2] or port | ||
| 6 | end | ||
| 7 | host = toip(host) | ||
| 8 | udp = udpsocket() | ||
| 9 | print("Using host '" ..host.. "' and port " ..port.. "...") | ||
| 10 | err = sendto(udp, "anything", host, port) | ||
| 11 | if err then print(err) exit() end | ||
| 12 | dgram, err = receive(udp) | ||
| 13 | if not dgram then print(err) exit() end | ||
| 14 | 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 @@ | |||
| 1 | host = host or "localhost" | ||
| 2 | port = port or 7 | ||
| 3 | if arg then | ||
| 4 | host = arg[1] or host | ||
| 5 | port = arg[2] or port | ||
| 6 | end | ||
| 7 | host = toip(host) | ||
| 8 | udp, err = udpsocket() | ||
| 9 | if not udp then print(err) exit() end | ||
| 10 | err = setpeername(udp, host, port) | ||
| 11 | if err then print(err) exit() end | ||
| 12 | print("Using host '" ..host.. "' and port " ..port.. "...") | ||
| 13 | while 1 do | ||
| 14 | line = read() | ||
| 15 | if not line then exit() end | ||
| 16 | err = send(udp, line) | ||
| 17 | if err then print(err) exit() end | ||
| 18 | dgram, err = receive(udp) | ||
| 19 | if not dgram then print(err) exit() end | ||
| 20 | print(dgram) | ||
| 21 | 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 @@ | |||
| 1 | host = host or "127.0.0.1" | ||
| 2 | port = port or 7 | ||
| 3 | if arg then | ||
| 4 | host = arg[1] or host | ||
| 5 | port = arg[2] or port | ||
| 6 | end | ||
| 7 | print("Binding to host '" ..host.. "' and port " ..port.. "...") | ||
| 8 | udp, err = udpsocket() | ||
| 9 | if not udp then print(err) exit() end | ||
| 10 | err = setsockname(udp, host, port) | ||
| 11 | if err then print(err) exit() end | ||
| 12 | timeout(udp, 5) | ||
| 13 | ip, port = getsockname(udp) | ||
| 14 | print("Waiting packets on " .. ip .. ":" .. port .. "...") | ||
| 15 | while 1 do | ||
| 16 | dgram, ip, port = receivefrom(udp) | ||
| 17 | if not dgram then print(ip) | ||
| 18 | else | ||
| 19 | print("Echoing from " .. ip .. ":" .. port) | ||
| 20 | sendto(udp, dgram, ip, port) | ||
| 21 | end | ||
| 22 | 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 @@ | |||
| 1 | assert(dofile("../lua/ftp.lua")) | ||
| 2 | assert(dofile("auxiliar.lua")) | ||
| 3 | |||
| 4 | pdir = "/home/i/diego/public/html/luasocket/test/" | ||
| 5 | ldir = "/home/luasocket/" | ||
| 6 | adir = "/home/ftp/test/" | ||
| 7 | |||
| 8 | -- needs an accound luasocket:password | ||
| 9 | -- and a copy of /home/i/diego/public/html/luasocket/test in ~ftp/test | ||
| 10 | |||
| 11 | print("testing authenticated upload") | ||
| 12 | bf = readfile(pdir .. "index.html") | ||
| 13 | e = ftp_put("ftp://luasocket:password@localhost/index.html", bf, "b") | ||
| 14 | assert(not e, e) | ||
| 15 | assert(compare(ldir .. "index.html", bf), "files differ") | ||
| 16 | remove(ldir .. "index.html") | ||
| 17 | |||
| 18 | print("testing authenticated download") | ||
| 19 | f, e = ftp_get("ftp://luasocket:password@localhost/test/index.html", "b") | ||
| 20 | assert(f, e) | ||
| 21 | assert(compare(pdir .. "index.html", f), "files differ") | ||
| 22 | |||
| 23 | print("testing anonymous download") | ||
| 24 | f, e = ftp_get("ftp://localhost/test/index.html", "b") | ||
| 25 | assert(f, e) | ||
| 26 | assert(compare(adir .. "index.html", f), "files differ") | ||
| 27 | |||
| 28 | print("testing directory listing") | ||
| 29 | f, e = ftp_get("ftp://localhost/test/") | ||
| 30 | assert(f, e) | ||
| 31 | assert(f == "index.html\r\n", "files differ") | ||
| 32 | |||
| 33 | 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 @@ | |||
| 1 | -- load http | ||
| 2 | assert(dofile("../lua/http.lua")) | ||
| 3 | assert(dofile("../lua/base64.lua")) | ||
| 4 | assert(dofile("auxiliar.lua")) | ||
| 5 | |||
| 6 | -- needs Alias from /home/i/diego/public/html/luasocket/test to | ||
| 7 | -- /luasocket-test | ||
| 8 | -- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi-bin | ||
| 9 | -- to /luasocket-cgi-bin/ | ||
| 10 | |||
| 11 | function join(s, e) | ||
| 12 | return tostring(s) .. ":" .. tostring(e) | ||
| 13 | end | ||
| 14 | |||
| 15 | function status(s) | ||
| 16 | local code | ||
| 17 | _,_, code = strfind(s, "(%d%d%d)") | ||
| 18 | return tonumber(code) | ||
| 19 | end | ||
| 20 | |||
| 21 | pdir = pdir or "/home/i/diego/public/html/luasocket/test/" | ||
| 22 | host = host or "localhost" | ||
| 23 | |||
| 24 | print("testing document retrieval") | ||
| 25 | url = "http://" .. host .. "/luasocket-test/index.html" | ||
| 26 | f, m, s, e = http_get(url) | ||
| 27 | assert(f and m and s and not e, join(s, e)) | ||
| 28 | assert(compare(pdir .. "index.html", f), "documents differ") | ||
| 29 | |||
| 30 | print("testing HTTP redirection") | ||
| 31 | url = "http://" .. host .. "/luasocket-test" | ||
| 32 | f, m, s, e = http_get(url) | ||
| 33 | assert(f and m and s and not e, join(s, e)) | ||
| 34 | assert(compare(pdir .. "index.html", f), "documents differ") | ||
| 35 | |||
| 36 | print("testing cgi output retrieval (probably chunked...)") | ||
| 37 | url = "http://" .. host .. "/luasocket-cgi-bin/cat-index-html" | ||
| 38 | f, m, s, e = http_get(url) | ||
| 39 | assert(f and m and s and not e, join(s, e)) | ||
| 40 | assert(compare(pdir .. "index.html", f), "documents differ") | ||
| 41 | |||
| 42 | print("testing post method") | ||
| 43 | url = "http://" .. host .. "/luasocket-cgi-bin/cat" | ||
| 44 | rf = strrep("!@#$!@#%", 80000) | ||
| 45 | f, m, s, e = http_post(url, rf) | ||
| 46 | assert(f and m and s and not e) | ||
| 47 | assert(rf == f, "files differ") | ||
| 48 | |||
| 49 | print("testing automatic auth failure") | ||
| 50 | url = "http://really:wrong@" .. host .. "/luasocket-test/auth/index.html" | ||
| 51 | f, m, s, e = http_get(url) | ||
| 52 | assert(f and m and s and not e and status(s) == 401) | ||
| 53 | |||
| 54 | write("testing host not found: ") | ||
| 55 | url = "http://wronghost/luasocket-test/index.html" | ||
| 56 | f, m, s, e = http_get(url) | ||
| 57 | assert(not f and not m and not s and e) | ||
| 58 | print(e) | ||
| 59 | |||
| 60 | write("testing auth failure: ") | ||
| 61 | url = "http://" .. host .. "/luasocket-test/auth/index.html" | ||
| 62 | f, m, s, e = http_get(url) | ||
| 63 | assert(f and m and s and not e and status(s) == 401) | ||
| 64 | print(s) | ||
| 65 | |||
| 66 | write("testing document not found: ") | ||
| 67 | url = "http://" .. host .. "/luasocket-test/wrongdocument.html" | ||
| 68 | f, m, s, e = http_get(url) | ||
| 69 | assert(f and m and s and not e and status(s) == 404) | ||
| 70 | print(s) | ||
| 71 | |||
| 72 | print("testing manual auth") | ||
| 73 | url = "http://" .. host .. "/luasocket-test/auth/index.html" | ||
| 74 | h = {authorization = "Basic " .. base64("luasocket:password")} | ||
| 75 | f, m, s, e = http_get(url, h) | ||
| 76 | assert(f and m and s and not e, join(s, e)) | ||
| 77 | assert(compare(pdir .. "auth/index.html", f), "documents differ") | ||
| 78 | |||
| 79 | print("testing automatic auth") | ||
| 80 | url = "http://luasocket:password@" .. host .. "/luasocket-test/auth/index.html" | ||
| 81 | f, m, s, e = http_get(url) | ||
| 82 | assert(f and m and s and not e, join(s, e)) | ||
| 83 | assert(compare(pdir .. "auth/index.html", f), "documents differ") | ||
| 84 | |||
| 85 | 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 @@ | |||
| 1 | -- load tftpclng.lua | ||
| 2 | assert(dofile("../examples/tftpclnt.lua")) | ||
| 3 | assert(dofile("auxiliar.lua")) | ||
| 4 | |||
| 5 | -- needs tftp server running on localhost, with root pointing to | ||
| 6 | -- /home/i/diego/public/html/luasocket/test | ||
| 7 | |||
| 8 | host = host or "localhost" | ||
| 9 | print("downloading") | ||
| 10 | err = tftp_get(host, 69, "test/index.html") | ||
| 11 | assert(not err, err) | ||
| 12 | original = readfile("/home/i/diego/public/html/luasocket/test/index.html") | ||
| 13 | retrieved = readfile("index.html") | ||
| 14 | remove("index.html") | ||
| 15 | assert(original == retrieved, "files differ!") | ||
| 16 | print("passed") | ||
