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 24, 2019
1 parent 3c26e78 commit ef2f7c6
Show file tree
Hide file tree
Showing 38 changed files with 206 additions and 174 deletions.
1 change: 1 addition & 0 deletions src/aktualizr_repo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(AKTUALIZR_REPO_LIBS
${AKTUALIZR_EXTERNAL_LIBS}
)
add_library(aktualizr_repo_lib ${AKTUALIZR_REPO_SRC})
target_include_directories(aktualizr_repo_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(aktualizr-repo main.cc ${AKTUALIZR_REPO_SRC})
target_link_libraries(aktualizr-repo ${AKTUALIZR_REPO_LIBS})

Expand Down
1 change: 1 addition & 0 deletions src/aktualizr_secondary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ add_aktualizr_test(NAME aktualizr_secondary_update
if(BUILD_OSTREE)
add_aktualizr_test(NAME aktualizr_secondary_uptane
SOURCES uptane_test.cc
LIBRARIES PUBLIC aktualizr_repo_lib
LIBRARIES aktualizr-posix
ARGS ${PROJECT_BINARY_DIR}/ostree_repo PROJECT_WORKING_DIRECTORY)
else(BUILD_OSTREE)
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/package_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif(BUILD_OSTREE)

add_aktualizr_test(NAME packagemanager_factory SOURCES packagemanagerfactory_test.cc NO_VALGRIND
ARGS ${PROJECT_BINARY_DIR}/ostree_repo)
add_aktualizr_test(NAME fetcher SOURCES fetcher_test.cc ARGS PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME fetcher SOURCES fetcher_test.cc ARGS PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)
add_aktualizr_test(NAME fetcher_death SOURCES fetcher_death_test.cc NO_VALGRIND ARGS PROJECT_WORKING_DIRECTORY)

aktualizr_source_file_checks(fetcher_death_test.cc fetcher_test.cc)
Expand Down
8 changes: 4 additions & 4 deletions src/libaktualizr/primary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(HEADERS aktualizr.h

add_library(primary OBJECT ${SOURCES})

add_aktualizr_test(NAME aktualizr SOURCES aktualizr_test.cc PROJECT_WORKING_DIRECTORY ARGS ${PROJECT_BINARY_DIR}/uptane_repos)
add_aktualizr_test(NAME aktualizr SOURCES aktualizr_test.cc PROJECT_WORKING_DIRECTORY ARGS ${PROJECT_BINARY_DIR}/uptane_repos LIBRARIES PUBLIC aktualizr_repo_lib)
add_dependencies(t_aktualizr uptane_repo_full_no_correlation_id)

if (BUILD_OSTREE)
Expand All @@ -25,10 +25,10 @@ else (BUILD_OSTREE)
aktualizr_source_file_checks(aktualizr_fullostree_test.cc)
endif (BUILD_OSTREE)

add_aktualizr_test(NAME reportqueue SOURCES reportqueue_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME reportqueue SOURCES reportqueue_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)
add_aktualizr_test(NAME emptytargets SOURCES empty_targets_test.cc PROJECT_WORKING_DIRECTORY
ARGS "$<TARGET_FILE:aktualizr-repo>")
add_aktualizr_test(NAME device_cred_prov SOURCES device_cred_prov_test.cc PROJECT_WORKING_DIRECTORY)
ARGS "$<TARGET_FILE:aktualizr-repo>" LIBRARIES PUBLIC aktualizr_repo_lib)
add_aktualizr_test(NAME device_cred_prov SOURCES device_cred_prov_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)
set_tests_properties(test_device_cred_prov PROPERTIES LABELS "crypto")


Expand Down
12 changes: 6 additions & 6 deletions src/libaktualizr/uptane/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ else(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS)
endif(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS)


add_aktualizr_test(NAME uptane SOURCES uptane_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME uptane SOURCES uptane_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)
set_tests_properties(test_uptane PROPERTIES LABELS "crypto")

add_aktualizr_test(NAME uptane_delegation SOURCES uptane_delegation_test.cc PROJECT_WORKING_DIRECTORY
ARGS "$<TARGET_FILE:aktualizr-repo>")
ARGS "$<TARGET_FILE:aktualizr-repo>" LIBRARIES PUBLIC aktualizr_repo_lib)
add_dependencies(t_uptane_delegation aktualizr-repo)
set_tests_properties(test_uptane_delegation PROPERTIES LABELS "crypto")

add_aktualizr_test(NAME uptane_key SOURCES uptane_key_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME uptane_key SOURCES uptane_key_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)
set_tests_properties(test_uptane_key PROPERTIES LABELS "crypto")

add_aktualizr_test(NAME uptane_network SOURCES uptane_network_test.cc PROJECT_WORKING_DIRECTORY)
set_tests_properties(test_uptane_network PROPERTIES LABELS "crypto")

add_aktualizr_test(NAME uptane_secondary SOURCES uptane_secondary_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME uptane_secondary SOURCES uptane_secondary_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)

add_aktualizr_test(NAME uptane_serial SOURCES uptane_serial_test.cc ARGS ${PROJECT_BINARY_DIR}
PROJECT_WORKING_DIRECTORY)
PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)

