Example #1
0
    def test_err(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        ctx = flow.ScriptContext(fm)

        s = script.Script(["nonexistent"], ctx)
        tutils.raises("no such file", s.load)

        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)
        tutils.raises(script.ScriptError, s.load)

        s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx)
        tutils.raises(script.ScriptError, s.load)
Example #2
0
    def test_err(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        ctx = flow.ScriptContext(fm)

        s = script.Script("nonexistent", ctx)
        libpry.raises("no such file", s.load)

        s = script.Script("scripts", ctx)
        libpry.raises("not a file", s.load)

        s = script.Script("scripts/syntaxerr.py", ctx)
        libpry.raises(script.ScriptError, s.load)

        s = script.Script("scripts/loaderr.py", ctx)
        libpry.raises(script.ScriptError, s.load)
Example #3
0
    def test_concurrent2(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        s = script.Script(
            tutils.test_data.path("scripts/concurrent_decorator.py"), fm)
        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()
            assert s.run(hook, d)[0]
            d.reply()
        while (time.time() - t_start) < 5 and m.call_count <= 5:
            if m.call_count == 5:
                return
            time.sleep(0.001)
        assert False
Example #4
0
def test_load_scripts():
    example_dir = utils.Data("libmproxy").path("../examples")
    scripts = glob.glob("%s/*.py" % example_dir)

    tmaster = tservers.TestMaster(config.ProxyConfig())

    for f in scripts:
        if "har_extractor" in f:
            continue
        if "flowwriter" in f:
            f += " -"
        if "iframe_injector" in f:
            f += " foo"  # one argument required
        if "filt" in f:
            f += " ~a"
        if "modify_response_body" in f:
            f += " foo bar"  # two arguments required
        try:
            s = script.Script(
                f, script.ScriptContext(tmaster))  # Loads the script file.
        except Exception as v:
            if "ImportError" not in str(v):
                raise
        else:
            s.unload()
Example #5
0
def test_err():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    sc = script.ScriptContext(fm)

    tutils.raises(
        "not found",
        script.Script, "nonexistent", sc
    )

    tutils.raises(
        "not a file",
        script.Script, tutils.test_data.path("scripts"), sc
    )

    tutils.raises(
        script.ScriptException,
        script.Script, tutils.test_data.path("scripts/syntaxerr.py"), sc
    )

    tutils.raises(
        script.ScriptException,
        script.Script, tutils.test_data.path("scripts/loaderr.py"), sc
    )

    scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), sc)
    tutils.raises(script.ScriptException, scr.unload)
Example #6
0
def test_load_scripts():

    example_dir = utils.Data("libmproxy").path("../examples")
    scripts = glob.glob("%s/*.py" % example_dir)

    tmaster = tservers.TestMaster(config.ProxyConfig())

    for f in scripts:
        if "modify_response_body" in f:
            f += " foo bar"  # two arguments required
        script.Script(f, tmaster)  # Loads the script file.
Example #7
0
def test_simple():
    s = flow.State()
    fm = flow.FlowMaster(None, s)
    sp = tutils.test_data.path("scripts/a.py")
    p = script.Script("%s --var 40" % sp, script.ScriptContext(fm))

    assert "here" in p.ns
    assert p.run("here") == 41
    assert p.run("here") == 42

    tutils.raises(script.ScriptException, p.run, "errargs")

    # Check reload
    p.load()
    assert p.run("here") == 41
Example #8
0
    def test_concurrent2(self):
        ctx = TScriptContext()
        s = script.Script(
            [tutils.test_data.path("scripts/concurrent_decorator.py")], ctx)
        s.load()
        f = tutils.tflow_full()
        f.error = tutils.terr(f.request)
        f.reply = f.request.reply

        print s.run("response", f)
        print s.run("error", f)
        print s.run("clientconnect", f)
        print s.run("clientdisconnect", f)
        print s.run("serverconnect", f)
        time.sleep(0.1)
        assert ctx.count == 5
Example #9
0
    def test_simple(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        sp = tutils.test_data.path("scripts/a.py")
        p = script.Script("%s --var 40" % sp, fm)

        assert "here" in p.ns
        assert p.run("here") == (True, 41)
        assert p.run("here") == (True, 42)

        ret = p.run("errargs")
        assert not ret[0]
        assert len(ret[1]) == 2

        # Check reload
        p.load()
        assert p.run("here") == (True, 41)
Example #10
0
    def test_simple(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        ctx = flow.ScriptContext(fm)

        p = script.Script(os.path.join("scripts", "a.py"), ctx)
        p.load()
        assert "here" in p.ns
        assert p.run("here") == (True, 1)
        assert p.run("here") == (True, 2)

        ret = p.run("errargs")
        assert not ret[0]
        assert len(ret[1]) == 2

        # Check reload
        p.load()
        assert p.run("here") == (True, 1)
Example #11
0
    def test_simple(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        ctx = flow.ScriptContext(fm)

        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, 41)
        assert p.run("here") == (True, 42)

        ret = p.run("errargs")
        assert not ret[0]
        assert len(ret[1]) == 2

        # Check reload
        p.load()
        assert p.run("here") == (True, 41)
Example #12
0
    def test_concurrent2(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        s = script.Script(
            tutils.test_data.path("scripts/concurrent_decorator.py"), fm)
        s.load()
        f = tutils.tflow_full()
        f.error = tutils.terr(f.request)
        f.reply = f.request.reply

        with mock.patch("libmproxy.controller.DummyReply.__call__") as m:
            t_start = time.time()
            s.run("clientconnect", f)
            s.run("serverconnect", f)
            s.run("response", f)
            s.run("error", f)
            s.run("clientdisconnect", f)
            while (time.time() - t_start) < 1 and m.call_count <= 5:
                if m.call_count == 5:
                    return
                time.sleep(0.001)
            assert False
Example #13
0
 def test_concurrent_err(self):
     s = script.Script(
         [tutils.test_data.path("scripts/concurrent_decorator_err.py")],
         TScriptContext())
     tutils.raises("decorator not supported for this method", s.load)
Example #14
0
from libmproxy import utils, script
import glob
from libmproxy.proxy import config
import tservers

example_dir = utils.Data("libmproxy").path("../examples")
scripts = glob.glob("%s/*.py" % example_dir)

tmaster = tservers.TestMaster(config.ProxyConfig())

for f in scripts:
    script.Script(f, tmaster)  # Loads the script file.
Example #15
0
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])