diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/http/http2/test_connections.py (renamed from test/http/http2/test_protocol.py) | 202 | ||||
-rw-r--r-- | test/http/http2/test_frames.py | 669 |
2 files changed, 102 insertions, 769 deletions
diff --git a/test/http/http2/test_protocol.py b/test/http/http2/test_connections.py index 0beec950..22a43266 100644 --- a/test/http/http2/test_protocol.py +++ b/test/http/http2/test_connections.py @@ -1,11 +1,14 @@ import OpenSSL import mock +import codecs -from netlib import tcp, http, tutils, tservers +from hyperframe.frame import * + +from netlib import tcp, http, utils, tservers +from netlib.tutils import raises from netlib.exceptions import TcpDisconnect -from netlib.http import Headers from netlib.http.http2.connections import HTTP2Protocol, TCPHandler -from netlib.http.http2.frame import * + class TestTCPHandlerWrapper: def test_wrapped(self): @@ -90,7 +93,8 @@ class TestCheckALPNMismatch(tservers.ServerTestBase): c.connect() c.convert_to_ssl(alpn_protos=[b'h2']) protocol = HTTP2Protocol(c) - tutils.raises(NotImplementedError, protocol.check_alpn) + with raises(NotImplementedError): + protocol.check_alpn() class TestPerformServerConnectionPreface(tservers.ServerTestBase): @@ -98,24 +102,23 @@ class TestPerformServerConnectionPreface(tservers.ServerTestBase): def handle(self): # send magic - self.wfile.write( - '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex')) + self.wfile.write(codecs.decode('505249202a20485454502f322e300d0a0d0a534d0d0a0d0a', 'hex_codec')) self.wfile.flush() # send empty settings frame - self.wfile.write('000000040000000000'.decode('hex')) + self.wfile.write(codecs.decode('000000040000000000', 'hex_codec')) self.wfile.flush() # check empty settings frame - assert self.rfile.read(9) ==\ - '000000040000000000'.decode('hex') + raw = utils.http2_read_raw_frame(self.rfile) + assert raw == codecs.decode('00000c040000000000000200000000000300000001', 'hex_codec') # check settings acknowledgement - assert self.rfile.read(9) == \ - '000000040100000000'.decode('hex') + raw = utils.http2_read_raw_frame(self.rfile) + assert raw == codecs.decode('000000040100000000', 'hex_codec') # send settings acknowledgement - self.wfile.write('000000040100000000'.decode('hex')) + self.wfile.write(codecs.decode('000000040100000000', 'hex_codec')) self.wfile.flush() def test_perform_server_connection_preface(self): @@ -127,7 +130,8 @@ class TestPerformServerConnectionPreface(tservers.ServerTestBase): protocol.perform_server_connection_preface() assert protocol.connection_preface_performed - tutils.raises(TcpDisconnect, protocol.perform_server_connection_preface, force=True) + with raises(TcpDisconnect): + protocol.perform_server_connection_preface(force=True) class TestPerformClientConnectionPreface(tservers.ServerTestBase): @@ -135,23 +139,22 @@ class TestPerformClientConnectionPreface(tservers.ServerTestBase): def handle(self): # check magic - assert self.rfile.read(24) ==\ - '505249202a20485454502f322e300d0a0d0a534d0d0a0d0a'.decode('hex') + assert self.rfile.read(24) == HTTP2Protocol.CLIENT_CONNECTION_PREFACE # check empty settings frame assert self.rfile.read(9) ==\ - '000000040000000000'.decode('hex') + codecs.decode('000000040000000000', 'hex_codec') # send empty settings frame - self.wfile.write('000000040000000000'.decode('hex')) + self.wfile.write(codecs.decode('000000040000000000', 'hex_codec')) self.wfile.flush() # check settings acknowledgement assert self.rfile.read(9) == \ - '000000040100000000'.decode('hex') + codecs.decode('000000040100000000', 'hex_codec') # send settings acknowledgement - self.wfile.write('000000040100000000'.decode('hex')) + self.wfile.write(codecs.decode('000000040100000000', 'hex_codec')) self.wfile.flush() def test_perform_client_connection_preface(self): @@ -164,7 +167,7 @@ class TestPerformClientConnectionPreface(tservers.ServerTestBase): assert protocol.connection_preface_performed -class TestClientStreamIds(): +class TestClientStreamIds(object): c = tcp.TCPClient(("127.0.0.1", 0)) protocol = HTTP2Protocol(c) @@ -178,7 +181,7 @@ class TestClientStreamIds(): assert self.protocol.current_stream_id == 5 -class TestServerStreamIds(): +class TestServerStreamIds(object): c = tcp.TCPClient(("127.0.0.1", 0)) protocol = HTTP2Protocol(c, is_server=True) @@ -196,7 +199,7 @@ class TestApplySettings(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): # check settings acknowledgement - assert self.rfile.read(9) == '000000040100000000'.decode('hex') + assert self.rfile.read(9) == codecs.decode('000000040100000000', 'hex_codec') self.wfile.write("OK") self.wfile.flush() self.rfile.safe_read(9) # just to keep the connection alive a bit longer @@ -210,22 +213,22 @@ class TestApplySettings(tservers.ServerTestBase): protocol = 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', + SettingsFrame.ENABLE_PUSH: 'foo', + SettingsFrame.MAX_CONCURRENT_STREAMS: 'bar', + SettingsFrame.INITIAL_WINDOW_SIZE: 'deadbeef', }) - assert c.rfile.safe_read(2) == "OK" + assert c.rfile.safe_read(2) == b"OK" assert protocol.http2_settings[ - SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 'foo' + SettingsFrame.ENABLE_PUSH] == 'foo' assert protocol.http2_settings[ - SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS] == 'bar' + SettingsFrame.MAX_CONCURRENT_STREAMS] == 'bar' assert protocol.http2_settings[ - SettingsFrame.SETTINGS.SETTINGS_INITIAL_WINDOW_SIZE] == 'deadbeef' + SettingsFrame.INITIAL_WINDOW_SIZE] == 'deadbeef' -class TestCreateHeaders(): +class TestCreateHeaders(object): c = tcp.TCPClient(("127.0.0.1", 0)) def test_create_headers(self): @@ -238,14 +241,12 @@ class TestCreateHeaders(): bytes = HTTP2Protocol(self.c)._create_headers( headers, 1, end_stream=True) assert b''.join(bytes) ==\ - '000014010500000001824488355217caf3a69a3f87408294e7838c767f'\ - .decode('hex') + codecs.decode('000014010500000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec') bytes = HTTP2Protocol(self.c)._create_headers( headers, 1, end_stream=False) assert b''.join(bytes) ==\ - '000014010400000001824488355217caf3a69a3f87408294e7838c767f'\ - .decode('hex') + codecs.decode('000014010400000001824488355217caf3a69a3f87408294e7838c767f', 'hex_codec') def test_create_headers_multiple_frames(self): headers = http.Headers([ @@ -256,44 +257,45 @@ class TestCreateHeaders(): (b'server', b'version')]) protocol = HTTP2Protocol(self.c) - protocol.http2_settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] = 8 + protocol.http2_settings[SettingsFrame.MAX_FRAME_SIZE] = 8 bytes = protocol._create_headers(headers, 1, end_stream=True) assert len(bytes) == 3 - assert bytes[0] == '000008010000000001828487408294e783'.decode('hex') - assert bytes[1] == '0000080900000000018c767f7685ee5b10'.decode('hex') - assert bytes[2] == '00000209050000000163d5'.decode('hex') + assert bytes[0] == codecs.decode('000008010100000001828487408294e783', 'hex_codec') + assert bytes[1] == codecs.decode('0000080900000000018c767f7685ee5b10', 'hex_codec') + assert bytes[2] == codecs.decode('00000209040000000163d5', 'hex_codec') -class TestCreateBody(): +class TestCreateBody(object): c = tcp.TCPClient(("127.0.0.1", 0)) def test_create_body_empty(self): protocol = HTTP2Protocol(self.c) bytes = protocol._create_body(b'', 1) - assert b''.join(bytes) == ''.decode('hex') + assert b''.join(bytes) == b'' def test_create_body_single_frame(self): protocol = HTTP2Protocol(self.c) - bytes = protocol._create_body('foobar', 1) - assert b''.join(bytes) == '000006000100000001666f6f626172'.decode('hex') + bytes = protocol._create_body(b'foobar', 1) + assert b''.join(bytes) == codecs.decode('000006000100000001666f6f626172', 'hex_codec') def test_create_body_multiple_frames(self): protocol = HTTP2Protocol(self.c) - protocol.http2_settings[SettingsFrame.SETTINGS.SETTINGS_MAX_FRAME_SIZE] = 5 - bytes = protocol._create_body('foobarmehm42', 1) + protocol.http2_settings[SettingsFrame.MAX_FRAME_SIZE] = 5 + bytes = protocol._create_body(b'foobarmehm42', 1) assert len(bytes) == 3 - assert bytes[0] == '000005000000000001666f6f6261'.decode('hex') - assert bytes[1] == '000005000000000001726d65686d'.decode('hex') - assert bytes[2] == '0000020001000000013432'.decode('hex') + assert bytes[0] == codecs.decode('000005000000000001666f6f6261', 'hex_codec') + assert bytes[1] == codecs.decode('000005000000000001726d65686d', 'hex_codec') + assert bytes[2] == codecs.decode('0000020001000000013432', 'hex_codec') class TestReadRequest(tservers.ServerTestBase): class handler(tcp.BaseHandler): + def handle(self): self.wfile.write( - b'000003010400000001828487'.decode('hex')) + codecs.decode('000003010400000001828487', 'hex_codec')) self.wfile.write( - b'000006000100000001666f6f626172'.decode('hex')) + codecs.decode('000006000100000001666f6f626172', 'hex_codec')) self.wfile.flush() self.rfile.safe_read(9) # just to keep the connection alive a bit longer @@ -309,7 +311,7 @@ class TestReadRequest(tservers.ServerTestBase): req = protocol.read_request(NotImplemented) assert req.stream_id - assert req.headers.fields == [[':method', 'GET'], [':path', '/'], [':scheme', 'https']] + assert req.headers.fields == [[b':method', b'GET'], [b':path', b'/'], [b':scheme', b'https']] assert req.content == b'foobar' @@ -317,7 +319,7 @@ class TestReadRequestRelative(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): self.wfile.write( - b'00000c0105000000014287d5af7e4d5a777f4481f9'.decode('hex')) + codecs.decode('00000c0105000000014287d5af7e4d5a777f4481f9', 'hex_codec')) self.wfile.flush() ssl = True @@ -340,7 +342,7 @@ class TestReadRequestAbsolute(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): self.wfile.write( - b'00001901050000000182448d9d29aee30c0e492c2a1170426366871c92585422e085'.decode('hex')) + codecs.decode('00001901050000000182448d9d29aee30c0e492c2a1170426366871c92585422e085', 'hex_codec')) self.wfile.flush() ssl = True @@ -364,9 +366,9 @@ class TestReadRequestConnect(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): self.wfile.write( - b'00001b0105000000014287bdab4e9c17b7ff44871c92585422e08541871c92585422e085'.decode('hex')) + codecs.decode('00001b0105000000014287bdab4e9c17b7ff44871c92585422e08541871c92585422e085', 'hex_codec')) self.wfile.write( - b'00001d0105000000014287bdab4e9c17b7ff44882f91d35d055c87a741882f91d35d055c87a7'.decode('hex')) + codecs.decode('00001d0105000000014287bdab4e9c17b7ff44882f91d35d055c87a741882f91d35d055c87a7', 'hex_codec')) self.wfile.flush() ssl = True @@ -395,9 +397,9 @@ class TestReadResponse(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): self.wfile.write( - b'00000801040000002a88628594e78c767f'.decode('hex')) + codecs.decode('00000801040000002a88628594e78c767f', 'hex_codec')) self.wfile.write( - b'00000600010000002a666f6f626172'.decode('hex')) + codecs.decode('00000600010000002a666f6f626172', 'hex_codec')) self.wfile.flush() self.rfile.safe_read(9) # just to keep the connection alive a bit longer @@ -412,10 +414,10 @@ class TestReadResponse(tservers.ServerTestBase): resp = protocol.read_response(NotImplemented, stream_id=42) - assert resp.http_version == (2, 0) + assert resp.http_version == '2.0' assert resp.status_code == 200 - assert resp.msg == "" - assert resp.headers.fields == [[':status', '200'], ['etag', 'foobar']] + assert resp.msg == '' + assert resp.headers.fields == [[b':status', b'200'], [b'etag', b'foobar']] assert resp.content == b'foobar' assert resp.timestamp_end @@ -424,7 +426,7 @@ class TestReadEmptyResponse(tservers.ServerTestBase): class handler(tcp.BaseHandler): def handle(self): self.wfile.write( - b'00000801050000002a88628594e78c767f'.decode('hex')) + codecs.decode('00000801050000002a88628594e78c767f', 'hex_codec')) self.wfile.flush() ssl = True @@ -439,10 +441,10 @@ class TestReadEmptyResponse(tservers.ServerTestBase): resp = protocol.read_response(NotImplemented, stream_id=42) assert resp.stream_id == 42 - assert resp.http_version == (2, 0) + assert resp.http_version == '2.0' assert resp.status_code == 200 - assert resp.msg == "" - assert resp.headers.fields == [[':status', '200'], ['etag', 'foobar']] + assert resp.msg == '' + assert resp.headers.fields == [[b':status', b'200'], [b'etag', b'foobar']] assert resp.content == b'' @@ -451,53 +453,53 @@ class TestAssembleRequest(object): def test_request_simple(self): bytes = HTTP2Protocol(self.c).assemble_request(http.Request( - '', - 'GET', - 'https', - '', - '', - '/', - (2, 0), + b'', + b'GET', + b'https', + b'', + b'', + b'/', + b'2.0', None, None, )) assert len(bytes) == 1 - assert bytes[0] == '00000d0105000000018284874188089d5c0b8170dc07'.decode('hex') + assert bytes[0] == codecs.decode('00000d0105000000018284874188089d5c0b8170dc07', 'hex_codec') def test_request_with_stream_id(self): req = http.Request( - '', - 'GET', - 'https', - '', - '', - '/', - (2, 0), + b'', + b'GET', + b'https', + b'', + b'', + b'/', + b'2.0', None, None, ) req.stream_id = 0x42 bytes = HTTP2Protocol(self.c).assemble_request(req) assert len(bytes) == 1 - assert bytes[0] == '00000d0105000000428284874188089d5c0b8170dc07'.decode('hex') + assert bytes[0] == codecs.decode('00000d0105000000428284874188089d5c0b8170dc07', 'hex_codec') def test_request_with_body(self): bytes = HTTP2Protocol(self.c).assemble_request(http.Request( - '', - 'GET', - 'https', - '', - '', - '/', - (2, 0), - http.Headers([('foo', 'bar')]), - 'foobar', + b'', + b'GET', + b'https', + b'', + b'', + b'/', + b'2.0', + http.Headers([(b'foo', b'bar')]), + b'foobar', )) assert len(bytes) == 2 assert bytes[0] ==\ - '0000150104000000018284874188089d5c0b8170dc07408294e7838c767f'.decode('hex') + codecs.decode('0000150104000000018284874188089d5c0b8170dc07408294e7838c767f', 'hex_codec') assert bytes[1] ==\ - '000006000100000001666f6f626172'.decode('hex') + codecs.decode('000006000100000001666f6f626172', 'hex_codec') class TestAssembleResponse(object): @@ -505,34 +507,34 @@ class TestAssembleResponse(object): def test_simple(self): bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response( - (2, 0), + b'2.0', 200, )) assert len(bytes) == 1 assert bytes[0] ==\ - '00000101050000000288'.decode('hex') + codecs.decode('00000101050000000288', 'hex_codec') def test_with_stream_id(self): resp = http.Response( - (2, 0), + b'2.0', 200, ) resp.stream_id = 0x42 bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(resp) assert len(bytes) == 1 assert bytes[0] ==\ - '00000101050000004288'.decode('hex') + codecs.decode('00000101050000004288', 'hex_codec') def test_with_body(self): bytes = HTTP2Protocol(self.c, is_server=True).assemble_response(http.Response( - (2, 0), + b'2.0', 200, - '', - Headers(foo="bar"), - 'foobar' + b'', + http.Headers(foo=b"bar"), + b'foobar' )) assert len(bytes) == 2 assert bytes[0] ==\ - '00000901040000000288408294e7838c767f'.decode('hex') + codecs.decode('00000901040000000288408294e7838c767f', 'hex_codec') assert bytes[1] ==\ - '000006000100000002666f6f626172'.decode('hex') + codecs.decode('000006000100000002666f6f626172', 'hex_codec') diff --git a/test/http/http2/test_frames.py b/test/http/http2/test_frames.py deleted file mode 100644 index 9f41c74d..00000000 --- a/test/http/http2/test_frames.py +++ /dev/null @@ -1,669 +0,0 @@ -from io import BytesIO - -from netlib import tcp, tutils -from netlib.http.http2.frame import * - - -def hex_to_file(data): - data = data.decode('hex') - return tcp.Reader(BytesIO(data)) - - -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 a == b - - -def test_too_large_frames(): - f = DataFrame( - length=9000, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar' * 3000) - tutils.raises(HttpSyntaxException, f.to_bytes) - - -def test_data_frame_to_bytes(): - f = DataFrame( - length=6, - flags=Frame.FLAG_END_STREAM, - stream_id=0x1234567, - payload='foobar') - assert 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 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(hex_to_file('000006000101234567666f6f626172')) - assert isinstance(f, DataFrame) - assert f.length == 6 - assert f.TYPE == DataFrame.TYPE - assert f.flags == Frame.FLAG_END_STREAM - assert f.stream_id == 0x1234567 - assert f.payload == 'foobar' - - f = Frame.from_file(hex_to_file('00000a00090123456703666f6f626172000000')) - assert isinstance(f, DataFrame) - assert f.length == 10 - assert f.TYPE == DataFrame.TYPE - assert f.flags == Frame.FLAG_END_STREAM | Frame.FLAG_PADDED - assert f.stream_id == 0x1234567 - assert 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 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 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 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 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 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(hex_to_file( - '000007010001234567668594e75e31d9')) - assert isinstance(f, HeadersFrame) - assert f.length == 7 - assert f.TYPE == HeadersFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == '668594e75e31d9'.decode('hex') - - f = Frame.from_file(hex_to_file( - '00000b01080123456703668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert f.length == 11 - assert f.TYPE == HeadersFrame.TYPE - assert f.flags == HeadersFrame.FLAG_PADDED - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == '668594e75e31d9'.decode('hex') - - f = Frame.from_file(hex_to_file( - '00000c012001234567876543212a668594e75e31d9')) - assert isinstance(f, HeadersFrame) - assert f.length == 12 - assert f.TYPE == HeadersFrame.TYPE - assert f.flags == HeadersFrame.FLAG_PRIORITY - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == '668594e75e31d9'.decode('hex') - assert f.exclusive == True - assert f.stream_dependency == 0x7654321 - assert f.weight == 42 - - f = Frame.from_file(hex_to_file( - '00001001280123456703876543212a668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert f.length == 16 - assert f.TYPE == HeadersFrame.TYPE - assert f.flags == HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == '668594e75e31d9'.decode('hex') - assert f.exclusive == True - assert f.stream_dependency == 0x7654321 - assert f.weight == 42 - - f = Frame.from_file(hex_to_file( - '00001001280123456703076543212a668594e75e31d9000000')) - assert isinstance(f, HeadersFrame) - assert f.length == 16 - assert f.TYPE == HeadersFrame.TYPE - assert f.flags == HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == '668594e75e31d9'.decode('hex') - assert f.exclusive == False - assert f.stream_dependency == 0x7654321 - assert 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=0x0, - weight=42) - assert f.to_bytes().encode('hex') == '000005020001234567800000002a' - - f = PriorityFrame( - length=5, - flags=(Frame.FLAG_NO_FLAGS), - stream_id=0x1234567, - exclusive=False, - stream_dependency=0x7654321, - weight=21) - assert 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) - - -def test_priority_frame_from_bytes(): - f = Frame.from_file(hex_to_file('000005020001234567876543212a')) - assert isinstance(f, PriorityFrame) - assert f.length == 5 - assert f.TYPE == PriorityFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x1234567 - assert f.exclusive == True - assert f.stream_dependency == 0x7654321 - assert f.weight == 42 - - f = Frame.from_file(hex_to_file('0000050200012345670765432115')) - assert isinstance(f, PriorityFrame) - assert f.length == 5 - assert f.TYPE == PriorityFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x1234567 - assert f.exclusive == False - assert f.stream_dependency == 0x7654321 - assert 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 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(hex_to_file('00000403000123456707654321')) - assert isinstance(f, RstStreamFrame) - assert f.length == 4 - assert f.TYPE == RstStreamFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x1234567 - assert 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 f.to_bytes().encode('hex') == '000000040000000000' - - f = SettingsFrame( - length=0, - flags=SettingsFrame.FLAG_ACK, - stream_id=0x0) - assert 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 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 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(hex_to_file('000000040000000000')) - assert isinstance(f, SettingsFrame) - assert f.length == 0 - assert f.TYPE == SettingsFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - - f = Frame.from_file(hex_to_file('000000040100000000')) - assert isinstance(f, SettingsFrame) - assert f.length == 0 - assert f.TYPE == SettingsFrame.TYPE - assert f.flags == SettingsFrame.FLAG_ACK - assert f.stream_id == 0x0 - - f = Frame.from_file(hex_to_file('000006040100000000000200000001')) - assert isinstance(f, SettingsFrame) - assert f.length == 6 - assert f.TYPE == SettingsFrame.TYPE - assert f.flags == SettingsFrame.FLAG_ACK, 0x0 - assert f.stream_id == 0x0 - assert len(f.settings) == 1 - assert f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 1 - - f = Frame.from_file(hex_to_file( - '00000c040000000000000200000001000312345678')) - assert isinstance(f, SettingsFrame) - assert f.length == 12 - assert f.TYPE == SettingsFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - assert len(f.settings) == 2 - assert f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 1 - assert 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 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 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(hex_to_file('00000a05000123456707654321666f6f626172')) - assert isinstance(f, PushPromiseFrame) - assert f.length == 10 - assert f.TYPE == PushPromiseFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x1234567 - assert f.header_block_fragment == 'foobar' - - f = Frame.from_file(hex_to_file( - '00000e0508012345670307654321666f6f626172000000')) - assert isinstance(f, PushPromiseFrame) - assert f.length == 14 - assert f.TYPE == PushPromiseFrame.TYPE - assert f.flags == PushPromiseFrame.FLAG_PADDED - assert f.stream_id == 0x1234567 - assert 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 f.to_bytes().encode('hex') == '000008060100000000666f6f6261720000' - - f = PingFrame( - length=8, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x0, - payload=b'foobardeadbeef') - assert 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(hex_to_file('000008060100000000666f6f6261720000')) - assert isinstance(f, PingFrame) - assert f.length == 8 - assert f.TYPE == PingFrame.TYPE - assert f.flags == PingFrame.FLAG_ACK - assert f.stream_id == 0x0 - assert f.payload == b'foobar\0\0' - - f = Frame.from_file(hex_to_file('000008060000000000666f6f6261726465')) - assert isinstance(f, PingFrame) - assert f.length == 8 - assert f.TYPE == PingFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - assert 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 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 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(hex_to_file( - '0000080700000000000123456787654321')) - assert isinstance(f, GoAwayFrame) - assert f.length == 8 - assert f.TYPE == GoAwayFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - assert f.last_stream == 0x1234567 - assert f.error_code == 0x87654321 - assert f.data == b'' - - f = Frame.from_file(hex_to_file( - '00000e0700000000000123456787654321666f6f626172')) - assert isinstance(f, GoAwayFrame) - assert f.length == 14 - assert f.TYPE == GoAwayFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - assert f.last_stream == 0x1234567 - assert f.error_code == 0x87654321 - assert 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 f.to_bytes().encode('hex') == '00000408000000000001234567' - - f = WindowUpdateFrame( - length=4, - flags=Frame.FLAG_NO_FLAGS, - stream_id=0x1234567, - window_size_increment=0x7654321) - assert 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(hex_to_file('00000408000000000001234567')) - assert isinstance(f, WindowUpdateFrame) - assert f.length == 4 - assert f.TYPE == WindowUpdateFrame.TYPE - assert f.flags == Frame.FLAG_NO_FLAGS - assert f.stream_id == 0x0 - assert 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 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(hex_to_file('000006090401234567666f6f626172')) - assert isinstance(f, ContinuationFrame) - assert f.length == 6 - assert f.TYPE == ContinuationFrame.TYPE - assert f.flags == ContinuationFrame.FLAG_END_HEADERS - assert f.stream_id == 0x1234567 - assert 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() |