示例#1
0
def test_encode_decode_sync_checksum_mode_disabled():

    encoded = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    encoded_bytes = bytes(encoded)

    decoded = Packet.from_bytes(encoded_bytes)
    decoded_bytes = decoded.as_bytes

    assert encoded_bytes == decoded_bytes
示例#2
0
def test_is_duplicate_ack_false():

    data = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    packet = Packet.ack(base_packet=data, )

    assert not packet.is_duplicate
示例#3
0
def test_message_type_ack():

    data = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    packet = Packet.ack(base_packet=data, )

    assert packet.message_type is MessageType.Ack
示例#4
0
def test_message_id_and_data_length_ack():

    data = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=12345,
        payload_data=b'dummy',
    )

    packet = Packet.ack(base_packet=data, )

    assert packet.message_id == 12345
    assert packet.message_data_length == 0
示例#5
0
def test_decode_invalid_packet_checksum():

    encoded = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckLastFragmentOnly,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    encoded._checksum = 12345

    with pytest.raises(ValueError):
        Packet.from_bytes(bytes(encoded))
示例#6
0
def test_invalid_ack():

    data = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    packet = Packet.ack(base_packet=data, )

    with pytest.raises(ValueError):
        Packet.ack(base_packet=packet, )
示例#7
0
def test_encode_decode_data_ack_last_fragment_only():

    encoded = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckLastFragmentOnly,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    encoded_bytes = bytes(encoded)

    decoded = Packet.from_bytes(encoded_bytes)
    decoded_bytes = decoded.as_bytes

    assert encoded_bytes == decoded_bytes
示例#8
0
def test_encode_decode_data():

    encoded = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=10,
        fragment_number=5,
        message_id=12345,
        payload_data=b'dummy',
    )

    encoded_bytes = bytes(encoded)

    decoded = Packet.from_bytes(encoded_bytes)
    decoded_bytes = decoded.as_bytes

    assert encoded_bytes == decoded_bytes
示例#9
0
def test_fragment_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.fragment_amount == 1
    assert packet.fragment_number == 0

    assert packet.is_single
    assert packet.is_last
示例#10
0
def test_fragment_ack_multiple_last():

    data = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=10,
        fragment_number=9,
        message_id=1,
        payload_data=b'dummy',
    )

    packet = Packet.ack(base_packet=data, )

    assert packet.fragment_amount == 10
    assert packet.fragment_number == 9

    assert not packet.is_single
    assert packet.is_last
示例#11
0
def test_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.is_sync
    assert packet.type == 'sync'

    assert not packet.is_ack
    assert not packet.is_data
示例#12
0
def test_transfer_mode_data_none():

    packet = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckNone,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    assert packet.transfer_mode is TransferMode.AckNone
    assert not packet.is_ack_needed
示例#13
0
def test_transfer_mode_data_last_fragment_only_when_ack_not_needed():

    packet = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckLastFragmentOnly,
        fragment_amount=10,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    assert packet.transfer_mode is TransferMode.AckLastFragmentOnly
    assert not packet.is_ack_needed
示例#14
0
def test_fragment_data_single():

    packet = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    assert packet.fragment_amount == 1
    assert packet.fragment_number == 0

    assert packet.is_single
    assert packet.is_last
示例#15
0
def test_data():

    packet = Packet.data(
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        fragment_amount=1,
        fragment_number=0,
        message_id=1,
        payload_data=b'dummy',
    )

    assert packet.is_data
    assert packet.type == 'data'

    assert not packet.is_ack
    assert not packet.is_sync
示例#16
0
def test_invalid():

    packet = Packet(
        message_type=MessageType.Ack,
        checksum_mode=ChecksumMode.Disabled,
        transfer_mode=TransferMode.AckEveryPacket,
        is_duplicate=True,
        fragment_amount=1,
        fragment_number=0,
        message_id=0,
        message_data_length=0,
        payload_data=b'dummy',
    )

    assert packet.type == 'invalid'

    assert not packet.is_ack
    assert not packet.is_sync
    assert not packet.is_data
示例#17
0
def test_checksum_mode_enabled():

    packet = Packet.sync(checksum_mode=ChecksumMode.Enabled, )

    assert packet.checksum == 0x2A20054
示例#18
0
def test_decode_invalid_packet_protocol_version():

    with pytest.raises(ValueError):
        Packet.from_bytes(b'000000000000')
示例#19
0
def test_decode_invalid_data_length():

    with pytest.raises(ValueError):
        Packet.from_bytes(b'dummy')
示例#20
0
def test_checksum_mode_disabled():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.checksum == 0
示例#21
0
def test_is_duplicate_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert not packet.is_duplicate
示例#22
0
def test_transfer_mode_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.transfer_mode is TransferMode.AckEveryPacket
    assert packet.is_ack_needed
示例#23
0
def test_message_id_and_data_length_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.message_id == 0
    assert packet.message_data_length == 0
示例#24
0
def test_message_type_sync():

    packet = Packet.sync(checksum_mode=ChecksumMode.Disabled, )

    assert packet.message_type is MessageType.Data