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

Commit

Permalink
For OTA-2712, remove test repo meta
Browse files Browse the repository at this point in the history
remove test repo meta from test/test_data/repo,
generate data from test code using aktualizr-repo

Signed-off-by: cheng.xiang <ext-cheng.xiang@here.com>
  • Loading branch information
xcheng-here committed Jun 20, 2019
1 parent 3c26e78 commit cfcede4
Show file tree
Hide file tree
Showing 33 changed files with 216 additions and 163 deletions.
13 changes: 9 additions & 4 deletions src/libaktualizr/uptane/uptane_secondary_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>

#include "metafake.h"
#include "uptane/partialverificationsecondary.h"
#include "uptane/secondaryconfig.h"
#include "uptane/secondaryfactory.h"
Expand Down Expand Up @@ -79,8 +80,10 @@ TEST(SecondaryFactory, Uptane_putMetadata_good) {
Uptane::PartialVerificationSecondary sec(sconfig);
Uptane::RawMetaPack metadata;

metadata.director_root = Utils::readFile("tests/test_data/repo/repo/director/root.json");
metadata.director_targets = Utils::readFile("tests/test_data/repo/repo/director/targets_hasupdates.json");
boost::filesystem::path meta_dir = temp_dir / "meta_fake";
MetaFake meta(meta_dir);
metadata.director_root = Utils::readFile(meta_dir / "repo/director/root.json");
metadata.director_targets = Utils::readFile(meta_dir / "repo/director/targets_hasupdates.json");
EXPECT_NO_THROW(sec.putMetadata(metadata));
}

