示例#1
0
    def test_should_set_message_info_field_when_to_bytes_method_is_called(
            self):
        # GIVEN
        ipv6_packet_data = bytearray([
            0x60, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x3A, 0x40, 0xfd, 0x00, 0x12,
            0x34, 0x45, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00,
            0x18, 0x00, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x87, 0x12,
            0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0xc7, 0xbf, 0x00, 0x00, 0x00,
            0x01, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
        ])

        message_info = any_message_info()
        message_info.source_ipv6 = "ff::"
        message_info.destination_address = "ff::"

        factory = IPv6PacketFactory(
            ulpf={
                58: ICMPv6Factory(
                    body_factories={128: ICMPv6EchoBodyFactory()})
            })

        # WHEN
        factory.parse(io.BytesIO(ipv6_packet_data), message_info)

        # THEN
        self.assertEqual("fd00:1234:4555::ff:fe00:1800",
                         message_info.source_ipv6.compressed)
        self.assertEqual("ff03::1", message_info.destination_ipv6.compressed)
示例#2
0
    def test_should_create_IPv6Packet_with_MPL_extension_header_and_ICMP_upper_layer_protocol_when_to_bytes_method_is_called(self):
        # GIVEN
        ipv6_packet_bytes = bytearray([0x60, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x40,
                                       0xfd, 0x00, 0x12, 0x34, 0x45, 0x55, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x18, 0x00,
                                       0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
                                       0x3a, 0x00, 0x6d, 0x04, 0x40, 0x02, 0x00, 0x18,
                                       0x80, 0x00, 0x87, 0x12, 0x00, 0x00, 0x00, 0x02,
                                       0x80, 0x00, 0xc7, 0xbf, 0x00, 0x00, 0x00, 0x01,
                                       0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
                                       0x41, 0x41])

        ipv6_factory = IPv6PacketFactory(
            ehf={
                0: HopByHopFactory(
                    hop_by_hop_options_factory=HopByHopOptionsFactory(
                        options_factories={
                            109: MPLOptionFactory()
                        }
                    )
                )
            },
            ulpf={
                58: ICMPv6Factory(body_factories={
                    128: ICMPv6EchoBodyFactory()
                })
            })

        # WHEN
        ipv6_packet = ipv6_factory.parse(io.BytesIO(ipv6_packet_bytes), any_message_info())

        # THEN
        self.assertEqual('fd00:1234:4555::ff:fe00:1800',  ipv6_packet.ipv6_header.source_address.compressed)
        self.assertEqual('ff03::1', ipv6_packet.ipv6_header.destination_address.compressed)
        self.assertEqual(64, ipv6_packet.ipv6_header.hop_limit)
        self.assertEqual(0, ipv6_packet.ipv6_header.next_header)
        self.assertEqual(34, ipv6_packet.ipv6_header.payload_length)
        self.assertEqual(0, ipv6_packet.ipv6_header.flow_label)
        self.assertEqual(0, ipv6_packet.ipv6_header.traffic_class)
        self.assertEqual(6, ipv6_packet.ipv6_header.version)

        self.assertEqual(1, ipv6_packet.extension_headers[0].options[0].value.S)
        self.assertEqual(0, ipv6_packet.extension_headers[0].options[0].value.M)
        self.assertEqual(0, ipv6_packet.extension_headers[0].options[0].value.V)
        self.assertEqual(2, ipv6_packet.extension_headers[0].options[0].value.sequence)
        self.assertEqual(bytearray([0x00, 0x18]), ipv6_packet.extension_headers[0].options[0].value.seed_id)

        self.assertEqual(34578, ipv6_packet.upper_layer_protocol.header.checksum)
        self.assertEqual(128, ipv6_packet.upper_layer_protocol.header.type)
        self.assertEqual(0, ipv6_packet.upper_layer_protocol.header.code)
        self.assertEqual(0, ipv6_packet.upper_layer_protocol.body.identifier)
        self.assertEqual(2, ipv6_packet.upper_layer_protocol.body.sequence_number)
        self.assertEqual(b'\x80\x00\xc7\xbf\x00\x00\x00\x01AAAAAAAAAA', ipv6_packet.upper_layer_protocol.body.data)
