def test_should_create_HopByHop_object_instance_when_to_bytes_method_is_called_with_data( self): # GIVEN hop_by_hop_option = any_hop_by_hop_mpl_option() hop_by_hop_option_type = hop_by_hop_option.header.type next_header = any_next_header() hdr_ext_len = self._calculate_hdr_ext_len(2 + len(hop_by_hop_option)) hop_by_hop_factory = HopByHopFactory( hop_by_hop_options_factory=HopByHopOptionsFactory( options_factories={hop_by_hop_option_type: MPLOptionFactory() })) data = bytearray([next_header, hdr_ext_len ]) + hop_by_hop_option.to_bytes() data += self.padding(len(data)) # WHEN hop_by_hop = hop_by_hop_factory.parse(io.BytesIO(data), any_message_info()) # THEN self.assertEqual(hop_by_hop_option.value.S, hop_by_hop.options[0].value.S) self.assertEqual(hop_by_hop_option.value.V, hop_by_hop.options[0].value.V) self.assertEqual(hop_by_hop_option.value.M, hop_by_hop.options[0].value.M) self.assertEqual(hop_by_hop_option.value.sequence, hop_by_hop.options[0].value.sequence) self.assertEqual(hop_by_hop_option.value.seed_id, hop_by_hop.options[0].value.seed_id)
def test_should_create_option_from_bytearray_when_to_bytes_method_is_called(self): # GIVEN class DummyOptionFactory: def parse(self, data, message_info): return data.read() factory=HopByHopOptionsFactory(options_factories = {2: DummyOptionFactory()}) data=bytearray([0x02, 0x03, 0x11, 0x22, 0x33, 0x01, 0x00]) # WHEN actual_options=factory.parse(io.BytesIO(data), any_message_info()) # THEN self.assertEqual(1, len(actual_options)) self.assertEqual(2, actual_options[0].header.type) self.assertEqual(3, actual_options[0].header.length)
def test_should_create_option_from_bytearray_when_to_bytes_method_is_called(self): # GIVEN class DummyOptionFactory: def parse(self, data, message_info): return data.read() factory=HopByHopOptionsFactory(options_factories = {2: DummyOptionFactory()}) data=bytearray([0x02, 0x03, 0x11, 0x22, 0x33, 0x01, 0x00]) # WHEN actual_options=factory.parse(io.BytesIO(data), any_message_info()) # THEN self.assertEqual(1, len(actual_options)) self.assertEqual(2, actual_options[0].header.type) self.assertEqual(3, actual_options[0].header.length)
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)
def test_should_raise_RuntimeError_when_no_option_factory_is_set_and_parse_method_is_called(self): # GIVEN hop_by_hop_option = any_hop_by_hop_mpl_option() hop_by_hop_option_type = hop_by_hop_option.header.type next_header = any_next_header() hdr_ext_len = self._calculate_hdr_ext_len(2 + len(hop_by_hop_option)) hop_by_hop_factory = HopByHopFactory(hop_by_hop_options_factory=HopByHopOptionsFactory()) data = bytes([next_header, hdr_ext_len]) + hop_by_hop_option.to_bytes() data += self.padding(len(data)) # THEN self.assertRaises(RuntimeError, hop_by_hop_factory.parse, io.BytesIO(data), any_message_info())