diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/httptest.lua | 16 | ||||
| -rw-r--r-- | test/mimetest.lua | 236 |
2 files changed, 244 insertions, 8 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index 3d0db87..dc90741 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
| @@ -5,14 +5,14 @@ | |||
| 5 | -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth | 5 | -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth |
| 6 | dofile("noglobals.lua") | 6 | dofile("noglobals.lua") |
| 7 | 7 | ||
| 8 | local host, proxyh, proxyp, request, response | 8 | local host, proxyhost, proxyport, request, response |
| 9 | local ignore, expect, index, prefix, cgiprefix | 9 | local ignore, expect, index, prefix, cgiprefix |
| 10 | 10 | ||
| 11 | local t = socket.time() | 11 | local t = socket.time() |
| 12 | 12 | ||
| 13 | host = host or "diego.princeton.edu" | 13 | host = host or "diego.princeton.edu" |
| 14 | proxyh = proxyh or "localhost" | 14 | proxyhost = proxyhost or "localhost" |
| 15 | proxyp = proxyp or 3128 | 15 | proxyport = proxyport or 3128 |
| 16 | prefix = prefix or "/luasocket-test" | 16 | prefix = prefix or "/luasocket-test" |
| 17 | cgiprefix = cgiprefix or "/luasocket-test-cgi" | 17 | cgiprefix = cgiprefix or "/luasocket-test-cgi" |
| 18 | 18 | ||
| @@ -129,8 +129,8 @@ request = { | |||
| 129 | method = "POST", | 129 | method = "POST", |
| 130 | body = index, | 130 | body = index, |
| 131 | headers = { ["content-length"] = string.len(index) }, | 131 | headers = { ["content-length"] = string.len(index) }, |
| 132 | port = proxyp, | 132 | proxyport = proxyport, |
| 133 | host = proxyh | 133 | proxyhost = proxyhost |
| 134 | } | 134 | } |
| 135 | expect = { | 135 | expect = { |
| 136 | body = index, | 136 | body = index, |
| @@ -170,8 +170,8 @@ check_request(request, expect, ignore) | |||
| 170 | io.write("testing proxy with redirection: ") | 170 | io.write("testing proxy with redirection: ") |
| 171 | request = { | 171 | request = { |
| 172 | url = "http://" .. host .. prefix, | 172 | url = "http://" .. host .. prefix, |
| 173 | host = proxyh, | 173 | proxyhost = proxyhost, |
| 174 | port = proxyp | 174 | proxyport = proxyport |
| 175 | } | 175 | } |
| 176 | expect = { | 176 | expect = { |
| 177 | body = index, | 177 | body = index, |
| @@ -267,7 +267,7 @@ io.write("testing manual basic auth: ") | |||
| 267 | request = { | 267 | request = { |
| 268 | url = "http://" .. host .. prefix .. "/auth/index.html", | 268 | url = "http://" .. host .. prefix .. "/auth/index.html", |
| 269 | headers = { | 269 | headers = { |
| 270 | authorization = "Basic " .. (socket.code.b64("luasocket:password")) | 270 | authorization = "Basic " .. (socket.mime.b64("luasocket:password")) |
| 271 | } | 271 | } |
| 272 | } | 272 | } |
| 273 | expect = { | 273 | expect = { |
diff --git a/test/mimetest.lua b/test/mimetest.lua new file mode 100644 index 0000000..5485db1 --- /dev/null +++ b/test/mimetest.lua | |||
| @@ -0,0 +1,236 @@ | |||
| 1 | dofile("noglobals.lua") | ||
| 2 | |||
| 3 | local qptest = "qptest.bin" | ||
| 4 | local eqptest = "qptest.bin2" | ||
| 5 | local dqptest = "qptest.bin3" | ||
| 6 | |||
| 7 | local b64test = "luasocket" | ||
| 8 | local eb64test = "b64test.bin" | ||
| 9 | local db64test = "b64test.bin2" | ||
| 10 | |||
| 11 | -- from Machado de Assis, "A Mão e a Rosa" | ||
| 12 | local mao = [[ | ||
| 13 | Cursavam estes dois moços a academia de S. Paulo, estando | ||
| 14 | Luís Alves no quarto ano e Estêvão no terceiro. | ||
| 15 | Conheceram-se na academia, e ficaram amigos íntimos, tanto | ||
| 16 | quanto podiam sê-lo dois espíritos diferentes, ou talvez por | ||
| 17 | isso mesmo que o eram. Estêvão, dotado de extrema | ||
| 18 | sensibilidade, e não menor fraqueza de ânimo, afetuoso e | ||
| 19 | bom, não daquela bondade varonil, que é apanágio de uma alma | ||
| 20 | forte, mas dessa outra bondade mole e de cera, que vai à | ||
| 21 | mercê de todas as circunstâncias, tinha, além de tudo isso, | ||
| 22 | o infortúnio de trazer ainda sobre o nariz os óculos | ||
| 23 | cor-de-rosa de suas virginais ilusões. Luís Alves via bem | ||
| 24 | com os olhos da cara. Não era mau rapaz, mas tinha o seu | ||
| 25 | grão de egoísmo, e se não era incapaz de afeições, sabia | ||
| 26 | regê-las, moderá-las, e sobretudo guiá-las ao seu próprio | ||
| 27 | interesse. Entre estes dois homens travara-se amizade | ||
| 28 | íntima, nascida para um na simpatia, para outro no costume. | ||
| 29 | Eram eles os naturais confidentes um do outro, com a | ||
| 30 | diferença que Luís Alves dava menos do que recebia, e, ainda | ||
| 31 | assim, nem tudo o que dava exprimia grande confiança. | ||
| 32 | ]] | ||
| 33 | |||
| 34 | local fail = function(s) | ||
| 35 | s = s or "failed" | ||
| 36 | assert(nil, s) | ||
| 37 | end | ||
| 38 | |||
| 39 | local readfile = function(name) | ||
| 40 | local f = io.open(name, "r") | ||
| 41 | if not f then return nil end | ||
| 42 | local s = f:read("*a") | ||
| 43 | f:close() | ||
| 44 | return s | ||
| 45 | end | ||
| 46 | |||
| 47 | local function transform(input, output, filter) | ||
| 48 | local fi, err = io.open(input, "rb") | ||
| 49 | if not fi then fail(err) end | ||
| 50 | local fo, err = io.open(output, "wb") | ||
| 51 | if not fo then fail(err) end | ||
| 52 | while 1 do | ||
| 53 | local chunk = fi:read(math.random(0, 256)) | ||
| 54 | fo:write(filter(chunk)) | ||
| 55 | if not chunk then break end | ||
| 56 | end | ||
| 57 | fi:close() | ||
| 58 | fo:close() | ||
| 59 | end | ||
| 60 | |||
| 61 | local function compare(input, output) | ||
| 62 | local original = readfile(input) | ||
| 63 | local recovered = readfile(output) | ||
| 64 | if original ~= recovered then fail("recovering failed") | ||
| 65 | else print("ok") end | ||
| 66 | end | ||
| 67 | |||
| 68 | local function encode_qptest(mode) | ||
| 69 | local encode = socket.mime.qprint.encode(mode) | ||
| 70 | local split = socket.mime.qprint.split() | ||
| 71 | local chain = socket.mime.chain(encode, split) | ||
| 72 | transform(qptest, eqptest, chain) | ||
| 73 | end | ||
| 74 | |||
| 75 | local function compare_qptest() | ||
| 76 | compare(qptest, dqptest) | ||
| 77 | end | ||
| 78 | |||
| 79 | local function decode_qptest() | ||
| 80 | local decode = socket.mime.qprint.decode() | ||
| 81 | transform(eqptest, dqptest, decode) | ||
| 82 | end | ||
| 83 | |||
| 84 | local function create_qptest() | ||
| 85 | local f, err = io.open(qptest, "wb") | ||
| 86 | if not f then fail(err) end | ||
| 87 | -- try all characters | ||
| 88 | for i = 0, 255 do | ||
| 89 | f:write(string.char(i)) | ||
| 90 | end | ||
| 91 | -- try all characters and different line sizes | ||
| 92 | for i = 0, 255 do | ||
| 93 | for j = 0, i do | ||
| 94 | f:write(string.char(i)) | ||
| 95 | end | ||
| 96 | f:write("\r\n") | ||
| 97 | end | ||
| 98 | -- test latin text | ||
| 99 | f:write(mao) | ||
| 100 | -- force soft line breaks and treatment of space/tab in end of line | ||
| 101 | local tab | ||
| 102 | f:write(string.gsub(mao, "(%s)", function(c) | ||
| 103 | if tab then | ||
| 104 | tab = nil | ||
| 105 | return "\t" | ||
| 106 | else | ||
| 107 | tab = 1 | ||
| 108 | return " " | ||
| 109 | end | ||
| 110 | end)) | ||
| 111 | -- test crazy end of line conventions | ||
| 112 | local eol = { "\r\n", "\r", "\n", "\n\r" } | ||
| 113 | local which = 0 | ||
| 114 | f:write(string.gsub(mao, "(\n)", function(c) | ||
| 115 | which = which + 1 | ||
| 116 | if which > 4 then which = 1 end | ||
| 117 | return eol[which] | ||
| 118 | end)) | ||
| 119 | for i = 1, 4 do | ||
| 120 | for j = 1, 4 do | ||
| 121 | f:write(eol[i]) | ||
| 122 | f:write(eol[j]) | ||
| 123 | end | ||
| 124 | end | ||
| 125 | -- try long spaced and tabbed lines | ||
| 126 | f:write("\r\n") | ||
| 127 | for i = 0, 255 do | ||
| 128 | f:write(string.char(9)) | ||
| 129 | end | ||
| 130 | f:write("\r\n") | ||
| 131 | for i = 0, 255 do | ||
| 132 | f:write(' ') | ||
| 133 | end | ||
| 134 | f:write("\r\n") | ||
| 135 | for i = 0, 255 do | ||
| 136 | f:write(string.char(9),' ') | ||
| 137 | end | ||
| 138 | f:write("\r\n") | ||
| 139 | for i = 0, 255 do | ||
| 140 | f:write(' ',string.char(32)) | ||
| 141 | end | ||
| 142 | f:write("\r\n") | ||
| 143 | |||
| 144 | f:close() | ||
| 145 | end | ||
| 146 | |||
| 147 | local function cleanup_qptest() | ||
| 148 | os.remove(qptest) | ||
| 149 | os.remove(eqptest) | ||
| 150 | os.remove(dqptest) | ||
| 151 | end | ||
| 152 | |||
| 153 | local function encode_b64test() | ||
| 154 | local e1 = socket.mime.base64.encode() | ||
| 155 | local e2 = socket.mime.base64.encode() | ||
| 156 | local e3 = socket.mime.base64.encode() | ||
| 157 | local e4 = socket.mime.base64.encode() | ||
| 158 | local sp4 = socket.mime.split() | ||
| 159 | local sp3 = socket.mime.split(59) | ||
| 160 | local sp2 = socket.mime.split(30) | ||
| 161 | local sp1 = socket.mime.split(27) | ||
| 162 | local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4) | ||
| 163 | transform(b64test, eb64test, chain) | ||
| 164 | end | ||
| 165 | |||
| 166 | local function decode_b64test() | ||
| 167 | local d1 = socket.mime.base64.decode() | ||
| 168 | local d2 = socket.mime.base64.decode() | ||
| 169 | local d3 = socket.mime.base64.decode() | ||
| 170 | local d4 = socket.mime.base64.decode() | ||
| 171 | local chain = socket.mime.chain(d1, d2, d3, d4) | ||
| 172 | transform(eb64test, db64test, chain) | ||
| 173 | end | ||
| 174 | |||
| 175 | local function cleanup_b64test() | ||
| 176 | os.remove(eb64test) | ||
| 177 | os.remove(db64test) | ||
| 178 | end | ||
| 179 | |||
| 180 | local function compare_b64test() | ||
| 181 | compare(b64test, db64test) | ||
| 182 | end | ||
| 183 | |||
| 184 | local function padcheck(original, encoded) | ||
| 185 | local e = (socket.mime.b64(original)) | ||
| 186 | local d = (socket.mime.unb64(encoded)) | ||
| 187 | if e ~= encoded then fail("encoding failed") end | ||
| 188 | if d ~= original then fail("decoding failed") end | ||
| 189 | end | ||
| 190 | |||
| 191 | local function chunkcheck(original, encoded) | ||
| 192 | local len = string.len(original) | ||
| 193 | for i = 0, len do | ||
| 194 | local a = string.sub(original, 1, i) | ||
| 195 | local b = string.sub(original, i+1) | ||
| 196 | local e, r = socket.mime.b64(a, b) | ||
| 197 | local f = (socket.mime.b64(r)) | ||
| 198 | if (e .. f ~= encoded) then fail(e .. f) end | ||
| 199 | end | ||
| 200 | end | ||
| 201 | |||
| 202 | local function padding_b64test() | ||
| 203 | padcheck("a", "YQ==") | ||
| 204 | padcheck("ab", "YWI=") | ||
| 205 | padcheck("abc", "YWJj") | ||
| 206 | padcheck("abcd", "YWJjZA==") | ||
| 207 | padcheck("abcde", "YWJjZGU=") | ||
| 208 | padcheck("abcdef", "YWJjZGVm") | ||
| 209 | padcheck("abcdefg", "YWJjZGVmZw==") | ||
| 210 | padcheck("abcdefgh", "YWJjZGVmZ2g=") | ||
| 211 | padcheck("abcdefghi", "YWJjZGVmZ2hp") | ||
| 212 | padcheck("abcdefghij", "YWJjZGVmZ2hpag==") | ||
| 213 | chunkcheck("abcdefgh", "YWJjZGVmZ2g=") | ||
| 214 | chunkcheck("abcdefghi", "YWJjZGVmZ2hp") | ||
| 215 | chunkcheck("abcdefghij", "YWJjZGVmZ2hpag==") | ||
| 216 | print("ok") | ||
| 217 | end | ||
| 218 | |||
| 219 | local t = socket.time() | ||
| 220 | |||
| 221 | create_qptest() | ||
| 222 | encode_qptest() | ||
| 223 | decode_qptest() | ||
| 224 | compare_qptest() | ||
| 225 | encode_qptest("binary") | ||
| 226 | decode_qptest() | ||
| 227 | compare_qptest() | ||
| 228 | cleanup_qptest() | ||
| 229 | |||
| 230 | encode_b64test() | ||
| 231 | decode_b64test() | ||
| 232 | compare_b64test() | ||
| 233 | cleanup_b64test() | ||
| 234 | padding_b64test() | ||
| 235 | |||
| 236 | print(string.format("done in %.2fs", socket.time() - t)) | ||
