From 9883509f894dde57c8a71340a69581ac46c44f51 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 12:44:29 +0200 Subject: simplify default ssl params for test servers --- test/test_tcp.py | 76 ++++++++------------------------------------------------ 1 file changed, 11 insertions(+), 65 deletions(-) (limited to 'test') diff --git a/test/test_tcp.py b/test/test_tcp.py index 14ba555d..cbe92f3c 100644 --- a/test/test_tcp.py +++ b/test/test_tcp.py @@ -135,10 +135,6 @@ class TestFinishFail(test.ServerTestBase): class TestServerSSL(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list="AES256-SHA", chain_file=tutils.test_data.path("data/server.crt") ) @@ -165,8 +161,6 @@ class TestServerSSL(test.ServerTestBase): class TestSSLv3Only(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), request_client_cert=False, v3_only=True ) @@ -188,9 +182,8 @@ class TestSSLClientCert(test.ServerTestBase): def handle(self): self.wfile.write("%s\n" % self.clientcert.serial) self.wfile.flush() + ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), request_client_cert=True, v3_only=False ) @@ -224,12 +217,7 @@ class TestSNI(test.ServerTestBase): self.wfile.write(self.sni) self.wfile.flush() - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -242,10 +230,6 @@ class TestSNI(test.ServerTestBase): class TestServerCipherList(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -264,11 +248,8 @@ class TestServerCurrentCipher(test.ServerTestBase): def handle(self): self.wfile.write("%s" % str(self.get_current_cipher())) self.wfile.flush() + ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -282,10 +263,6 @@ class TestServerCurrentCipher(test.ServerTestBase): class TestServerCipherListError(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='bogus' ) @@ -298,10 +275,6 @@ class TestServerCipherListError(test.ServerTestBase): class TestClientCipherListError(test.ServerTestBase): handler = ClientCipherListHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list='RC4-SHA' ) @@ -321,12 +294,8 @@ class TestSSLDisconnect(test.ServerTestBase): def handle(self): self.finish() - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -341,12 +310,7 @@ class TestSSLDisconnect(test.ServerTestBase): class TestSSLHardDisconnect(test.ServerTestBase): handler = HardDisconnectHandler - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_echo(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -400,13 +364,9 @@ class TestTimeOut(test.ServerTestBase): class TestALPN(test.ServerTestBase): - handler = HangHandler + handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, - alpn_select="h2" + alpn_select="foobar" ) if OpenSSL._util.lib.Cryptography_HAS_ALPN: @@ -414,19 +374,13 @@ class TestALPN(test.ServerTestBase): def test_alpn(self): c = tcp.TCPClient(("127.0.0.1", self.port)) c.connect() - c.convert_to_ssl(alpn_protos=["h2"]) - print "ALPN: %s" % c.get_alpn_proto_negotiated() - assert c.get_alpn_proto_negotiated() == "h2" + c.convert_to_ssl(alpn_protos=["foobar"]) + assert c.get_alpn_proto_negotiated() == "foobar" class TestSSLTimeOut(test.ServerTestBase): handler = HangHandler - ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False - ) + ssl = True def test_timeout_client(self): c = tcp.TCPClient(("127.0.0.1", self.port)) @@ -439,10 +393,6 @@ class TestSSLTimeOut(test.ServerTestBase): class TestDHParams(test.ServerTestBase): handler = HangHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, dhparams=certutils.CertStore.load_dhparam( tutils.test_data.path("data/dhparam.pem"), ), @@ -643,10 +593,6 @@ class TestAddress: class TestSSLKeyLogger(test.ServerTestBase): handler = EchoHandler ssl = dict( - cert=tutils.test_data.path("data/server.crt"), - key=tutils.test_data.path("data/server.key"), - request_client_cert=False, - v3_only=False, cipher_list="AES256-SHA" ) -- cgit v1.2.3 From b84001e8f082a9198f56037aa6861a360d5d76cf Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 15:16:00 +0200 Subject: http2: explicitly mention all arguments in tests --- test/h2/test_frames.py | 325 ++++++++++++++++++++++++++++++------------------- 1 file changed, 203 insertions(+), 122 deletions(-) (limited to 'test') diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 313ef405..310336b0 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,43 +3,59 @@ import tutils from nose.tools import assert_equal -# TODO test stream association if valid or not - - def test_invalid_flags(): tutils.raises( ValueError, DataFrame, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + payload='foobar') def test_frame_equality(): - a = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') - b = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') + a = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') + b = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') assert_equal(a, b) def test_too_large_frames(): - DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567) + DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567) def test_data_frame_to_bytes(): - f = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar') + f = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') assert_equal(f.to_bytes().encode('hex'), '000006000101234567666f6f626172') f = DataFrame( - 11, - Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, - 0x1234567, - 'foobar', + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', pad_length=3) assert_equal( f.to_bytes().encode('hex'), '00000a00090123456703666f6f626172000000') - f = DataFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar') + f = DataFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload='foobar') tutils.raises(ValueError, f.to_bytes) @@ -63,26 +79,26 @@ def test_data_frame_from_bytes(): def test_data_frame_human_readable(): f = DataFrame( - 11, - Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, - 0x1234567, - 'foobar', + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', pad_length=3) assert f.human_readable() def test_headers_frame_to_bytes(): f = HeadersFrame( - 6, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=6, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, headers=[('host', 'foo.bar')]) assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') f = HeadersFrame( - 10, - HeadersFrame.FLAG_PADDED, - 0x1234567, + length=10, + flags=(HeadersFrame.FLAG_PADDED), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3) assert_equal( @@ -90,9 +106,9 @@ def test_headers_frame_to_bytes(): '00000b01080123456703668594e75e31d9000000') f = HeadersFrame( - 10, - HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=10, + flags=(HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], exclusive=True, stream_dependency=0x7654321, @@ -102,9 +118,9 @@ def test_headers_frame_to_bytes(): '00000c012001234567876543212a668594e75e31d9') f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=True, @@ -115,9 +131,9 @@ def test_headers_frame_to_bytes(): '00001001280123456703876543212a668594e75e31d9000000') f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=False, @@ -127,7 +143,11 @@ def test_headers_frame_to_bytes(): f.to_bytes().encode('hex'), '00001001280123456703076543212a668594e75e31d9000000') - f = HeadersFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar') + f = HeadersFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + headers=[('host', 'foo.bar')]) tutils.raises(ValueError, f.to_bytes) @@ -188,9 +208,9 @@ def test_headers_frame_from_bytes(): def test_headers_frame_human_readable(): f = HeadersFrame( - 7, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=7, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[], pad_length=3, exclusive=False, @@ -199,9 +219,9 @@ def test_headers_frame_human_readable(): assert f.human_readable() f = HeadersFrame( - 14, - HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY, - 0x1234567, + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, headers=[('host', 'foo.bar')], pad_length=3, exclusive=False, @@ -212,27 +232,35 @@ def test_headers_frame_human_readable(): def test_priority_frame_to_bytes(): f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, exclusive=True, stream_dependency=0x7654321, weight=42) assert_equal(f.to_bytes().encode('hex'), '000005020001234567876543212a') f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21) assert_equal(f.to_bytes().encode('hex'), '0000050200012345670765432115') - f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x0, stream_dependency=0x1234567) + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + stream_dependency=0x1234567) tutils.raises(ValueError, f.to_bytes) - f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x1234567, stream_dependency=0x0) + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + stream_dependency=0x0) tutils.raises(ValueError, f.to_bytes) @@ -260,9 +288,9 @@ def test_priority_frame_from_bytes(): def test_priority_frame_human_readable(): f = PriorityFrame( - 5, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21) @@ -270,10 +298,17 @@ def test_priority_frame_human_readable(): def test_rst_stream_frame_to_bytes(): - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, error_code=0x7654321) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) assert_equal(f.to_bytes().encode('hex'), '00000403000123456707654321') - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x0) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) tutils.raises(ValueError, f.to_bytes) @@ -288,28 +323,39 @@ def test_rst_stream_frame_from_bytes(): def test_rst_stream_frame_human_readable(): - f = RstStreamFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, error_code=0x7654321) + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) assert f.human_readable() def test_settings_frame_to_bytes(): - f = SettingsFrame(0, Frame.FLAG_NO_FLAGS, 0x0) + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) assert_equal(f.to_bytes().encode('hex'), '000000040000000000') - f = SettingsFrame(0, SettingsFrame.FLAG_ACK, 0x0) + f = SettingsFrame( + length=0, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0) assert_equal(f.to_bytes().encode('hex'), '000000040100000000') f = SettingsFrame( - 6, - SettingsFrame.FLAG_ACK, 0x0, + length=6, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1}) assert_equal(f.to_bytes().encode('hex'), '000006040100000000000200000001') f = SettingsFrame( - 12, - Frame.FLAG_NO_FLAGS, - 0x0, + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) @@ -317,7 +363,10 @@ def test_settings_frame_to_bytes(): f.to_bytes().encode('hex'), '00000c040000000000000200000001000312345678') - f = SettingsFrame(0, Frame.FLAG_NO_FLAGS, 0x1234567) + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) tutils.raises(ValueError, f.to_bytes) @@ -361,13 +410,17 @@ def test_settings_frame_from_bytes(): def test_settings_frame_human_readable(): - f = SettingsFrame(12, Frame.FLAG_NO_FLAGS, 0x0, settings={}) + f = SettingsFrame( + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + settings={}) assert f.human_readable() f = SettingsFrame( - 12, - Frame.FLAG_NO_FLAGS, - 0x0, + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, settings={ SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) @@ -376,30 +429,38 @@ def test_settings_frame_human_readable(): def test_push_promise_frame_to_bytes(): f = PushPromiseFrame( - 10, - Frame.FLAG_NO_FLAGS, - 0x1234567, - 0x7654321, - 'foobar') + length=10, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar') assert_equal( f.to_bytes().encode('hex'), '00000a05000123456707654321666f6f626172') f = PushPromiseFrame( - 14, - HeadersFrame.FLAG_PADDED, - 0x1234567, - 0x7654321, - 'foobar', + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', pad_length=3) assert_equal( f.to_bytes().encode('hex'), '00000e0508012345670307654321666f6f626172000000') - f = PushPromiseFrame(4, Frame.FLAG_NO_FLAGS, 0x0, 0x1234567) + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + promised_stream=0x1234567) tutils.raises(ValueError, f.to_bytes) - f = PushPromiseFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, 0x0) + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x0) tutils.raises(ValueError, f.to_bytes) @@ -424,27 +485,38 @@ def test_push_promise_frame_from_bytes(): def test_push_promise_frame_human_readable(): f = PushPromiseFrame( - 14, - HeadersFrame.FLAG_PADDED, - 0x1234567, - 0x7654321, - 'foobar', + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', pad_length=3) assert f.human_readable() def test_ping_frame_to_bytes(): - f = PingFrame(8, PingFrame.FLAG_ACK, 0x0, payload=b'foobar') + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') assert_equal( f.to_bytes().encode('hex'), '000008060100000000666f6f6261720000') - f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x0, payload=b'foobardeadbeef') + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload=b'foobardeadbeef') assert_equal( f.to_bytes().encode('hex'), '000008060000000000666f6f6261726465') - f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x1234567) + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) tutils.raises(ValueError, f.to_bytes) @@ -467,15 +539,19 @@ def test_ping_frame_from_bytes(): def test_ping_frame_human_readable(): - f = PingFrame(8, PingFrame.FLAG_ACK, 0x0, payload=b'foobar') + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') assert f.human_readable() def test_goaway_frame_to_bytes(): f = GoAwayFrame( - 8, - Frame.FLAG_NO_FLAGS, - 0x0, + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'') @@ -484,9 +560,9 @@ def test_goaway_frame_to_bytes(): '0000080700000000000123456787654321') f = GoAwayFrame( - 14, - Frame.FLAG_NO_FLAGS, - 0x0, + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar') @@ -495,16 +571,17 @@ def test_goaway_frame_to_bytes(): '00000e0700000000000123456787654321666f6f626172') f = GoAwayFrame( - 8, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, last_stream=0x1234567, error_code=0x87654321) tutils.raises(ValueError, f.to_bytes) def test_goaway_frame_from_bytes(): - f = Frame.from_bytes('0000080700000000000123456787654321'.decode('hex')) + f = Frame.from_bytes( + '0000080700000000000123456787654321'.decode('hex')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -528,9 +605,9 @@ def test_goaway_frame_from_bytes(): def test_go_away_frame_human_readable(): f = GoAwayFrame( - 14, - Frame.FLAG_NO_FLAGS, - 0x0, + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar') @@ -539,23 +616,23 @@ def test_go_away_frame_human_readable(): def test_window_update_frame_to_bytes(): f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x0, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, window_size_increment=0x1234567) assert_equal(f.to_bytes().encode('hex'), '00000408000000000001234567') f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, window_size_increment=0x7654321) assert_equal(f.to_bytes().encode('hex'), '00000408000123456707654321') f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x0, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, window_size_increment=0xdeadbeef) tutils.raises(ValueError, f.to_bytes) @@ -575,22 +652,26 @@ def test_window_update_frame_from_bytes(): def test_window_update_frame_human_readable(): f = WindowUpdateFrame( - 4, - Frame.FLAG_NO_FLAGS, - 0x1234567, + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, window_size_increment=0x7654321) assert f.human_readable() def test_continuation_frame_to_bytes(): f = ContinuationFrame( - 6, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') assert_equal(f.to_bytes().encode('hex'), '000006090401234567666f6f626172') - f = ContinuationFrame(6, ContinuationFrame.FLAG_END_HEADERS, 0x0, 'foobar') + f = ContinuationFrame( + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x0, + header_block_fragment='foobar') tutils.raises(ValueError, f.to_bytes) @@ -606,8 +687,8 @@ def test_continuation_frame_from_bytes(): def test_continuation_frame_human_readable(): f = ContinuationFrame( - 6, - ContinuationFrame.FLAG_END_HEADERS, - 0x1234567, - 'foobar') + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') assert f.human_readable() -- cgit v1.2.3 From 5cecbdc1687346bb2bf139c904ffda2b37dc8276 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 1 Jun 2015 12:34:50 +0200 Subject: http2: add basic protocol handling --- test/h2/example.py | 18 ------------------ test/h2/test_frames.py | 1 + 2 files changed, 1 insertion(+), 18 deletions(-) delete mode 100644 test/h2/example.py (limited to 'test') diff --git a/test/h2/example.py b/test/h2/example.py deleted file mode 100644 index fc4f2f10..00000000 --- a/test/h2/example.py +++ /dev/null @@ -1,18 +0,0 @@ -from netlib.h2.frame import * -from netlib.h2.h2 import * - -c = H2Client(("127.0.0.1", 443)) -c.connect() - -c.send_frame(HeadersFrame( - flags=(Frame.FLAG_END_HEADERS | Frame.FLAG_END_STREAM), - stream_id=0x1, - headers=[ - (b':method', 'GET'), - (b':path', b'/index.html'), - (b':scheme', b'https'), - (b':authority', b'localhost'), - ])) - -while True: - print c.read_frame().human_readable() diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 310336b0..babf8069 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,6 +3,7 @@ import tutils from nose.tools import assert_equal + def test_invalid_flags(): tutils.raises( ValueError, -- cgit v1.2.3 From 40fa113116a2d3a549bc57c1b1381bbb55c7014b Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 14:11:19 +0200 Subject: http2: change header_block_fragment handling --- test/h2/test_frames.py | 111 ++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 51 deletions(-) (limited to 'test') diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index babf8069..30dc71e8 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -3,6 +3,22 @@ import tutils from nose.tools import assert_equal +class FileAdapter(object): + def __init__(self, data, is_hex=True): + self.position = 0 + if is_hex: + self.data = data.decode('hex') + else: + self.data = data + + def safe_read(self, length): + if self.position + length > len(self.data): + raise ValueError("not enough bytes to read") + + value = self.data[self.position:self.position + length] + self.position += length + return value + def test_invalid_flags(): tutils.raises( @@ -26,14 +42,6 @@ def test_frame_equality(): payload='foobar') assert_equal(a, b) - -def test_too_large_frames(): - DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567) - - def test_data_frame_to_bytes(): f = DataFrame( length=6, @@ -61,7 +69,7 @@ def test_data_frame_to_bytes(): def test_data_frame_from_bytes(): - f = Frame.from_bytes('000006000101234567666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) assert isinstance(f, DataFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, DataFrame.TYPE) @@ -69,7 +77,7 @@ def test_data_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.payload, 'foobar') - f = Frame.from_bytes('00000a00090123456703666f6f626172000000'.decode('hex')) + f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) assert isinstance(f, DataFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, DataFrame.TYPE) @@ -93,14 +101,14 @@ def test_headers_frame_to_bytes(): length=6, flags=(Frame.FLAG_NO_FLAGS), stream_id=0x1234567, - headers=[('host', 'foo.bar')]) + header_block_fragment='668594e75e31d9'.decode('hex')) assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') f = HeadersFrame( length=10, flags=(HeadersFrame.FLAG_PADDED), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3) assert_equal( f.to_bytes().encode('hex'), @@ -110,7 +118,7 @@ def test_headers_frame_to_bytes(): length=10, flags=(HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), exclusive=True, stream_dependency=0x7654321, weight=42) @@ -122,7 +130,7 @@ def test_headers_frame_to_bytes(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=True, stream_dependency=0x7654321, @@ -135,7 +143,7 @@ def test_headers_frame_to_bytes(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -148,60 +156,61 @@ def test_headers_frame_to_bytes(): length=6, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, - headers=[('host', 'foo.bar')]) + header_block_fragment='668594e75e31d9'.decode('hex')) tutils.raises(ValueError, f.to_bytes) def test_headers_frame_from_bytes(): - f = Frame.from_bytes('000007010001234567668594e75e31d9'.decode('hex')) + f = Frame.from_file(FileAdapter( + '000007010001234567668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 7) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_bytes( - '00000b01080123456703668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000b01080123456703668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 11) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_bytes( - '00000c012001234567876543212a668594e75e31d9'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000c012001234567876543212a668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 12) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, True) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes( - '00001001280123456703876543212a668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00001001280123456703876543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, True) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes( - '00001001280123456703076543212a668594e75e31d9000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00001001280123456703076543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) assert_equal(f.TYPE, HeadersFrame.TYPE) assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) assert_equal(f.stream_id, 0x1234567) - assert_equal(f.headers, [('host', 'foo.bar')]) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) assert_equal(f.exclusive, False) assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) @@ -212,7 +221,7 @@ def test_headers_frame_human_readable(): length=7, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[], + header_block_fragment=b'', pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -223,7 +232,7 @@ def test_headers_frame_human_readable(): length=14, flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), stream_id=0x1234567, - headers=[('host', 'foo.bar')], + header_block_fragment='668594e75e31d9'.decode('hex'), pad_length=3, exclusive=False, stream_dependency=0x7654321, @@ -266,7 +275,7 @@ def test_priority_frame_to_bytes(): def test_priority_frame_from_bytes(): - f = Frame.from_bytes('000005020001234567876543212a'.decode('hex')) + f = Frame.from_file(FileAdapter('000005020001234567876543212a')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -276,7 +285,7 @@ def test_priority_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_bytes('0000050200012345670765432115'.decode('hex')) + f = Frame.from_file(FileAdapter('0000050200012345670765432115')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -314,7 +323,7 @@ def test_rst_stream_frame_to_bytes(): def test_rst_stream_frame_from_bytes(): - f = Frame.from_bytes('00000403000123456707654321'.decode('hex')) + f = Frame.from_file(FileAdapter('00000403000123456707654321')) assert isinstance(f, RstStreamFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, RstStreamFrame.TYPE) @@ -372,21 +381,21 @@ def test_settings_frame_to_bytes(): def test_settings_frame_from_bytes(): - f = Frame.from_bytes('000000040000000000'.decode('hex')) + f = Frame.from_file(FileAdapter('000000040000000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x0) - f = Frame.from_bytes('000000040100000000'.decode('hex')) + f = Frame.from_file(FileAdapter('000000040100000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, SettingsFrame.FLAG_ACK) assert_equal(f.stream_id, 0x0) - f = Frame.from_bytes('000006040100000000000200000001'.decode('hex')) + f = Frame.from_file(FileAdapter('000006040100000000000200000001')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -395,8 +404,8 @@ def test_settings_frame_from_bytes(): assert_equal(len(f.settings), 1) assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - f = Frame.from_bytes( - '00000c040000000000000200000001000312345678'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000c040000000000000200000001000312345678')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 12) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -466,7 +475,7 @@ def test_push_promise_frame_to_bytes(): def test_push_promise_frame_from_bytes(): - f = Frame.from_bytes('00000a05000123456707654321666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -474,8 +483,8 @@ def test_push_promise_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, 'foobar') - f = Frame.from_bytes( - '00000e0508012345670307654321666f6f626172000000'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000e0508012345670307654321666f6f626172000000')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 14) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -522,7 +531,7 @@ def test_ping_frame_to_bytes(): def test_ping_frame_from_bytes(): - f = Frame.from_bytes('000008060100000000666f6f6261720000'.decode('hex')) + f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -530,7 +539,7 @@ def test_ping_frame_from_bytes(): assert_equal(f.stream_id, 0x0) assert_equal(f.payload, b'foobar\0\0') - f = Frame.from_bytes('000008060000000000666f6f6261726465'.decode('hex')) + f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -581,8 +590,8 @@ def test_goaway_frame_to_bytes(): def test_goaway_frame_from_bytes(): - f = Frame.from_bytes( - '0000080700000000000123456787654321'.decode('hex')) + f = Frame.from_file(FileAdapter( + '0000080700000000000123456787654321')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -592,8 +601,8 @@ def test_goaway_frame_from_bytes(): assert_equal(f.error_code, 0x87654321) assert_equal(f.data, b'') - f = Frame.from_bytes( - '00000e0700000000000123456787654321666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter( + '00000e0700000000000123456787654321666f6f626172')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 14) assert_equal(f.TYPE, GoAwayFrame.TYPE) @@ -642,7 +651,7 @@ def test_window_update_frame_to_bytes(): def test_window_update_frame_from_bytes(): - f = Frame.from_bytes('00000408000000000001234567'.decode('hex')) + f = Frame.from_file(FileAdapter('00000408000000000001234567')) assert isinstance(f, WindowUpdateFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, WindowUpdateFrame.TYPE) @@ -677,7 +686,7 @@ def test_continuation_frame_to_bytes(): def test_continuation_frame_from_bytes(): - f = Frame.from_bytes('000006090401234567666f6f626172'.decode('hex')) + f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) assert isinstance(f, ContinuationFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, ContinuationFrame.TYPE) -- cgit v1.2.3 From 623dd850e0ce15630e0950b4de843c0af8046618 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 14:28:09 +0200 Subject: http2: add logging and error handling --- test/h2/test_frames.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 30dc71e8..42a0c1cf 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -1,7 +1,7 @@ -from netlib.h2.frame import * import tutils - from nose.tools import assert_equal +from netlib.h2.frame import * + class FileAdapter(object): def __init__(self, data, is_hex=True): @@ -42,6 +42,16 @@ def test_frame_equality(): payload='foobar') assert_equal(a, b) + +def test_too_large_frames(): + f = DataFrame( + length=9000, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar' * 3000) + tutils.raises(FrameSizeError, f.to_bytes) + + def test_data_frame_to_bytes(): f = DataFrame( length=6, -- cgit v1.2.3 From f003f87197a6dffe1b51a82f7dd218121c75e206 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Thu, 4 Jun 2015 19:44:48 +0200 Subject: http2: rename module and refactor as strategy --- test/h2/test_frames.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py index 42a0c1cf..d8a4febc 100644 --- a/test/h2/test_frames.py +++ b/test/h2/test_frames.py @@ -1,6 +1,6 @@ import tutils from nose.tools import assert_equal -from netlib.h2.frame import * +from netlib.http2.frame import * class FileAdapter(object): -- cgit v1.2.3 From fdc908cb9811628435ef02e3168c4d5931c6a3c5 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 13:28:09 +0200 Subject: http2: add protocol tests --- test/__init__.py | 0 test/h2/__init__.py | 0 test/h2/test_frames.py | 714 -------------------------------------- test/http2/test_frames.py | 714 ++++++++++++++++++++++++++++++++++++++ test/http2/test_http2_protocol.py | 216 ++++++++++++ 5 files changed, 930 insertions(+), 714 deletions(-) create mode 100644 test/__init__.py delete mode 100644 test/h2/__init__.py delete mode 100644 test/h2/test_frames.py create mode 100644 test/http2/test_frames.py create mode 100644 test/http2/test_http2_protocol.py (limited to 'test') diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/h2/__init__.py b/test/h2/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/test/h2/test_frames.py b/test/h2/test_frames.py deleted file mode 100644 index d8a4febc..00000000 --- a/test/h2/test_frames.py +++ /dev/null @@ -1,714 +0,0 @@ -import tutils -from nose.tools import assert_equal -from netlib.http2.frame import * - - -class FileAdapter(object): - def __init__(self, data, is_hex=True): - self.position = 0 - if is_hex: - self.data = data.decode('hex') - else: - self.data = data - - def safe_read(self, length): - if self.position + length > len(self.data): - raise ValueError("not enough bytes to read") - - value = self.data[self.position:self.position + length] - self.position += length - return value - - -def test_invalid_flags(): - tutils.raises( - ValueError, - DataFrame, - flags=ContinuationFrame.FLAG_END_HEADERS, - stream_id=0x1234567, - payload='foobar') - - -def test_frame_equality(): - a = DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar') - b = DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar') - assert_equal(a, b) - - -def test_too_large_frames(): - f = DataFrame( - length=9000, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar' * 3000) - tutils.raises(FrameSizeError, f.to_bytes) - - -def test_data_frame_to_bytes(): - f = DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar') - assert_equal(f.to_bytes().encode('hex'), '000006000101234567666f6f626172') - - f = DataFrame( - length=11, - flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), - stream_id=0x1234567, - payload='foobar', - pad_length=3) - assert_equal( - f.to_bytes().encode('hex'), - '00000a00090123456703666f6f626172000000') - - f = DataFrame( - length=6, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - payload='foobar') - tutils.raises(ValueError, f.to_bytes) - - -def test_data_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) - assert isinstance(f, DataFrame) - assert_equal(f.length, 6) - assert_equal(f.TYPE, DataFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_END_STREAM) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.payload, 'foobar') - - f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) - assert isinstance(f, DataFrame) - assert_equal(f.length, 10) - assert_equal(f.TYPE, DataFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_END_STREAM | Frame.FLAG_PADDED) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.payload, 'foobar') - - -def test_data_frame_human_readable(): - f = DataFrame( - length=11, - flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), - stream_id=0x1234567, - payload='foobar', - pad_length=3) - assert f.human_readable() - - -def test_headers_frame_to_bytes(): - f = HeadersFrame( - length=6, - flags=(Frame.FLAG_NO_FLAGS), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex')) - assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') - - f = HeadersFrame( - length=10, - flags=(HeadersFrame.FLAG_PADDED), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex'), - pad_length=3) - assert_equal( - f.to_bytes().encode('hex'), - '00000b01080123456703668594e75e31d9000000') - - f = HeadersFrame( - length=10, - flags=(HeadersFrame.FLAG_PRIORITY), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex'), - exclusive=True, - stream_dependency=0x7654321, - weight=42) - assert_equal( - f.to_bytes().encode('hex'), - '00000c012001234567876543212a668594e75e31d9') - - f = HeadersFrame( - length=14, - flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex'), - pad_length=3, - exclusive=True, - stream_dependency=0x7654321, - weight=42) - assert_equal( - f.to_bytes().encode('hex'), - '00001001280123456703876543212a668594e75e31d9000000') - - f = HeadersFrame( - length=14, - flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex'), - pad_length=3, - exclusive=False, - stream_dependency=0x7654321, - weight=42) - assert_equal( - f.to_bytes().encode('hex'), - '00001001280123456703076543212a668594e75e31d9000000') - - f = HeadersFrame( - length=6, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - header_block_fragment='668594e75e31d9'.decode('hex')) - tutils.raises(ValueError, f.to_bytes) - - -def test_headers_frame_from_bytes(): - f = Frame.from_file(FileAdapter( - '000007010001234567668594e75e31d9')) - assert isinstance(f, HeadersFrame) - assert_equal(f.length, 7) - assert_equal(f.TYPE, HeadersFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - - f = Frame.from_file(FileAdapter( - '00000b01080123456703668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert_equal(f.length, 11) - assert_equal(f.TYPE, HeadersFrame.TYPE) - assert_equal(f.flags, HeadersFrame.FLAG_PADDED) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - - f = Frame.from_file(FileAdapter( - '00000c012001234567876543212a668594e75e31d9')) - assert isinstance(f, HeadersFrame) - assert_equal(f.length, 12) - assert_equal(f.TYPE, HeadersFrame.TYPE) - assert_equal(f.flags, HeadersFrame.FLAG_PRIORITY) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - assert_equal(f.exclusive, True) - assert_equal(f.stream_dependency, 0x7654321) - assert_equal(f.weight, 42) - - f = Frame.from_file(FileAdapter( - '00001001280123456703876543212a668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert_equal(f.length, 16) - assert_equal(f.TYPE, HeadersFrame.TYPE) - assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - assert_equal(f.exclusive, True) - assert_equal(f.stream_dependency, 0x7654321) - assert_equal(f.weight, 42) - - f = Frame.from_file(FileAdapter( - '00001001280123456703076543212a668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert_equal(f.length, 16) - assert_equal(f.TYPE, HeadersFrame.TYPE) - assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - assert_equal(f.exclusive, False) - assert_equal(f.stream_dependency, 0x7654321) - assert_equal(f.weight, 42) - - -def test_headers_frame_human_readable(): - f = HeadersFrame( - length=7, - flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), - stream_id=0x1234567, - header_block_fragment=b'', - pad_length=3, - exclusive=False, - stream_dependency=0x7654321, - weight=42) - assert f.human_readable() - - f = HeadersFrame( - length=14, - flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), - stream_id=0x1234567, - header_block_fragment='668594e75e31d9'.decode('hex'), - pad_length=3, - exclusive=False, - stream_dependency=0x7654321, - weight=42) - assert f.human_readable() - - -def test_priority_frame_to_bytes(): - f = PriorityFrame( - length=5, - flags=(Frame.FLAG_NO_FLAGS), - stream_id=0x1234567, - exclusive=True, - stream_dependency=0x7654321, - weight=42) - assert_equal(f.to_bytes().encode('hex'), '000005020001234567876543212a') - - f = PriorityFrame( - length=5, - flags=(Frame.FLAG_NO_FLAGS), - stream_id=0x1234567, - exclusive=False, - stream_dependency=0x7654321, - weight=21) - assert_equal(f.to_bytes().encode('hex'), '0000050200012345670765432115') - - f = PriorityFrame( - length=5, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - stream_dependency=0x1234567) - tutils.raises(ValueError, f.to_bytes) - - f = PriorityFrame( - length=5, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - stream_dependency=0x0) - tutils.raises(ValueError, f.to_bytes) - - -def test_priority_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000005020001234567876543212a')) - assert isinstance(f, PriorityFrame) - assert_equal(f.length, 5) - assert_equal(f.TYPE, PriorityFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.exclusive, True) - assert_equal(f.stream_dependency, 0x7654321) - assert_equal(f.weight, 42) - - f = Frame.from_file(FileAdapter('0000050200012345670765432115')) - assert isinstance(f, PriorityFrame) - assert_equal(f.length, 5) - assert_equal(f.TYPE, PriorityFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.exclusive, False) - assert_equal(f.stream_dependency, 0x7654321) - assert_equal(f.weight, 21) - - -def test_priority_frame_human_readable(): - f = PriorityFrame( - length=5, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - exclusive=False, - stream_dependency=0x7654321, - weight=21) - assert f.human_readable() - - -def test_rst_stream_frame_to_bytes(): - f = RstStreamFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - error_code=0x7654321) - assert_equal(f.to_bytes().encode('hex'), '00000403000123456707654321') - - f = RstStreamFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0) - tutils.raises(ValueError, f.to_bytes) - - -def test_rst_stream_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000403000123456707654321')) - assert isinstance(f, RstStreamFrame) - assert_equal(f.length, 4) - assert_equal(f.TYPE, RstStreamFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.error_code, 0x07654321) - - -def test_rst_stream_frame_human_readable(): - f = RstStreamFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - error_code=0x7654321) - assert f.human_readable() - - -def test_settings_frame_to_bytes(): - f = SettingsFrame( - length=0, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0) - assert_equal(f.to_bytes().encode('hex'), '000000040000000000') - - f = SettingsFrame( - length=0, - flags=SettingsFrame.FLAG_ACK, - stream_id=0x0) - assert_equal(f.to_bytes().encode('hex'), '000000040100000000') - - f = SettingsFrame( - length=6, - flags=SettingsFrame.FLAG_ACK, - stream_id=0x0, - settings={ - SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1}) - assert_equal(f.to_bytes().encode('hex'), '000006040100000000000200000001') - - f = SettingsFrame( - length=12, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - settings={ - SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, - SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) - assert_equal( - f.to_bytes().encode('hex'), - '00000c040000000000000200000001000312345678') - - f = SettingsFrame( - length=0, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567) - tutils.raises(ValueError, f.to_bytes) - - -def test_settings_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000000040000000000')) - assert isinstance(f, SettingsFrame) - assert_equal(f.length, 0) - assert_equal(f.TYPE, SettingsFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - - f = Frame.from_file(FileAdapter('000000040100000000')) - assert isinstance(f, SettingsFrame) - assert_equal(f.length, 0) - assert_equal(f.TYPE, SettingsFrame.TYPE) - assert_equal(f.flags, SettingsFrame.FLAG_ACK) - assert_equal(f.stream_id, 0x0) - - f = Frame.from_file(FileAdapter('000006040100000000000200000001')) - assert isinstance(f, SettingsFrame) - assert_equal(f.length, 6) - assert_equal(f.TYPE, SettingsFrame.TYPE) - assert_equal(f.flags, SettingsFrame.FLAG_ACK, 0x0) - assert_equal(f.stream_id, 0x0) - assert_equal(len(f.settings), 1) - assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - - f = Frame.from_file(FileAdapter( - '00000c040000000000000200000001000312345678')) - assert isinstance(f, SettingsFrame) - assert_equal(f.length, 12) - assert_equal(f.TYPE, SettingsFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - assert_equal(len(f.settings), 2) - assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - assert_equal( - f.settings[ - SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS], - 0x12345678) - - -def test_settings_frame_human_readable(): - f = SettingsFrame( - length=12, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - settings={}) - assert f.human_readable() - - f = SettingsFrame( - length=12, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - settings={ - SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, - SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) - assert f.human_readable() - - -def test_push_promise_frame_to_bytes(): - f = PushPromiseFrame( - length=10, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - promised_stream=0x7654321, - header_block_fragment='foobar') - assert_equal( - f.to_bytes().encode('hex'), - '00000a05000123456707654321666f6f626172') - - f = PushPromiseFrame( - length=14, - flags=HeadersFrame.FLAG_PADDED, - stream_id=0x1234567, - promised_stream=0x7654321, - header_block_fragment='foobar', - pad_length=3) - assert_equal( - f.to_bytes().encode('hex'), - '00000e0508012345670307654321666f6f626172000000') - - f = PushPromiseFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - promised_stream=0x1234567) - tutils.raises(ValueError, f.to_bytes) - - f = PushPromiseFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - promised_stream=0x0) - tutils.raises(ValueError, f.to_bytes) - - -def test_push_promise_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) - assert isinstance(f, PushPromiseFrame) - assert_equal(f.length, 10) - assert_equal(f.TYPE, PushPromiseFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, 'foobar') - - f = Frame.from_file(FileAdapter( - '00000e0508012345670307654321666f6f626172000000')) - assert isinstance(f, PushPromiseFrame) - assert_equal(f.length, 14) - assert_equal(f.TYPE, PushPromiseFrame.TYPE) - assert_equal(f.flags, PushPromiseFrame.FLAG_PADDED) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, 'foobar') - - -def test_push_promise_frame_human_readable(): - f = PushPromiseFrame( - length=14, - flags=HeadersFrame.FLAG_PADDED, - stream_id=0x1234567, - promised_stream=0x7654321, - header_block_fragment='foobar', - pad_length=3) - assert f.human_readable() - - -def test_ping_frame_to_bytes(): - f = PingFrame( - length=8, - flags=PingFrame.FLAG_ACK, - stream_id=0x0, - payload=b'foobar') - assert_equal( - f.to_bytes().encode('hex'), - '000008060100000000666f6f6261720000') - - f = PingFrame( - length=8, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - payload=b'foobardeadbeef') - assert_equal( - f.to_bytes().encode('hex'), - '000008060000000000666f6f6261726465') - - f = PingFrame( - length=8, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567) - tutils.raises(ValueError, f.to_bytes) - - -def test_ping_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) - assert isinstance(f, PingFrame) - assert_equal(f.length, 8) - assert_equal(f.TYPE, PingFrame.TYPE) - assert_equal(f.flags, PingFrame.FLAG_ACK) - assert_equal(f.stream_id, 0x0) - assert_equal(f.payload, b'foobar\0\0') - - f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) - assert isinstance(f, PingFrame) - assert_equal(f.length, 8) - assert_equal(f.TYPE, PingFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - assert_equal(f.payload, b'foobarde') - - -def test_ping_frame_human_readable(): - f = PingFrame( - length=8, - flags=PingFrame.FLAG_ACK, - stream_id=0x0, - payload=b'foobar') - assert f.human_readable() - - -def test_goaway_frame_to_bytes(): - f = GoAwayFrame( - length=8, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - last_stream=0x1234567, - error_code=0x87654321, - data=b'') - assert_equal( - f.to_bytes().encode('hex'), - '0000080700000000000123456787654321') - - f = GoAwayFrame( - length=14, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - last_stream=0x1234567, - error_code=0x87654321, - data=b'foobar') - assert_equal( - f.to_bytes().encode('hex'), - '00000e0700000000000123456787654321666f6f626172') - - f = GoAwayFrame( - length=8, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - last_stream=0x1234567, - error_code=0x87654321) - tutils.raises(ValueError, f.to_bytes) - - -def test_goaway_frame_from_bytes(): - f = Frame.from_file(FileAdapter( - '0000080700000000000123456787654321')) - assert isinstance(f, GoAwayFrame) - assert_equal(f.length, 8) - assert_equal(f.TYPE, GoAwayFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - assert_equal(f.last_stream, 0x1234567) - assert_equal(f.error_code, 0x87654321) - assert_equal(f.data, b'') - - f = Frame.from_file(FileAdapter( - '00000e0700000000000123456787654321666f6f626172')) - assert isinstance(f, GoAwayFrame) - assert_equal(f.length, 14) - assert_equal(f.TYPE, GoAwayFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - assert_equal(f.last_stream, 0x1234567) - assert_equal(f.error_code, 0x87654321) - assert_equal(f.data, b'foobar') - - -def test_go_away_frame_human_readable(): - f = GoAwayFrame( - length=14, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - last_stream=0x1234567, - error_code=0x87654321, - data=b'foobar') - assert f.human_readable() - - -def test_window_update_frame_to_bytes(): - f = WindowUpdateFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - window_size_increment=0x1234567) - assert_equal(f.to_bytes().encode('hex'), '00000408000000000001234567') - - f = WindowUpdateFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - window_size_increment=0x7654321) - assert_equal(f.to_bytes().encode('hex'), '00000408000123456707654321') - - f = WindowUpdateFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - window_size_increment=0xdeadbeef) - tutils.raises(ValueError, f.to_bytes) - - f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0) - tutils.raises(ValueError, f.to_bytes) - - -def test_window_update_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000408000000000001234567')) - assert isinstance(f, WindowUpdateFrame) - assert_equal(f.length, 4) - assert_equal(f.TYPE, WindowUpdateFrame.TYPE) - assert_equal(f.flags, Frame.FLAG_NO_FLAGS) - assert_equal(f.stream_id, 0x0) - assert_equal(f.window_size_increment, 0x1234567) - - -def test_window_update_frame_human_readable(): - f = WindowUpdateFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - window_size_increment=0x7654321) - assert f.human_readable() - - -def test_continuation_frame_to_bytes(): - f = ContinuationFrame( - length=6, - flags=ContinuationFrame.FLAG_END_HEADERS, - stream_id=0x1234567, - header_block_fragment='foobar') - assert_equal(f.to_bytes().encode('hex'), '000006090401234567666f6f626172') - - f = ContinuationFrame( - length=6, - flags=ContinuationFrame.FLAG_END_HEADERS, - stream_id=0x0, - header_block_fragment='foobar') - tutils.raises(ValueError, f.to_bytes) - - -def test_continuation_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) - assert isinstance(f, ContinuationFrame) - assert_equal(f.length, 6) - assert_equal(f.TYPE, ContinuationFrame.TYPE) - assert_equal(f.flags, ContinuationFrame.FLAG_END_HEADERS) - assert_equal(f.stream_id, 0x1234567) - assert_equal(f.header_block_fragment, 'foobar') - - -def test_continuation_frame_human_readable(): - f = ContinuationFrame( - length=6, - flags=ContinuationFrame.FLAG_END_HEADERS, - stream_id=0x1234567, - header_block_fragment='foobar') - assert f.human_readable() diff --git a/test/http2/test_frames.py b/test/http2/test_frames.py new file mode 100644 index 00000000..d8f00dec --- /dev/null +++ b/test/http2/test_frames.py @@ -0,0 +1,714 @@ +from test import tutils +from nose.tools import assert_equal +from netlib.http2.frame import * + + +class FileAdapter(object): + def __init__(self, data, is_hex=True): + self.position = 0 + if is_hex: + self.data = data.decode('hex') + else: + self.data = data + + def safe_read(self, length): + if self.position + length > len(self.data): + raise ValueError("not enough bytes to read") + + value = self.data[self.position:self.position + length] + self.position += length + return value + + +def test_invalid_flags(): + tutils.raises( + ValueError, + DataFrame, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + payload='foobar') + + +def test_frame_equality(): + a = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') + b = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') + assert_equal(a, b) + + +def test_too_large_frames(): + f = DataFrame( + length=9000, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar' * 3000) + tutils.raises(FrameSizeError, f.to_bytes) + + +def test_data_frame_to_bytes(): + f = DataFrame( + length=6, + flags=Frame.FLAG_END_STREAM, + stream_id=0x1234567, + payload='foobar') + assert_equal(f.to_bytes().encode('hex'), '000006000101234567666f6f626172') + + f = DataFrame( + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', + pad_length=3) + assert_equal( + f.to_bytes().encode('hex'), + '00000a00090123456703666f6f626172000000') + + f = DataFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload='foobar') + tutils.raises(ValueError, f.to_bytes) + + +def test_data_frame_from_bytes(): + f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) + assert isinstance(f, DataFrame) + assert_equal(f.length, 6) + assert_equal(f.TYPE, DataFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_END_STREAM) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.payload, 'foobar') + + f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) + assert isinstance(f, DataFrame) + assert_equal(f.length, 10) + assert_equal(f.TYPE, DataFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_END_STREAM | Frame.FLAG_PADDED) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.payload, 'foobar') + + +def test_data_frame_human_readable(): + f = DataFrame( + length=11, + flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED), + stream_id=0x1234567, + payload='foobar', + pad_length=3) + assert f.human_readable() + + +def test_headers_frame_to_bytes(): + f = HeadersFrame( + length=6, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex')) + assert_equal(f.to_bytes().encode('hex'), '000007010001234567668594e75e31d9') + + f = HeadersFrame( + length=10, + flags=(HeadersFrame.FLAG_PADDED), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex'), + pad_length=3) + assert_equal( + f.to_bytes().encode('hex'), + '00000b01080123456703668594e75e31d9000000') + + f = HeadersFrame( + length=10, + flags=(HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex'), + exclusive=True, + stream_dependency=0x7654321, + weight=42) + assert_equal( + f.to_bytes().encode('hex'), + '00000c012001234567876543212a668594e75e31d9') + + f = HeadersFrame( + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex'), + pad_length=3, + exclusive=True, + stream_dependency=0x7654321, + weight=42) + assert_equal( + f.to_bytes().encode('hex'), + '00001001280123456703876543212a668594e75e31d9000000') + + f = HeadersFrame( + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex'), + pad_length=3, + exclusive=False, + stream_dependency=0x7654321, + weight=42) + assert_equal( + f.to_bytes().encode('hex'), + '00001001280123456703076543212a668594e75e31d9000000') + + f = HeadersFrame( + length=6, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + header_block_fragment='668594e75e31d9'.decode('hex')) + tutils.raises(ValueError, f.to_bytes) + + +def test_headers_frame_from_bytes(): + f = Frame.from_file(FileAdapter( + '000007010001234567668594e75e31d9')) + assert isinstance(f, HeadersFrame) + assert_equal(f.length, 7) + assert_equal(f.TYPE, HeadersFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) + + f = Frame.from_file(FileAdapter( + '00000b01080123456703668594e75e31d9000000')) + assert isinstance(f, HeadersFrame) + assert_equal(f.length, 11) + assert_equal(f.TYPE, HeadersFrame.TYPE) + assert_equal(f.flags, HeadersFrame.FLAG_PADDED) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) + + f = Frame.from_file(FileAdapter( + '00000c012001234567876543212a668594e75e31d9')) + assert isinstance(f, HeadersFrame) + assert_equal(f.length, 12) + assert_equal(f.TYPE, HeadersFrame.TYPE) + assert_equal(f.flags, HeadersFrame.FLAG_PRIORITY) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) + assert_equal(f.exclusive, True) + assert_equal(f.stream_dependency, 0x7654321) + assert_equal(f.weight, 42) + + f = Frame.from_file(FileAdapter( + '00001001280123456703876543212a668594e75e31d9000000')) + assert isinstance(f, HeadersFrame) + assert_equal(f.length, 16) + assert_equal(f.TYPE, HeadersFrame.TYPE) + assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) + assert_equal(f.exclusive, True) + assert_equal(f.stream_dependency, 0x7654321) + assert_equal(f.weight, 42) + + f = Frame.from_file(FileAdapter( + '00001001280123456703076543212a668594e75e31d9000000')) + assert isinstance(f, HeadersFrame) + assert_equal(f.length, 16) + assert_equal(f.TYPE, HeadersFrame.TYPE) + assert_equal(f.flags, HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) + assert_equal(f.exclusive, False) + assert_equal(f.stream_dependency, 0x7654321) + assert_equal(f.weight, 42) + + +def test_headers_frame_human_readable(): + f = HeadersFrame( + length=7, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, + header_block_fragment=b'', + pad_length=3, + exclusive=False, + stream_dependency=0x7654321, + weight=42) + assert f.human_readable() + + f = HeadersFrame( + length=14, + flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY), + stream_id=0x1234567, + header_block_fragment='668594e75e31d9'.decode('hex'), + pad_length=3, + exclusive=False, + stream_dependency=0x7654321, + weight=42) + assert f.human_readable() + + +def test_priority_frame_to_bytes(): + f = PriorityFrame( + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, + exclusive=True, + stream_dependency=0x7654321, + weight=42) + assert_equal(f.to_bytes().encode('hex'), '000005020001234567876543212a') + + f = PriorityFrame( + length=5, + flags=(Frame.FLAG_NO_FLAGS), + stream_id=0x1234567, + exclusive=False, + stream_dependency=0x7654321, + weight=21) + assert_equal(f.to_bytes().encode('hex'), '0000050200012345670765432115') + + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + stream_dependency=0x1234567) + tutils.raises(ValueError, f.to_bytes) + + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + stream_dependency=0x0) + tutils.raises(ValueError, f.to_bytes) + + +def test_priority_frame_from_bytes(): + f = Frame.from_file(FileAdapter('000005020001234567876543212a')) + assert isinstance(f, PriorityFrame) + assert_equal(f.length, 5) + assert_equal(f.TYPE, PriorityFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.exclusive, True) + assert_equal(f.stream_dependency, 0x7654321) + assert_equal(f.weight, 42) + + f = Frame.from_file(FileAdapter('0000050200012345670765432115')) + assert isinstance(f, PriorityFrame) + assert_equal(f.length, 5) + assert_equal(f.TYPE, PriorityFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.exclusive, False) + assert_equal(f.stream_dependency, 0x7654321) + assert_equal(f.weight, 21) + + +def test_priority_frame_human_readable(): + f = PriorityFrame( + length=5, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + exclusive=False, + stream_dependency=0x7654321, + weight=21) + assert f.human_readable() + + +def test_rst_stream_frame_to_bytes(): + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) + assert_equal(f.to_bytes().encode('hex'), '00000403000123456707654321') + + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) + tutils.raises(ValueError, f.to_bytes) + + +def test_rst_stream_frame_from_bytes(): + f = Frame.from_file(FileAdapter('00000403000123456707654321')) + assert isinstance(f, RstStreamFrame) + assert_equal(f.length, 4) + assert_equal(f.TYPE, RstStreamFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.error_code, 0x07654321) + + +def test_rst_stream_frame_human_readable(): + f = RstStreamFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + error_code=0x7654321) + assert f.human_readable() + + +def test_settings_frame_to_bytes(): + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0) + assert_equal(f.to_bytes().encode('hex'), '000000040000000000') + + f = SettingsFrame( + length=0, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0) + assert_equal(f.to_bytes().encode('hex'), '000000040100000000') + + f = SettingsFrame( + length=6, + flags=SettingsFrame.FLAG_ACK, + stream_id=0x0, + settings={ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1}) + assert_equal(f.to_bytes().encode('hex'), '000006040100000000000200000001') + + f = SettingsFrame( + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + settings={ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) + assert_equal( + f.to_bytes().encode('hex'), + '00000c040000000000000200000001000312345678') + + f = SettingsFrame( + length=0, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) + tutils.raises(ValueError, f.to_bytes) + + +def test_settings_frame_from_bytes(): + f = Frame.from_file(FileAdapter('000000040000000000')) + assert isinstance(f, SettingsFrame) + assert_equal(f.length, 0) + assert_equal(f.TYPE, SettingsFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + + f = Frame.from_file(FileAdapter('000000040100000000')) + assert isinstance(f, SettingsFrame) + assert_equal(f.length, 0) + assert_equal(f.TYPE, SettingsFrame.TYPE) + assert_equal(f.flags, SettingsFrame.FLAG_ACK) + assert_equal(f.stream_id, 0x0) + + f = Frame.from_file(FileAdapter('000006040100000000000200000001')) + assert isinstance(f, SettingsFrame) + assert_equal(f.length, 6) + assert_equal(f.TYPE, SettingsFrame.TYPE) + assert_equal(f.flags, SettingsFrame.FLAG_ACK, 0x0) + assert_equal(f.stream_id, 0x0) + assert_equal(len(f.settings), 1) + assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) + + f = Frame.from_file(FileAdapter( + '00000c040000000000000200000001000312345678')) + assert isinstance(f, SettingsFrame) + assert_equal(f.length, 12) + assert_equal(f.TYPE, SettingsFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + assert_equal(len(f.settings), 2) + assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) + assert_equal( + f.settings[ + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS], + 0x12345678) + + +def test_settings_frame_human_readable(): + f = SettingsFrame( + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + settings={}) + assert f.human_readable() + + f = SettingsFrame( + length=12, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + settings={ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1, + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678}) + assert f.human_readable() + + +def test_push_promise_frame_to_bytes(): + f = PushPromiseFrame( + length=10, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar') + assert_equal( + f.to_bytes().encode('hex'), + '00000a05000123456707654321666f6f626172') + + f = PushPromiseFrame( + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', + pad_length=3) + assert_equal( + f.to_bytes().encode('hex'), + '00000e0508012345670307654321666f6f626172000000') + + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + promised_stream=0x1234567) + tutils.raises(ValueError, f.to_bytes) + + f = PushPromiseFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + promised_stream=0x0) + tutils.raises(ValueError, f.to_bytes) + + +def test_push_promise_frame_from_bytes(): + f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) + assert isinstance(f, PushPromiseFrame) + assert_equal(f.length, 10) + assert_equal(f.TYPE, PushPromiseFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, 'foobar') + + f = Frame.from_file(FileAdapter( + '00000e0508012345670307654321666f6f626172000000')) + assert isinstance(f, PushPromiseFrame) + assert_equal(f.length, 14) + assert_equal(f.TYPE, PushPromiseFrame.TYPE) + assert_equal(f.flags, PushPromiseFrame.FLAG_PADDED) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, 'foobar') + + +def test_push_promise_frame_human_readable(): + f = PushPromiseFrame( + length=14, + flags=HeadersFrame.FLAG_PADDED, + stream_id=0x1234567, + promised_stream=0x7654321, + header_block_fragment='foobar', + pad_length=3) + assert f.human_readable() + + +def test_ping_frame_to_bytes(): + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') + assert_equal( + f.to_bytes().encode('hex'), + '000008060100000000666f6f6261720000') + + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + payload=b'foobardeadbeef') + assert_equal( + f.to_bytes().encode('hex'), + '000008060000000000666f6f6261726465') + + f = PingFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567) + tutils.raises(ValueError, f.to_bytes) + + +def test_ping_frame_from_bytes(): + f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) + assert isinstance(f, PingFrame) + assert_equal(f.length, 8) + assert_equal(f.TYPE, PingFrame.TYPE) + assert_equal(f.flags, PingFrame.FLAG_ACK) + assert_equal(f.stream_id, 0x0) + assert_equal(f.payload, b'foobar\0\0') + + f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) + assert isinstance(f, PingFrame) + assert_equal(f.length, 8) + assert_equal(f.TYPE, PingFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + assert_equal(f.payload, b'foobarde') + + +def test_ping_frame_human_readable(): + f = PingFrame( + length=8, + flags=PingFrame.FLAG_ACK, + stream_id=0x0, + payload=b'foobar') + assert f.human_readable() + + +def test_goaway_frame_to_bytes(): + f = GoAwayFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + last_stream=0x1234567, + error_code=0x87654321, + data=b'') + assert_equal( + f.to_bytes().encode('hex'), + '0000080700000000000123456787654321') + + f = GoAwayFrame( + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + last_stream=0x1234567, + error_code=0x87654321, + data=b'foobar') + assert_equal( + f.to_bytes().encode('hex'), + '00000e0700000000000123456787654321666f6f626172') + + f = GoAwayFrame( + length=8, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + last_stream=0x1234567, + error_code=0x87654321) + tutils.raises(ValueError, f.to_bytes) + + +def test_goaway_frame_from_bytes(): + f = Frame.from_file(FileAdapter( + '0000080700000000000123456787654321')) + assert isinstance(f, GoAwayFrame) + assert_equal(f.length, 8) + assert_equal(f.TYPE, GoAwayFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + assert_equal(f.last_stream, 0x1234567) + assert_equal(f.error_code, 0x87654321) + assert_equal(f.data, b'') + + f = Frame.from_file(FileAdapter( + '00000e0700000000000123456787654321666f6f626172')) + assert isinstance(f, GoAwayFrame) + assert_equal(f.length, 14) + assert_equal(f.TYPE, GoAwayFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + assert_equal(f.last_stream, 0x1234567) + assert_equal(f.error_code, 0x87654321) + assert_equal(f.data, b'foobar') + + +def test_go_away_frame_human_readable(): + f = GoAwayFrame( + length=14, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + last_stream=0x1234567, + error_code=0x87654321, + data=b'foobar') + assert f.human_readable() + + +def test_window_update_frame_to_bytes(): + f = WindowUpdateFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + window_size_increment=0x1234567) + assert_equal(f.to_bytes().encode('hex'), '00000408000000000001234567') + + f = WindowUpdateFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + window_size_increment=0x7654321) + assert_equal(f.to_bytes().encode('hex'), '00000408000123456707654321') + + f = WindowUpdateFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x0, + window_size_increment=0xdeadbeef) + tutils.raises(ValueError, f.to_bytes) + + f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0) + tutils.raises(ValueError, f.to_bytes) + + +def test_window_update_frame_from_bytes(): + f = Frame.from_file(FileAdapter('00000408000000000001234567')) + assert isinstance(f, WindowUpdateFrame) + assert_equal(f.length, 4) + assert_equal(f.TYPE, WindowUpdateFrame.TYPE) + assert_equal(f.flags, Frame.FLAG_NO_FLAGS) + assert_equal(f.stream_id, 0x0) + assert_equal(f.window_size_increment, 0x1234567) + + +def test_window_update_frame_human_readable(): + f = WindowUpdateFrame( + length=4, + flags=Frame.FLAG_NO_FLAGS, + stream_id=0x1234567, + window_size_increment=0x7654321) + assert f.human_readable() + + +def test_continuation_frame_to_bytes(): + f = ContinuationFrame( + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') + assert_equal(f.to_bytes().encode('hex'), '000006090401234567666f6f626172') + + f = ContinuationFrame( + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x0, + header_block_fragment='foobar') + tutils.raises(ValueError, f.to_bytes) + + +def test_continuation_frame_from_bytes(): + f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) + assert isinstance(f, ContinuationFrame) + assert_equal(f.length, 6) + assert_equal(f.TYPE, ContinuationFrame.TYPE) + assert_equal(f.flags, ContinuationFrame.FLAG_END_HEADERS) + assert_equal(f.stream_id, 0x1234567) + assert_equal(f.header_block_fragment, 'foobar') + + +def test_continuation_frame_human_readable(): + f = ContinuationFrame( + length=6, + flags=ContinuationFrame.FLAG_END_HEADERS, + stream_id=0x1234567, + header_block_fragment='foobar') + assert f.human_readable() diff --git a/test/http2/test_http2_protocol.py b/test/http2/test_http2_protocol.py new file mode 100644 index 00000000..6a275430 --- /dev/null +++ b/test/http2/test_http2_protocol.py @@ -0,0 +1,216 @@ + +import OpenSSL + +from netlib import http2 +from netlib import tcp +from netlib import test +from netlib.http2.frame import * +from test import tutils + + +class EchoHandler(tcp.BaseHandler): + sni = None + + def handle(self): + v = self.rfile.readline() + self.wfile.write(v) + self.wfile.flush() + + +class TestCheckALPNMatch(test.ServerTestBase): + handler = EchoHandler + ssl = dict( + alpn_select=http2.HTTP2Protocol.ALPN_PROTO_H2, + ) + + if OpenSSL._util.lib.Cryptography_HAS_ALPN: + + def test_check_alpn(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2]) + protocol = http2.HTTP2Protocol(c) + assert protocol.check_alpn() + + +class TestCheckALPNMismatch(test.ServerTestBase): + handler = EchoHandler + ssl = dict( + alpn_select=None, + ) + + if OpenSSL._util.lib.Cryptography_HAS_ALPN: + + def test_check_alpn(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl(alpn_protos=[http2.HTTP2Protocol.ALPN_PROTO_H2]) + protocol = http2.HTTP2Protocol(c) + tutils.raises(NotImplementedError, protocol.check_alpn) + + +class TestPerformConnectionPreface(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + # check magic + assert self.rfile.read(24) ==\ + '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex') + + # check empty settings frame + assert self.rfile.read(9) ==\ + '000000040000000000'.decode('hex') + + # send empty settings frame + self.wfile.write('000000040000000000'.decode('hex')) + self.wfile.flush() + + # check settings acknowledgement + assert self.rfile.read(9) == \ + '000000040100000000'.decode('hex') + + # send settings acknowledgement + self.wfile.write('000000040100000000'.decode('hex')) + self.wfile.flush() + + ssl = True + + def test_perform_connection_preface(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + protocol.perform_connection_preface() + + +class TestStreamIds(): + c = tcp.TCPClient(("127.0.0.1", 0)) + protocol = http2.HTTP2Protocol(c) + + def test_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 TestApplySettings(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + # check settings acknowledgement + assert self.rfile.read(9) == '000000040100000000'.decode('hex') + self.wfile.write("OK") + self.wfile.flush() + + ssl = True + + def test_apply_settings(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + + protocol._apply_settings({ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 'foo', + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 'bar', + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE: 'deadbeef', + }) + + assert c.rfile.safe_read(2) == "OK" + + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 'foo' + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS] == 'bar' + assert protocol.http2_settings[ + SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE] == 'deadbeef' + + +class TestCreateHeaders(): + c = tcp.TCPClient(("127.0.0.1", 0)) + + def test_create_headers(self): + headers = [ + (b':method', b'GET'), + (b':path', b'index.html'), + (b':scheme', b'https'), + (b'foo', b'bar')] + + bytes = http2.HTTP2Protocol(self.c)._create_headers( + headers, 1, end_stream=True) + assert b''.join(bytes) ==\ + '000014010500000001824488355217caf3a69a3f87408294e7838c767f'\ + .decode('hex') + + bytes = http2.HTTP2Protocol(self.c)._create_headers( + headers, 1, end_stream=False) + assert b''.join(bytes) ==\ + '000014010400000001824488355217caf3a69a3f87408294e7838c767f'\ + .decode('hex') + + # TODO: add test for too large header_block_fragments + + +class TestCreateBody(): + c = tcp.TCPClient(("127.0.0.1", 0)) + protocol = http2.HTTP2Protocol(c) + + def test_create_body_empty(self): + bytes = self.protocol._create_body(b'', 1) + assert b''.join(bytes) == ''.decode('hex') + + def test_create_body_single_frame(self): + bytes = self.protocol._create_body('foobar', 1) + assert b''.join(bytes) == '000006000100000001666f6f626172'.decode('hex') + + def test_create_body_multiple_frames(self): + pass + # bytes = self.protocol._create_body('foobar' * 3000, 1) + # TODO: add test for too large frames + + +class TestCreateRequest(): + c = tcp.TCPClient(("127.0.0.1", 0)) + + def test_create_request_simple(self): + bytes = http2.HTTP2Protocol(self.c).create_request('GET', '/') + assert len(bytes) == 1 + assert bytes[0] == '000003010500000001828487'.decode('hex') + + def test_create_request_with_body(self): + bytes = http2.HTTP2Protocol(self.c).create_request( + 'GET', '/', [(b'foo', b'bar')], 'foobar') + assert len(bytes) == 2 + assert bytes[0] ==\ + '00000b010400000001828487408294e7838c767f'.decode('hex') + assert bytes[1] ==\ + '000006000100000001666f6f626172'.decode('hex') + + +class TestReadResponse(test.ServerTestBase): + class handler(tcp.BaseHandler): + + def handle(self): + self.wfile.write( + b'00000801040000000188628594e78c767f'.decode('hex')) + self.wfile.write( + b'000006000100000001666f6f626172'.decode('hex')) + self.wfile.flush() + + ssl = True + + def test_read_response(self): + c = tcp.TCPClient(("127.0.0.1", self.port)) + c.connect() + c.convert_to_ssl() + protocol = http2.HTTP2Protocol(c) + + status, headers, body = protocol.read_response() + + assert headers == {':status': '200', 'etag': 'foobar'} + assert status == '200' + assert body == b'foobar' -- cgit v1.2.3 From 49043131cc49a602f54c1671ef5637b606c401b7 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 19:39:15 +0200 Subject: increase test coverage --- test/test_tcp.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test') diff --git a/test/test_tcp.py b/test/test_tcp.py index cbe92f3c..f8fc6a28 100644 --- a/test/test_tcp.py +++ b/test/test_tcp.py @@ -575,6 +575,11 @@ class TestFileLike: s = tcp.Reader(o) tutils.raises(tcp.NetLibDisconnect, s.readline, 10) + def test_reader_incomplete_error(self): + s = cStringIO.StringIO("foobar") + s = tcp.Reader(s) + tutils.raises(tcp.NetLibIncomplete, s.safe_read, 10) + class TestAddress: -- cgit v1.2.3 From e7c84a1ce14ca339184de1cd615727144d50d381 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:05:05 +0200 Subject: make travis run all tests --- test/http2/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/http2/__init__.py (limited to 'test') diff --git a/test/http2/__init__.py b/test/http2/__init__.py new file mode 100644 index 00000000..e69de29b -- cgit v1.2.3 From 6c1c6f5f0ad375d5a8f37007e6cf2d6862282de9 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:49:03 +0200 Subject: http2: fix EchoHandler test helper --- test/http2/test_http2_protocol.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/http2/test_http2_protocol.py b/test/http2/test_http2_protocol.py index 6a275430..cb46bc68 100644 --- a/test/http2/test_http2_protocol.py +++ b/test/http2/test_http2_protocol.py @@ -12,9 +12,10 @@ class EchoHandler(tcp.BaseHandler): sni = None def handle(self): - v = self.rfile.readline() - self.wfile.write(v) - self.wfile.flush() + while True: + v = self.rfile.safe_read(1) + self.wfile.write(v) + self.wfile.flush() class TestCheckALPNMatch(test.ServerTestBase): -- cgit v1.2.3 From e39d8aed6d77b6cf5d57c795c69e735a7c1430fa Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Fri, 5 Jun 2015 20:55:32 +0200 Subject: http2: refactor hex to file adapter --- test/http2/test_frames.py | 64 ++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) (limited to 'test') diff --git a/test/http2/test_frames.py b/test/http2/test_frames.py index d8f00dec..76a4b712 100644 --- a/test/http2/test_frames.py +++ b/test/http2/test_frames.py @@ -1,23 +1,13 @@ +import cStringIO from test import tutils from nose.tools import assert_equal +from netlib import tcp from netlib.http2.frame import * -class FileAdapter(object): - def __init__(self, data, is_hex=True): - self.position = 0 - if is_hex: - self.data = data.decode('hex') - else: - self.data = data - - def safe_read(self, length): - if self.position + length > len(self.data): - raise ValueError("not enough bytes to read") - - value = self.data[self.position:self.position + length] - self.position += length - return value +def hex_to_file(data): + data = data.decode('hex') + return tcp.Reader(cStringIO.StringIO(data)) def test_invalid_flags(): @@ -79,7 +69,7 @@ def test_data_frame_to_bytes(): def test_data_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006000101234567666f6f626172')) + f = Frame.from_file(hex_to_file('000006000101234567666f6f626172')) assert isinstance(f, DataFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, DataFrame.TYPE) @@ -87,7 +77,7 @@ def test_data_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.payload, 'foobar') - f = Frame.from_file(FileAdapter('00000a00090123456703666f6f626172000000')) + f = Frame.from_file(hex_to_file('00000a00090123456703666f6f626172000000')) assert isinstance(f, DataFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, DataFrame.TYPE) @@ -171,7 +161,7 @@ def test_headers_frame_to_bytes(): def test_headers_frame_from_bytes(): - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '000007010001234567668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 7) @@ -180,7 +170,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000b01080123456703668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 11) @@ -189,7 +179,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, '668594e75e31d9'.decode('hex')) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000c012001234567876543212a668594e75e31d9')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 12) @@ -201,7 +191,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00001001280123456703876543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) @@ -213,7 +203,7 @@ def test_headers_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00001001280123456703076543212a668594e75e31d9000000')) assert isinstance(f, HeadersFrame) assert_equal(f.length, 16) @@ -285,7 +275,7 @@ def test_priority_frame_to_bytes(): def test_priority_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000005020001234567876543212a')) + f = Frame.from_file(hex_to_file('000005020001234567876543212a')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -295,7 +285,7 @@ def test_priority_frame_from_bytes(): assert_equal(f.stream_dependency, 0x7654321) assert_equal(f.weight, 42) - f = Frame.from_file(FileAdapter('0000050200012345670765432115')) + f = Frame.from_file(hex_to_file('0000050200012345670765432115')) assert isinstance(f, PriorityFrame) assert_equal(f.length, 5) assert_equal(f.TYPE, PriorityFrame.TYPE) @@ -333,7 +323,7 @@ def test_rst_stream_frame_to_bytes(): def test_rst_stream_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000403000123456707654321')) + f = Frame.from_file(hex_to_file('00000403000123456707654321')) assert isinstance(f, RstStreamFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, RstStreamFrame.TYPE) @@ -391,21 +381,21 @@ def test_settings_frame_to_bytes(): def test_settings_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000000040000000000')) + f = Frame.from_file(hex_to_file('000000040000000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, Frame.FLAG_NO_FLAGS) assert_equal(f.stream_id, 0x0) - f = Frame.from_file(FileAdapter('000000040100000000')) + f = Frame.from_file(hex_to_file('000000040100000000')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 0) assert_equal(f.TYPE, SettingsFrame.TYPE) assert_equal(f.flags, SettingsFrame.FLAG_ACK) assert_equal(f.stream_id, 0x0) - f = Frame.from_file(FileAdapter('000006040100000000000200000001')) + f = Frame.from_file(hex_to_file('000006040100000000000200000001')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, SettingsFrame.TYPE) @@ -414,7 +404,7 @@ def test_settings_frame_from_bytes(): assert_equal(len(f.settings), 1) assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1) - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000c040000000000000200000001000312345678')) assert isinstance(f, SettingsFrame) assert_equal(f.length, 12) @@ -485,7 +475,7 @@ def test_push_promise_frame_to_bytes(): def test_push_promise_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000a05000123456707654321666f6f626172')) + f = Frame.from_file(hex_to_file('00000a05000123456707654321666f6f626172')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 10) assert_equal(f.TYPE, PushPromiseFrame.TYPE) @@ -493,7 +483,7 @@ def test_push_promise_frame_from_bytes(): assert_equal(f.stream_id, 0x1234567) assert_equal(f.header_block_fragment, 'foobar') - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000e0508012345670307654321666f6f626172000000')) assert isinstance(f, PushPromiseFrame) assert_equal(f.length, 14) @@ -541,7 +531,7 @@ def test_ping_frame_to_bytes(): def test_ping_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000008060100000000666f6f6261720000')) + f = Frame.from_file(hex_to_file('000008060100000000666f6f6261720000')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -549,7 +539,7 @@ def test_ping_frame_from_bytes(): assert_equal(f.stream_id, 0x0) assert_equal(f.payload, b'foobar\0\0') - f = Frame.from_file(FileAdapter('000008060000000000666f6f6261726465')) + f = Frame.from_file(hex_to_file('000008060000000000666f6f6261726465')) assert isinstance(f, PingFrame) assert_equal(f.length, 8) assert_equal(f.TYPE, PingFrame.TYPE) @@ -600,7 +590,7 @@ def test_goaway_frame_to_bytes(): def test_goaway_frame_from_bytes(): - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '0000080700000000000123456787654321')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 8) @@ -611,7 +601,7 @@ def test_goaway_frame_from_bytes(): assert_equal(f.error_code, 0x87654321) assert_equal(f.data, b'') - f = Frame.from_file(FileAdapter( + f = Frame.from_file(hex_to_file( '00000e0700000000000123456787654321666f6f626172')) assert isinstance(f, GoAwayFrame) assert_equal(f.length, 14) @@ -661,7 +651,7 @@ def test_window_update_frame_to_bytes(): def test_window_update_frame_from_bytes(): - f = Frame.from_file(FileAdapter('00000408000000000001234567')) + f = Frame.from_file(hex_to_file('00000408000000000001234567')) assert isinstance(f, WindowUpdateFrame) assert_equal(f.length, 4) assert_equal(f.TYPE, WindowUpdateFrame.TYPE) @@ -696,7 +686,7 @@ def test_continuation_frame_to_bytes(): def test_continuation_frame_from_bytes(): - f = Frame.from_file(FileAdapter('000006090401234567666f6f626172')) + f = Frame.from_file(hex_to_file('000006090401234567666f6f626172')) assert isinstance(f, ContinuationFrame) assert_equal(f.length, 6) assert_equal(f.TYPE, ContinuationFrame.TYPE) -- cgit v1.2.3