Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
def test_invalid_flags():
    tutils.raises(
        ValueError,
        DataFrame,
        flags=ContinuationFrame.FLAG_END_HEADERS,
        stream_id=0x1234567,
        payload='foobar')
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
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)
Пример #9
0
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)
Пример #10
0
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)
Пример #11
0
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)
Пример #12
0
 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)
Пример #13
0
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)