Exemple #1
0
def test_remain():
    mux = Multiplexer()
    p0, p1, p2 = 0, 1, 2
    mux.add_protocol(p0)
    mux.add_protocol(p1)
    mux.add_protocol(p2)

    # test buffer remains, incomplete frames
    packet1 = Packet(p1, cmd_id=0, payload='\x00' * 100)
    mux.add_packet(packet1)
    message = mux.pop_all_frames_as_bytes()
    tail = message[:50]
    message += tail
    packets = mux.decode(message)
    assert packets[0] == packet1
    assert len(packets) == 1
    assert len(mux._decode_buffer) == len(tail)

    # test buffer decode with invalid data
    message = message[1:]
    exception_raised = False
    try:
        packets = mux.decode(message)
    except DeserializationError:
        exception_raised = True
    assert exception_raised
Exemple #2
0
def test_chunked():
    mux = Multiplexer()
    p0, p1, p2 = 0, 1, 2
    mux.add_protocol(p0)
    mux.add_protocol(p1)
    mux.add_protocol(p2)

    # big packet
    print 'size', mux.max_window_size * 2
    packet1 = Packet(p1, cmd_id=0, payload='\x00' * mux.max_window_size * 2 + 'x')
    mux.add_packet(packet1)
    frames = mux.pop_all_frames()
    all_frames_length = sum(f.frame_size() for f in frames)
    assert sum(len(f.payload) for f in frames) == len(packet1.payload)
    for i, f, in enumerate(frames):
        print i, f.frame_size()
        print 'frame payload', len(f.payload)
        print f._frame_type()
    mux.add_packet(packet1)
    message = mux.pop_all_frames_as_bytes()
    assert len(message) == all_frames_length
    packets = mux.decode(message)
    assert len(mux._decode_buffer) == 0
    assert packets[0].payload == packet1.payload
    assert packets[0] == packet1
    assert len(packets) == 1
Exemple #3
0
def test_multiplexing():
    initiator, responder = test_session()
    imux = Multiplexer(frame_cipher=initiator)
    rmux = Multiplexer(frame_cipher=responder)
    p1 = 1
    imux.add_protocol(p1)
    rmux.add_protocol(p1)

    packet1 = Packet(p1, cmd_id=0, payload='\x00' * 100)
    imux.add_packet(packet1)
    msg = imux.pop_all_frames_as_bytes()
    packets = rmux.decode(msg)
    assert len(packets) == 1
    assert packet1 == packets[0]
def test_multiplexing():
    initiator, responder = test_session()
    imux = Multiplexer(frame_cipher=initiator)
    rmux = Multiplexer(frame_cipher=responder)
    p1 = 1
    imux.add_protocol(p1)
    rmux.add_protocol(p1)

    packet1 = Packet(p1, cmd_id=0, payload='\x00' * 100)
    imux.add_packet(packet1)
    msg = imux.pop_all_frames_as_bytes()
    packets = rmux.decode(msg)
    assert len(packets) == 1
    assert packet1 == packets[0]
Exemple #5
0
def test_multiplexer():
    mux = Multiplexer()
    p0, p1, p2 = 0, 1, 2
    mux.add_protocol(p0)
    mux.add_protocol(p1)
    mux.add_protocol(p2)

    assert mux.next_protocol == p0
    assert mux.next_protocol == p1
    assert mux.next_protocol == p2
    assert mux.next_protocol == p0

    assert mux.pop_frames() == []
    assert mux.num_active_protocols == 0

    # test normal packet
    packet0 = Packet(p0, cmd_id=0, payload='x' * 100)

    mux.add_packet(packet0)
    assert mux.num_active_protocols == 1

    frames = mux.pop_frames()
    assert len(frames) == 1
    f = frames[0]
    assert len(f.as_bytes()) == f.frame_size()

    mux.add_packet(packet0)
    assert mux.num_active_protocols == 1
    message = mux.pop_all_frames_as_bytes()
    packets = mux.decode(message)
    assert len(packets[0].payload) == len(packet0.payload)
    assert packets[0].payload == packet0.payload
    assert packets[0] == packet0

    # nothing left to pop
    assert len(mux.pop_frames()) == 0

    # big packet
    packet1 = Packet(p1, cmd_id=0, payload='\x00' * mux.max_window_size * 2)
    mux.add_packet(packet1)

    # decode packets from buffer
    message = mux.pop_all_frames_as_bytes()
    packets = mux.decode(message)
    assert packets[0].payload == packet1.payload
    assert packets[0] == packet1
    assert len(packets) == 1

    # mix packet types
    packet2 = Packet(p0, cmd_id=0, payload='\x00' * 200, prioritize=True)
    mux.add_packet(packet1)
    mux.add_packet(packet0)
    mux.add_packet(packet2)
    message = mux.pop_all_frames_as_bytes()
    packets = mux.decode(message)
    assert packets == [packet2, packet0, packet1]

    # packets with different protocols
    packet3 = Packet(p1, cmd_id=0, payload='\x00' * 3000, prioritize=False)
    mux.add_packet(packet1)
    mux.add_packet(packet0)
    mux.add_packet(packet2)
    mux.add_packet(packet3)
    mux.add_packet(packet3)
    mux.add_packet(packet3)
    assert mux.next_protocol == p0
    # thus next with data is p1 w/ packet3
    message = mux.pop_all_frames_as_bytes()
    packets = mux.decode(message)
    assert packets == [packet3, packet2, packet0, packet3, packet3, packet1]

    # test buffer remains, incomplete frames
    packet1 = Packet(p1, cmd_id=0, payload='\x00' * 100)
    mux.add_packet(packet1)
    message = mux.pop_all_frames_as_bytes()
    tail = message[:50]
    message += tail
    packets = mux.decode(message)
    assert packets[0] == packet1
    assert len(packets) == 1
    assert len(mux._decode_buffer) == len(tail)