Esempio n. 1
0
def test_ipv4_parsing_ipv4_option_value(ipv4_option_value: pyrflx.MessageValue) -> None:
    expected = b"\x44\x03\x2a"
    ipv4_option_value.parse(expected)
    assert ipv4_option_value.get("Copied") == "False"
    assert ipv4_option_value.get("Option_Class") == "IPv4::Debugging_And_Measurement"
    assert ipv4_option_value.get("Option_Number") == 4
    assert ipv4_option_value.get("Option_Length") == 3
    ip_option = ipv4_option_value.get("Option_Data")
    assert isinstance(ip_option, bytes)
    assert len(ip_option) == 1
Esempio n. 2
0
def test_tlv_value(tlv_checksum: MessageValue) -> None:
    v1 = b"\x01\x02\x03\x04\x05\x06\x07\x08"
    tlv_checksum.set("Tag", "Msg_Data")
    tlv_checksum.set("Length", 8)
    tlv_checksum.set("Value", v1)
    tlv_checksum.set("Checksum", 2 ** 32 - 1)
    assert tlv_checksum.get("Tag") == "Msg_Data"
    assert tlv_checksum.get("Length") == 8
    assert tlv_checksum.get("Value") == v1
    assert tlv_checksum.get("Checksum") == 0xFFFFFFFF
