diff --git a/src/libaktualizr/primary/reregistration_test.cc b/src/libaktualizr/primary/reregistration_test.cc index 7b3a8de27..ae8aeb3a5 100644 --- a/src/libaktualizr/primary/reregistration_test.cc +++ b/src/libaktualizr/primary/reregistration_test.cc @@ -17,16 +17,34 @@ class HttpFakeRegistration : public HttpFake { : HttpFake(test_dir_in, "noupdates", meta_dir_in) {} HttpResponse post(const std::string& url, const Json::Value& data) override { + if (url.find("/devices") != std::string::npos) { + device_registration_count++; + auto this_device_id = data["deviceId"].asString(); + if (ecu_registration_count <= 1) { + device_id = this_device_id; + } else { + EXPECT_EQ(device_id, this_device_id) << "deviceId should change during provisioning"; + } + } if (url.find("/director/ecus") != std::string::npos) { - registration_count += 1; + ecu_registration_count++; EXPECT_EQ(data["primary_ecu_serial"].asString(), "CA:FE:A6:D2:84:9D"); EXPECT_EQ(data["ecus"][0]["ecu_serial"].asString(), "CA:FE:A6:D2:84:9D"); EXPECT_EQ(data["ecus"][0]["hardware_identifier"].asString(), "primary_hw"); + if (ecu_registration_count == 1) { + primary_ecu_info = data["ecus"][0]; + } else { + EXPECT_EQ(primary_ecu_info, data["ecus"][0]) << "Information about primary ECU shouldn't change"; + } } + return HttpFake::post(url, data); } - unsigned int registration_count{0}; + unsigned int ecu_registration_count{0}; + unsigned int device_registration_count{0}; + Json::Value primary_ecu_info; + std::string device_id; }; /* @@ -45,14 +63,16 @@ TEST(Aktualizr, AddSecondary) { std::vector expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"}; UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 1); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); ecu_config.ecu_serial = "ecuserial4"; aktualizr.AddSecondary(std::make_shared(ecu_config)); aktualizr.Initialize(); expected_ecus.push_back(ecu_config.ecu_serial); UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 2); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 2); } /* @@ -72,7 +92,8 @@ TEST(Aktualizr, RemoveSecondary) { std::vector expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"}; UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 1); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); } { @@ -81,7 +102,8 @@ TEST(Aktualizr, RemoveSecondary) { std::vector expected_ecus = {"CA:FE:A6:D2:84:9D", "secondary_ecu_serial"}; UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 2); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 2); } } @@ -102,7 +124,8 @@ TEST(Aktualizr, ReplaceSecondary) { std::vector expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial3", "secondary_ecu_serial"}; UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 1); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); } { @@ -114,7 +137,62 @@ TEST(Aktualizr, ReplaceSecondary) { std::vector expected_ecus = {"CA:FE:A6:D2:84:9D", "ecuserial4", "secondary_ecu_serial"}; UptaneTestCommon::verifyEcus(temp_dir, expected_ecus); - EXPECT_EQ(http->registration_count, 2); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 2); + } +} + +/** + * Restarting Aktualizr without changing the secondaries should not result in it getting re-registered + */ +TEST(Aktualizr, RestartNoRegisterSecondaries) { + TemporaryDirectory temp_dir; + auto http = std::make_shared(temp_dir.Path(), fake_meta_dir); + Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); + auto storage = INvStorage::newStorage(conf.storage); + + { + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + Primary::VirtualSecondaryConfig ecu_config = UptaneTestCommon::altVirtualConfiguration(temp_dir.Path()); + aktualizr.AddSecondary(std::make_shared(ecu_config)); + aktualizr.Initialize(); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); + } + + { + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + Primary::VirtualSecondaryConfig ecu_config = UptaneTestCommon::altVirtualConfiguration(temp_dir.Path()); + aktualizr.AddSecondary(std::make_shared(ecu_config)); + aktualizr.Initialize(); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); + } +} + +/** + * Restarting Aktualizr should not result in it getting re-registered if it has no secondaries. + * This is similar to RestartNoRegisterSecondaries, but with zero secondaries. + */ +TEST(Aktualizr, RestartNoRegisterPrimaryOnly) { + TemporaryDirectory temp_dir; + auto http = std::make_shared(temp_dir.Path(), fake_meta_dir); + Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); + + { + auto storage = INvStorage::newStorage(conf.storage); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + aktualizr.Initialize(); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); + } + + { + auto storage = INvStorage::newStorage(conf.storage); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + aktualizr.Initialize(); + EXPECT_EQ(http->device_registration_count, 1); + EXPECT_EQ(http->ecu_registration_count, 1); } }