diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-10-24 11:32:53 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-10-24 11:32:53 +1300 |
commit | c684f7417d75660048351470990818505bfb1d53 (patch) | |
tree | 1c09074e250122a7ed133e11f3cd254161160c29 /test/test_language.py | |
parent | e83392bfc8e44323c326e0a677210b9c1e6a3268 (diff) | |
download | mitmproxy-c684f7417d75660048351470990818505bfb1d53.tar.gz mitmproxy-c684f7417d75660048351470990818505bfb1d53.tar.bz2 mitmproxy-c684f7417d75660048351470990818505bfb1d53.zip |
Extend Action and Value classes
- Values now know how to print their own specs
- Actions now know how to print their own specs
- Actions have a resolve_offset method that resolves relative and random offsets.
Diffstat (limited to 'test/test_language.py')
-rw-r--r-- | test/test_language.py | 180 |
1 files changed, 121 insertions, 59 deletions
diff --git a/test/test_language.py b/test/test_language.py index 7680492f..e1697f95 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -5,42 +5,21 @@ import tutils language.TESTING = True -class TestMisc: - def test_generators(self): - v = language.Value.parseString("'val'")[0] - g = v.get_generator({}) - assert g[:] == "val" +class TestValueNakedLiteral: + def test_expr(self): + v = language.ValueNakedLiteral("foo") + assert v.expr() - def test_randomgenerator(self): - g = language.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_spec(self): + v = language.ValueNakedLiteral("foo") + assert v.spec() == repr(v) == "foo" - def test_literalgenerator(self): - g = language.LiteralGenerator("one") - assert repr(g) - assert g == "one" - assert g[:] == "one" - assert g[1] == "n" + v = language.ValueNakedLiteral("f\x00oo") + assert v.spec() == repr(v) == r"f\x00oo" - 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 = language.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): +class TestValueLiteral: + def test_espr(self): v = language.ValueLiteral("foo") assert v.expr() assert v.val == "foo" @@ -50,11 +29,51 @@ class TestMisc: assert v.val == "foo\n" assert repr(v) - def test_valuenakedliteral(self): - v = language.ValueNakedLiteral("foo") - assert v.expr() - assert repr(v) + def test_spec(self): + v = language.ValueLiteral("foo") + assert v.spec() == r'"foo"' + + v = language.ValueLiteral("f\x00oo") + assert v.spec() == repr(v) == r'"f\x00oo"' + + +class TestValueGenerate: + def test_basic(self): + v = language.Value.parseString("@10b")[0] + assert v.usize == 10 + assert v.unit == "b" + assert v.bytes() == 10 + v = language.Value.parseString("@10")[0] + assert v.unit == "b" + v = language.Value.parseString("@10k")[0] + assert v.bytes() == 10240 + v = language.Value.parseString("@10g")[0] + assert v.bytes() == 1024**3 * 10 + v = language.Value.parseString("@10g,digits")[0] + assert v.datatype == "digits" + g = v.get_generator({}) + assert g[:100] + + v = language.Value.parseString("@10,digits")[0] + assert v.unit == "b" + assert v.datatype == "digits" + + def test_spec(self): + v = language.ValueGenerate(1, "b", "bytes") + assert v.spec() == repr(v) == "@1" + + v = language.ValueGenerate(1, "k", "bytes") + assert v.spec() == repr(v) == "@1k" + + v = language.ValueGenerate(1, "k", "ascii") + assert v.spec() == repr(v) == "@1k,ascii" + + v = language.ValueGenerate(1, "b", "ascii") + assert v.spec() == repr(v) == "@1,ascii" + + +class TestValueFile: def test_file_value(self): v = language.Value.parseString("<'one two'")[0] assert str(v) @@ -63,6 +82,8 @@ class TestMisc: v = language.Value.parseString("<path")[0] assert v.path == "path" + def test_access_control(self): + v = language.Value.parseString("<path")[0] with tutils.tmpdir() as t: p = os.path.join(t, "path") f = open(p, "w") @@ -78,26 +99,46 @@ class TestMisc: v = language.Value.parseString("</outside")[0] tutils.raises("outside", v.get_generator, dict(staticdir=t)) - def test_generated_value(self): - v = language.Value.parseString("@10b")[0] - assert v.usize == 10 - assert v.unit == "b" - assert v.bytes() == 10 - v = language.Value.parseString("@10")[0] - assert v.unit == "b" - v = language.Value.parseString("@10k")[0] - assert v.bytes() == 10240 - v = language.Value.parseString("@10g")[0] - assert v.bytes() == 1024**3 * 10 + def test_spec(self): + v = language.Value.parseString("<'one two'")[0] + v2 = language.Value.parseString(v.spec())[0] + assert v2.path == "one two" - v = language.Value.parseString("@10g,digits")[0] - assert v.datatype == "digits" + +class TestMisc: + def test_generators(self): + v = language.Value.parseString("'val'")[0] g = v.get_generator({}) - assert g[:100] + assert g[:] == "val" - v = language.Value.parseString("@10,digits")[0] - assert v.unit == "b" - assert v.datatype == "digits" + def test_randomgenerator(self): + g = language.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 = language.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 = language.FileGenerator(path) + assert len(g) == 10000 + assert g[0] == "x" + assert g[-1] == "x" + assert g[0:5] == "xxxxx" + assert repr(g) def test_value(self): assert language.Value.parseString("'val'")[0].val == "val" @@ -126,7 +167,7 @@ class TestMisc: assert v.value.val == "foo" v = e.parseString("b@100")[0] - assert str(v.value) == "@100b,bytes" + assert str(v.value) == "@100" v = e.parseString("b@100g,digits", parseAll=True)[0] assert v.value.datatype == "digits" @@ -179,6 +220,12 @@ class Test_Action: l.sort() assert l[0].offset == 0 + def test_resolve_offset(self): + r = language.parse_request({}, 'GET:"/foo"') + e = language.DisconnectAt("r") + ret = e.resolve_offset(r) + assert isinstance(ret.offset, int) + class TestDisconnects: def test_parse_response(self): @@ -198,6 +245,10 @@ class TestDisconnects: v = e.parseString("dr")[0] assert v.offset == "r" + def test_spec(self): + assert language.DisconnectAt("r").spec() == "dr" + assert language.DisconnectAt(10).spec() == "d10" + class TestInject: def test_parse_response(self): @@ -224,11 +275,11 @@ class TestInject: r = language.parse_response({}, "400:i0,'foo'") assert r.serve(s, None) - -class TestShortcuts: - def test_parse_response(self): - assert language.parse_response({}, "400:c'foo'").headers[0][0][:] == "Content-Type" - assert language.parse_response({}, "400:l'foo'").headers[0][0][:] == "Location" + def test_spec(self): + e = language.InjectAt.expr() + v = e.parseString("i0,'foo'")[0] + assert v.spec() == 'i0,"foo"' + class TestPauses: @@ -251,6 +302,17 @@ class TestPauses: r = language.parse_response({}, '400:p10,10') assert r.actions[0] == (10, "pause", 10) + def test_spec(self): + assert language.PauseAt("r", 5).spec() == "pr,5" + assert language.PauseAt(0, 5).spec() == "p0,5" + assert language.PauseAt(0, "f").spec() == "p0,f" + + +class TestShortcuts: + def test_parse_response(self): + assert language.parse_response({}, "400:c'foo'").headers[0][0][:] == "Content-Type" + assert language.parse_response({}, "400:l'foo'").headers[0][0][:] == "Location" + class TestParseRequest: def test_file(self): |