示例#1
0
    def test_should_build_IPv6Packet_with_UDP_payload_from_well_know_values_when_to_bytes_method_is_called(
            self):
        # GIVEN
        ipv6_header = IPv6Header(source_address="fe80::1",
                                 destination_address="ff02::2",
                                 hop_limit=255)

        udp_dgram = UDPDatagram(
            UDPHeader(src_port=19788, dst_port=19788),
            BytesPayload(
                bytearray([
                    0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x01, 0x09, 0x01, 0x01, 0x0b, 0x03, 0x04, 0xc6, 0x69, 0x73,
                    0x51, 0x0e, 0x01, 0x80, 0x12, 0x02, 0x00, 0x01, 0xde, 0xad,
                    0xbe, 0xef
                ])))

        ipv6_packet = IPv6Packet(ipv6_header, udp_dgram)

        # WHEN
        ipv6_packet_bytes = ipv6_packet.to_bytes()

        # THEN
        expected_ipv6_packet_bytes = bytearray([
            0x60, 0x00, 0x00, 0x00, 0x00, 0x28, 0x11, 0xff, 0xfe, 0x80, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x4d, 0x4c, 0x4d, 0x4c,
            0x00, 0x28, 0xe9, 0xf4, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x01, 0x09, 0x01, 0x01, 0x0b, 0x03, 0x04, 0xc6,
            0x69, 0x73, 0x51, 0x0e, 0x01, 0x80, 0x12, 0x02, 0x00, 0x01, 0xde,
            0xad, 0xbe, 0xef
        ])

        self.assertEqual(expected_ipv6_packet_bytes, ipv6_packet_bytes)
示例#2
0
    def test_should_set_src_and_dst_port_in_message_info_when_parse_method_is_called(
            self):
        # GIVEN
        message_info = any_message_info()

        src_port = any_port()
        dst_port = any_port()
        checksum = any_checksum()

        payload = any_payload()
        payload_length = len(payload) + len(UDPHeader(0, 0))

        data = bytearray([(src_port >> 8), (src_port & 0xFF), (dst_port >> 8),
                          (dst_port & 0xFF), (payload_length >> 8),
                          (payload_length & 0xFF), (checksum >> 8),
                          (checksum & 0xFF)]) + payload

        factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory())

        # WHEN
        udp_dgram = factory.parse(io.BytesIO(data), message_info)

        # THEN
        self.assertEqual(src_port, message_info.src_port)
        self.assertEqual(dst_port, message_info.dst_port)
示例#3
0
    def test_should_return_17_when_type_property_is_called(self):
        # GIVEN
        udp_header = UDPHeader(any_port(), any_port(), any_payload_length(),
                               any_checksum())

        # THEN
        self.assertEqual(17, udp_header.type)
示例#4
0
    def test_should_produce_UDPDatagram_from_bytes_when_to_bytes_method_is_called_with_data(
            self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        checksum = any_checksum()

        payload = any_payload()
        payload_length = len(payload) + len(UDPHeader(0, 0))

        data = bytearray([(src_port >> 8), (src_port & 0xFF), (dst_port >> 8),
                          (dst_port & 0xFF), (payload_length >> 8),
                          (payload_length & 0xFF), (checksum >> 8),
                          (checksum & 0xFF)]) + payload

        factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory())

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

        # THEN
        self.assertEqual(udp_dgram.header.src_port, src_port)
        self.assertEqual(udp_dgram.header.dst_port, dst_port)
        self.assertEqual(udp_dgram.header.payload_length, payload_length)
        self.assertEqual(udp_dgram.header.checksum, checksum)
        self.assertEqual(udp_dgram.payload.data, payload)
示例#5
0
    def test_should_convert_UDP_header_to_bytes_when_to_bytes_method_is_called(self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        payload_length = any_payload_length()
        checksum = any_checksum()

        udp_header = UDPHeader(src_port, dst_port, payload_length, checksum)

        # WHEN
        data = udp_header.to_bytes()

        # THEN
        self.assertEqual(src_port, struct.unpack("!H", data[0:2])[0])
        self.assertEqual(dst_port, struct.unpack("!H", data[2:4])[0])
        self.assertEqual(payload_length, struct.unpack("!H", data[4:6])[0])
        self.assertEqual(checksum, struct.unpack("!H", data[6:])[0])
示例#6
0
    def test_should_convert_UDP_header_to_bytes_when_to_bytes_method_is_called(self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        payload_length = any_payload_length()
        checksum = any_checksum()

        udp_header = UDPHeader(src_port, dst_port, payload_length, checksum)

        # WHEN
        data = udp_header.to_bytes()

        # THEN
        self.assertEqual(src_port, struct.unpack("!H", data[0:2])[0])
        self.assertEqual(dst_port, struct.unpack("!H", data[2:4])[0])
        self.assertEqual(payload_length, struct.unpack("!H", data[4:6])[0])
        self.assertEqual(checksum, struct.unpack("!H", data[6:])[0])
示例#7
0
    def test_should_return_proper_header_length_when_UDPHeader_object_is_called_in_len(self):
        # GIVEN
        udp_header = UDPHeader(any_port(), any_port(), any_payload_length(), any_checksum())

        # WHEN
        udp_header_length = len(udp_header)

        # THEN
        self.assertEqual(8, udp_header_length)
示例#8
0
    def test_should_create_UDPHeader_when_from_bytes_classmethod_is_called(self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        payload_length = any_payload_length()
        checksum = any_checksum()

        data = struct.pack("!H", src_port) + struct.pack("!H", dst_port) + \
            struct.pack("!H", payload_length) + struct.pack("!H", checksum)

        # WHEN
        udp_header = UDPHeader.from_bytes(io.BytesIO(data))

        # THEN
        self.assertEqual(src_port, udp_header.src_port)
        self.assertEqual(dst_port, udp_header.dst_port)
        self.assertEqual(payload_length, udp_header.payload_length)
        self.assertEqual(checksum, udp_header.checksum)
示例#9
0
    def test_should_create_UDPHeader_when_from_bytes_classmethod_is_called(self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        payload_length = any_payload_length()
        checksum = any_checksum()

        data = struct.pack("!H", src_port) + struct.pack("!H", dst_port) + \
            struct.pack("!H", payload_length) + struct.pack("!H", checksum)

        # WHEN
        udp_header = UDPHeader.from_bytes(io.BytesIO(data))

        # THEN
        self.assertEqual(src_port, udp_header.src_port)
        self.assertEqual(dst_port, udp_header.dst_port)
        self.assertEqual(payload_length, udp_header.payload_length)
        self.assertEqual(checksum, udp_header.checksum)
示例#10
0
    def test_should_creates_bytes_from_UDPHeader_and_payload_when_to_bytes_method_is_called(self):
        # GIVEN
        src_port = any_port()
        dst_port = any_port()
        checksum = any_checksum()

        payload = any_payload()
        payload_length = len(payload) + 8  # UDP length consists of UDP header length and payload length

        udp_header = UDPHeader(src_port, dst_port, payload_length, checksum)
        udp_payload = UDPBytesPayload(payload)
        udp_dgram = UDPDatagram(udp_header, udp_payload)

        # WHEN
        udp_dgram_bytes = udp_dgram.to_bytes()

        # THEN
        expected_udp_dgram_bytes = struct.pack("!H", src_port) + struct.pack("!H", dst_port) + \
            struct.pack("!H", payload_length) + struct.pack("!H", checksum) + payload

        self.assertEqual(expected_udp_dgram_bytes, udp_dgram_bytes)