示例#3
0
    def test_should_raise_RuntimeError_when_method_parse_is_called_but_body_factory_is_not_present(self):
        # GIVEN
        _type = any_type()
        code = any_code()
        checksum = any_checksum()
        body = any_body()

        data = bytes([_type, code, (checksum >> 8), (checksum & 0xFF)]) + body

        factory = ICMPv6Factory()

        # WHEN
        self.assertRaises(RuntimeError, factory.parse, io.BytesIO(data), any_message_info())
示例#4
0
    def test_should_create_IPv6Packet_without_any_extension_header_with_ICMP_when_to_bytes_method_is_called(
            self):
        # GIVEN
        ipv6_packet_bytes = bytearray([
            0x60, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x3A, 0x40, 0xfd, 0x00, 0x12,
            0x34, 0x45, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00,
            0x18, 0x00, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x87, 0x12,
            0x00, 0x00, 0x00, 0x02, 0x80, 0x00, 0xc7, 0xbf, 0x00, 0x00, 0x00,
            0x01, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41
        ])

        ipv6_factory = IPv6PacketFactory(
            ulpf={
                58: ICMPv6Factory(
                    body_factories={128: ICMPv6EchoBodyFactory()})
            })

        # WHEN
        ipv6_packet = ipv6_factory.parse(io.BytesIO(ipv6_packet_bytes),
                                         any_message_info())
        ipv6_packet._validate_checksum()

        # THEN
        self.assertEqual('fd00:1234:4555::ff:fe00:1800',
                         ipv6_packet.ipv6_header.source_address.compressed)
        self.assertEqual(
            'ff03::1', ipv6_packet.ipv6_header.destination_address.compressed)
        self.assertEqual(64, ipv6_packet.ipv6_header.hop_limit)
        self.assertEqual(58, ipv6_packet.ipv6_header.next_header)
        self.assertEqual(26, ipv6_packet.ipv6_header.payload_length)
        self.assertEqual(0, ipv6_packet.ipv6_header.flow_label)
        self.assertEqual(0, ipv6_packet.ipv6_header.traffic_class)
        self.assertEqual(6, ipv6_packet.ipv6_header.version)

        self.assertEqual(34578,
                         ipv6_packet.upper_layer_protocol.header.checksum)
        self.assertEqual(128, ipv6_packet.upper_layer_protocol.header.type)
        self.assertEqual(0, ipv6_packet.upper_layer_protocol.header.code)
        self.assertEqual(0, ipv6_packet.upper_layer_protocol.body.identifier)
        self.assertEqual(2,
                         ipv6_packet.upper_layer_protocol.body.sequence_number)
        self.assertEqual(b'\x80\x00\xc7\xbf\x00\x00\x00\x01AAAAAAAAAA',
                         ipv6_packet.upper_layer_protocol.body.data)
示例#5
0
    def test_should_produce_ICMPv6_from_bytes_when_to_bytes_method_is_called_with_data(self):
        # GIVEN
        _type = any_type()
        code = any_code()
        checksum = any_checksum()
        body = any_body()

        data = bytearray([_type, code, (checksum >> 8), (checksum & 0xFF)]) + body

        factory = ICMPv6Factory(body_factories={_type: ICMPv6BytesBodyFactory()})

        # WHEN
        icmpv6_msg = factory.parse(io.BytesIO(data), any_message_info())

        # THEN
        self.assertEqual(icmpv6_msg.header.type, _type)
        self.assertEqual(icmpv6_msg.header.code, code)
        self.assertEqual(icmpv6_msg.header.checksum, checksum)
        self.assertEqual(icmpv6_msg.body.bytes, body)