add_aktualizr_test(NAME uptane_init SOURCES uptane_init_test.cc PROJECT_WORKING_DIRECTORY)
add_aktualizr_test(NAME uptane_init SOURCES uptane_init_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC aktualizr_repo_lib)

add_aktualizr_test(NAME director SOURCES director_test.cc PROJECT_WORKING_DIRECTORY
ARGS "$<TARGET_FILE:aktualizr-repo>")
Expand Down
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_
126 changes: 126 additions & 0 deletions tests/metafake.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#ifndef METAFAKE_H_
#define METAFAKE_H_

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

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

enum BackupList {
};

class MetaFake {
public:
MetaFake(const boost::filesystem::path &meta_dir_in)
: meta_dir(meta_dir_in), repo(meta_dir_in, "2021-07-04T16:33:27Z", "id0") {
// generate original repo data
repo.generateRepo(KeyType::kRSA2048);
backup();
create_image();
create_testData();
}

private:
void create_testData(void) {
boost::filesystem::path file_name;
Delegation delegation;

// add image for "has update" meta
file_name = "dummy_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);

file_name = "primary_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);
repo.addTarget(file_name.string(), "primary_hw", "CA:FE:A6:D2:84:9D");

file_name = "secondary_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);
repo.addTarget(file_name.string(), "secondary_hw", "secondary_ecu_serial");

repo.signTargets();
rename("_hasupdates");

// add image for "no update" meta
restore();

file_name = "dummy_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);

repo.signTargets();
rename("_noupdates");

// add image for "multi secondary ecu" meta
restore();

file_name = "dummy_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);

file_name = "secondary_firmware.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);
repo.addTarget(file_name.string(), "sec_hwid1", "sec_serial1");

file_name = "secondary_firmware2.txt";
repo.addImage(meta_dir / file_name, file_name, delegation);
repo.addTarget(file_name.string(), "sec_hwid2", "sec_serial2");

repo.signTargets();
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) {
backup_files.push_back(meta_dir / "repo/director/targets.json");
backup_content.push_back(Utils::readFile(backup_files[0], false));

backup_files.push_back(meta_dir / "repo/image/snapshot.json");
backup_content.push_back(Utils::readFile(backup_files[1], false));

backup_files.push_back(meta_dir / "repo/image/targets.json");
backup_content.push_back(Utils::readFile(backup_files[2], false));

backup_files.push_back(meta_dir / "repo/image/timestamp.json");
backup_content.push_back(Utils::readFile(backup_files[3], false));
}

void restore(void) {
for (unsigned int i=0; i < backup_files.size(); i++) {
Utils::writeFile(backup_files[i], backup_content[i]);
}
}

void delete_backup(void) {
for (unsigned int i=0; i < backup_files.size(); i++) {
boost::filesystem::remove(backup_files[i]);
}
}

void rename(const std::string &appendix) {
for (unsigned int i=0; i < backup_files.size(); i++) {
boost::filesystem::rename(backup_files[i],
(backup_files[i].parent_path() / backup_files[i].stem()).string() + appendix + ".json");
}
}

private:
boost::filesystem::path meta_dir;
UptaneRepo repo;
std::vector<boost::filesystem::path> backup_files;
std::vector<std::string> backup_content;
};

#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.

Loading

0 comments on commit ef2f7c6

Please sign in to comment.