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

Commit

Permalink
Implement emptytargets command
Browse files Browse the repository at this point in the history
Signed-off-by: Serhiy Stetskovych <patriotyk@gmail.com>
  • Loading branch information
patriotyk committed Dec 17, 2018
1 parent 08120e7 commit 6904bfa
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/aktualizr_repo/director_repo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ void DirectorRepo::signTargets() {
Utils::writeFile(path_ / "repo/director/targets.json",
Utils::jsonToCanonicalStr(signTuf(Uptane::Role::Targets(), targets_unsigned)));
boost::filesystem::remove(path_ / "repo/director/staging/targets.json");
}
void DirectorRepo::emptyTargets() {
const boost::filesystem::path staging = path_ / "repo/director/staging/targets.json";
Json::Value targets_unsigned;
targets_unsigned = Utils::parseJSONFile(staging);
targets_unsigned["targets"].clear();
Utils::writeFile(staging, Utils::jsonToCanonicalStr(targets_unsigned));
}
1 change: 1 addition & 0 deletions src/aktualizr_repo/director_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DirectorRepo : public Repo {
void addTarget(const std::string &target_name, const Json::Value &target, const std::string &hardware_id,
const std::string &ecu_serial);
void signTargets();
void emptyTargets();
};

#endif // DIRECTOR_REPO_H_
4 changes: 3 additions & 1 deletion src/aktualizr_repo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char **argv) {
// clang-format off
desc.add_options()
("help,h", "print usage")
("command", po::value<std::string>(), "generate|sign|image|addtarget|signtargets")
("command", po::value<std::string>(), "generate|sign|image|addtarget|emptytargets|signtargets")
("path", po::value<boost::filesystem::path>(), "path to the repository")
("filename", po::value<std::string>(), "path to the image")
("hwid", po::value<std::string>(), "target hardware identifier")
Expand Down Expand Up @@ -79,6 +79,8 @@ int main(int argc, char **argv) {
repo.addTarget(vm["filename"].as<std::string>(), vm["hwid"].as<std::string>(), vm["serial"].as<std::string>());
} else if (command == "signtargets") {
repo.signTargets();
} else if (command == "emptytargets") {
repo.emptyTargets();
} else if (command == "sign") {
if (vm.count("repotype") == 0 || vm.count("keyname") == 0) {
std::cerr << "--repotype or --keyname is missing\n";
Expand Down
43 changes: 43 additions & 0 deletions src/aktualizr_repo/repo_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,49 @@ TEST(aktualizr_repo, image_custom) {
EXPECT_EQ(image_targets["signed"]["targets"]["target1"]["length"].asUInt(), 123);
}

TEST(aktualizr_repo, emptytargets) {
TemporaryDirectory temp_dir;
std::ostringstream keytype_stream;
keytype_stream << key_type;
std::string cmd = generate_repo_exec + " generate " + temp_dir.Path().string() + " --keytype " + keytype_stream.str();
std::string output;
int retval = Utils::shell(cmd, &output);
if (retval) {
FAIL() << "'" << cmd << "' exited with error code\n";
}
cmd = generate_repo_exec + " image " + temp_dir.Path().string();
cmd +=
" --targetname target1 --targethash 8ab755c16de6ee9b6224169b36cbf0f2a545f859be385501ad82cdccc240d0a6 "
"--targetlength 123";
retval = Utils::shell(cmd, &output);
if (retval) {
FAIL() << "'" << cmd << "' exited with error code\n";
}

cmd = generate_repo_exec + " addtarget " + temp_dir.Path().string();
cmd += " --filename target1 --hwid hwid123 --serial serial123";
retval = Utils::shell(cmd, &output);
if (retval) {
FAIL() << "'" << cmd << "' exited with error code\n";
}

Json::Value targets = Utils::parseJSONFile(temp_dir.Path() / "repo/director/staging/targets.json");
EXPECT_EQ(targets["targets"].size(), 1);
EXPECT_EQ(targets["targets"]["target1"]["length"].asUInt(), 123);
EXPECT_EQ(targets["targets"]["target1"]["hashes"]["sha256"].asString(),
"8ab755c16de6ee9b6224169b36cbf0f2a545f859be385501ad82cdccc240d0a6");

cmd = generate_repo_exec + " emptytargets " + temp_dir.Path().string();
retval = Utils::shell(cmd, &output);
if (retval) {
FAIL() << "'" << cmd << "' exited with error code\n"
<< "output: " << output;
}

Json::Value empty_targets = Utils::parseJSONFile(temp_dir.Path() / "repo/director/staging/targets.json");
EXPECT_EQ(empty_targets["targets"].size(), 0);
}

#ifndef __NO_MAIN__
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
4 changes: 3 additions & 1 deletion src/aktualizr_repo/uptane_repo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ void UptaneRepo::addImage(const std::string &name, const std::string &hash, uint
image_repo_.addImage(name, hash, length);
}

void UptaneRepo::signTargets() { director_repo_.signTargets(); }
void UptaneRepo::signTargets() { director_repo_.signTargets(); }

void UptaneRepo::emptyTargets() { director_repo_.emptyTargets(); }
1 change: 1 addition & 0 deletions src/aktualizr_repo/uptane_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class UptaneRepo {
void addImage(const boost::filesystem::path &image_path);
void addImage(const std::string &name, const std::string &hash, uint64_t length);
void signTargets();
void emptyTargets();

private:
DirectorRepo director_repo_;
Expand Down

0 comments on commit 6904bfa

Please sign in to comment.