def test_timeout(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_replay(self): o = dump.Options(server_replay=["nonexistent"], replay_kill_extra=True) tutils.raises(exceptions.OptionsError, dump.DumpMaster, o, proxy.DummyServer()) with tutils.tmpdir() as t: p = os.path.join(t, "rep") self.flowfile(p) o = dump.Options(server_replay=[p], replay_kill_extra=True) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"content") self.cycle(m, b"content") o = dump.Options(server_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"nonexistent") o = dump.Options(client_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer())
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises("cipher specification", c.convert_to_ssl, sni="foo.com", cipher_list="bogus")
def test_client_greeting_assert_socks5(): raw = tutils.treader(b"\x00\x00") msg = socks.ClientGreeting.from_file(raw) tutils.raises(socks.SocksError, msg.assert_socks5) raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 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(b"GET / HTTP/1.1" + b" " * 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(b"XX") tutils.raises(socks.SocksError, socks.ClientGreeting.from_file, raw, fail_early=True)
def test_always_bytes(): assert strutils.always_bytes(bytes(range(256))) == bytes(range(256)) assert strutils.always_bytes("foo") == b"foo" with tutils.raises(ValueError): strutils.always_bytes(u"\u2605", "ascii") with tutils.raises(TypeError): strutils.always_bytes(42, "ascii")
def test_client_greeting_assert_socks5(): raw = tutils.treader(b"\x00\x00") msg = socks.ClientGreeting.from_file(raw) tutils.raises(socks.SocksError, msg.assert_socks5) raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 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(b"GET / HTTP/1.1" + b" " * 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(b"XX") tutils.raises( socks.SocksError, socks.ClientGreeting.from_file, raw, fail_early=True)
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_simple(): r = intercept.Intercept() with taddons.context(options=Options()) as tctx: assert not r.filt tctx.configure(r, intercept="~q") assert r.filt tutils.raises( exceptions.OptionsError, tctx.configure, r, intercept="~~" ) tctx.configure(r, intercept=None) assert not r.filt tctx.configure(r, intercept="~s") f = tflow.tflow(resp=True) tctx.cycle(r, f) assert f.intercepted f = tflow.tflow(resp=False) tctx.cycle(r, f) assert not f.intercepted f = tflow.tflow(resp=True) f.reply._state = "handled" r.response(f) assert f.intercepted
def test_bidi(): b = bidi.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, bidi.BiDi, one=1, two=1)
def test_no_script_file(self): with tutils.raises("not found"): script.parse_command("notfound") with tutils.tmpdir() as dir: with tutils.raises("not a file"): script.parse_command(dir)
def test_clientcert_err(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises(exceptions.TlsException, c.convert_to_ssl, cert=tutils.test_data.path( "mitmproxy/net/data/clientcert/make"))
def test_http2_without_ssl(self): fp = io.StringIO() c = pathoc.Pathoc(("127.0.0.1", self.d.port), use_http2=True, ssl=False, fp=fp) tutils.raises(NotImplementedError, c.connect)
def test_serialize(): o = TD2() o.three = "set" assert "dfour" in o.serialize(None, defaults=True) data = o.serialize(None) assert "dfour" not in data o2 = TD2() o2.load(data) assert o2 == o t = """ unknown: foo """ data = o.serialize(t) o2 = TD2() o2.load(data) assert o2 == o t = "invalid: foo\ninvalid" tutils.raises("config error", o2.load, t) t = "invalid" tutils.raises("config error", o2.load, t) t = "" o2.load(t)
def test_config(self): sc = stickycookie.StickyCookie() o = options.Options(stickycookie = "~b") tutils.raises( "invalid filter", sc.configure, o, o.keys() )
def test_reconnect(self): """ Tests proper functionality of ConnectionHandler.server_reconnect mock. If we have a disconnect on a secure connection that's transparently proxified to an upstream http proxy, we need to send the CONNECT request again. """ self.chain[0].tmaster.addons.add(RequestKiller([1, 2])) self.chain[1].tmaster.addons.add(RequestKiller([1])) p = self.pathoc() with p.connect(): req = p.request("get:'/p/418:b\"content\"'") assert req.content == b"content" assert req.status_code == 418 # First request goes through all three proxies exactly once assert self.proxy.tmaster.state.flow_count() == 1 assert self.chain[0].tmaster.state.flow_count() == 1 assert self.chain[1].tmaster.state.flow_count() == 1 req = p.request("get:'/p/418:b\"content2\"'") assert req.status_code == 502 assert self.proxy.tmaster.state.flow_count() == 2 assert self.chain[0].tmaster.state.flow_count() == 2 # Upstream sees two requests due to reconnection attempt assert self.chain[1].tmaster.state.flow_count() == 3 assert not self.chain[1].tmaster.state.flows[-1].response assert not self.chain[1].tmaster.state.flows[-2].response # Reconnection failed, so we're now disconnected tutils.raises(exceptions.HttpException, p.request, "get:'/p/418:b\"content3\"'")
def test_simple(): r = intercept.Intercept() with taddons.context(options=Options()) as tctx: assert not r.filt tctx.configure(r, intercept="~q") assert r.filt tutils.raises(exceptions.OptionsError, tctx.configure, r, intercept="~~") tctx.configure(r, intercept=None) assert not r.filt tctx.configure(r, intercept="~s") f = tflow.tflow(resp=True) tctx.cycle(r, f) assert f.intercepted f = tflow.tflow(resp=False) tctx.cycle(r, f) assert not f.intercepted f = tflow.tflow(resp=True) f.reply._state = "handled" r.response(f) assert f.intercepted
def test_configure(): up = proxyauth.ProxyAuth() with taddons.context() as ctx: tutils.raises( exceptions.OptionsError, ctx.configure, up, auth_singleuser="******" ) ctx.configure(up, auth_singleuser="******") assert up.singleuser == ["foo", "bar"] ctx.configure(up, auth_singleuser=None) assert up.singleuser is None ctx.configure(up, auth_nonanonymous=True) assert up.nonanonymous ctx.configure(up, auth_nonanonymous=False) assert not up.nonanonymous tutils.raises( exceptions.OptionsError, ctx.configure, up, auth_htpasswd = tutils.test_data.path( "mitmproxy/net/data/server.crt" ) ) tutils.raises( exceptions.OptionsError, ctx.configure, up, auth_htpasswd = "nonexistent" ) ctx.configure( up, auth_htpasswd = tutils.test_data.path( "mitmproxy/net/data/htpasswd" ) ) assert up.htpasswd assert up.htpasswd.check_password("test", "test") assert not up.htpasswd.check_password("test", "foo") ctx.configure(up, auth_htpasswd = None) assert not up.htpasswd tutils.raises( exceptions.OptionsError, ctx.configure, up, auth_nonanonymous = True, mode = "transparent" ) tutils.raises( exceptions.OptionsError, ctx.configure, up, auth_nonanonymous = True, mode = "socks5" )
def test_options(): o = TO(two="three") assert o.keys() == set(["one", "two"]) assert o.one is None assert o.two == "three" o.one = "one" assert o.one == "one" with tutils.raises(TypeError): TO(nonexistent = "value") with tutils.raises("no such option"): o.nonexistent = "value" with tutils.raises("no such option"): o.update(nonexistent = "value") rec = [] def sub(opts, updated): rec.append(copy.copy(opts)) o.changed.connect(sub) o.one = "ninety" assert len(rec) == 1 assert rec[-1].one == "ninety" o.update(one="oink") assert len(rec) == 2 assert rec[-1].one == "oink"
def test_read_chunked(): req = treq(content=None) req.headers["Transfer-Encoding"] = "chunked" data = b"1\r\na\r\n0\r\n" with raises(exceptions.HttpSyntaxException): b"".join(_read_chunked(BytesIO(data))) data = b"1\r\na\r\n0\r\n\r\n" assert b"".join(_read_chunked(BytesIO(data))) == b"a" data = b"\r\n\r\n1\r\na\r\n1\r\nb\r\n0\r\n\r\n" assert b"".join(_read_chunked(BytesIO(data))) == b"ab" data = b"\r\n" with raises("closed prematurely"): b"".join(_read_chunked(BytesIO(data))) data = b"1\r\nfoo" with raises("malformed chunked body"): b"".join(_read_chunked(BytesIO(data))) data = b"foo\r\nfoo" with raises(exceptions.HttpSyntaxException): b"".join(_read_chunked(BytesIO(data))) data = b"5\r\naaaaa\r\n0\r\n\r\n" with raises("too large"): b"".join(_read_chunked(BytesIO(data), limit=2))
def test_invalid_ssl_method_should_fail(self): fake_ssl_method = 100500 c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises(exceptions.TlsException, c.convert_to_ssl, method=fake_ssl_method)
def test_writer_flush_error(self): s = BytesIO() s = tcp.Writer(s) o = mock.MagicMock() o.flush = mock.MagicMock(side_effect=socket.error) s.o = o tutils.raises(exceptions.TcpDisconnect, s.flush)
def test_options(): o = TO(two="three") assert o.keys() == set(["one", "two"]) assert o.one is None assert o.two == "three" o.one = "one" assert o.one == "one" with tutils.raises(TypeError): TO(nonexistent="value") with tutils.raises("no such option"): o.nonexistent = "value" with tutils.raises("no such option"): o.update(nonexistent="value") rec = [] def sub(opts, updated): rec.append(copy.copy(opts)) o.changed.connect(sub) o.one = "ninety" assert len(rec) == 1 assert rec[-1].one == "ninety" o.update(one="oink") assert len(rec) == 2 assert rec[-1].one == "oink"
def test_reader_read_error(self): s = BytesIO(b"foobar\nfoobar") s = tcp.Reader(s) o = mock.MagicMock() o.read = mock.MagicMock(side_effect=socket.error) s.o = o tutils.raises(exceptions.TcpDisconnect, s.read, 10)
def test_connect_err(self): tutils.raises( exceptions.HttpException, self.pathoc, [r"get:'http://foo.com/p/202':da"], connect_to=("localhost", self.d.port) )
def test_configure(self): sh = setheaders.SetHeaders() with taddons.context() as tctx: tutils.raises("invalid setheader filter pattern", tctx.configure, sh, setheaders=[("~b", "one", "two")]) tctx.configure(sh, setheaders=["/foo/bar/voing"])
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl() # Exercise SSL.SysCallError c.rfile.read(10) c.close() tutils.raises(exceptions.TcpDisconnect, c.wfile.write, b"foo")
def test_clientcert_err(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises( exceptions.TlsException, c.convert_to_ssl, cert=tutils.test_data.path("mitmproxy/net/data/clientcert/make") )
def test_parse_setheaders(self): x = setheaders.parse_setheader("/foo/bar/voing") assert x == ("foo", "bar", "voing") x = setheaders.parse_setheader("/foo/bar/vo/ing/") assert x == ("foo", "bar", "vo/ing/") x = setheaders.parse_setheader("/bar/voing") assert x == (".*", "bar", "voing") tutils.raises("invalid replacement", setheaders.parse_setheader, "/")
def test_config(): s = serverplayback.ServerPlayback() with tutils.tmpdir() as p: with taddons.context() as tctx: fpath = os.path.join(p, "flows") tdump(fpath, [tflow.tflow(resp=True)]) tctx.configure(s, server_replay = [fpath]) tutils.raises(exceptions.OptionsError, tctx.configure, s, server_replay = [p])
def test_parse_hook(self): x = replace.parse_hook("/foo/bar/voing") assert x == ("foo", "bar", "voing") x = replace.parse_hook("/foo/bar/vo/ing/") assert x == ("foo", "bar", "vo/ing/") x = replace.parse_hook("/bar/voing") assert x == (".*", "bar", "voing") tutils.raises("invalid replacement", replace.parse_hook, "/")
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_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_configure(): r = stickyauth.StickyAuth() with taddons.context() as tctx: tctx.configure(r, stickyauth="~s") tutils.raises(exceptions.OptionsError, tctx.configure, r, stickyauth="~~")
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_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises( "cipher specification", c.convert_to_ssl, sni="foo.com", cipher_list="bogus" )
def test_simple(self): f = tflow.tflow() r = f.request u = r.url r.url = u tutils.raises(ValueError, setattr, r, "url", "") assert r.url == u r2 = r.copy() assert r.get_state() == r2.get_state()
def test_invalid_content_length(self): tutils.raises( exceptions.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_error(self): sio = io.BytesIO() sio.write(b"bogus") sio.seek(0) r = mitmproxy.io.FlowReader(sio) tutils.raises(FlowReadException, list, r.stream()) f = FlowReadException("foo") assert str(f) == "foo"
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl() # Excercise SSL.ZeroReturnError c.rfile.read(10) c.close() tutils.raises(exceptions.TcpDisconnect, c.wfile.write, b"foo") tutils.raises(queue.Empty, self.q.get_nowait)
def test_invalid_ssl_method_should_fail(self): fake_ssl_method = 100500 c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): tutils.raises( exceptions.TlsException, c.convert_to_ssl, method=fake_ssl_method )
def test_http2_without_ssl(self): fp = io.StringIO() c = pathoc.Pathoc( ("127.0.0.1", self.d.port), use_http2=True, ssl=False, fp=fp ) tutils.raises(NotImplementedError, c.connect)
def test_parse_size(): assert human.parse_size("0") == 0 assert human.parse_size("0b") == 0 assert human.parse_size("1") == 1 assert human.parse_size("1k") == 1024 assert human.parse_size("1m") == 1024**2 assert human.parse_size("1g") == 1024**3 tutils.raises(ValueError, human.parse_size, "1f") tutils.raises(ValueError, human.parse_size, "ak")