def test_client_greeting_assert_socks5(): raw = tutils.treader("\x00\x00") msg = socks.ClientGreeting.from_file(raw) tutils.raises(socks.SocksError, msg.assert_socks5) raw = tutils.treader("HTTP/1.1 200 OK" + " " * 100) msg = socks.ClientGreeting.from_file(raw) try: msg.assert_socks5() except socks.SocksError as e: assert "Invalid SOCKS version" in str(e) assert "HTTP" not in str(e) else: assert False raw = tutils.treader("GET / HTTP/1.1" + " " * 100) msg = socks.ClientGreeting.from_file(raw) try: msg.assert_socks5() except socks.SocksError as e: assert "Invalid SOCKS version" in str(e) assert "HTTP" in str(e) else: assert False raw = tutils.treader("XX") tutils.raises(socks.SocksError, socks.ClientGreeting.from_file, raw, fail_early=True)
def test_connect_err(self): tutils.raises( HttpException, self.pathoc, [r"get:'http://foo.com/p/202':da"], connect_to=("localhost", self.d.port) )
def test_read_chunked(): h = odict.ODictCaseless() h["transfer-encoding"] = ["chunked"] s = cStringIO.StringIO("1\r\na\r\n0\r\n") tutils.raises("malformed chunked body", http.read_http_body, s, h, None, "GET", None, True) s = cStringIO.StringIO("1\r\na\r\n0\r\n\r\n") assert http.read_http_body(s, h, None, "GET", None, True) == "a" s = cStringIO.StringIO("\r\n\r\n1\r\na\r\n0\r\n\r\n") assert http.read_http_body(s, h, None, "GET", None, True) == "a" s = cStringIO.StringIO("\r\n") tutils.raises("closed prematurely", http.read_http_body, s, h, None, "GET", None, True) s = cStringIO.StringIO("1\r\nfoo") tutils.raises("malformed chunked body", http.read_http_body, s, h, None, "GET", None, True) s = cStringIO.StringIO("foo\r\nfoo") tutils.raises(http.HttpError, http.read_http_body, s, h, None, "GET", None, True) s = cStringIO.StringIO("5\r\naaaaa\r\n0\r\n\r\n") tutils.raises("too large", http.read_http_body, s, h, 2, "GET", None, True)
def test_connect_err(self): tutils.raises( http.HttpError, self.pathoc, r"get:'http://foo.com/p/202':da", connect_to=("localhost", self.d.port) )
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)
def test_intfield(): class TT(base.IntField): preamble = "t" names = { "one": 1, "two": 2, "three": 3 } max = 4 e = TT.expr() v = e.parseString("tone")[0] assert v.value == 1 assert v.spec() == "tone" assert v.values(language.Settings()) v = e.parseString("t1")[0] assert v.value == 1 assert v.spec() == "t1" v = e.parseString("t4")[0] assert v.value == 4 assert v.spec() == "t4" tutils.raises("can't exceed", e.parseString, "t5")
def test_logs(self): assert self.d.clear_log() tutils.raises("no requests logged", self.d.last_log) rsp = self.get("202:da") assert len(self.d.log()) == 1 assert self.d.clear_log() assert len(self.d.log()) == 0
def test_simple(self): d = test.Daemon() rsp = requests.get("http://localhost:%s/p/202" % d.port) assert rsp.ok assert rsp.status_code == 202 d.shutdown() tutils.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202" % d.port)
def test_absolute_form_in(self): s = StringIO("GET oops-no-protocol.com HTTP/1.1") tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) s = StringIO("GET http://address:22/ HTTP/1.1") r = HTTPRequest.from_stream(s) assert r.assemble( ) == "GET http://address:22/ HTTP/1.1\r\nHost: address:22\r\nContent-Length: 0\r\n\r\n"
def test_writer_flush_error(self): s = cStringIO.StringIO() s = tcp.Writer(s) o = mock.MagicMock() o.flush = mock.MagicMock(side_effect=socket.error) s.o = o tutils.raises(tcp.NetLibDisconnect, s.flush)
def test_parse_err(self): tutils.raises(rparse.ParseException, rparse.parse_response, {}, "400:msg,b:") try: rparse.parse_response({}, "400'msg':b:") except rparse.ParseException, v: assert v.marked() assert str(v)
def test_bidi(): b = utils.BiDi(a=1, b=2) assert b.a == 1 assert b.get_name(1) == "a" assert b.get_name(5) is None tutils.raises(AttributeError, getattr, b, "c") tutils.raises(ValueError, utils.BiDi, one=1, two=1)
def test_startstop_ssl(self): d = test.Daemon(ssl=True) rsp = requests.get("https://localhost:%s/p/202"%d.port, verify=False) assert rsp.ok assert rsp.status_code == 202 d.shutdown() tutils.raises(requests.ConnectionError, requests.get, "http://localhost:%s/p/202"%d.port)
def test_read_http_body(): h = odict.ODictCaseless() s = cStringIO.StringIO("testing") assert http.read_http_body(500, s, h, False, None) == "" h["content-length"] = ["foo"] s = cStringIO.StringIO("testing") tutils.raises(http.HttpError, http.read_http_body, 500, s, h, False, None) h["content-length"] = [5] s = cStringIO.StringIO("testing") assert len(http.read_http_body(500, s, h, False, None)) == 5 s = cStringIO.StringIO("testing") tutils.raises(http.HttpError, http.read_http_body, 500, s, h, False, 4) h = odict.ODictCaseless() s = cStringIO.StringIO("testing") assert len(http.read_http_body(500, s, h, True, 4)) == 4 s = cStringIO.StringIO("testing") assert len(http.read_http_body(500, s, h, True, 100)) == 7 h = odict.ODictCaseless() h["transfer-encoding"] = ["chunked"] s = cStringIO.StringIO("5\r\naaaaa\r\n0\r\n\r\n") assert http.read_http_body(500, s, h, True, 100) == "aaaaa"
def test_reader_read_error(self): s = cStringIO.StringIO("foobar\nfoobar") s = tcp.Reader(s) o = mock.MagicMock() o.read = mock.MagicMock(side_effect=socket.error) s.o = o tutils.raises(tcp.NetLibDisconnect, s.read, 10)
def test_parse_err(self): tutils.raises(language.ParseException, language.parse_pathod, "400:msg,b:") try: language.parse_pathod("400'msg':b:") except language.ParseException as v: assert v.marked() assert str(v)
def test_ignore(self): spec = '304:h"Alternate-Protocol"="mitmproxy-will-remove-this"' n = self.pathod(spec) self._ignore_on() i = self.pathod(spec) i2 = self.pathod(spec) self._ignore_off() assert i.status_code == i2.status_code == n.status_code == 304 assert "Alternate-Protocol" in i.headers assert "Alternate-Protocol" in i2.headers assert "Alternate-Protocol" not in n.headers # Test that we get the original SSL cert if self.ssl: i_cert = SSLCert(i.sslinfo.certchain[0]) i2_cert = SSLCert(i2.sslinfo.certchain[0]) n_cert = SSLCert(n.sslinfo.certchain[0]) assert i_cert == i2_cert assert i_cert != n_cert # Test Non-HTTP traffic spec = "200:i0,@100:d0" # this results in just 100 random bytes assert self.pathod(spec).status_code == 502 # mitmproxy responds with bad gateway self._ignore_on() tutils.raises("invalid server response", self.pathod, spec) # pathoc tries to parse answer as HTTP self._ignore_off()
def test_concurrent_err(self): s = flow.State() fm = flow.FlowMaster(None, s) tutils.raises( "decorator not supported for this method", script.Script, tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm )
def test_noweb(self): # FIXME: Add float values to spec language, reduce test timeout to # increase test performance # This is a bodge - we have some platform difference that causes # different exceptions to be raised here. tutils.raises(Exception, self.pathoc, ["get:/:p1,1"]) assert self.d.last_log()["type"] == "timeout"
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() c.convert_to_ssl() # Exercise SSL.SysCallError c.rfile.read(10) c.close() tutils.raises(tcp.NetLibDisconnect, c.wfile.write, "foo")
def test_clientcert_err(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() tutils.raises( tcp.NetLibError, c.convert_to_ssl, cert=tutils.test_data.path("data/clientcert/make") )
def test_parse_size(): assert not utils.parse_size("") assert utils.parse_size("1") == 1 assert utils.parse_size("1k") == 1024 assert utils.parse_size("1m") == 1024**2 assert utils.parse_size("1g") == 1024**3 tutils.raises(ValueError, utils.parse_size, "1f") tutils.raises(ValueError, utils.parse_size, "ak")
def test_parse_websocket_frames(self): wf = language.parse_websocket_frame("wf:x10") assert len(list(wf)) == 10 tutils.raises( language.ParseException, language.parse_websocket_frame, "wf:x" )
def test_instantiation(self): p = pathod.Pathod( ("127.0.0.1", 0), anchors = [(".*", "200:da")] ) assert p.anchors tutils.raises("invalid regex", pathod.Pathod, ("127.0.0.1", 0), anchors=[("*", "200:da")]) tutils.raises("invalid page spec", pathod.Pathod, ("127.0.0.1", 0), anchors=[("foo", "bar")])
def test_authority_form_in(self): s = StringIO("CONNECT oops-no-port.com HTTP/1.1") tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s) s = StringIO("CONNECT address:22 HTTP/1.1") r = HTTPRequest.from_stream(s) r.scheme, r.host, r.port = "http", "address", 22 assert r.assemble() == ("CONNECT address:22 HTTP/1.1\r\n" "Host: address:22\r\n" "Content-Length: 0\r\n\r\n") assert r.pretty_url(False) == "address:22"
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() tutils.raises( "cipher specification", c.convert_to_ssl, sni="foo.com", cipher_list="bogus")
def test_fromSize(self): h = hilbert.Hilbert.fromSize(2, 256*256) assert h.dimensions() == [256, 256] h = hilbert.Hilbert(3, 1) h2 = hilbert.Hilbert.fromSize(3, len(h)) assert h.dimension == h2.dimension assert h.order == h2.order tutils.raises(ValueError, hilbert.Hilbert.fromSize, 3, 3)
def test_error(self): sio = StringIO() sio.write("bogus") sio.seek(0) r = flow.FlowReader(sio) tutils.raises(flow.FlowReadError, list, r.stream()) f = flow.FlowReadError("foo") assert f.strerror == "foo"
def test_write_err(self): tutils.raises( dump.DumpError, self._dummy_cycle, 1, None, "", wfile = "nonexistentdir/foo" )
def test_invalid_content_length(self): tutils.raises( HttpException, self.pathoc, ["get:/:h'content-length'='foo'"] ) l = self.d.last_log() assert l["type"] == "error" assert "Unparseable Content Length" in l["msg"]
def test_fixedlengthvalue(self): class TT(base.FixedLengthValue): preamble = "m" length = 4 e = TT.expr() assert e.parseString("m@4") tutils.raises("invalid value length", e.parseString, "m@100") tutils.raises("invalid value length", e.parseString, "m@1") with tutils.tmpdir() as t: p = os.path.join(t, "path") s = base.Settings(staticdir=t) with open(p, "wb") as f: f.write("a" * 20) v = e.parseString("m<path")[0] tutils.raises("invalid value length", v.values, s) p = os.path.join(t, "path") with open(p, "wb") as f: f.write("a" * 4) v = e.parseString("m<path")[0] assert v.values(s)
def test_file_value(self): v = rparse.Value.parseString("<'one two'")[0] assert str(v) assert v.path == "one two" v = rparse.Value.parseString("<path")[0] assert v.path == "path" with tutils.tmpdir() as t: p = os.path.join(t, "path") f = open(p, "w") f.write("x" * 10000) f.close() assert v.get_generator(dict(staticdir=t)) v = rparse.Value.parseString("<path2")[0] tutils.raises(rparse.FileAccessDenied, v.get_generator, dict(staticdir=t)) tutils.raises("access disabled", v.get_generator, dict()) v = rparse.Value.parseString("</outside")[0] tutils.raises("outside", v.get_generator, dict(staticdir=t))
def test_data_path(): tutils.raises(ValueError, utils.data.path, "nonexistent")
def test_origin_form(self): s = StringIO("GET /foo\xff HTTP/1.1") tutils.raises("Bad HTTP request line", HTTPRequest.from_stream, s)
def test_pkg_data(): assert utils.pkg_data.path("console") tutils.raises("does not exist", utils.pkg_data.path, "nonexistent")
def test_connect_err(self): tutils.raises(HttpException, self.pathoc, [r"get:'http://foo.com/p/202':da"], connect_to=("localhost", self.d.port))
def test_err(self): tutils.raises(language.ParseException, language.parse_request, {}, 'GET')
def assert_err(self, err, *args): tutils.raises(err, self.p, *args)
def test_invalid_content_length(self): tutils.raises(HttpException, self.pathoc, ["get:/:h'content-length'='foo'"]) l = self.d.last_log() assert l["type"] == "error" assert "Unparseable Content Length" in l["msg"]
def test_parse_size(): assert utils.parse_size("100") == 100 assert utils.parse_size("100k") == 100 * 1024 tutils.raises("invalid size spec", utils.parse_size, "foo") tutils.raises("invalid size spec", utils.parse_size, "100kk")
def test_err(self): conf = ProxyConfig(port=1) tutils.raises("error starting proxy server", ProxyServer, conf)
def test_proxy_ioerror(self): # Tests a difficult-to-trigger condition, where an IOError is raised # within our read loop. with mock.patch("libmproxy.protocol.http.HTTPRequest.from_stream") as m: m.side_effect = IOError("error!") tutils.raises("server disconnect", self.pathod, "304")
def test_app_err(self): tutils.raises("handshake error", self.pathod, "200:b@100")
def test_kill(self): tutils.raises("server disconnect", self.pathod, "200") # The server should have seen a request assert self.server.last_log()
def test_kill(self): tutils.raises("server disconnect", self.pathod, "200") # Nothing should have hit the server assert not self.server.last_log()
def test_go(self): p = self.pathoc_raw() dst = ("localhost", self.proxy.port) p.connect(connect_to=dst) tutils.raises("400 - Bad Request", p.http_connect, dst)
def test_nonascii(self): tutils.raises("ascii", parse_request, "get:\xf0")
def test_ssl_error(self): c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=True, fp=None) tutils.raises("ssl handshake", c.connect)
def test_err_2(self): conf = ProxyConfig(host="invalidhost") tutils.raises("error starting proxy server", ProxyServer, conf)
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)
def test_assemble_unknown_form(self): r = tutils.treq() tutils.raises("Invalid request form", r.assemble, "antiauthority")
def test_getLayoutComponentErr(self): tutils.raises("layout component \"nonexistent\"", self.d._getLayoutComponent, "nonexistent")
def test_invalid_headers(self): tutils.raises(HttpException, self.pathoc, ["get:/:h'\t'='foo'"]) l = self.d.last_log() assert l["type"] == "error" assert "Invalid headers" in l["msg"]
def test_render(self): tutils.raises(SyntaxError, doc.Doc, doc.DocRoot(tutils.test_data.path("doctree_err")))
def test_invalid(self): tutils.raises("bad http request", self.tst, "xxx") tutils.raises("bad http request line", self.tst, "get /\xff HTTP/1.1") tutils.raises("invalid headers", self.tst, "get / HTTP/1.1\r\nfoo")
def test_err(self): parser = argparse.ArgumentParser() cmdline.common_options(parser) opts = parser.parse_args(args=[]) tutils.raises("error starting proxy server", proxy.ProxyServer, opts, 1)
def test_nonascii(self): tutils.raises("ascii", language.parse_request, {}, "get:\xf0")
def test_invalid_body(self): tutils.raises(http.HttpError, self.pathoc, "get:/:h'content-length'='foo'") l = self.d.last_log() assert l["type"] == "error" assert "Invalid" in l["msg"]
def test_nonascii(self): tutils.raises("ascii", language.parse_response, {}, "foo:b\xf0")
def test_invalid_headers(self): tutils.raises(http.HttpError, self.pathoc, "get:/:h'\t'='foo'") l = self.d.last_log() assert l["type"] == "error" assert "Invalid headers" in l["msg"]