From bee50b803217cf0feac1d365f8373bfdbbfde733 Mon Sep 17 00:00:00 2001 From: Maliat Manzur Date: Wed, 30 Jan 2019 14:18:52 -0500 Subject: [PATCH 1/2] initial push. still buggy --- tests/test_director.py | 28 ++++++++++++++++++++++++++++ uptane/services/director.py | 30 +++++++++++++++++++++++++++++- uptane/services/inventorydb.py | 16 +++++++++++++++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/tests/test_director.py b/tests/test_director.py index 4ab0946..6d4917e 100644 --- a/tests/test_director.py +++ b/tests/test_director.py @@ -803,6 +803,34 @@ def test_60_register_vehicle(self): + def test_61_delete_existing_vehicle(self): + + vin = 'democar' + + self.assertTrue(inventory.ecus_by_vin) + self.assertTrue(inventory.ecu_public_keys) + self.assertTrue(inventory.primary_ecus_by_vin) + self.assertTrue(inventory.vehicle_manifests) + self.assertTrue(inventory.ecu_manifests) + + self.assertIsNotNone(inventory.get_last_vehicle_manifest(vin)) + + #Delete the vehicle + TestDirector.instance.remove_vehicle(vin) + os.chdir(uptane.WORKING_DIR) + + self.assertNotIn(vin, inventory.ecus_by_vin) + self.assertNotIn(vin, inventory.primary_ecus_by_vin) + + with self.assertRaises(uptane.UnknownVehicle): + inventory.get_last_vehicle_manifest(vin) + + #check vin no longer exists in the repo + self.assertNotIn(vin, TestDirector.instance.vehicle_repositories) + + + + # Run unit test. if __name__ == '__main__': diff --git a/uptane/services/director.py b/uptane/services/director.py index df9a259..ef6be11 100644 --- a/uptane/services/director.py +++ b/uptane/services/director.py @@ -43,6 +43,7 @@ import os import hashlib +import shutil from uptane.encoding.asn1_codec import DATATYPE_TIME_ATTESTATION from uptane.encoding.asn1_codec import DATATYPE_ECU_MANIFEST @@ -464,6 +465,16 @@ def add_new_vehicle(self, vin, primary_ecu_serial=None): + def remove_vehicle(self, vin): + + inventory.deregister_vehicle(vin) + + self.delete_director_repo_for_vehicle(vin) + + + + + def create_director_repo_for_vehicle(self, vin): """ Creates a separate repository object for a given vehicle identifier. @@ -485,7 +496,7 @@ def create_director_repo_for_vehicle(self, vin): These repository objects can be manipulated as described in TUF documentation; for example, to produce metadata files afterwards for that vehicle: - d.vehicle_repositories[vin].write() + d.vehicle_reposaitories[vin].write() # TODO: This may be outside of the scope of the reference implementation, @@ -526,6 +537,23 @@ def create_director_repo_for_vehicle(self, vin): + + def delete_director_repo_for_vehicle(self, vin): + uptane.formats.VIN_SCHEMA.check_match(vin) + os.chdir(self.director_repos_dir) + + vin = uptane.common.scrub_filename(vin, self.director_repos_dir) + vin = os.path.relpath(vin, self.director_repos_dir) + + existing_vehicle_repo = self.vehicle_repositories.get(vin) + + if existing_vehicle_repo: + shutil.rmtree(existing_vehicle_repo.repository_name) + + + + + def add_target_for_ecu(self, vin, ecu_serial, target_filepath): """ Add a target to the repository for a vehicle, marked as being for a diff --git a/uptane/services/inventorydb.py b/uptane/services/inventorydb.py index e07c207..45da48d 100644 --- a/uptane/services/inventorydb.py +++ b/uptane/services/inventorydb.py @@ -353,6 +353,20 @@ def register_vehicle(vin, primary_ecu_serial=None, overwrite=True): +def deregister_vehicle(vin): + + _check_registration_is_sane(vin) + print("deregistering: " + str(vin)) + print("The dictionary is: ") + print(ecus_by_vin) + if check_vin_registered(vin): + ecus_by_vin.pop(vin) + vehicle_manifest.pop(vin) + primary_ecus_by_vin.pop(vin) + + + + def check_vin_registered(vin): @@ -361,13 +375,13 @@ def check_vin_registered(vin): if vin not in vehicle_manifests: # TODO: Should we also log here? Review logging before exceptions # throughout the reference implementation. + print(vin) raise uptane.UnknownVehicle('The given VIN, ' + repr(vin) + ', is not ' 'known.') - def _check_registration_is_sane(vin): """ Asserts that a data structure invariant remains correct. A vehicle must be From 9bdb741476cc5a787080e8c27421796a5ed757f6 Mon Sep 17 00:00:00 2001 From: Maliat Manzur Date: Thu, 31 Jan 2019 16:30:53 -0500 Subject: [PATCH 2/2] removed bugs --- uptane/services/director.py | 3 ++- uptane/services/inventorydb.py | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/uptane/services/director.py b/uptane/services/director.py index ef6be11..3bf5a56 100644 --- a/uptane/services/director.py +++ b/uptane/services/director.py @@ -496,7 +496,7 @@ def create_director_repo_for_vehicle(self, vin): These repository objects can be manipulated as described in TUF documentation; for example, to produce metadata files afterwards for that vehicle: - d.vehicle_reposaitories[vin].write() + d.vehicle_repositories[vin].write() # TODO: This may be outside of the scope of the reference implementation, @@ -549,6 +549,7 @@ def delete_director_repo_for_vehicle(self, vin): if existing_vehicle_repo: shutil.rmtree(existing_vehicle_repo.repository_name) + self.vehicle_repositories.pop(vin) diff --git a/uptane/services/inventorydb.py b/uptane/services/inventorydb.py index 45da48d..e8042ff 100644 --- a/uptane/services/inventorydb.py +++ b/uptane/services/inventorydb.py @@ -356,13 +356,12 @@ def register_vehicle(vin, primary_ecu_serial=None, overwrite=True): def deregister_vehicle(vin): _check_registration_is_sane(vin) - print("deregistering: " + str(vin)) - print("The dictionary is: ") - print(ecus_by_vin) - if check_vin_registered(vin): - ecus_by_vin.pop(vin) - vehicle_manifest.pop(vin) - primary_ecus_by_vin.pop(vin) + check_vin_registered(vin) + + #If no error is found, delete from dictionaries + ecus_by_vin.pop(vin) + vehicle_manifests.pop(vin) + primary_ecus_by_vin.pop(vin) @@ -375,13 +374,13 @@ def check_vin_registered(vin): if vin not in vehicle_manifests: # TODO: Should we also log here? Review logging before exceptions # throughout the reference implementation. - print(vin) raise uptane.UnknownVehicle('The given VIN, ' + repr(vin) + ', is not ' 'known.') + def _check_registration_is_sane(vin): """ Asserts that a data structure invariant remains correct. A vehicle must be