diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/scripts/a.py | 8 | ||||
-rw-r--r-- | test/scripts/starterr.py | 2 | ||||
-rw-r--r-- | test/test_cmdline.py | 13 | ||||
-rw-r--r-- | test/test_flow.py | 21 | ||||
-rw-r--r-- | test/test_script.py | 21 |
5 files changed, 49 insertions, 16 deletions
diff --git a/test/scripts/a.py b/test/scripts/a.py index 0a21b619..31cdaf0c 100644 --- a/test/scripts/a.py +++ b/test/scripts/a.py @@ -1,5 +1,13 @@ +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--var') var = 0 +def __init__(ctx, argv): + global var + var = parser.parse_args(argv).var + def here(ctx): global var var += 1 diff --git a/test/scripts/starterr.py b/test/scripts/starterr.py index 456fecc0..b217bdfe 100644 --- a/test/scripts/starterr.py +++ b/test/scripts/starterr.py @@ -1,3 +1,3 @@ -def start(ctx): +def start(ctx, argv): raise ValueError diff --git a/test/test_cmdline.py b/test/test_cmdline.py index e1c5c88d..92e2adbd 100644 --- a/test/test_cmdline.py +++ b/test/test_cmdline.py @@ -1,6 +1,7 @@ import argparse from libmproxy import cmdline import tutils +import os.path def test_parse_replace_hook(): @@ -39,6 +40,18 @@ def test_parse_setheaders(): x = cmdline.parse_setheader("/foo/bar/voing") assert x == ("foo", "bar", "voing") +def test_shlex(): + """ + shlex.split assumes posix=True by default, we do manual detection for windows. + Test whether script paths are parsed correctly + """ + absfilepath = os.path.normcase(os.path.abspath(__file__)) + + parser = argparse.ArgumentParser() + cmdline.common_options(parser) + opts = parser.parse_args(args=["-s",absfilepath]) + + assert os.path.isfile(opts.scripts[0][0]) def test_common(): parser = argparse.ArgumentParser() diff --git a/test/test_flow.py b/test/test_flow.py index 718f3551..085801af 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -544,9 +544,11 @@ class TestFlowMaster: fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) assert not fm.load_script(tutils.test_data.path("scripts/a.py")) - assert not fm.load_script(None) + assert not fm.unload_script(fm.scripts[0]) + assert not fm.unload_script(fm.scripts[0]) assert fm.load_script("nonexistent") assert "ValueError" in fm.load_script(tutils.test_data.path("scripts/starterr.py")) + assert len(fm.scripts) == 0 def test_replay(self): s = flow.State() @@ -572,20 +574,27 @@ class TestFlowMaster: assert not fm.load_script(tutils.test_data.path("scripts/all.py")) req = tutils.treq() fm.handle_clientconnect(req.client_conn) - assert fm.script.ns["log"][-1] == "clientconnect" + assert fm.scripts[0].ns["log"][-1] == "clientconnect" f = fm.handle_request(req) - assert fm.script.ns["log"][-1] == "request" + assert fm.scripts[0].ns["log"][-1] == "request" resp = tutils.tresp(req) fm.handle_response(resp) - assert fm.script.ns["log"][-1] == "response" + assert fm.scripts[0].ns["log"][-1] == "response" + #load second script + assert not fm.load_script(tutils.test_data.path("scripts/all.py")) + assert len(fm.scripts) == 2 dc = flow.ClientDisconnect(req.client_conn) dc.reply = controller.DummyReply() fm.handle_clientdisconnect(dc) - assert fm.script.ns["log"][-1] == "clientdisconnect" + assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" + assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" + #unload first script + fm.unload_script(fm.scripts[0]) + assert len(fm.scripts) == 1 err = flow.Error(f.request, "msg") err.reply = controller.DummyReply() fm.handle_error(err) - assert fm.script.ns["log"][-1] == "error" + assert fm.scripts[0].ns["log"][-1] == "error" def test_duplicate_flow(self): s = flow.State() diff --git a/test/test_script.py b/test/test_script.py index 407c9b4b..9033c4fc 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -1,5 +1,7 @@ from libmproxy import script, flow import tutils +import shlex +import os class TestScript: def test_simple(self): @@ -7,11 +9,12 @@ class TestScript: fm = flow.FlowMaster(None, s) ctx = flow.ScriptContext(fm) - p = script.Script(tutils.test_data.path("scripts/a.py"), ctx) + p = script.Script(shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40", posix=(os.name != "nt")), ctx) p.load() + assert "here" in p.ns - assert p.run("here") == (True, 1) - assert p.run("here") == (True, 2) + assert p.run("here") == (True, 41) + assert p.run("here") == (True, 42) ret = p.run("errargs") assert not ret[0] @@ -19,12 +22,12 @@ class TestScript: # Check reload p.load() - assert p.run("here") == (True, 1) + assert p.run("here") == (True, 41) def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) + fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")]) r = tutils.treq() fm.handle_request(r) assert fm.state.flow_count() == 2 @@ -37,25 +40,25 @@ class TestScript: ctx = flow.ScriptContext(fm) - s = script.Script("nonexistent", ctx) + s = script.Script(["nonexistent"], ctx) tutils.raises( "no such file", s.load ) - s = script.Script(tutils.test_data.path("scripts"), ctx) + s = script.Script([tutils.test_data.path("scripts")], ctx) tutils.raises( "not a file", s.load ) - s = script.Script(tutils.test_data.path("scripts/syntaxerr.py"), ctx) + s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx) tutils.raises( script.ScriptError, s.load ) - s = script.Script(tutils.test_data.path("scripts/loaderr.py"), ctx) + s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx) tutils.raises( script.ScriptError, s.load |