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

Commit

Permalink
Fix tests for matching Target metadata.
Browse files Browse the repository at this point in the history
This required adding functionality to aktualizr-repo to add HWIDs to the
image repo.

Signed-off-by: Patrick Vacek <[email protected]>
  • Loading branch information
pattivacek committed Jul 17, 2019
1 parent bccc1aa commit 644abf9
Show file tree
Hide file tree
Showing 31 changed files with 173 additions and 120 deletions.
2 changes: 1 addition & 1 deletion docs/aktualizr-repo.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ aktualizr-repo --path <repo path> --command generate
2. Add a target to the images metadata:
+
```
aktualizr-repo --path <repo path> --command image --filename <image name> --targetname <target name>
aktualizr-repo --path <repo path> --command image --filename <image name> --targetname <target name> --hwid <hardware ID>
```
+
This step can be repeated as many times as necessary for each target. `--targetname` is optional. If it is not provided, it is assumed to be the same as the image name provided to `--filename`.
Expand Down
21 changes: 13 additions & 8 deletions src/aktualizr_info/aktualizr_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,12 @@ 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}};
db_storage_->savePrimaryInstalledVersion(
{"update.bin", {{Uptane::Hash::Type::kSha256, current_ecu_version}}, 1, "corrid"},
{"update.bin", ecu_map, {{Uptane::Hash::Type::kSha256, current_ecu_version}}, 1, "corrid"},
InstalledVersionUpdateMode::kCurrent);
db_storage_->savePrimaryInstalledVersion(
{"update-01.bin", {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
{"update-01.bin", ecu_map, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
InstalledVersionUpdateMode::kPending);

aktualizr_info_process_.run();
Expand Down Expand Up @@ -420,8 +421,9 @@ 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}};
db_storage_->savePrimaryInstalledVersion(
{"update-01.bin", {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
{"update-01.bin", ecu_map, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
InstalledVersionUpdateMode::kPending);

aktualizr_info_process_.run();
Expand All @@ -431,7 +433,7 @@ TEST_F(AktualizrInfoTest, PrintPrimaryEcuCurrentAndPendingVersionsNegative) {
EXPECT_NE(aktualizr_info_output.find("Pending primary ecu version: " + pending_ecu_version), std::string::npos);

db_storage_->savePrimaryInstalledVersion(
{"update-01.bin", {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
{"update-01.bin", ecu_map, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1, "corrid-01"},
InstalledVersionUpdateMode::kCurrent);

aktualizr_info_process_.run();
Expand Down Expand Up @@ -461,12 +463,15 @@ TEST_F(AktualizrInfoTest, PrintSecondaryEcuCurrentAndPendingVersions) {
db_storage_->storeEcuSerials({{primary_ecu_serial, primary_hw_id}, {secondary_ecu_serial, secondary_hw_id}});
db_storage_->storeEcuRegistered();

db_storage_->saveInstalledVersion(secondary_ecu_serial,
{secondary_ecu_filename, {{Uptane::Hash::Type::kSha256, current_ecu_version}}, 1},
InstalledVersionUpdateMode::kCurrent);
std::map<Uptane::EcuSerial, Uptane::HardwareIdentifier> 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},
InstalledVersionUpdateMode::kCurrent);

db_storage_->saveInstalledVersion(
secondary_ecu_serial, {secondary_ecu_filename_update, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1},
secondary_ecu_serial.ToString(),
{secondary_ecu_filename_update, ecu_map, {{Uptane::Hash::Type::kSha256, pending_ecu_version}}, 1},
InstalledVersionUpdateMode::kPending);

aktualizr_info_process_.run();
Expand Down
3 changes: 2 additions & 1 deletion src/aktualizr_lite/test_lite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ add_target() {
}
EOF
akrepo --command image \
--targetname $name --targetsha256 $sha --targetlength 0 --targetcustom $custom_json
--targetname $name --targetsha256 $sha --targetlength 0 \
--hwid hwid-for-test --targetcustom $custom_json
}

akrepo --command generate --expires 2021-07-04T16:33:27Z
Expand Down
1 change: 1 addition & 0 deletions src/aktualizr_repo/director_repo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void DirectorRepo::addTarget(const std::string &target_name, const Json::Value &
"!");
}
director_targets["targets"][target_name] = target;
director_targets["targets"][target_name]["custom"].removeMember("hardwareIds");
director_targets["targets"][target_name]["custom"]["ecuIdentifiers"][ecu_serial]["hardwareId"] = hardware_id;
director_targets["version"] = (Utils::parseJSONFile(current)["signed"]["version"].asUInt()) + 1;
Utils::writeFile(staging, Utils::jsonToCanonicalStr(director_targets));
Expand Down
14 changes: 9 additions & 5 deletions src/aktualizr_repo/image_repo.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include "image_repo.h"

void ImageRepo::addImage(const std::string &name, const Json::Value &target, const Delegation &delegation) {
void ImageRepo::addImage(const std::string &name, Json::Value &target, const std::string &hardware_id,
const Delegation &delegation) {
boost::filesystem::path repo_dir(path_ / "repo/image");

boost::filesystem::path targets_path =
delegation ? ((repo_dir / "delegations") / delegation.name).string() + ".json" : repo_dir / "targets.json";
Json::Value targets = Utils::parseJSONFile(targets_path)["signed"];
// TODO: support multiple hardware IDs.
target["custom"]["hardwareIds"][0] = hardware_id;
targets["targets"][name] = target;
targets["version"] = (targets["version"].asUInt()) + 1;

Expand All @@ -16,7 +19,7 @@ void ImageRepo::addImage(const std::string &name, const Json::Value &target, con
}

void ImageRepo::addBinaryImage(const boost::filesystem::path &image_path, const boost::filesystem::path &targetname,
const Delegation &delegation) {
const std::string &hardware_id, const Delegation &delegation) {
boost::filesystem::path repo_dir(path_ / "repo/image");

boost::filesystem::path targets_path = repo_dir / "targets";
Expand All @@ -34,11 +37,12 @@ void ImageRepo::addBinaryImage(const boost::filesystem::path &image_path, const
target["hashes"]["sha256"] = boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(image)));
target["hashes"]["sha512"] = boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha512digest(image)));
target["custom"]["targetFormat"] = "BINARY";
addImage(targetname.string(), target, delegation);
addImage(targetname.string(), target, hardware_id, delegation);
}

void ImageRepo::addCustomImage(const std::string &name, const Uptane::Hash &hash, const uint64_t length,
const Delegation &delegation, const Json::Value &custom) {
const std::string &hardware_id, const Delegation &delegation,
const Json::Value &custom) {
Json::Value target;
target["length"] = Json::UInt(length);
if (hash.type() == Uptane::Hash::Type::kSha256) {
Expand All @@ -47,7 +51,7 @@ void ImageRepo::addCustomImage(const std::string &name, const Uptane::Hash &hash
target["hashes"]["sha512"] = hash.HashString();
}
target["custom"] = custom;
addImage(name, target, delegation);
addImage(name, target, hardware_id, delegation);
}

void ImageRepo::addDelegation(const Uptane::Role &name, const Uptane::Role &parent_role, const std::string &path,
Expand Down
9 changes: 5 additions & 4 deletions src/aktualizr_repo/image_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ class ImageRepo : public Repo {
ImageRepo(boost::filesystem::path path, const std::string &expires, std::string correlation_id)
: Repo(Uptane::RepositoryType::Image(), std::move(path), expires, std::move(correlation_id)) {}
void addBinaryImage(const boost::filesystem::path &image_path, const boost::filesystem::path &targetname,
const Delegation &delegation = {});
void addCustomImage(const std::string &name, const Uptane::Hash &hash, uint64_t length, const Delegation &delegation,
const Json::Value &custom = {});
const std::string &hardware_id, const Delegation &delegation = {});
void addCustomImage(const std::string &name, const Uptane::Hash &hash, uint64_t length,
const std::string &hardware_id, const Delegation &delegation, const Json::Value &custom = {});
void addDelegation(const Uptane::Role &name, const Uptane::Role &parent_role, const std::string &path,
bool terminating, KeyType key_type);
void revokeDelegation(const Uptane::Role &name);
std::vector<std::string> getDelegationTargets(const Uptane::Role &name);

private:
void addImage(const std::string &name, const Json::Value &target, const Delegation &delegation = {});
void addImage(const std::string &name, Json::Value &target, const std::string &hardware_id,
const Delegation &delegation = {});
void removeDelegationRecursive(const Uptane::Role &name, const Uptane::Role &parent_name);
};

Expand Down
15 changes: 10 additions & 5 deletions src/aktualizr_repo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ int main(int argc, char **argv) {
std::cerr << "image command requires --targetname or --filename\n";
exit(EXIT_FAILURE);
}
if (vm.count("hwid") == 0) {
std::cerr << "image command requires --hwid\n";
exit(EXIT_FAILURE);
}
auto targetname = (vm.count("targetname") > 0) ? vm["targetname"].as<std::string>()
: vm["filename"].as<boost::filesystem::path>();
const std::string hwid = vm["hwid"].as<std::string>();

Delegation delegation;
if (vm.count("dname") != 0) {
Expand All @@ -111,7 +116,7 @@ int main(int argc, char **argv) {
std::cout << "Added a target " << targetname << " to a delegated role " << dname << std::endl;
}
if (vm.count("filename") > 0) {
repo.addImage(vm["filename"].as<boost::filesystem::path>(), targetname, delegation);
repo.addImage(vm["filename"].as<boost::filesystem::path>(), targetname, hwid, delegation);
std::cout << "Added a target " << targetname << " to the images metadata" << std::endl;
} else {
if ((vm.count("targetsha256") == 0 && vm.count("targetsha512") == 0) || vm.count("targetlength") == 0) {
Expand All @@ -137,17 +142,17 @@ int main(int argc, char **argv) {
custom = Json::Value();
custom["targetFormat"] = vm["targetformat"].as<std::string>();
}
repo.addCustomImage(targetname.string(), *hash, vm["targetlength"].as<uint64_t>(), delegation, custom);
repo.addCustomImage(targetname.string(), *hash, vm["targetlength"].as<uint64_t>(), hwid, delegation, custom);
std::cout << "Added a custom image target " << targetname.string() << std::endl;
}
} else if (command == "addtarget") {
if (vm.count("targetname") == 0 || vm.count("hwid") == 0 || vm.count("serial") == 0) {
std::cerr << "addtarget command requires --targetname, --hwid, and --serial\n";
exit(EXIT_FAILURE);
}
std::string targetname = vm["targetname"].as<std::string>();
std::string hwid = vm["hwid"].as<std::string>();
std::string serial = vm["serial"].as<std::string>();
const std::string targetname = vm["targetname"].as<std::string>();
const std::string hwid = vm["hwid"].as<std::string>();
const std::string serial = vm["serial"].as<std::string>();
repo.addTarget(targetname, hwid, serial);
std::cout << "Added target " << targetname << " to director targets metadata for ECU with serial " << serial
<< " and hardware ID " << hwid << std::endl;
Expand Down
Loading

0 comments on commit 644abf9

Please sign in to comment.