def test_should_create_Prefix_from_bytearray_when_parse_method_is_called( self): # GIVEN domain_id = any_domain_id() prefix_length = any_prefix_length() prefix = any_prefix(prefix_length) sub_tlvs = any_prefix_sub_tlvs() factory = network_data.PrefixFactory( config.create_default_network_data_prefix_sub_tlvs_factory()) data = (bytearray([domain_id, prefix_length]) + prefix + convert_prefix_sub_tlvs_to_bytearray(sub_tlvs)) message_info = common.MessageInfo() # WHEN actual_prefix = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue(isinstance(actual_prefix, network_data.Prefix)) self.assertEqual(domain_id, actual_prefix.domain_id) self.assertEqual(prefix_length, actual_prefix.prefix_length) self.assertEqual(prefix, actual_prefix.prefix) self.assertEqual(sub_tlvs, actual_prefix.sub_tlvs)
def create(self, data): message = Message() message.channel = struct.unpack(">B", data.read(1)) # Parse MAC header mac_frame = self._parse_mac_frame(data) message.mac_header = mac_frame.header if message.mac_header.frame_type != mac802154.MacHeader.FrameType.DATA: return [message] message_info = common.MessageInfo() message_info.source_mac_address = message.mac_header.src_address message_info.destination_mac_address = message.mac_header.dest_address # Create stream with 6LoWPAN datagram lowpan_payload = io.BytesIO(mac_frame.payload.data) ipv6_packet = self._lowpan_parser.parse(lowpan_payload, message_info) if ipv6_packet is None: return [message] message.ipv6_packet = ipv6_packet if message.type == MessageType.MLE: self._add_device_descriptors(message) return message.try_extract_dtls_messages()
def test_should_encrypt_and_decrypt_random_data_content_when_proper_methods_are_called( self): # GIVEN data = any_data() master_key = any_master_key() key_id_mode = 2 security_level = 5 message_info = common.MessageInfo() message_info.source_mac_address = common.MacAddress.from_eui64( any_eui64()) message_info.source_ipv6 = any_ip_address() message_info.destination_ipv6 = any_ip_address() message_info.aux_sec_hdr = net_crypto.AuxiliarySecurityHeader( key_id_mode=key_id_mode, security_level=security_level, frame_counter=any_frame_counter(), key_id=any_key_id(key_id_mode)) message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray( message_info.aux_sec_hdr) net_crypto_engine = net_crypto.CryptoEngine( net_crypto.MleCryptoMaterialCreator(master_key)) # WHEN enc_data, mic = net_crypto_engine.encrypt(data, message_info) dec_data = net_crypto_engine.decrypt(enc_data, mic, message_info) # THEN self.assertEqual(data, dec_data)
def test_should_create_key_and_nonce_and_auth_data_when_create_key_and_nonce_and_auth_data_is_called( self): # GIVEN message_info = common.MessageInfo() message_info.source_mac_address = common.MacAddress.from_eui64( any_eui64()) message_info.source_ipv6 = any_ip_address() message_info.destination_ipv6 = any_ip_address() message_info.aux_sec_hdr = any_auxiliary_security_header() message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray( message_info.aux_sec_hdr) creator = net_crypto.MleCryptoMaterialCreator(master_key) # WHEN key, nonce, auth_data = creator.create_key_and_nonce_and_authenticated_data( message_info) # THEN self.assertEqual( message_info.source_mac_address.mac_address + struct.pack(">LB", message_info.aux_sec_hdr.frame_counter, message_info.aux_sec_hdr.security_level), nonce) self.assertEqual( message_info.source_ipv6.packed + message_info.destination_ipv6.packed + message_info.aux_sec_hdr_bytes, auth_data)
def test_should_create_Service_from_bytearray_when_parse_method_is_called( self): # GIVEN t = any_t() _id = any_id() enterprise_number = any_enterprise_number() service_data_length = any_service_data_length() service_data = any_service_data(service_data_length) sub_tlvs = any_service_sub_tlvs() stable = any_stable() factory = network_data.ServiceFactory( config.create_default_network_data_service_sub_tlvs_factory()) data = convert_service_to_bytearray( network_data.Service(t, _id, enterprise_number, service_data_length, service_data, sub_tlvs, stable)) message_info = common.MessageInfo() message_info.stable = stable # WHEN actual_service = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue(isinstance(actual_service, network_data.Service)) self.assertEqual(t, actual_service.t) self.assertEqual(_id, actual_service.id) self.assertEqual(enterprise_number, actual_service.enterprise_number) self.assertEqual(service_data_length, actual_service.service_data_length) self.assertEqual(service_data, actual_service.service_data) self.assertEqual(sub_tlvs, actual_service.sub_tlvs)
def test_should_return_source_eui64_value_when_source_eui64_property_is_called(self): # GIVEN source_mac_address = any_eui64() message_info = common.MessageInfo() message_info.source_mac_address = source_mac_address # WHEN actual_source_mac_address = message_info.source_mac_address # THEN self.assertEqual(source_mac_address, actual_source_mac_address)
def test_should_raise_RuntimeError_when_parse_method_is_called_but_required_factory_is_not_defined(self): # GIVEN data = any_data() message_info = common.MessageInfo() message_info.src_port = any_port() message_info.dst_port = any_port() factory = UdpBasedOnSrcDstPortsPayloadFactory(src_dst_port_based_payload_factories={}) # THEN self.assertRaises(RuntimeError, factory.parse, io.BytesIO(data), message_info)
def test_should_return_destination_eui64_value_when_destination_eui64_property_is_called(self): # GIVEN destination_mac_address = any_eui64() message_info = common.MessageInfo() message_info.destination_mac_address = destination_mac_address # WHEN actual_destination_mac_address = message_info.destination_mac_address # THEN self.assertEqual(destination_mac_address, actual_destination_mac_address)
def test_should_return_destination_ipv6_value_when_destination_ipv6_property_is_called(self): # GIVEN destination_ipv6 = any_ipv6_address() message_info = common.MessageInfo() message_info.destination_ipv6 = destination_ipv6 # WHEN actual_destination_ipv6 = message_info.destination_ipv6 # THEN self.assertEqual(ipaddress.ip_address(bytes(destination_ipv6)), actual_destination_ipv6)
def test_should_create_MlEid_from_bytearray_when_parse_method_is_called( self): # GIVEN ml_eid = any_ml_eid() factory = network_layer.MlEidFactory() # WHEN ml_eid_obj = factory.parse(io.BytesIO(ml_eid), common.MessageInfo()) # THEN self.assertTrue(isinstance(ml_eid_obj, network_layer.MlEid)) self.assertEqual(ml_eid, ml_eid_obj.ml_eid)
def test_should_create_TargetEid_from_bytearray_when_parse_method_is_called( self): # GIVEN eid = any_eid() factory = network_layer.TargetEidFactory() # WHEN target_eid = factory.parse(io.BytesIO(eid), common.MessageInfo()) # THEN self.assertTrue(isinstance(target_eid, network_layer.TargetEid)) self.assertEqual(eid, target_eid.eid)
def test_should_create_SubTlvs_from_bytearray_when_parse_method_is_called(self): # GIVEN sub_tlvs = any_prefix_sub_tlvs() factory = network_data.PrefixSubTlvsFactory(config.create_default_network_data_prefix_sub_tlvs_factories()) data = convert_prefix_sub_tlvs_to_bytearray(sub_tlvs) # WHEN actual_sub_tlvs = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue(isinstance(actual_sub_tlvs, list)) self.assertEqual(sub_tlvs, actual_sub_tlvs)
def test_should_create_MacExtendedAddress_from_bytearray_when_parse_method_is_called( self): # GIVEN mac_address = any_mac_extended_address() factory = network_layer.MacExtendedAddressFactory() # WHEN mac_extended_address = factory.parse(io.BytesIO(mac_address), common.MessageInfo()) # THEN self.assertTrue( isinstance(mac_extended_address, network_layer.MacExtendedAddress)) self.assertEqual(mac_address, mac_extended_address.mac_address)
def test_should_create_NdOption_from_bytearray_when_parse_method_is_called( self): # GIVEN options = any_options() factory = network_layer.NdOptionFactory() data = bytearray(options) # WHEN nd_option = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue(isinstance(nd_option, network_layer.NdOption)) self.assertEqual(options, nd_option.options)
def test_should_create_Status_from_bytearray_when_parse_method_is_called( self): # GIVEN status = any_status() factory = network_layer.StatusFactory() data = bytearray([status]) # WHEN status_obj = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue(isinstance(status_obj, network_layer.Status)) self.assertEqual(status, status_obj.status)
def test_should_create_Rloc16_from_bytearray_when_parse_method_is_called( self): # GIVEN rloc16 = any_rloc16() factory = network_layer.Rloc16Factory() data = bytearray(struct.pack(">H", rloc16)) # WHEN rloc16_obj = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue(isinstance(rloc16_obj, network_layer.Rloc16)) self.assertEqual(rloc16, rloc16_obj.rloc16)
def test_should_create_RouterMask_from_bytearray_when_parse_method_is_called( self): # GIVEN id_sequence = any_id_sequence() router_id_mask = any_router_id_mask() factory = network_layer.RouterMaskFactory() data = bytearray([id_sequence]) + struct.pack(">Q", router_id_mask) # WHEN router_mask = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue(isinstance(router_mask, network_layer.RouterMask)) self.assertEqual(id_sequence, router_mask.id_sequence) self.assertEqual(router_id_mask, router_mask.router_id_mask)
def test_should_create_TimeSinceLastTransaction_from_bytearray_when_parse_method_is_called( self): # GIVEN seconds = any_seconds() factory = network_layer.TimeSinceLastTransactionFactory() data = bytearray(struct.pack(">L", seconds)) # WHEN time_since_last_transaction = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue( isinstance(time_since_last_transaction, network_layer.TimeSinceLastTransaction)) self.assertEqual(seconds, time_since_last_transaction.seconds)
def test_should_create_payload_from_data_when_dst_port_factory_is_defined_and_parse_method_is_called(self): # GIVEN data = any_data() message_info = common.MessageInfo() message_info.src_port = any_port() message_info.dst_port = any_port() factory = UdpBasedOnSrcDstPortsPayloadFactory( src_dst_port_based_payload_factories={ message_info.dst_port: UDPBytesPayloadFactory() }) # WHEN actual_data = factory.parse(io.BytesIO(data), message_info) # THEN self.assertEqual(data, actual_data.data)
def test_should_create_HasRoute_from_bytearray_when_parse_method_is_called(self): # GIVEN routes = any_routes() stable = any_stable() factory = network_data.HasRouteFactory(network_data.RoutesFactory(network_data.RouteFactory())) data = convert_routes_to_bytearray(routes) message_info = common.MessageInfo() message_info.stable = stable # WHEN actual_has_route = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue(isinstance(actual_has_route, network_data.HasRoute)) self.assertEqual(routes, actual_has_route.routes) self.assertEqual(stable, actual_has_route.stable)
def test_should_create_ThreadNetworkData_from_bytearray_when_parse_method_is_called( self): # GIVEN tlvs = any_tlvs_data() class DummyNetworkDataTlvsFactory: def parse(self, data, message_info): return bytearray(data.read()) factory = network_layer.ThreadNetworkDataFactory( DummyNetworkDataTlvsFactory()) # WHEN thread_network_data = factory.parse(io.BytesIO(tlvs), common.MessageInfo()) # THEN self.assertTrue( isinstance(thread_network_data, network_layer.ThreadNetworkData)) self.assertEqual(tlvs, thread_network_data.tlvs)
def test_should_create_Server_from_bytearray_when_parse_method_is_called(self): # GIVEN server_16 = any_server_16() server_data = any_server_data() stable = any_stable() factory = network_data.ServerFactory() data = convert_server_to_bytearray(network_data.Server(server_16, server_data, stable)) message_info = common.MessageInfo() message_info.stable = stable # WHEN actual_server = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue(isinstance(actual_server, network_data.Server)) self.assertEqual(server_16, actual_server.server_16) self.assertEqual(server_data, actual_server.server_data) self.assertEqual(stable, actual_server.stable)
def test_should_create_BorderRouter_from_bytearray_when_parse_method_is_called( self): # GIVEN border_router_16 = any_border_router_16() prf = any_prf() p = any_p() s = any_s() d = any_d() c = any_c() r = any_r() o = any_o() n = any_n() stable = any_stable() factory = network_data.BorderRouterFactory() data = convert_border_router_to_bytearray( network_data.BorderRouter(border_router_16, prf, p, s, d, c, r, o, n, stable)) message_info = common.MessageInfo() message_info.stable = stable # WHEN actual_border_router = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue( isinstance(actual_border_router, network_data.BorderRouter)) self.assertEqual(border_router_16, actual_border_router.border_router_16) self.assertEqual(prf, actual_border_router.prf) self.assertEqual(p, actual_border_router.p) self.assertEqual(s, actual_border_router.s) self.assertEqual(d, actual_border_router.d) self.assertEqual(c, actual_border_router.c) self.assertEqual(r, actual_border_router.r) self.assertEqual(o, actual_border_router.o) self.assertEqual(n, actual_border_router.n) self.assertEqual(stable, actual_border_router.stable)
def test_should_create_AuxiliarySecurityHeader_from_bytearray_when_parse_method_is_called( self): # GIVEN key_id_mode = any_key_id_mode() sec_lvl = any_security_level() frame_counter = any_frame_counter() key_id = any_key_id(key_id_mode) factory = net_crypto.AuxiliarySecurityHeaderFactory() data = bytearray([sec_lvl | key_id_mode << 3]) + struct.pack( "<I", frame_counter) + key_id # WHEN aux_sec_hdr = factory.parse(io.BytesIO(data), common.MessageInfo()) # THEN self.assertTrue( isinstance(aux_sec_hdr, net_crypto.AuxiliarySecurityHeader)) self.assertEqual(key_id_mode, aux_sec_hdr.key_id_mode) self.assertEqual(sec_lvl, aux_sec_hdr.security_level) self.assertEqual(frame_counter, aux_sec_hdr.frame_counter)
def test_should_decrypt_bytearray_to_mle_message_when_decrypt_method_is_called( self): # GIVEN message_info = common.MessageInfo() message_info.source_mac_address = common.MacAddress.from_eui64( bytearray([0x00, 0x35, 0xcc, 0x94, 0xd7, 0x7a, 0x07, 0xe8])) message_info.source_ipv6 = "fe80::235:cc94:d77a:07e8" message_info.destination_ipv6 = "ff02::2" message_info.aux_sec_hdr = net_crypto.AuxiliarySecurityHeader( key_id_mode=2, security_level=5, frame_counter=262165, key_id=bytearray([0x00, 0x00, 0x00, 0x00, 0x01])) message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray( message_info.aux_sec_hdr) data = bytearray([ 0x9a, 0x5a, 0x9a, 0x5b, 0xba, 0x25, 0x9c, 0x5e, 0x58, 0xa2, 0x7e, 0x75, 0x74, 0xef, 0x79, 0xbc, 0x4f, 0xa3, 0xf9, 0xae, 0xa8, 0x34, 0xf6, 0xf2, 0x37, 0x21, 0x93, 0x60 ]) mic = bytearray([0xe1, 0xb5, 0xa2, 0x53]) net_crypto_engine = net_crypto.CryptoEngine( net_crypto.MleCryptoMaterialCreator(master_key)) # WHEN mle_msg = net_crypto_engine.decrypt(data, mic, message_info) # THEN expected_mle_msg = bytearray([ 0x04, 0x00, 0x02, 0x00, 0x00, 0x09, 0x0b, 0x8f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x01, 0xf1, 0x0b, 0x08, 0x65, 0x5e, 0x0f, 0x83, 0x40, 0xc7, 0x83, 0x31 ]) self.assertEqual(expected_mle_msg, mle_msg)
def test_should_create_LowpanId_from_bytearray_when_parse_method_is_called(self): # GIVEN c = any_c() cid = any_cid() context_length = any_context_length() stable = any_stable() factory = network_data.LowpanIdFactory() data = convert_lowpan_id_to_bytearray(network_data.LowpanId(c, cid, context_length, stable)) message_info = common.MessageInfo() message_info.stable = stable # WHEN actual_lowpan_id = factory.parse(io.BytesIO(data), message_info) # THEN self.assertTrue(isinstance(actual_lowpan_id, network_data.LowpanId)) self.assertEqual(c, actual_lowpan_id.c) self.assertEqual(cid, actual_lowpan_id.cid) self.assertEqual(context_length, actual_lowpan_id.context_length)
def create(self, data): try: message = Message() message.channel = struct.unpack(">B", data.read(1)) # Parse MAC header mac_frame = self._parse_mac_frame(data) message.mac_header = mac_frame.header if message.mac_header.frame_type != mac802154.MacHeader.FrameType.DATA: return [message] message_info = common.MessageInfo() message_info.source_mac_address = message.mac_header.src_address message_info.destination_mac_address = message.mac_header.dest_address # Create stream with 6LoWPAN datagram lowpan_payload = io.BytesIO(mac_frame.payload.data) ipv6_packet = self._lowpan_parser.parse(lowpan_payload, message_info) if ipv6_packet is None: return [message] message.ipv6_packet = ipv6_packet if message.type == MessageType.MLE: self._add_device_descriptors(message) return message.try_extract_dtls_messages() except mac802154.KeyIdMode0Exception: print( 'Received packet with key_id_mode = 0, cannot be handled in test scripts' ) raise DropPacketException
def any_message_info(): return common.MessageInfo()