def test_should_create_IPv6Header_when_from_bytes_classmethod_is_called(self): # GIVEN traffic_class = any_traffic_class() flow_label = any_flow_label() payload_length = any_payload_length() next_header = any_next_header() hop_limit = any_hop_limit() source_address = any_ip_address() destination_address = any_ip_address() data = bytearray([(6 << 4) | (traffic_class >> 4), (traffic_class & 0xF) << 4 | (flow_label >> 16) & 0xF, (flow_label >> 8) & 0xFF, flow_label & 0xFF, payload_length >> 8, payload_length & 0xFF, next_header, hop_limit]) data += ip_address(bytes(source_address)).packed + ip_address(bytes(destination_address)).packed # WHEN ipv6_header = IPv6Header.from_bytes(io.BytesIO(data)) # THEN self.assertEqual(6, ipv6_header.version) self.assertEqual(traffic_class, ipv6_header.traffic_class) self.assertEqual(flow_label, ipv6_header.flow_label) self.assertEqual(payload_length, ipv6_header.payload_length) self.assertEqual(next_header, ipv6_header.next_header) self.assertEqual(hop_limit, ipv6_header.hop_limit) self.assertEqual(source_address, ipv6_header.source_address.packed) self.assertEqual(destination_address, ipv6_header.destination_address.packed)
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)
def test_should_create_IPv6Header_when_from_bytes_classmethod_is_called( self): # GIVEN traffic_class = any_traffic_class() flow_label = any_flow_label() payload_length = any_payload_length() next_header = any_next_header() hop_limit = any_hop_limit() source_address = any_ip_address() destination_address = any_ip_address() data = bytearray([ (6 << 4) | (traffic_class >> 4), (traffic_class & 0xF) << 4 | (flow_label >> 16) & 0xF, (flow_label >> 8) & 0xFF, flow_label & 0xFF, payload_length >> 8, payload_length & 0xFF, next_header, hop_limit ]) data += ip_address(bytes(source_address)).packed + ip_address( bytes(destination_address)).packed # WHEN ipv6_header = IPv6Header.from_bytes(io.BytesIO(data)) # THEN self.assertEqual(6, ipv6_header.version) self.assertEqual(traffic_class, ipv6_header.traffic_class) self.assertEqual(flow_label, ipv6_header.flow_label) self.assertEqual(payload_length, ipv6_header.payload_length) self.assertEqual(next_header, ipv6_header.next_header) self.assertEqual(hop_limit, ipv6_header.hop_limit) self.assertEqual(source_address, ipv6_header.source_address.packed) self.assertEqual(destination_address, ipv6_header.destination_address.packed)
def test_should_convert_IPv6_header_to_bytes_when_to_bytes_method_is_called( self): # GIVEN traffic_class = any_traffic_class() flow_label = any_flow_label() payload_length = any_payload_length() next_header = any_next_header() hop_limit = any_hop_limit() source_address = any_ip_address() destination_address = any_ip_address() ipv6_header = IPv6Header(source_address, destination_address, traffic_class, flow_label, hop_limit, payload_length, next_header) # WHEN data = ipv6_header.to_bytes() # THEN self.assertEqual(6, data[0] >> 4) self.assertEqual(traffic_class, ((data[0] << 8 | data[1]) >> 4) & 0xFF) self.assertEqual(flow_label, ((data[1] & 0x0F) << 16) | (data[2] << 8) | data[3]) self.assertEqual(payload_length, struct.unpack("!H", data[4:6])[0]) self.assertEqual(next_header, data[6]) self.assertEqual(hop_limit, data[7]) self.assertEqual(source_address, data[8:24]) self.assertEqual(destination_address, data[24:40])
def test_should_build_IPv6Packet_with_ICMP_payload_from_well_know_values_when_to_bytes_method_is_called(self): # GIVEN ipv6_packet = IPv6Packet(IPv6Header(source_address="fd00:1234:4555::ff:fe00:1800", destination_address="ff03::1"), ICMPv6(ICMPv6Header(128, 0), ICMPv6EchoBody(0, 2, bytearray([0x80, 0x00, 0xc7, 0xbf, 0x00, 0x00, 0x00, 0x01, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41]))), [HopByHop(options=[ HopByHopOption(HopByHopOptionHeader(_type=0x6d), MPLOption(S=1, M=0, V=0, sequence=2, seed_id=bytearray([0x00, 0x18]))) ])]) # WHEN ipv6_packet_bytes = ipv6_packet.to_bytes() # THEN expected_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]) self.assertEqual(expected_ipv6_packet_bytes, ipv6_packet_bytes)
def test_should_return_proper_header_length_when_IPv6Packet_object_is_called_in_len(self): # GIVEN ipv6_header = IPv6Header(any_traffic_class(), any_flow_label(), any_payload_length(), any_next_header(), any_hop_limit(), any_ip_address(), any_ip_address()) # WHEN ipv6_header_length = len(ipv6_header) # THEN self.assertEqual(40, ipv6_header_length)