Expand All @@ -101,9 +104,11 @@ TEST(SecondaryFactory, Uptane_putMetadata_bad) {
Uptane::PartialVerificationSecondary sec(sconfig);
Uptane::RawMetaPack metadata;

metadata.director_root = Utils::readFile("tests/test_data/repo/repo/director/root.json");
boost::filesystem::path meta_dir = temp_dir / "meta_fake";
MetaFake meta(meta_dir);
metadata.director_root = Utils::readFile(meta_dir / "repo/director/root.json");

Json::Value json_targets = Utils::parseJSONFile("tests/test_data/repo/repo/director/targets_hasupdates.json");
Json::Value json_targets = Utils::parseJSONFile(meta_dir / "repo/director/targets_hasupdates.json");
json_targets["signatures"][0]["sig"] = "Wrong signature";
metadata.director_targets = Utils::jsonToStr(json_targets);
EXPECT_THROW(sec.putMetadata(metadata), Uptane::BadKeyId);
Expand Down
61 changes: 57 additions & 4 deletions tests/httpfake.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "crypto/crypto.h"
#include "http/httpinterface.h"
#include "logging/logging.h"
#include "metafake.h"
#include "utilities/utils.h"

enum class ProvisioningResult { kOK, kFailure };
Expand All @@ -26,10 +27,13 @@ class HttpFake : public HttpInterface {
: test_dir(test_dir_in), flavor_(std::move(flavor)), meta_dir(meta_dir_in) {
if (meta_dir.empty()) {
meta_dir = temp_meta_dir.Path();
Utils::copyDir("tests/test_data/repo/repo/image", meta_dir / "repo");
Utils::copyDir("tests/test_data/repo/repo/director", meta_dir / "director");
if (boost::filesystem::is_directory("tests/test_data/repo/campaigner")) {
Utils::copyDir("tests/test_data/repo/campaigner", meta_dir / "campaigner");

boost::filesystem::path gen_dir = meta_dir / "meta_fake";
MetaFake meta(gen_dir);
Utils::copyDir(gen_dir / "repo/image", meta_dir / "repo");
Utils::copyDir(gen_dir / "repo/director", meta_dir / "director");
if (boost::filesystem::is_directory("tests/test_data/campaigner")) {
Utils::copyDir("tests/test_data/campaigner", meta_dir / "campaigner");
}
}
}
Expand Down Expand Up @@ -173,6 +177,55 @@ class HttpFake : public HttpInterface {
*/
HttpResponse post(const std::string &url, const std::string data);
HttpResponse put(const std::string &url, const std::string data);

void backup_metadata(const boost::filesystem::path &dir) {
director_targets_path = dir / "repo/director/targets.json";
director_targets = Utils::readFile(director_targets_path, false);
image_snapshot_path = dir / "repo/image/snapshot.json";
image_snapshot = Utils::readFile(image_snapshot_path, false);
image_targets_path = dir / "repo/image/targets.json";
image_targets = Utils::readFile(image_targets_path, false);
image_timestamp_path = dir / "repo/image/timestamp.json";
image_timestamp = Utils::readFile(image_timestamp_path, false);
}

void restore_metadata(void) {
Utils::writeFile(director_targets_path, director_targets);
Utils::writeFile(image_snapshot_path, image_snapshot);
Utils::writeFile(image_targets_path, image_targets);
Utils::writeFile(image_timestamp_path, image_timestamp);
}

void delete_backup(void) {
boost::filesystem::remove(director_targets_path);
boost::filesystem::remove(image_snapshot_path);
boost::filesystem::remove(image_targets_path);
boost::filesystem::remove(image_timestamp_path);
}

void rename_metadata(const std::string &appendix) {
boost::filesystem::rename(
director_targets_path,
(director_targets_path.parent_path() / director_targets_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(
image_snapshot_path,
(image_snapshot_path.parent_path() / image_snapshot_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(
image_targets_path,
(image_targets_path.parent_path() / image_targets_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(
image_timestamp_path,
(image_timestamp_path.parent_path() / image_timestamp_path.stem()).string() + appendix + ".json");
}

boost::filesystem::path director_targets_path;
std::string director_targets;
boost::filesystem::path image_snapshot_path;
std::string image_snapshot;
boost::filesystem::path image_targets_path;
std::string image_targets;
boost::filesystem::path image_timestamp_path;
std::string image_timestamp;
};

#endif // HTTPFAKE_H_
149 changes: 149 additions & 0 deletions tests/metafake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#ifndef METAFAKE_H_
#define METAFAKE_H_

#include <boost/filesystem.hpp>
#include <chrono>
#include <fstream>
#include <string>

#include "logging/logging.h"
#include "utilities/utils.h"

#define SHELL_CMD(cmd) if (system(cmd)) {LOG_ERROR << "system command failed: " << cmd;}

class MetaFake {
public:
MetaFake(const boost::filesystem::path &meta_dir_in): meta_dir(meta_dir_in) {
// generate original repo data
cmd_head = "build/src/aktualizr_repo/aktualizr-repo --path " + meta_dir.string() + " --command ";
repo_cmd = cmd_head + "generate --keytype RSA2048 --expires 2021-07-04T16:33:27Z --correlationid id0";
SHELL_CMD(repo_cmd.c_str());
backup();
create_image();
create_testData();
}

private:
void create_testData(void) {
// add image for "has update" meta
std::string image_head = cmd_head + "image --filename " + meta_dir.string() + "/";
repo_cmd = image_head + "dummy_firmware.txt --targetname dummy_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = image_head + "primary_firmware.txt --targetname primary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = image_head + "secondary_firmware.txt --targetname secondary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

std::string addtarget_head = cmd_head + "addtarget --hwid ";
repo_cmd = addtarget_head + "primary_hw --serial CA:FE:A6:D2:84:9D --targetname primary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = addtarget_head + "secondary_hw --serial secondary_ecu_serial --targetname secondary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = cmd_head + "signtargets";
SHELL_CMD(repo_cmd.c_str());

rename("_hasupdates");
restore();

// add image for "no update" meta
image_head = cmd_head + "image --filename " + meta_dir.string() + "/";
repo_cmd = image_head + "dummy_firmware.txt --targetname dummy_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = cmd_head + "signtargets";
SHELL_CMD(repo_cmd.c_str());

rename("_noupdates");
restore();

// add image for "multi secondary ecu" meta
image_head = cmd_head + "image --filename " + meta_dir.string() + "/";
repo_cmd = image_head + "dummy_firmware.txt --targetname dummy_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = image_head + "secondary_firmware.txt --targetname secondary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = image_head + "secondary_firmware2.txt --targetname secondary_firmware2.txt";
SHELL_CMD(repo_cmd.c_str());

addtarget_head = cmd_head + "addtarget --hwid ";
repo_cmd = addtarget_head + "sec_hwid1 --serial sec_serial1 --targetname secondary_firmware.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = addtarget_head + "sec_hwid2 --serial sec_serial2 --targetname secondary_firmware2.txt";
SHELL_CMD(repo_cmd.c_str());

repo_cmd = cmd_head + "signtargets";
SHELL_CMD(repo_cmd.c_str());

rename("_multisec");
delete_backup();
}

void create_image(void) {
std::string content = "Just to increment timestamp, ignore it\n";
Utils::writeFile(meta_dir / "dummy_firmware.txt", content);
content = "This is a dummy firmware file (should never be downloaded)\n";
Utils::writeFile(meta_dir / "primary_firmware.txt", content);
content = "This is content";
Utils::writeFile(meta_dir / "secondary_firmware.txt", content);
content = "This is more content\n";
Utils::writeFile(meta_dir / "secondary_firmware2.txt", content);
}

void backup(void) {
director_targets_path = meta_dir / "repo/director/targets.json";
director_targets = Utils::readFile(director_targets_path, false);
image_snapshot_path = meta_dir / "repo/image/snapshot.json";
image_snapshot = Utils::readFile(image_snapshot_path, false);
image_targets_path = meta_dir / "repo/image/targets.json";
image_targets = Utils::readFile(image_targets_path, false);
image_timestamp_path = meta_dir / "repo/image/timestamp.json";
image_timestamp = Utils::readFile(image_timestamp_path, false);
}

void restore(void) {
Utils::writeFile(director_targets_path, director_targets);
Utils::writeFile(image_snapshot_path, image_snapshot);
Utils::writeFile(image_targets_path, image_targets);
Utils::writeFile(image_timestamp_path, image_timestamp);
}

void delete_backup(void) {
boost::filesystem::remove(director_targets_path);
boost::filesystem::remove(image_snapshot_path);
boost::filesystem::remove(image_targets_path);
boost::filesystem::remove(image_timestamp_path);
}

void rename(const std::string &appendix) {
boost::filesystem::rename(director_targets_path,
(director_targets_path.parent_path() / director_targets_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(image_snapshot_path,
(image_snapshot_path.parent_path() / image_snapshot_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(image_targets_path,
(image_targets_path.parent_path() / image_targets_path.stem()).string() + appendix + ".json");
boost::filesystem::rename(image_timestamp_path,
(image_timestamp_path.parent_path() / image_timestamp_path.stem()).string() + appendix + ".json");
}

private:
boost::filesystem::path meta_dir;
boost::filesystem::path director_targets_path;
std::string director_targets;
boost::filesystem::path image_snapshot_path;
std::string image_snapshot;
boost::filesystem::path image_targets_path;
std::string image_targets;
boost::filesystem::path image_timestamp_path;
std::string image_timestamp;
std::string cmd_head;
std::string repo_cmd;
};

#endif // METAFAKE_H
1 change: 1 addition & 0 deletions tests/test_data/campaigner/campaigns.json
1 change: 0 additions & 1 deletion tests/test_data/repo/campaigner/campaigns.json

This file was deleted.

60 changes: 0 additions & 60 deletions tests/test_data/repo/generate.sh

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_data/repo/keys/director/key_type

This file was deleted.

27 changes: 0 additions & 27 deletions tests/test_data/repo/keys/director/private.key

This file was deleted.

9 changes: 0 additions & 9 deletions tests/test_data/repo/keys/director/public.key

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_data/repo/keys/image/key_type

This file was deleted.

Loading

0 comments on commit cfcede4

Please sign in to comment.