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_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_no_script_file(self): with pytest.raises(Exception, match="not found"): script.parse_command("notfound") with tutils.tmpdir() as dir: with pytest.raises(Exception, match="Not a file"): script.parse_command(dir)
def test_access_control(self): v = base.TokValue.parseString("<path")[0] with tutils.tmpdir() as t: p = os.path.join(t, "path") with open(p, "wb") as f: f.write(b"x" * 10000) assert v.get_generator(language.Settings(staticdir=t)) v = base.TokValue.parseString("<path2")[0] tutils.raises( exceptions.FileAccessDenied, v.get_generator, language.Settings(staticdir=t) ) tutils.raises( "access disabled", v.get_generator, language.Settings() ) v = base.TokValue.parseString("</outside")[0] tutils.raises( "outside", v.get_generator, language.Settings(staticdir=t) )
def test_fixedlengthvalue(self): class TT(base.FixedLengthValue): preamble = "m" length = 4 e = TT.expr() assert e.parseString("m@4") with pytest.raises("invalid value length"): e.parseString("m@100") with pytest.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(b"a" * 20) v = e.parseString("m<path")[0] with pytest.raises("invalid value length"): v.values(s) p = os.path.join(t, "path") with open(p, "wb") as f: f.write(b"a" * 4) v = e.parseString("m<path")[0] assert v.values(s)
def test_simple(self): r = replace.ReplaceFile() with tutils.tmpdir() as td: rp = os.path.join(td, "replacement") with open(rp, "w") as f: f.write("bar") with taddons.context() as tctx: tctx.configure( r, replacement_files = [ ("~q", "foo", rp), ("~s", "foo", rp), ("~b nonexistent", "nonexistent", "nonexistent"), ] ) f = tflow.tflow() f.request.content = b"foo" r.request(f) assert f.request.content == b"bar" f = tflow.tflow(resp=True) f.response.content = b"foo" r.response(f) assert f.response.content == b"bar" f = tflow.tflow() f.request.content = b"nonexistent" assert not tctx.master.event_log r.request(f) assert tctx.master.event_log
def test_client_certs(self): with tutils.tmpdir() as cadir: self.assert_noerr("--client-certs", cadir) self.assert_noerr( "--client-certs", os.path.join(tutils.test_data.path("mitmproxy/data/clientcert"), "client.pem")) with pytest.raises(Exception, match="path does not exist"): self.p("--client-certs", "nonexistent")
def test_sans(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") c1 = ca.get_cert(b"foo.com", [b"*.bar.com"]) ca.get_cert(b"foo.bar.com", []) # assert c1 == c2 c3 = ca.get_cert(b"bar.com", []) assert not c1 == c3
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_create_explicit(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") assert ca.get_cert(b"foo", []) ca2 = certs.CertStore.from_store(d, "test") assert ca2.get_cert(b"foo", []) assert ca.default_ca.get_serial_number() == ca2.default_ca.get_serial_number()
def test_create_tmp(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") assert ca.get_cert(b"foo.com", []) assert ca.get_cert(b"foo.com", []) assert ca.get_cert(b"*.foo.com", []) r = ca.get_cert(b"*.foo.com", []) assert r[1] == ca.default_privatekey
def test_client_certs(self): with tutils.tmpdir() as cadir: self.assert_noerr("--client-certs", cadir) self.assert_noerr( "--client-certs", os.path.join( tutils.test_data.path("mitmproxy/data/clientcert"), "client.pem")) self.assert_err("path does not exist", "--client-certs", "nonexistent")
def test_client_certs(self): with tutils.tmpdir() as cadir: self.assert_noerr("--client-certs", cadir) self.assert_noerr( "--client-certs", os.path.join(tutils.test_data.path("mitmproxy/data/clientcert"), "client.pem")) self.assert_err( "path does not exist", "--client-certs", "nonexistent")
def test_configure(self): cp = clientplayback.ClientPlayback() with taddons.context() as tctx: with tutils.tmpdir() as td: path = os.path.join(td, "flows") tdump(path, [tflow.tflow()]) tctx.configure(cp, client_replay=[path]) tctx.configure(cp, client_replay=[]) tctx.configure(cp) with pytest.raises(exceptions.OptionsError): tctx.configure(cp, client_replay=["nonexistent"])
def test_simple(self): with tutils.tmpdir() as tdir: path = os.path.join(tdir, "somefile") m, sc = tscript("complex/har_dump.py", shlex.quote(path)) m.addons.invoke(m, "response", self.flow()) m.addons.remove(sc) with open(path, "r") as inp: har = json.load(inp) assert len(har["log"]["entries"]) == 1
def test_base64(self): with tutils.tmpdir() as tdir: path = os.path.join(tdir, "somefile") m, sc = tscript("complex/har_dump.py", shlex.quote(path)) m.addons.invoke(m, "response", self.flow(resp_content=b"foo" + b"\xFF" * 10)) m.addons.remove(sc) with open(path, "r") as inp: har = json.load(inp) assert har["log"]["entries"][0]["response"]["content"]["encoding"] == "base64"
def test_read(self): with tutils.tmpdir() as t: p = os.path.join(t, "read") self.flowfile(p) self.dummy_cycle( self.mkmaster(None, rfile=p), 1, b"", ) with pytest.raises(exceptions.OptionsError): self.mkmaster(None, rfile="/nonexistent") with pytest.raises(exceptions.OptionsError): self.mkmaster(None, rfile="test_dump.py")
def test_tcp(): sa = streamfile.StreamFile() with taddons.context() as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") tctx.configure(sa, streamfile=p) tt = tflow.ttcpflow() sa.tcp_start(tt) sa.tcp_end(tt) tctx.configure(sa, streamfile=None) assert rd(p)
def test_configure(): sa = streamfile.StreamFile() with taddons.context(options=dump.Options()) as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") with pytest.raises(exceptions.OptionsError): tctx.configure(sa, streamfile=tdir) with pytest.raises(Exception, match="Invalid filter"): tctx.configure(sa, streamfile=p, filtstr="~~") tctx.configure(sa, filtstr="foo") assert sa.filt tctx.configure(sa, filtstr=None) assert not sa.filt
def test_configure(): sa = streamfile.StreamFile() with taddons.context(options=options.Options()) as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") with pytest.raises(exceptions.OptionsError): tctx.configure(sa, streamfile=tdir) with pytest.raises(Exception, match="Invalid filter"): tctx.configure(sa, streamfile=p, filtstr="~~") tctx.configure(sa, filtstr="foo") assert sa.filt tctx.configure(sa, filtstr=None) assert not sa.filt
def test_configure(): sa = streamfile.StreamFile() with taddons.context(options=dump.Options()) as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") tutils.raises( exceptions.OptionsError, tctx.configure, sa, streamfile=tdir ) tutils.raises( "invalid filter", tctx.configure, sa, streamfile=p, filtstr="~~" )
def test_saving(): o = TD2() o.three = "set" with tutils.tmpdir() as tdir: dst = os.path.join(tdir, "conf") o.save(dst, defaults=True) o2 = TD2() o2.load_paths(dst) o2.three = "foo" o2.save(dst, defaults=True) o.load_paths(dst) assert o.three == "foo"
def test_reload(self): with taddons.context() as tctx: with tutils.tmpdir(): with open("foo.py", "w"): pass sc = script.Script("foo.py") tctx.configure(sc) for _ in range(100): with open("foo.py", "a") as f: f.write(".") sc.tick() time.sleep(0.1) if tctx.master.event_log: return raise AssertionError("Change event not detected.")
def test_overrides(self): with tutils.tmpdir() as d: ca1 = certs.CertStore.from_store(os.path.join(d, "ca1"), "test") ca2 = certs.CertStore.from_store(os.path.join(d, "ca2"), "test") assert not ca1.default_ca.get_serial_number( ) == ca2.default_ca.get_serial_number() dc = ca2.get_cert(b"foo.com", [b"sans.example.com"]) dcp = os.path.join(d, "dc") f = open(dcp, "wb") f.write(dc[0].to_pem()) f.close() ca1.add_cert_file(b"foo.com", dcp) ret = ca1.get_cert(b"foo.com", []) assert ret[0].serial == dc[0].serial
def test_read(self): with tutils.tmpdir() as t: p = os.path.join(t, "read") self.flowfile(p) self.dummy_cycle( self.mkmaster(None, rfile=p), 1, b"", ) tutils.raises( dump.DumpError, self.mkmaster, None, rfile="/nonexistent" ) tutils.raises( dump.DumpError, self.mkmaster, None, rfile="test_dump.py" )
def test_filegenerator(): with tutils.tmpdir() as t: path = os.path.join(t, "foo") f = open(path, "wb") f.write(b"x" * 10000) f.close() g = generators.FileGenerator(path) assert len(g) == 10000 assert g[0] == b"x" assert g[-1] == b"x" assert g[0:5] == b"xxxxx" assert len(g[1:10]) == 9 assert len(g[10000:10001]) == 0 assert repr(g) # remove all references to FileGenerator instance to close the file # handle. del g
def test_with_ca(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") r = certs.dummy_cert( ca.default_privatekey, ca.default_ca, b"foo.com", [b"one.com", b"two.com", b"*.three.com"] ) assert r.cn == b"foo.com" r = certs.dummy_cert( ca.default_privatekey, ca.default_ca, None, [] ) assert r.cn is None
def test_saving(): o = TD2() o.three = "set" with tutils.tmpdir() as tdir: dst = os.path.join(tdir, "conf") o.save(dst, defaults=True) o2 = TD2() o2.load_paths(dst) o2.three = "foo" o2.save(dst, defaults=True) o.load_paths(dst) assert o.three == "foo" with open(dst, 'a') as f: f.write("foobar: '123'") with pytest.raises(exceptions.OptionsError, matches=''): o.load_paths(dst)
def test_simple(): sa = streamfile.StreamFile() with taddons.context() as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") tctx.configure(sa, streamfile=p) f = tflow.tflow(resp=True) sa.request(f) sa.response(f) tctx.configure(sa, streamfile=None) assert rd(p)[0].response tctx.configure(sa, streamfile=p, streamfile_append=True) f = tflow.tflow() sa.request(f) tctx.configure(sa, streamfile=None) assert not rd(p)[1].response
def test_with_ca(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") r = certs.dummy_cert( ca.default_privatekey, ca.default_ca, b"foo.com", [b"one.com", b"two.com", b"*.three.com", b"127.0.0.1"] ) assert r.cn == b"foo.com" assert r.altnames == [b'one.com', b'two.com', b'*.three.com'] r = certs.dummy_cert( ca.default_privatekey, ca.default_ca, None, [] ) assert r.cn is None assert r.altnames == []
def test_binary(self): f = self.flow() f.request.method = "POST" f.request.headers["content-type"] = "application/x-www-form-urlencoded" f.request.content = b"foo=bar&baz=s%c3%bc%c3%9f" f.response.headers["random-junk"] = bytes(range(256)) f.response.content = bytes(range(256)) with tutils.tmpdir() as tdir: path = os.path.join(tdir, "somefile") m, sc = tscript("complex/har_dump.py", shlex.quote(path)) m.addons.invoke(m, "response", f) m.addons.remove(sc) with open(path, "r") as inp: har = json.load(inp) assert len(har["log"]["entries"]) == 1
def test_log(self): testval = b"echo!\n" _logfun = tcp.log_ssl_key with tutils.tmpdir() as d: logfile = os.path.join(d, "foo", "bar", "logfile") tcp.log_ssl_key = tcp.SSLKeyLogger(logfile) c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl() c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval c.finish() tcp.log_ssl_key.close() with open(logfile, "rb") as f: assert f.read().count(b"CLIENT_RANDOM") == 2 tcp.log_ssl_key = _logfun
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(b"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(b"a" * 4) v = e.parseString("m<path")[0] assert v.values(s)
def test_expire(self): with tutils.tmpdir() as d: ca = certs.CertStore.from_store(d, "test") ca.STORE_CAP = 3 ca.get_cert(b"one.com", []) ca.get_cert(b"two.com", []) ca.get_cert(b"three.com", []) assert (b"one.com", ()) in ca.certs assert (b"two.com", ()) in ca.certs assert (b"three.com", ()) in ca.certs ca.get_cert(b"one.com", []) assert (b"one.com", ()) in ca.certs assert (b"two.com", ()) in ca.certs assert (b"three.com", ()) in ca.certs ca.get_cert(b"four.com", []) assert (b"one.com", ()) not in ca.certs assert (b"two.com", ()) in ca.certs assert (b"three.com", ()) in ca.certs assert (b"four.com", ()) in ca.certs
def test_create_dhparams(self): with tutils.tmpdir() as d: filename = os.path.join(d, "dhparam.pem") certs.CertStore.load_dhparam(filename) assert os.path.exists(filename)