import os, cStringIO from libpathod import rparse, utils import tutils rparse.TESTING = True class TestMisc: def test_generators(self): v = rparse.Value.parseString("'val'")[0] g = v.get_generator({}) assert g[:] == "val" def test_randomgenerator(self): g = rparse.RandomGenerator("bytes", 100) assert repr(g) assert len(g[:10]) == 10 assert len(g[1:10]) == 9 assert len(g[:1000]) == 100 assert len(g[1000:1001]) == 0 assert g[0] def test_literalgenerator(self): g = rparse.LiteralGenerator("one") assert repr(g) assert g == "one" assert g[:] == "one" assert g[1] == "n" def test_filegenerator(self): with tutils.tmpdir() as t: path = os.path.join(t, "foo") f = open(path, "w") f.write("x"*10000) f.close() g = rparse.FileGenerator(path) assert len(g) == 10000 assert g[0] == "x" assert g[-1] == "x" assert g[0:5] == "xxxxx" assert repr(g) def test_valueliteral(self): v = rparse.ValueLiteral("foo") assert v.expr() assert v.val == "foo" v = rparse.ValueLiteral(r"foo\n") assert v.expr() assert v.val == "foo\n" assert repr(v) def test_valuenakedliteral(self): v = rparse.ValueNakedLiteral("foo") assert v.expr() assert repr(v) def test_file_value(self): v = rparse.Value.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" v = rparse.Value.parseString(" 100 x = [(1, 5), (0, 5)] assert rparse.ready_actions(100, x) == sorted(x) class TestResponse: def dummy_response(self): return rparse.parse_response({}, "400'msg'") def test_file(self): p = tutils.test_data.path("data") d = dict(staticdir=p) r = rparse.parse_response(d, "+response") assert r.code == 202 def test_response(self): r = rparse.parse_response({}, "400'msg'") assert r.code == 400 assert r.msg == "msg" r = rparse.parse_response({}, "400'msg':b@100b") assert r.msg == "msg" assert r.body[:] assert str(r) def test_checkfunc(self): s = cStringIO.StringIO() r = rparse.parse_response({}, "400:b@100k") def check(req, acts): return "errmsg" assert r.serve(s, check=check)["error"] == "errmsg" def test_render(self): s = cStringIO.StringIO() r = rparse.parse_response({}, "400'msg'") assert r.serve(s, None) def test_raw(self): s = cStringIO.StringIO() r = rparse.parse_response({}, "400:b'foo'") r.serve(s, None) v = s.getvalue() assert "Content-Length" in v assert "Date" in v s = cStringIO.StringIO() r = rparse.parse_response({}, "400:b'foo':r") r.serve(s, None) v = s.getvalue() assert not "Content-Length" in v assert not "Date" in v def test_length(self): def testlen(x): s = cStringIO.StringIO() x.serve(s, None) assert x.length() == len(s.getvalue()) testlen(rparse.parse_response({}, "400'msg'")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar'")) testlen(rparse.parse_response({}, "400'msg':h'foo'='bar':b@100b")) def test_effective_length(self): def testlen(x, actions): s = cStringIO.StringIO() x.serve(s, None) assert x.effective_length(actions) == len(s.getvalue()) actions = [ ] r = rparse.parse_response({}, "400'msg':b@100") actions = [ (0, "disconnect"), ] r.actions = actions testlen(r, actions) actions = [ (0, "disconnect"), (0, "inject", "foo") ] r.actions = actions testlen(r, actions) actions = [ (0, "inject", "foo") ] r.actions = actions testlen(r, actions) def test_render(self): r = rparse.parse_response({}, "400:p0,100:dr") assert r.actions[0][1] == "pause" r.preview_safe() assert not r.actions[0][1] == "pause" def test_read_file(): tutils.raises(rparse.FileAccessDenied, rparse.read_file, {}, "=/foo") p = tutils.test_data.path("data") d = dict(staticdir=p) assert rparse.read_file(d, "+./file").strip() == "testfile" assert rparse.read_file(d, "+file").strip() == "testfile" tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+./nonexistent") tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+/nonexistent") tutils.raises(rparse.FileAccessDenied, rparse.read_file, d, "+../test_rparse.py") d["unconstrained_file_access"] = True assert rparse.read_file(d, "+../test_rparse.py")