Esempio n. 1
0
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        f = tutils.tflow(resp=True)

        fm.handle_clientconnect(f.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        fm.handle_serverconnect(f.server_conn)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        fm.handle_request(f)
        assert fm.scripts[0].ns["log"][-1] == "request"
        fm.handle_response(f)
        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
        fm.handle_clientdisconnect(f.server_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"

        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))

        f.error = tutils.terr()
        fm.handle_error(f)
        assert fm.scripts[0].ns["log"][-1] == "error"
Esempio n. 2
0
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        sc = ServerConnection((req.get_host(), req.get_port()), None)
        sc.reply = controller.DummyReply()
        fm.handle_serverconnection(sc)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        f = fm.handle_request(req)
        assert fm.scripts[0].ns["log"][-1] == "request"
        resp = tutils.tresp(req)
        fm.handle_response(resp)
        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
        fm.handle_clientdisconnect(sc)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"

        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0

        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        err = tutils.terr()
        err.reply = controller.DummyReply()
        fm.handle_error(err)
        assert fm.scripts[0].ns["log"][-1] == "error"
Esempio n. 3
0
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        f = tutils.tflow(resp=True)

        fm.handle_clientconnect(f.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        fm.handle_serverconnect(f.server_conn)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        fm.handle_request(f)
        assert fm.scripts[0].ns["log"][-1] == "request"
        fm.handle_response(f)
        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
        fm.handle_clientdisconnect(f.server_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"

        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))

        f.error = tutils.terr()
        fm.handle_error(f)
        assert fm.scripts[0].ns["log"][-1] == "error"
Esempio n. 4
0
    def test_script(self):
        s = flow.State()
        fm = flow.FlowMaster(None, s)
        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        req = tutils.treq()
        fm.handle_clientconnect(req.flow.client_conn)
        assert fm.scripts[0].ns["log"][-1] == "clientconnect"
        sc = ServerConnection((req.get_host(), req.get_port()), None)
        sc.reply = controller.DummyReply()
        fm.handle_serverconnection(sc)
        assert fm.scripts[0].ns["log"][-1] == "serverconnect"
        f = fm.handle_request(req)
        assert fm.scripts[0].ns["log"][-1] == "request"
        resp = tutils.tresp(req)
        fm.handle_response(resp)
        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
        fm.handle_clientdisconnect(sc)
        assert fm.scripts[0].ns["log"][-1] == "clientdisconnect"
        assert fm.scripts[1].ns["log"][-1] == "clientdisconnect"


        #unload first script
        fm.unload_scripts()
        assert len(fm.scripts) == 0

        assert not fm.load_script(tutils.test_data.path("scripts/all.py"))
        err = tutils.terr()
        err.reply = controller.DummyReply()
        fm.handle_error(err)
        assert fm.scripts[0].ns["log"][-1] == "error"
Esempio n. 5
0
    def test_err(self):
        c = flow.State()
        f = tutils.tflow()
        c.add_request(f)
        f.error = Error("message")
        assert c.add_error(f)

        c = flow.State()
        f = tutils.tflow()
        c.add_request(f)
        c.set_limit("~e")
        assert not c.view
        f.error = tutils.terr()
        assert c.add_error(f)
        assert c.view
Esempio n. 6
0
    def test_err(self):
        c = flow.State()
        f = tutils.tflow()
        c.add_request(f)
        f.error = Error("message")
        assert c.add_error(f)

        c = flow.State()
        f = tutils.tflow()
        c.add_request(f)
        c.set_limit("~e")
        assert not c.view
        f.error = tutils.terr()
        assert c.add_error(f)
        assert c.view
Esempio n. 7
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
Esempio n. 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
Esempio n. 9
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:
            s.run("clientconnect", f)
            s.run("serverconnect", f)
            s.run("response", f)
            s.run("error", f)
            s.run("clientdisconnect", f)
            time.sleep(0.1)
            assert m.call_count == 5
Esempio n. 10
0
    def test_err(self):
        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)
        f.error = Error("message")
        assert c.add_error(f.error)

        e = Error("message")
        assert not c.add_error(e)

        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)
        e = tutils.terr()
        c.set_limit("~e")
        assert not c.view
        assert c.add_error(e)
        assert c.view
Esempio n. 11
0
    def test_err(self):
        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)
        f.error = Error("message")
        assert c.add_error(f.error)

        e = Error("message")
        assert not c.add_error(e)

        c = flow.State()
        req = tutils.treq()
        f = c.add_request(req)
        e = tutils.terr()
        c.set_limit("~e")
        assert not c.view
        assert c.add_error(e)
        assert c.view
Esempio n. 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