def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.rfile.read(10) c.wfile.write(b"foo") c.close() c.close()
def test_echo(self): testval = b"echo!\n" c = tcp.TCPClient(tcp.Address(("::1", self.port), use_ipv6=True)) with c.connect(): c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
def test_echo(self): testval = b"echo!\n" c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
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_invalid_http(self): t = tcp.TCPClient(("127.0.0.1", self.proxy.port)) t.connect() t.wfile.write("invalid\r\n\r\n") t.wfile.flush() line = t.rfile.readline() assert ("Bad Request" in line) or ("Bad Gateway" in line)
def test_disconnect_in_finish(self): testval = "echo!\n" c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() c.wfile.write("foo\n") c.wfile.flush() c.rfile.read(4)
def test_failure(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() tutils.raises(tcp.NetLibError, c.convert_to_ssl, sni="foo.com", method=tcp.TLSv1_METHOD)
def test_check_alpn(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() c.convert_to_ssl(alpn_protos=[b'h2']) protocol = HTTP2Protocol(c) with raises(NotImplementedError): protocol.check_alpn()
class TestAssembleResponse(object): c = tcp.TCPClient(("127.0.0.1", 0)) def test_simple(self): bytes = HTTP2Protocol(self.c, is_server=True).assemble_response( http.Response( b"HTTP/2.0", 200, )) assert len(bytes) == 1 assert bytes[0] ==\ codecs.decode('00000101050000000288', 'hex_codec') def test_with_stream_id(self): resp = http.Response( b"HTTP/2.0", 200, ) resp.stream_id = 0x42 bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(resp) assert len(bytes) == 1 assert bytes[0] ==\ codecs.decode('00000101050000004288', 'hex_codec') def test_with_body(self): bytes = HTTP2Protocol(self.c, is_server=True).assemble_response( http.Response(b"HTTP/2.0", 200, b'', http.Headers(foo=b"bar"), b'foobar')) assert len(bytes) == 2 assert bytes[0] ==\ codecs.decode('00000901040000000288408294e7838c767f', 'hex_codec') assert bytes[1] ==\ codecs.decode('000006000100000002666f6f626172', 'hex_codec')
class TestCreateHeaders(object): c = tcp.TCPClient(("127.0.0.1", 0)) def test_create_headers(self): headers = http.Headers([(b':method', b'GET'), (b':path', b'index.html'), (b':scheme', b'https'), (b'foo', b'bar')]) bytes = HTTP2Protocol(self.c)._create_headers(headers, 1, end_stream=True) assert b''.join(bytes) ==\ codecs.decode('000014010500000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec') bytes = HTTP2Protocol(self.c)._create_headers(headers, 1, end_stream=False) assert b''.join(bytes) ==\ codecs.decode('000014010400000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec') def test_create_headers_multiple_frames(self): headers = http.Headers([(b':method', b'GET'), (b':path', b'/'), (b':scheme', b'https'), (b'foo', b'bar'), (b'server', b'version')]) protocol = HTTP2Protocol(self.c) protocol.http2_settings[SettingsFrame.MAX_FRAME_SIZE] = 8 bytes = protocol._create_headers(headers, 1, end_stream=True) assert len(bytes) == 3 assert bytes[0] == codecs.decode('000008010100000001828487408294e783', 'hex_codec') assert bytes[1] == codecs.decode('0000080900000000018c767f7685ee5b10', 'hex_codec') assert bytes[2] == codecs.decode('00000209040000000163d5', 'hex_codec')
def test_thread_start_error(self): with mock.patch.object(threading.Thread, "start", side_effect=threading.ThreadError("nonewthread")) as m: c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() assert not c.rfile.read(1) assert m.called assert "nonewthread" in self.q.get_nowait() self.test_echo()
def test_clientcert_err(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() tutils.raises( TlsException, c.convert_to_ssl, cert=tutils.test_data.path("data/clientcert/make") )
def test_get_current_cipher(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): assert not c.get_current_cipher() c.convert_to_ssl(sni=b"foo.com") ret = c.get_current_cipher() assert ret assert "AES" in ret[0]
def test_echo(self): c = tcp.TCPClient("127.0.0.1", self.port) c.connect() c.convert_to_ssl(sni="foo.com") testval = "echo!\n" c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl(sni=b"foo.com", options=SSL.OP_ALL) testval = b"echo!\n" c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval
def test_should_fail_without_sni(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): with tutils.raises(TlsException): c.convert_to_ssl( verify_options=SSL.VERIFY_PEER, ca_pemfile=tutils.test_data.path( "data/verificationcerts/trusted-root.crt"))
def test_echo(self): c = tcp.TCPClient("127.0.0.1", self.port) c.connect() # Excercise SSL.ZeroReturnError c.rfile.read(10) c.wfile.write("foo") c.close() c.close()
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(TcpDisconnect, c.wfile.write, b"foo")
def test_perform_client_connection_preface(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() protocol = HTTP2Protocol(c) assert not protocol.connection_preface_performed protocol.perform_client_connection_preface() assert protocol.connection_preface_performed
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(TcpDisconnect, c.wfile.write, b"foo") tutils.raises(queue.Empty, self.q.get_nowait)
class TestAssembleRequest(object): c = tcp.TCPClient(("127.0.0.1", 0)) def test_request_simple(self): bytes = HTTP2Protocol(self.c).assemble_request( http.Request( b'', b'GET', b'https', b'', b'', b'/', b"HTTP/2.0", None, None, )) assert len(bytes) == 1 assert bytes[0] == codecs.decode( '00000d0105000000018284874188089d5c0b8170dc07', 'hex_codec') def test_request_with_stream_id(self): req = http.Request( b'', b'GET', b'https', b'', b'', b'/', b"HTTP/2.0", None, None, ) req.stream_id = 0x42 bytes = HTTP2Protocol(self.c).assemble_request(req) assert len(bytes) == 1 assert bytes[0] == codecs.decode( '00000d0105000000428284874188089d5c0b8170dc07', 'hex_codec') def test_request_with_body(self): bytes = HTTP2Protocol(self.c).assemble_request( http.Request( b'', b'GET', b'https', b'', b'', b'/', b"HTTP/2.0", http.Headers([(b'foo', b'bar')]), b'foobar', )) assert len(bytes) == 2 assert bytes[0] ==\ codecs.decode('0000150104000000018284874188089d5c0b8170dc07408294e7838c767f', 'hex_codec') assert bytes[1] ==\ codecs.decode('000006000100000001666f6f626172', 'hex_codec')
def test_perform_server_connection_preface(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() protocol = HTTP2Protocol(c) assert not protocol.connection_preface_performed protocol.perform_server_connection_preface() assert protocol.connection_preface_performed tutils.raises(tcp.NetLibDisconnect, protocol.perform_server_connection_preface, force=True)
def test_ssl_conn_failure(self): c = tcp.TCPClient(("localhost", self.d.port)) c.rbufsize = 0 c.wbufsize = 0 c.connect() c.wfile.write("\0\0\0\0") tutils.raises(TlsException, c.convert_to_ssl) l = self.d.last_log() assert l["type"] == "error" assert "SSL" in l["msg"]
def test_invalid_first_line(self): c = tcp.TCPClient(("localhost", self.d.port)) c.connect() if self.ssl: c.convert_to_ssl() c.wfile.write("foo\n\n\n") c.wfile.flush() l = self.d.last_log() assert l["type"] == "error" assert "foo" in l["msg"]
def test_should_fail(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): with tutils.raises(InvalidCertificateException): c.convert_to_ssl( sni=b"mitmproxy.org", verify_options=SSL.VERIFY_PEER, ca_pemfile=tutils.test_data.path( "data/verificationcerts/trusted-root.crt")) assert c.ssl_verification_error is not None
def test_perform_server_connection_preface(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() protocol = HTTP2Protocol(c) assert not protocol.connection_preface_performed protocol.perform_server_connection_preface() assert protocol.connection_preface_performed with raises(TcpDisconnect): protocol.perform_server_connection_preface(force=True)
class TestClientStreamIds(object): c = tcp.TCPClient(("127.0.0.1", 0)) protocol = HTTP2Protocol(c) def test_client_stream_ids(self): assert self.protocol.current_stream_id is None assert self.protocol._next_stream_id() == 1 assert self.protocol.current_stream_id == 1 assert self.protocol._next_stream_id() == 3 assert self.protocol.current_stream_id == 3 assert self.protocol._next_stream_id() == 5 assert self.protocol.current_stream_id == 5
class TestServerStreamIds(object): c = tcp.TCPClient(("127.0.0.1", 0)) protocol = HTTP2Protocol(c, is_server=True) def test_server_stream_ids(self): assert self.protocol.current_stream_id is None assert self.protocol._next_stream_id() == 2 assert self.protocol.current_stream_id == 2 assert self.protocol._next_stream_id() == 4 assert self.protocol.current_stream_id == 4 assert self.protocol._next_stream_id() == 6 assert self.protocol.current_stream_id == 6
def test_asterisk_form_in(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() c.convert_to_ssl() protocol = HTTP2Protocol(c, is_server=True) protocol.connection_preface_performed = True req = protocol.read_request(NotImplemented) assert req.form_in == "relative" assert req.method == "OPTIONS" assert req.path == "*"
def test_mode_none_should_pass(self): c = tcp.TCPClient(("127.0.0.1", self.port)) with c.connect(): c.convert_to_ssl(verify_options=SSL.VERIFY_NONE) # Verification errors should be saved even if connection isn't aborted assert c.ssl_verification_error is not None testval = b"echo!\n" c.wfile.write(testval) c.wfile.flush() assert c.rfile.readline() == testval