Esempio n. 3
0
def test_ethernet_parsing_ethernet_2(ethernet_frame_value: pyrflx.MessageValue) -> None:
    with open(CAPTURED_DIR / "ethernet_ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    ethernet_frame_value.parse(msg_as_bytes)
    assert ethernet_frame_value.get("Destination") == int("ffffffffffff", 16)
    assert ethernet_frame_value.get("Source") == int("0", 16)
    assert ethernet_frame_value.get("Type_Length_TPID") == int("0800", 16)
    k = ethernet_frame_value._fields["Payload"].typeval.size
    assert isinstance(k, expr.Number)
    assert k.value // 8 == 46
    assert ethernet_frame_value.valid_message
    assert ethernet_frame_value.bytestring == msg_as_bytes
Esempio n. 4
0
def test_ethernet_parsing_ethernet_2(frame: MessageValue) -> None:
    with open("tests/ethernet_ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    frame.parse(msg_as_bytes)
    assert frame.get("Destination") == int("ffffffffffff", 16)
    assert frame.get("Source") == int("0", 16)
    assert frame.get("Type_Length_TPID") == int("0800", 16)
    k = frame._fields["Payload"].typeval.size
    assert isinstance(k, Number)
    assert k.value // 8 == 46
    assert frame.valid_message
    assert frame.bytestring == msg_as_bytes
Esempio n. 5
0
def test_ethernet_parsing_incomplete(ethernet_frame_value: pyrflx.MessageValue) -> None:
    test_bytes = b"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x02"
    with pytest.raises(
        PyRFLXError,
        match="^pyrflx: error: Bitstring representing the message is too short"
        " - stopped while parsing field: Type_Length_TPID$",
    ):
        ethernet_frame_value.parse(test_bytes)

    assert ethernet_frame_value.get("Destination") == int("000000000001", 16)
    assert ethernet_frame_value.get("Source") == int("000000000002", 16)
    assert len(ethernet_frame_value.valid_fields) == 2
    assert not ethernet_frame_value.valid_message
Esempio n. 6
0
def test_ethernet_parsing_incomplete(frame: MessageValue) -> None:
    test_bytes = b"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x02"
    with pytest.raises(
        IndexError,
        match="Bitstring representing the message is too short"
        " - stopped while parsing field: Type_Length_TPID",
    ):
        frame.parse(test_bytes)

    assert frame.get("Destination") == int("000000000001", 16)
    assert frame.get("Source") == int("000000000002", 16)
    assert len(frame.valid_fields) == 2
    assert not frame.valid_message
Esempio n. 7
0
def test_no_verification_icmp_checksum(
        icmp_checksum_message_value: MessageValue,
        icmp_message: Message) -> None:
    test_data = (
        b"\x47\xb4\x67\x5e\x00\x00\x00\x00"
        b"\x4a\xfc\x0d\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17"
        b"\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27"
        b"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37")
    icmp_checksum_unv = MessageValue(
        icmp_message.copy(
            structure=[
                Link(l.source,
                     l.target,
                     condition=And(l.condition, ValidChecksum("Checksum")))
                if l.target == FINAL else l for l in icmp_message.structure
            ],
            checksums={
                ID("Checksum"): [
                    ValueRange(First("Tag"), Sub(First("Checksum"),
                                                 Number(1))),
                    Size("Checksum"),
                    ValueRange(Add(Last("Checksum"), Number(1)),
                               Last("Message")),
                ]
            },
        ),
        skip_verification=True,
    )
    icmp_checksum_message_value.set_checksum_function(
        {"Checksum": icmp_checksum_function})
    icmp_checksum_message_value.set("Tag", "Echo_Request")
    icmp_checksum_message_value.set("Code_Zero", 0)
    icmp_checksum_message_value.set("Identifier", 5)
    icmp_checksum_message_value.set("Sequence_Number", 1)
    icmp_checksum_message_value.set("Data", test_data)
    icmp_checksum_unv.set_checksum_function(
        {"Checksum": icmp_checksum_function})
    icmp_checksum_unv.set("Tag", "Echo_Request")
    icmp_checksum_unv.set("Code_Zero", 0)
    icmp_checksum_unv.set("Checksum", 0)
    icmp_checksum_unv.set("Identifier", 5)
    icmp_checksum_unv.set("Sequence_Number", 1)
    icmp_checksum_unv.set("Data", test_data)
    icmp_checksum_unv.update_checksums()
    assert icmp_checksum_unv.valid_message
    assert icmp_checksum_unv.get(
        "Checksum") == icmp_checksum_message_value.get("Checksum")
    assert icmp_checksum_unv.bytestring == icmp_checksum_message_value.bytestring
Esempio n. 8
0
def test_in_ipv4_parsing_udp_in_ipv4(ipv4_packet_value: pyrflx.MessageValue) -> None:
    with open(CAPTURED_DIR / "ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    ipv4_packet_value.parse(msg_as_bytes)
    nested_udp = ipv4_packet_value.get("Payload")
    assert isinstance(nested_udp, pyrflx.MessageValue)
    assert nested_udp.valid_message
Esempio n. 9
0
def test_ipv4_parsing_udp_in_ipv4(ipv4: MessageValue) -> None:
    with open("tests/ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    ipv4.parse(msg_as_bytes)
    nested_udp = ipv4.get("Payload")
    assert isinstance(nested_udp, MessageValue)
    assert nested_udp.valid_message
Esempio n. 10
0
 def assert_string(message: MessageValue,
                   encoding: str,
                   expected: str,
                   prefix: str = "") -> None:
     value = message.get(f"{prefix}Value")
     assert isinstance(value, bytes)
     assert value == expected.encode(encoding)
Esempio n. 11
0
def test_in_ipv4_parsing_udp_in_ipv4_in_ethernet(ethernet_frame_value: pyrflx.MessageValue) -> None:
    with open(CAPTURED_DIR / "ethernet_ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    ethernet_frame_value.parse(msg_as_bytes)
    nested_ipv4 = ethernet_frame_value.get("Payload")
    assert isinstance(nested_ipv4, pyrflx.MessageValue)
    assert nested_ipv4.valid_message
    assert nested_ipv4.identifier == ID("IPv4") * "Packet"
    nested_udp = nested_ipv4.get("Payload")
    assert isinstance(nested_udp, pyrflx.MessageValue)
    assert nested_udp.valid_message
    assert nested_udp.identifier == ID("UDP") * "Datagram"
Esempio n. 12
0
def test_ipv4_parsing_udp_in_ipv4_in_ethernet(frame: MessageValue) -> None:
    with open("tests/ethernet_ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    frame.parse(msg_as_bytes)
    nested_ipv4 = frame.get("Payload")
    assert isinstance(nested_ipv4, MessageValue)
    assert nested_ipv4.valid_message
    assert nested_ipv4.identifier == ID("IPv4.Packet")
    nested_udp = nested_ipv4.get("Payload")
    assert isinstance(nested_udp, MessageValue)
    assert nested_udp.valid_message
    assert nested_udp.identifier == ID("UDP.Datagram")
Esempio n. 13
0
def test_ipv4_parsing_ipv4(ipv4_packet_value: pyrflx.MessageValue) -> None:
    with open(CAPTURED_DIR / "ipv4_udp.raw", "rb") as file:
        msg_as_bytes: bytes = file.read()
    ipv4_packet_value.parse(msg_as_bytes)
    assert ipv4_packet_value.get("Version") == 4
    assert ipv4_packet_value.get("IHL") == 5
    assert ipv4_packet_value.get("DSCP") == 0
    assert ipv4_packet_value.get("ECN") == 0
    assert ipv4_packet_value.get("Total_Length") == 44
    assert ipv4_packet_value.get("Identification") == 1
    assert ipv4_packet_value.get("Flag_R") == "False"
    assert ipv4_packet_value.get("Flag_DF") == "False"
    assert ipv4_packet_value.get("Flag_MF") == "False"
    assert ipv4_packet_value.get("Fragment_Offset") == 0
    assert ipv4_packet_value.get("TTL") == 64
    assert ipv4_packet_value.get("Protocol") == "IPv4::P_UDP"
    assert ipv4_packet_value.get("Header_Checksum") == int("7CBE", 16)
    assert ipv4_packet_value.get("Source") == int("7f000001", 16)
    assert ipv4_packet_value.get("Destination") == int("7f000001", 16)
    assert ipv4_packet_value._fields["Payload"].typeval.size == expr.Number(192)
Esempio n. 14
0
def test_tlv_parsing_tlv_error(tlv: MessageValue) -> None:
    test_bytes = b"\xc0"
    tlv.parse(test_bytes)
    assert tlv.get("Tag") == "Msg_Error"
    assert tlv.valid_message
Esempio n. 15
0
def test_tlv_parsing_tlv_data_zero(tlv: MessageValue) -> None:
    test_bytes = b"\x40\x00"
    tlv.parse(test_bytes)
    assert tlv.get("Tag") == "Msg_Data"
    assert tlv.get("Length") == 0
    assert tlv.valid_message
Esempio n. 16
0
def test_tlv_parsing_tlv_data_zero(tlv_message_value: pyrflx.MessageValue) -> None:
    test_bytes = b"\x01\x00\x00"
    tlv_message_value.parse(test_bytes)
    assert tlv_message_value.get("Tag") == "TLV::Msg_Data"
    assert tlv_message_value.get("Length") == 0
    assert tlv_message_value.valid_message
Esempio n. 17
0
def test_tlv_parsing_tlv_error(tlv_message_value: pyrflx.MessageValue) -> None:
    test_bytes = b"\x03"
    tlv_message_value.parse(test_bytes)
    assert tlv_message_value.get("Tag") == "TLV::Msg_Error"
    assert tlv_message_value.valid_message
Esempio n. 18
0
def test_tlv_get_invalid_field(tlv_checksum: MessageValue) -> None:
    with pytest.raises(ValueError, match=r"field nofield not valid"):
        tlv_checksum.get("nofield")