Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Create EcuMap type to improve readability.
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Vacek <[email protected]>
  • Loading branch information
pattivacek committed Jul 18, 2019
1 parent c33871b commit 907e488
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/aktualizr_info/aktualizr_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ TEST_F(AktualizrInfoTest, PrintPrimaryEcuCurrentAndPendingVersions) {
const std::string current_ecu_version = "639a4e39-e6ba-4832-ace4-8b12cf20d562";
const std::string pending_ecu_version = "9636753d-2a09-4c80-8b25-64b2c2d0c4df";

std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map{{primary_ecu_serial, primary_hw_id}};
Uptane::EcuMap ecu_map{{primary_ecu_serial, primary_hw_id}};
db_storage_->savePrimaryInstalledVersion(
{"update.bin", ecu_map, {{Uptane::Hash::Type::kSha256, current_ecu_version}}, 1, "corrid"},
InstalledVersionUpdateMode::kCurrent);
Expand Down Expand Up @@ -421,7 +421,7 @@ TEST_F(AktualizrInfoTest, PrintPrimaryEcuCurrentAndPendingVersionsNegative) {
EXPECT_NE(aktualizr_info_output.find("No currently running version on primary ecu"), std::string::npos);
EXPECT_EQ(aktualizr_info_output.find("Pending primary ecu version:"), std::string::npos);

std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map{{primary_ecu_serial, primary_hw_id}};
Uptane::EcuMap ecu_map{{primary_ecu_serial, primary_hw_id}};
db_storage_->savePrimaryInstalledVersion(
{"update-01.bin", ecu_map, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
InstalledVersionUpdateMode::kPending);
Expand Down Expand Up @@ -463,7 +463,7 @@ TEST_F(AktualizrInfoTest, PrintSecondaryEcuCurrentAndPendingVersions) {
db_storage_->storeEcuSerials({{primary_ecu_serial, primary_hw_id}, {secondary_ecu_serial, secondary_hw_id}});
db_storage_->storeEcuRegistered();

std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map{{secondary_ecu_serial, secondary_hw_id}};
Uptane::EcuMap ecu_map{{secondary_ecu_serial, secondary_hw_id}};
db_storage_->saveInstalledVersion(
secondary_ecu_serial.ToString(),
{secondary_ecu_filename, ecu_map, {{Uptane::Hash::Type::kSha256, current_ecu_version}}, 1},
Expand Down
5 changes: 2 additions & 3 deletions src/libaktualizr/package_manager/packagemanagerfake_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST(PackageManagerFake, FinalizeAfterReboot) {

PackageManagerFake fakepm(config.pacman, storage, bootloader, nullptr);

std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> primary_ecu;
Uptane::EcuMap primary_ecu;
Uptane::Target target("pkg", primary_ecu, {Uptane::Hash(Uptane::Hash::Type::kSha256, "hash")}, 1, "");
auto result = fakepm.install(target);
EXPECT_EQ(result.result_code, data::ResultCode::Numeric::kNeedCompletion);
Expand All @@ -52,8 +52,7 @@ TEST(PackageManagerFake, FailureInjection) {
fiu_init(0);

// no fault
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> primary_ecu{
{Uptane::EcuSerial("primary"), Uptane::HardwareIdentifier("primary_hw")}};
Uptane::EcuMap primary_ecu{{Uptane::EcuSerial("primary"), Uptane::HardwareIdentifier("primary_hw")}};
Uptane::Target target("pkg", primary_ecu, {Uptane::Hash(Uptane::Hash::Type::kSha256, "hash")}, 1, "");
auto result = fakepm.install(target);
EXPECT_EQ(result.result_code, data::ResultCode::Numeric::kOk);
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/primary/sotauptaneclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class SotaUptaneClient {
std::shared_ptr<Bootloader> bootloader;
std::shared_ptr<ReportQueue> report_queue;
Json::Value last_network_info_reported;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> hw_ids;
Uptane::EcuMap hw_ids;
std::shared_ptr<event::Channel> events_channel;
boost::signals2::connection conn;
Uptane::Exception last_exception{"", ""};
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/storage/sqlstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ bool SQLStorage::loadInstalledVersions(const std::string& ecu_serial, std::vecto
}
}

std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
auto statement =
db.prepareStatement<std::string>("SELECT hardware_id FROM ecu_serials WHERE serial = ?;", ecu_serial_real);
if (statement.step() == SQLITE_ROW) {
Expand Down
6 changes: 2 additions & 4 deletions src/libaktualizr/storage/storage_common_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ TEST(storage, load_store_installed_versions) {
Uptane::Hash{Uptane::Hash::Type::kSha256, "2561"},
Uptane::Hash{Uptane::Hash::Type::kSha512, "5121"},
};
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> primary_ecu{
{Uptane::EcuSerial("primary"), Uptane::HardwareIdentifier("primary_hw")}};
Uptane::EcuMap primary_ecu{{Uptane::EcuSerial("primary"), Uptane::HardwareIdentifier("primary_hw")}};
Uptane::Target t1{"update.bin", primary_ecu, hashes, 1, "corrid"};
storage->savePrimaryInstalledVersion(t1, InstalledVersionUpdateMode::kCurrent);

Expand Down Expand Up @@ -376,8 +375,7 @@ TEST(storage, load_store_installed_versions) {
}

// Add a secondary installed version
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> secondary_ecu{
{Uptane::EcuSerial("secondary1"), Uptane::HardwareIdentifier("secondary_hw")}};
Uptane::EcuMap secondary_ecu{{Uptane::EcuSerial("secondary1"), Uptane::HardwareIdentifier("secondary_hw")}};
Uptane::Target tsec{"secondary.bin", secondary_ecu, {Uptane::Hash{Uptane::Hash::Type::kSha256, "256s"}}, 4};
storage->saveInstalledVersion("secondary_1", tsec, InstalledVersionUpdateMode::kCurrent);

Expand Down
11 changes: 5 additions & 6 deletions src/libaktualizr/uptane/tuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ Target::Target(std::string filename, const Json::Value &content) : filename_(std
std::sort(hashes_.begin(), hashes_.end(), [](const Hash &l, const Hash &r) { return l.type() < r.type(); });
}

Target::Target(std::string filename, std::map<EcuSerial, HardwareIdentifier> ecus, std::vector<Hash> hashes,
uint64_t length, std::string correlation_id)
Target::Target(std::string filename, EcuMap ecus, std::vector<Hash> hashes, uint64_t length, std::string correlation_id)
: filename_(std::move(filename)),
ecus_(std::move(ecus)),
hashes_(std::move(hashes)),
Expand Down Expand Up @@ -234,13 +233,13 @@ bool Target::operator==(const Target &t2) const {
// ECU->HWID map empty). Figure out which Target has the map, and then for
// every item in the map, make sure it's in the other Target's HWID vector.
if (hwids_ != t2.hwids_ || ecus_ != t2.ecus_) {
std::shared_ptr<std::map<EcuSerial, HardwareIdentifier>> ecu_map; // Director
std::shared_ptr<std::vector<HardwareIdentifier>> hwid_vector; // Image repo
std::shared_ptr<EcuMap> ecu_map; // Director
std::shared_ptr<std::vector<HardwareIdentifier>> hwid_vector; // Image repo
if (!hwids_.empty() && ecus_.empty() && t2.hwids_.empty() && !t2.ecus_.empty()) {
ecu_map = std::make_shared<std::map<EcuSerial, HardwareIdentifier>>(t2.ecus_);
ecu_map = std::make_shared<EcuMap>(t2.ecus_);
hwid_vector = std::make_shared<std::vector<HardwareIdentifier>>(hwids_);
} else if (!t2.hwids_.empty() && t2.ecus_.empty() && hwids_.empty() && !ecus_.empty()) {
ecu_map = std::make_shared<std::map<EcuSerial, HardwareIdentifier>>(ecus_);
ecu_map = std::make_shared<EcuMap>(ecus_);
hwid_vector = std::make_shared<std::vector<HardwareIdentifier>>(t2.hwids_);
} else {
return false;
Expand Down
9 changes: 5 additions & 4 deletions src/libaktualizr/uptane/tuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,19 @@ class Hash {

std::ostream &operator<<(std::ostream &os, const Hash &h);

using EcuMap = std::map<EcuSerial, HardwareIdentifier>;

class Target {
public:
// From Uptane metadata
Target(std::string filename, const Json::Value &content);
// Internal, does not have type. Only used for reading installation_versions
// list and by various tests.
Target(std::string filename, std::map<EcuSerial, HardwareIdentifier> ecus, std::vector<Hash> hashes, uint64_t length,
std::string correlation_id = "");
Target(std::string filename, EcuMap ecus, std::vector<Hash> hashes, uint64_t length, std::string correlation_id = "");

static Target Unknown();

const std::map<EcuSerial, HardwareIdentifier> &ecus() const { return ecus_; }
const EcuMap &ecus() const { return ecus_; }
std::string filename() const { return filename_; }
std::string sha256Hash() const;
std::string sha512Hash() const;
Expand Down Expand Up @@ -268,7 +269,7 @@ class Target {
bool valid{true};
std::string filename_;
std::string type_;
std::map<EcuSerial, HardwareIdentifier> ecus_; // Director only
EcuMap ecus_; // Director only
std::vector<Hash> hashes_;
std::vector<HardwareIdentifier> hwids_; // Images repo only
Json::Value custom_;
Expand Down
21 changes: 10 additions & 11 deletions src/libaktualizr/uptane/tuf_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ Json::Value generateTarget(const std::string& hash, const int length) {
return target;
}

Json::Value generateDirectorTarget(const std::string& hash, const int length,
const std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier>& ecu_map) {
Json::Value generateDirectorTarget(const std::string& hash, const int length, const Uptane::EcuMap& ecu_map) {
Json::Value target = generateTarget(hash, length);
Json::Value custom;
Json::Value ecus;
Expand Down Expand Up @@ -129,7 +128,7 @@ Json::Value generateImagesTarget(const std::string& hash, const int length,
TEST(Target, Match) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -142,7 +141,7 @@ TEST(Target, Match) {
TEST(Target, MatchDirector) {
Uptane::HardwareIdentifier hwid("first-test");
Uptane::HardwareIdentifier hwid2("second-test");
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
ecu_map.insert({Uptane::EcuSerial("serial2"), hwid2});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -168,7 +167,7 @@ TEST(Target, MatchImages) {
TEST(Target, MatchExtraHwId) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -183,7 +182,7 @@ TEST(Target, MatchTwo) {
Uptane::HardwareIdentifier hwid("first-test");
Uptane::HardwareIdentifier hwid2("second-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
hardwareIds.emplace_back(hwid2);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Expand Down Expand Up @@ -211,7 +210,7 @@ TEST(Target, MultipleHwIdMismatch) {
TEST(Target, MissingHwId) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -225,7 +224,7 @@ TEST(Target, MissingHwId) {
TEST(Target, FilenameMismatch) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -238,7 +237,7 @@ TEST(Target, FilenameMismatch) {
TEST(Target, LengthMismatch) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -251,7 +250,7 @@ TEST(Target, LengthMismatch) {
TEST(Target, HardwareIdMismatch) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand All @@ -265,7 +264,7 @@ TEST(Target, HardwareIdMismatch) {
TEST(Target, HashMismatch) {
Uptane::HardwareIdentifier hwid("fake-test");
std::vector<Uptane::HardwareIdentifier> hardwareIds;
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map;
Uptane::EcuMap ecu_map;
hardwareIds.emplace_back(hwid);
ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
Expand Down
2 changes: 1 addition & 1 deletion tests/uptane_vector_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST_P(UptaneVector, Test) {
Uptane::EcuSerial ecu_serial(config.provision.primary_ecu_serial);
Uptane::HardwareIdentifier hw_id(config.provision.primary_ecu_hardware_id);
uptane_client->hw_ids.insert(std::make_pair(ecu_serial, hw_id));
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> ecu_map{{ecu_serial, hw_id}};
Uptane::EcuMap ecu_map{{ecu_serial, hw_id}};
Uptane::Target target("test_filename", ecu_map, {{Uptane::Hash::Type::kSha256, "sha256"}}, 1, "");
storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);

Expand Down

0 comments on commit 907e488

Please sign in to comment.