def test_flow(self): """ normal flow: connect -> request -> response """ bc = flow.ClientConnect(("address", 22)) c = flow.State() req = tutils.treq(bc) f = c.add_request(req) assert f assert c.flow_count() == 1 assert c._flow_map.get(req) assert c.active_flow_count() == 1 newreq = tutils.treq() assert c.add_request(newreq) assert c._flow_map.get(newreq) assert c.active_flow_count() == 2 resp = tutils.tresp(req) assert c.add_response(resp) assert c.flow_count() == 2 assert c._flow_map.get(resp.request) assert c.active_flow_count() == 1 unseen_resp = tutils.tresp() assert not c.add_response(unseen_resp) assert not c._flow_map.get(unseen_resp.request) assert c.active_flow_count() == 1 resp = tutils.tresp(newreq) assert c.add_response(resp) assert c.active_flow_count() == 0
def test_flow(self): """ normal flow: connect -> request -> response """ bc = tutils.tclient_conn() c = flow.State() req = tutils.treq(bc) f = c.add_request(req) assert f assert c.flow_count() == 1 assert c.active_flow_count() == 1 newreq = tutils.treq() assert c.add_request(newreq) assert c.active_flow_count() == 2 resp = tutils.tresp(req) assert c.add_response(resp) assert c.flow_count() == 2 assert c.active_flow_count() == 1 unseen_resp = tutils.tresp() unseen_resp.flow = None assert not c.add_response(unseen_resp) assert c.active_flow_count() == 1 resp = tutils.tresp(newreq) assert c.add_response(resp) assert c.active_flow_count() == 0
def test_set_limit(self): c = flow.State() req = tutils.treq() assert len(c.view) == 0 c.add_request(req) assert len(c.view) == 1 c.set_limit("~s") assert c.limit_txt == "~s" assert len(c.view) == 0 resp = tutils.tresp(req) c.add_response(resp) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 req = tutils.treq() c.add_request(req) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_set_limit(self): c = flow.State() req = tutils.treq() c.clientconnect(req.client_conn) assert len(c.view) == 0 f = c.add_request(req) assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 0 resp = tutils.tresp(req) c.add_response(resp) assert len(c.view) == 1 c.set_limit(None) assert len(c.view) == 1 req = tutils.treq() c.clientconnect(req.client_conn) c.add_request(req) assert len(c.view) == 2 c.set_limit("~q") assert len(c.view) == 1 c.set_limit("~s") assert len(c.view) == 1 assert "Invalid" in c.set_limit("~")
def test_decodeencode(self): r = tutils.treq() r.headers["content-encoding"] = ["identity"] r.content = "falafel" r.decode() assert not r.headers["content-encoding"] assert r.content == "falafel" r = tutils.treq() r.content = "falafel" assert not r.decode() r = tutils.treq() r.headers["content-encoding"] = ["identity"] r.content = "falafel" r.encode("identity") assert r.headers["content-encoding"] == ["identity"] assert r.content == "falafel" r = tutils.treq() r.headers["content-encoding"] = ["identity"] r.content = "falafel" r.encode("gzip") assert r.headers["content-encoding"] == ["gzip"] assert r.content != "falafel" r.decode() assert not r.headers["content-encoding"] assert r.content == "falafel"
def test_killall(self): s = flow.State() fm = flow.FlowMaster(None, s) r = tutils.treq() fm.handle_request(r) r = tutils.treq() fm.handle_request(r) for i in s.view: assert not i.request.acked s.killall(fm) for i in s.view: assert i.request.acked
def test_get_cert(self): req = tutils.treq() resp = flow.Response(req, 200, "msg", flow.ODictCaseless(), "content", file("data/dercert").read()) assert resp.get_cert() resp = tutils.tresp() assert not resp.get_cert()
def test_header_size(self): h = flow.ODictCaseless() h["headername"] = ["headervalue"] r = tutils.treq() r.headers = h raw = r._assemble_headers() assert len(raw) == 62
def test_concurrent(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py")) with mock.patch("libmproxy.controller.DummyReply.__call__") as m: r1, r2 = tutils.treq(), tutils.treq() t_start = time.time() fm.handle_request(r1) r1.reply() fm.handle_request(r2) r2.reply() # Two instantiations assert m.call_count == 2 assert (time.time() - t_start) < 0.09
def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py")) req = tutils.treq() fm.handle_clientconnect(req.client_conn) assert fm.handle_request(req)
def test_backup(self): c = flow.State() req = tutils.treq() f = c.add_request(req) f.backup() c.revert(f)
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True req = tutils.treq() fm.handle_clientconnect(req.flow.client_conn) f = fm.handle_request(req) assert s.flow_count() == 1 resp = tutils.tresp(req) fm.handle_response(resp) assert s.flow_count() == 1 rx = tutils.tresp() rx.flow = None assert not fm.handle_response(rx) fm.handle_clientdisconnect(req.flow.client_conn) f.error = Error("msg") f.error.reply = controller.DummyReply() fm.handle_error(f.error) fm.load_script(tutils.test_data.path("scripts/a.py")) fm.shutdown()
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True req = tutils.treq() fm.handle_clientconnect(req.client_conn) f = fm.handle_request(req) assert s.flow_count() == 1 resp = tutils.tresp(req) fm.handle_response(resp) assert s.flow_count() == 1 rx = tutils.tresp() assert not fm.handle_response(rx) dc = flow.ClientDisconnect(req.client_conn) req.client_conn.requestcount = 1 fm.handle_clientdisconnect(dc) err = flow.Error(f.request, "msg") fm.handle_error(err) fm.load_script(tutils.test_data.path("scripts/a.py")) fm.shutdown()
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"
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.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" 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 dc = flow.ClientDisconnect(req.client_conn) dc.reply = controller.DummyReply() fm.handle_clientdisconnect(dc) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" assert fm.scripts[1].ns["log"][-1] == "clientdisconnect" #unload first script fm.unload_script(fm.scripts[0]) assert len(fm.scripts) == 1 err = flow.Error(f.request, "msg") err.reply = controller.DummyReply() fm.handle_error(err) assert fm.scripts[0].ns["log"][-1] == "error"
def test_get_form_for_urlencoded(self): r = tutils.treq() r.headers.add("content-type", "application/x-www-form-urlencoded") r.get_form_urlencoded = MagicMock() r.get_form() assert r.get_form_urlencoded.called
def test_app_registry(): ar = flow.AppRegistry() ar.add("foo", "domain", 80) r = tutils.treq() r.set_url("http://domain:80/") assert ar.get(r) r.port = 81 assert not ar.get(r) r = tutils.treq() r.host = "domain2" r.port = 80 assert not ar.get(r) r.headers["host"] = ["domain"] assert ar.get(r)
def _serve(self, app): w = wsgi.WSGIAdaptor(app, "foo", 80) r = tutils.treq() r.host = "foo" r.port = 80 wfile = cStringIO.StringIO() err = w.serve(r, wfile) return wfile.getvalue()
def test_backup(self): bc = proxy.ClientConnect(("address", 22)) c = flow.State() req = tutils.treq() f = c.add_request(req) f.backup() c.revert(f)
def test_concurrent(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.load_script([tutils.test_data.path("scripts/concurrent_decorator.py")]) reply = TCounter() r1, r2 = tutils.treq(), tutils.treq() r1.reply, r2.reply = reply, reply t_start = time.time() fm.handle_request(r1) r1.reply() fm.handle_request(r2) r2.reply() assert reply.count < 2 assert (time.time() - t_start) < 0.09 time.sleep(0.2) assert reply.count == 2
def test_get_form_for_multipart(self): r = tutils.treq() r.headers.add("content-type", "multipart/form-data") r.get_form_multipart = MagicMock() r.get_form() assert r.get_form_multipart.called
def test_get_cookies_single(self): h = flow.ODictCaseless() h["Cookie"] = ["cookiename=cookievalue"] r = tutils.treq() r.headers = h result = r.get_cookies() assert len(result)==1 assert result['cookiename']==('cookievalue',{})
def test_make_environ(self): w = wsgi.WSGIAdaptor(None, "foo", 80) tr = tutils.treq() assert w.make_environ(tr, None) tr.path = "/foo?bar=voing" r = w.make_environ(tr, None) assert r["QUERY_STRING"] == "bar=voing"
def test_get_decoded_content(self): r = tutils.treq() r.content = None r.headers["content-encoding"] = ["identity"] assert r.get_decoded_content() == None r.content = "falafel" r.encode("gzip") assert r.get_decoded_content() == "falafel"
def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) 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
def test_app_registry(): ar = proxy.AppRegistry() ar.add("foo", "domain", 80) r = tutils.treq() r.host = "domain" r.port = 80 assert ar.get(r) r.port = 81 assert not ar.get(r) r = tutils.treq() r.host = "domain2" r.port = 80 assert not ar.get(r) r.headers["host"] = ["domain"] assert ar.get(r)
def test_get_cookies_withequalsign(self): h = flow.ODictCaseless() h["Cookie"] = ["cookiename=coo=kievalue;othercookiename=othercookievalue"] r = tutils.treq() r.headers = h result = r.get_cookies() assert len(result)==2 assert result['cookiename']==('coo=kievalue',{}) assert result['othercookiename']==('othercookievalue',{})
def test_anticache(self): h = flow.ODictCaseless() r = tutils.treq() r.headers = h h["if-modified-since"] = ["test"] h["if-none-match"] = ["test"] r.anticache() assert not "if-modified-since" in r.headers assert not "if-none-match" in r.headers
def test_replace(self): r = tutils.treq() r.path = "path/foo" r.headers["Foo"] = ["fOo"] r.content = "afoob" assert r.replace("foo(?i)", "boo") == 4 assert r.path == "path/boo" assert not "foo" in r.content assert r.headers["boo"] == ["boo"]
def test_err(self): c = flow.State() req = tutils.treq() f = c.add_request(req) e = flow.Error(f.request, "message") assert c.add_error(e) e = flow.Error(tutils.tflow().request, "message") assert not c.add_error(e)
def test_decoded(): r = tutils.treq() assert r.content == "content" assert not r.headers["content-encoding"] r.encode("gzip") assert r.headers["content-encoding"] assert r.content != "content" with decoded(r): assert not r.headers["content-encoding"] assert r.content == "content" assert r.headers["content-encoding"] assert r.content != "content" with decoded(r): r.content = "foo" assert r.content != "foo" r.decode() assert r.content == "foo"
def test_get_url(self): r = tutils.treq() assert r.url == "http://address:22/path" r.scheme = "https" assert r.url == "https://address:22/path" r.host = "host" r.port = 42 assert r.url == "https://host:42/path" r.host = "address" r.port = 22 assert r.url == "https://address:22/path" assert r.pretty_url(True) == "https://address:22/path" r.headers["Host"] = ["foo.com"] assert r.pretty_url(False) == "https://address:22/path" assert r.pretty_url(True) == "https://foo.com:22/path"
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.client_conn) assert fm.script.ns["log"][-1] == "clientconnect" f = fm.handle_request(req) assert fm.script.ns["log"][-1] == "request" resp = tutils.tresp(req) fm.handle_response(resp) assert fm.script.ns["log"][-1] == "response" dc = flow.ClientDisconnect(req.client_conn) dc.reply = controller.DummyReply() fm.handle_clientdisconnect(dc) assert fm.script.ns["log"][-1] == "clientdisconnect" err = flow.Error(f.request, "msg") err.reply = controller.DummyReply() fm.handle_error(err) assert fm.script.ns["log"][-1] == "error"
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True req = tutils.treq() fm.handle_clientconnect(req.client_conn) f = fm.handle_request(req) assert s.flow_count() == 1 resp = tutils.tresp(req) fm.handle_response(resp) assert s.flow_count() == 1 rx = tutils.tresp() assert not fm.handle_response(rx) dc = flow.ClientDisconnect(req.client_conn) fm.handle_clientdisconnect(dc) err = flow.Error(f.request, "msg") fm.handle_error(err)
def test_getset_query(self): h = flow.ODictCaseless() r = tutils.treq() r.path = "/foo?x=y&a=b" q = r.get_query() assert q.lst == [("x", "y"), ("a", "b")] r.path = "/" q = r.get_query() assert not q r.path = "/?adsfa" q = r.get_query() assert q.lst == [("adsfa", "")] r.path = "/foo?x=y&a=b" assert r.get_query() r.set_query(flow.ODict([])) assert not r.get_query() qv = flow.ODict([("a", "b"), ("c", "d")]) r.set_query(qv) assert r.get_query() == qv
def test_all(self): s = flow.State() fm = flow.FlowMaster(None, s) fm.anticache = True fm.anticomp = True f = tutils.tflow(req=None) fm.handle_clientconnect(f.client_conn) f.request = tutils.treq() fm.handle_request(f) assert s.flow_count() == 1 f.response = tutils.tresp() fm.handle_response(f) assert not fm.handle_response(None) assert s.flow_count() == 1 fm.handle_clientdisconnect(f.client_conn) f.error = Error("msg") f.error.reply = controller.DummyReply() fm.handle_error(f) fm.load_script(tutils.test_data.path("scripts/a.py")) fm.shutdown()
def _add_request(self, state): req = tutils.treq() f = state.add_request(req) return f
def test_serialization(self): f = flow.Flow(None) f.request = tutils.treq()
def _add_request(self, state): r = tutils.treq() return state.add_request(r)
def test_get_cookies_none(self): h = odict.ODictCaseless() r = tutils.treq() r.headers = h assert len(r.get_cookies()) == 0
def test_repr(self): r = tutils.treq() assert repr(r)
def test_assemble_unknown_form(self): r = tutils.treq() tutils.raises("Invalid request form", r.assemble, "antiauthority")
def test_get_cookies_none(self): h = flow.ODictCaseless() r = tutils.treq() r.headers = h assert r.get_cookies() is None
def test_constrain_encoding(self): r = tutils.treq() r.headers["accept-encoding"] = ["gzip", "oink"] r.constrain_encoding() assert "oink" not in r.headers["accept-encoding"]
def _add_error(self, state): req = tutils.treq() f = state.add_request(req) f.error = flow.Error(f.request, "msg")
def _response(self, cookie, host): s = flow.StickyCookieState(filt.parse(".*")) f = tutils.tflow(req=tutils.treq(host=host, port=80), resp=True) f.response.headers["Set-Cookie"] = [cookie] s.handle_response(f) return s, f
def _add_error(self, state): req = tutils.treq() f = state.add_request(req) f.error = Error("msg")
def _add_response(self, state): req = tutils.treq() state.add_request(req) resp = tutils.tresp(req) state.add_response(resp)