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
|
2013-12-15 01:43:16 +00:00
|
|
|
import time
|
2014-01-11 23:49:19 +00:00
|
|
|
import mock
|
2013-12-15 01:43:16 +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)
|
2014-01-12 10:01:59 +00:00
|
|
|
sp = tutils.test_data.path("scripts/a.py")
|
2015-05-30 00:03:28 +00:00
|
|
|
p = script.Script("%s --var 40" % sp, fm)
|
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)
|
2014-01-12 10:01:59 +00:00
|
|
|
fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py"))
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
|
|
|
fm.handle_request(f)
|
2012-02-24 23:19:54 +00:00
|
|
|
assert fm.state.flow_count() == 2
|
2014-02-05 13:33:17 +00:00
|
|
|
assert not fm.state.view[0].request.is_replay
|
|
|
|
assert fm.state.view[1].request.is_replay
|
2012-02-24 23:19:54 +00:00
|
|
|
|
2011-08-03 01:20:36 +00:00
|
|
|
def test_err(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2014-01-13 01:15:17 +00:00
|
|
|
"not found",
|
2014-01-12 10:01:59 +00:00
|
|
|
script.Script, "nonexistent", fm
|
2011-08-03 04:36:20 +00:00
|
|
|
)
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 04:36:20 +00:00
|
|
|
"not a file",
|
2014-01-12 10:01:59 +00:00
|
|
|
script.Script, tutils.test_data.path("scripts"), fm
|
2011-08-03 01:20:36 +00:00
|
|
|
)
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 01:20:36 +00:00
|
|
|
script.ScriptError,
|
2014-01-12 10:01:59 +00:00
|
|
|
script.Script, tutils.test_data.path("scripts/syntaxerr.py"), fm
|
2011-08-03 01:20:36 +00:00
|
|
|
)
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
tutils.raises(
|
2011-08-03 01:20:36 +00:00
|
|
|
script.ScriptError,
|
2014-01-12 10:01:59 +00:00
|
|
|
script.Script, tutils.test_data.path("scripts/loaderr.py"), fm
|
2011-08-03 01:20:36 +00:00
|
|
|
)
|
|
|
|
|
2013-12-15 01:43:16 +00:00
|
|
|
def test_concurrent(self):
|
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2014-01-12 10:01:59 +00:00
|
|
|
fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py"))
|
2013-12-15 01:43:16 +00:00
|
|
|
|
2014-01-11 23:49:19 +00:00
|
|
|
with mock.patch("libmproxy.controller.DummyReply.__call__") as m:
|
2014-09-03 14:57:56 +00:00
|
|
|
f1, f2 = tutils.tflow(), tutils.tflow()
|
2014-01-11 23:49:19 +00:00
|
|
|
t_start = time.time()
|
2014-09-03 14:57:56 +00:00
|
|
|
fm.handle_request(f1)
|
|
|
|
f1.reply()
|
|
|
|
fm.handle_request(f2)
|
|
|
|
f2.reply()
|
2014-01-11 23:49:19 +00:00
|
|
|
|
|
|
|
# Two instantiations
|
2014-08-17 23:47:39 +00:00
|
|
|
assert m.call_count == 0 # No calls yet.
|
2014-01-11 23:49:19 +00:00
|
|
|
assert (time.time() - t_start) < 0.09
|
2013-12-15 01:43:16 +00:00
|
|
|
|
|
|
|
def test_concurrent2(self):
|
2014-01-11 23:49:19 +00:00
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2015-05-30 00:03:28 +00:00
|
|
|
s = script.Script(
|
|
|
|
tutils.test_data.path("scripts/concurrent_decorator.py"),
|
|
|
|
fm)
|
2013-12-15 01:43:16 +00:00
|
|
|
s.load()
|
2014-08-17 23:47:39 +00:00
|
|
|
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()
|
|
|
|
assert s.run(hook, d)[0]
|
|
|
|
d.reply()
|
2014-09-04 22:18:17 +00:00
|
|
|
while (time.time() - t_start) < 20 and m.call_count <= 5:
|
2014-08-17 23:47:39 +00:00
|
|
|
if m.call_count == 5:
|
|
|
|
return
|
|
|
|
time.sleep(0.001)
|
|
|
|
assert False
|
2013-12-15 01:51:35 +00:00
|
|
|
|
|
|
|
def test_concurrent_err(self):
|
2014-01-11 23:49:19 +00:00
|
|
|
s = flow.State()
|
|
|
|
fm = flow.FlowMaster(None, s)
|
2013-12-15 01:51:35 +00:00
|
|
|
tutils.raises(
|
|
|
|
"decorator not supported for this method",
|
2015-05-30 00:03:28 +00:00
|
|
|
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"))
|
|
|
|
s = script.Script(absfilepath, fm)
|
|
|
|
assert os.path.isfile(s.argv[0])
|