2011-08-03 01:20:36 +00:00
|
|
|
from libmproxy import script, flow
|
2012-02-24 23:19:54 +00:00
|
|
|
import tutils
|
2013-06-13 14:04:04 +00:00
|
|
|
import shlex
|
|
|
|
import os
|
2011-08-03 01:20:36 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestScript:
|
2011-08-03 01:20:36 +00:00
|
|
|
def test_simple(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-08-03 22:14:44 +00:00
|
|
|
ctx = flow.ScriptContext(fm)
|
2011-08-03 01:20:36 +00:00
|
|
|
|
2013-06-13 14:04:04 +00:00
|
|
|
p = script.Script(shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40", posix=(os.name != "nt")), ctx)
|
2011-08-03 01:20:36 +00:00
|
|
|
p.load()
|
2013-06-13 14:04:04 +00:00
|
|
|
|
2011-08-03 01:20:36 +00:00
|
|
|
assert "here" in p.ns
|
2013-06-13 14:04:04 +00:00
|
|
|
assert p.run("here") == (True, 41)
|
|
|
|
assert p.run("here") == (True, 42)
|
2012-02-24 23:19:54 +00:00
|
|
|
|
2011-08-03 01:20:36 +00:00
|
|
|
ret = p.run("errargs")
|
2012-02-24 23:19:54 +00:00
|
|
|
assert not ret[0]
|
2011-08-03 01:20:36 +00:00
|
|
|
assert len(ret[1]) == 2
|
|
|
|
|
|
|
|
# Check reload
|
|
|
|
p.load()
|
2013-06-13 14:04:04 +00:00
|
|
|
assert p.run("here") == (True, 41)
|
2011-08-03 01:20:36 +00:00
|
|
|
|
2012-02-24 23:19:54 +00:00
|
|
|
def test_duplicate_flow(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2013-06-13 14:04:04 +00:00
|
|
|
fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")])
|
2012-02-24 23:19:54 +00:00
|
|
|
r = tutils.treq()
|
|
|
|
fm.handle_request(r)
|
|
|
|
assert fm.state.flow_count() == 2
|
|
|
|
assert not fm.state.view[0].request.is_replay()
|
|
|
|
assert fm.state.view[1].request.is_replay()
|
|
|
|
|
2011-08-03 01:20:36 +00:00
|
|
|
def test_err(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2011-08-03 22:14:44 +00:00
|
|
|
ctx = flow.ScriptContext(fm)
|
2011-08-03 01:20:36 +00:00
|
|
|
|
2011-08-03 22:14:44 +00:00
|
|
|
|
2013-06-13 14:04:04 +00:00
|
|
|
s = script.Script(["nonexistent"], ctx)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 04:36:20 +00:00
|
|
|
"no such file",
|
|
|
|
s.load
|
|
|
|
)
|
|
|
|
|
2013-06-13 14:04:04 +00:00
|
|
|
s = script.Script([tutils.test_data.path("scripts")], ctx)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 04:36:20 +00:00
|
|
|
"not a file",
|
2011-08-03 01:20:36 +00:00
|
|
|
s.load
|
|
|
|
)
|
|
|
|
|
2013-06-13 14:04:04 +00:00
|
|
|
s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 01:20:36 +00:00
|
|
|
script.ScriptError,
|
|
|
|
s.load
|
|
|
|
)
|
|
|
|
|
2013-06-13 14:04:04 +00:00
|
|
|
s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx)
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 01:20:36 +00:00
|
|
|
script.ScriptError,
|
|
|
|
s.load
|
|
|
|
)
|
|
|
|
|