2013-06-13 14:04:04 +00:00
|
|
|
import os
|
2013-12-15 01:43:16 +00:00
|
|
|
import time
|
2014-01-11 23:49:19 +00:00
|
|
|
import mock
|
2015-07-24 00:57:56 +00:00
|
|
|
from libmproxy import script, flow
|
|
|
|
import tutils
|
|
|
|
|
|
|
|
|
|
|
|
def test_simple():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
sp = tutils.test_data.path("scripts/a.py")
|
2015-11-14 03:21:38 +00:00
|
|
|
p = script.Script("%s --var 40" % sp, script.ScriptContext(fm))
|
2015-07-24 00:57:56 +00:00
|
|
|
|
|
|
|
assert "here" in p.ns
|
|
|
|
assert p.run("here") == 41
|
|
|
|
assert p.run("here") == 42
|
|
|
|
|
2015-11-14 03:21:38 +00:00
|
|
|
tutils.raises(script.ScriptException, p.run, "errargs")
|
2015-07-24 00:57:56 +00:00
|
|
|
|
|
|
|
# Check reload
|
|
|
|
p.load()
|
|
|
|
assert p.run("here") == 41
|
|
|
|
|
|
|
|
|
|
|
|
def test_duplicate_flow():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py"))
|
|
|
|
f = tutils.tflow()
|
|
|
|
fm.handle_request(f)
|
|
|
|
assert fm.state.flow_count() == 2
|
|
|
|
assert not fm.state.view[0].request.is_replay
|
|
|
|
assert fm.state.view[1].request.is_replay
|
|
|
|
|
|
|
|
|
|
|
|
def test_err():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2015-11-14 03:21:38 +00:00
|
|
|
sc = script.ScriptContext(fm)
|
2015-07-24 00:57:56 +00:00
|
|
|
|
|
|
|
tutils.raises(
|
|
|
|
"not found",
|
2015-11-14 03:21:38 +00:00
|
|
|
script.Script, "nonexistent", sc
|
2015-07-24 00:57:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
tutils.raises(
|
|
|
|
"not a file",
|
2015-11-14 03:21:38 +00:00
|
|
|
script.Script, tutils.test_data.path("scripts"), sc
|
2015-07-24 00:57:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
tutils.raises(
|
2015-11-14 03:21:38 +00:00
|
|
|
script.ScriptException,
|
|
|
|
script.Script, tutils.test_data.path("scripts/syntaxerr.py"), sc
|
2015-07-24 00:57:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
tutils.raises(
|
2015-11-14 03:21:38 +00:00
|
|
|
script.ScriptException,
|
|
|
|
script.Script, tutils.test_data.path("scripts/loaderr.py"), sc
|
2015-07-24 00:57:56 +00:00
|
|
|
)
|
|
|
|
|
2015-11-14 03:21:38 +00:00
|
|
|
scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), sc)
|
|
|
|
tutils.raises(script.ScriptException, scr.unload)
|
2013-12-15 01:43:16 +00:00
|
|
|
|
|
|
|
|
2015-11-29 02:13:08 +00:00
|
|
|
@tutils.skip_appveyor
|
2015-07-24 00:57:56 +00:00
|
|
|
def test_concurrent():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py"))
|
2014-08-17 23:47:39 +00:00
|
|
|
|
2015-07-24 00:57:56 +00:00
|
|
|
with mock.patch("libmproxy.controller.DummyReply.__call__") as m:
|
|
|
|
f1, f2 = tutils.tflow(), tutils.tflow()
|
2014-08-17 23:47:39 +00:00
|
|
|
t_start = time.time()
|
2015-07-24 00:57:56 +00:00
|
|
|
fm.handle_request(f1)
|
|
|
|
f1.reply()
|
|
|
|
fm.handle_request(f2)
|
|
|
|
f2.reply()
|
|
|
|
|
|
|
|
# Two instantiations
|
|
|
|
assert m.call_count == 0 # No calls yet.
|
2015-11-29 02:13:08 +00:00
|
|
|
assert (time.time() - t_start) < 0.1
|
2015-07-24 00:57:56 +00:00
|
|
|
|
2014-08-17 23:47:39 +00:00
|
|
|
|
2015-07-24 00:57:56 +00:00
|
|
|
def test_concurrent2():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
s = script.Script(
|
|
|
|
tutils.test_data.path("scripts/concurrent_decorator.py"),
|
2015-11-14 03:21:38 +00:00
|
|
|
script.ScriptContext(fm))
|
2015-07-24 00:57:56 +00:00
|
|
|
s.load()
|
|
|
|
m = mock.Mock()
|
|
|
|
|
|
|
|
class Dummy:
|
|
|
|
def __init__(self):
|
|
|
|
self.response = self
|
|
|
|
self.error = self
|
|
|
|
self.reply = m
|
|
|
|
|
|
|
|
t_start = time.time()
|
|
|
|
|
|
|
|
for hook in ("clientconnect",
|
|
|
|
"serverconnect",
|
|
|
|
"response",
|
|
|
|
"error",
|
|
|
|
"clientconnect"):
|
|
|
|
d = Dummy()
|
|
|
|
s.run(hook, d)
|
|
|
|
d.reply()
|
|
|
|
while (time.time() - t_start) < 20 and m.call_count <= 5:
|
|
|
|
if m.call_count == 5:
|
|
|
|
return
|
|
|
|
time.sleep(0.001)
|
|
|
|
assert False
|
|
|
|
|
|
|
|
|
|
|
|
def test_concurrent_err():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
tutils.raises(
|
|
|
|
"Concurrent decorator not supported for 'start' method",
|
|
|
|
script.Script,
|
|
|
|
tutils.test_data.path("scripts/concurrent_decorator_err.py"),
|
|
|
|
fm)
|
2014-01-12 10:01:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_command_parsing():
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
absfilepath = os.path.normcase(tutils.test_data.path("scripts/a.py"))
|
2015-11-14 03:21:38 +00:00
|
|
|
s = script.Script(absfilepath, script.ScriptContext(fm))
|
2015-07-24 00:57:56 +00:00
|
|
|
assert os.path.isfile(s.args[0])
|
2015-11-09 20:07:58 +00:00
|
|
|
|