diff --git a/config/secondary/virtualsec.json b/config/secondary/virtualsec.json index 73ad71a1d4..7ac2a3b9a9 100644 --- a/config/secondary/virtualsec.json +++ b/config/secondary/virtualsec.json @@ -1,12 +1,25 @@ { - "secondary_type" : "virtual", - "partial_verifying" : "false", - "ecu_hardware_id" : "demo-virtual", + "virtual": [ + { + "partial_verifying": "false", + "ecu_hardware_id": "demo-virtual", + "full_client_dir": "storage/demo-vsec1", + "ecu_private_key": "sec.private", + "ecu_public_key": "sec.public", + "firmware_path": "storage/demo-vsec1/firmware.bin", + "target_name_path": "storage/demo-vsec1/target_name", + "metadata_path": "storage/demo-vsec1/metadata" + }, + { + "partial_verifying": "false", + "ecu_hardware_id": "demo-virtual", + "full_client_dir": "storage/demo-vsec2", + "ecu_private_key": "sec.private", + "ecu_public_key": "sec.public", + "firmware_path": "storage/demo-vsec2/firmware.bin", + "target_name_path": "storage/demo-vsec2/target_name", + "metadata_path": "storage/demo-vsec2/metadata" - "full_client_dir" : "storage/demo-vsec1", - "ecu_private_key" : "sec.private", - "ecu_public_key" : "sec.public", - "firmware_path" : "storage/demo-vsec1/firmware.bin", - "target_name_path" : "storage/demo-vsec1/target_name", - "metadata_path" : "storage/demo-vsec1/metadata" + } + ] } diff --git a/config/sota-local.toml b/config/sota-local.toml index 078b2ab1f8..4708d5b0cb 100644 --- a/config/sota-local.toml +++ b/config/sota-local.toml @@ -2,9 +2,6 @@ provision_path = "credentials.zip" primary_ecu_hardware_id = "local-fake" -[uptane] -secondary_configs_dir = "secondary_configs" - [logger] loglevel = 1 @@ -13,3 +10,6 @@ path = "storage" [pacman] type = "none" + +[uptane] +secondary_config_file = "virtualsec.json" diff --git a/config/sota-ubuntu.toml b/config/sota-ubuntu.toml index 0977b51f94..4a5f683a55 100644 --- a/config/sota-ubuntu.toml +++ b/config/sota-ubuntu.toml @@ -2,9 +2,6 @@ provision_path = "/var/sota/sota_provisioning_credentials.zip" primary_ecu_hardware_id = "ubuntu" -[uptane] -secondary_configs_dir = "/usr/lib/sota/secondaries" - [storage] type = "sqlite" diff --git a/docs/configuration.adoc b/docs/configuration.adoc index fa687583e5..af500e7350 100644 --- a/docs/configuration.adoc +++ b/docs/configuration.adoc @@ -79,6 +79,7 @@ If you intend to provision with a server by using https://github.com/advancedtel Options for Uptane. [options="header"] +[cols="1,1,20"] |========================================================================================== | Name | Default | Description | `polling_sec` | `10` | Interval between polls (in seconds). @@ -86,8 +87,8 @@ Options for Uptane. | `repo_server` | | Image repository server URL. If empty, set to `tls.server` with `/repo` appended. | `key_source` | `"file"` | Where to read the device's private key from. Options: `"file"`, `"pkcs11"`. | `key_type` | `"RSA2048"` | Type of cryptographic keys to use. Options: `"ED25519"`, `"RSA2048"`, `"RSA3072"` or `"RSA4096"`. -| `secondary_configs_dir` | `""` | Directory containing individual secondary json configuration files. Example here: link:{aktualizr-github-url}/config/secondary/virtualsec.json[]. This is currently only used for virtual secondaries and will be soon replaced entirely by `secondary_configs_file`. -| `secondary_config_file` | `""` | A path to a json file containing configuration of Secondary ECU(s) to be registered with the primary. Example here: link:{aktualizr-github-url}/config/posix-secondary-config.json[]. +| `secondary_config_file` | `""` | A path to a json file containing configuration of Secondary ECU(s) to be registered with the primary. + Examples here: link:{aktualizr-github-url}/config/secondary/virtualsec.json[Virtual Secondary config] and link:{aktualizr-github-url}/config/posix-secondary-config.json[Posix/IP Secondary config] | `force_install_completion`| false | Forces installation completion. Causes a system reboot in case of an ostree package manager. Emulates a reboot in case of a fake package manager. |========================================================================================== diff --git a/docs/secondaries.svg b/docs/secondaries.svg deleted file mode 100644 index d12dbe9748..0000000000 --- a/docs/secondaries.svg +++ /dev/null @@ -1,1001 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - image/svg+xml - - - - - - - SotaUptaneClient - SecondaryInterface - VirtualSecondary - Customer Secondary - UptaneSecondary - - Customer flasher - - Legacy ECU - UDSSecondary - DoIPSecondary - ... - - - - - - - SecondaryFactory - - - - Uptane ECU - Uptane ECU - - - - - - - - - - No border - - Dashed border - - Solid border - - — C++ class - — Executable - — External device - - — Is a parent of - — Communicates - — Creates - — Is given to - - - - - SecondaryConfig - - - - diff --git a/src/aktualizr_primary/CMakeLists.txt b/src/aktualizr_primary/CMakeLists.txt index bfca635e8c..da7651e006 100644 --- a/src/aktualizr_primary/CMakeLists.txt +++ b/src/aktualizr_primary/CMakeLists.txt @@ -2,7 +2,7 @@ set(TARGET aktualizr) set(SOURCES main.cc secondary_config.h secondary_config.cc secondary.h secondary.cc) add_executable(${TARGET} ${SOURCES}) -target_link_libraries(${TARGET} aktualizr_static_lib aktualizr-posix ${AKTUALIZR_EXTERNAL_LIBS}) +target_link_libraries(${TARGET} aktualizr_static_lib aktualizr-posix virtual_secondary ${AKTUALIZR_EXTERNAL_LIBS}) target_include_directories(${TARGET} PUBLIC ${PROJECT_SOURCE_DIR}/src/libaktualizr-posix ${PROJECT_SOURCE_DIR}/third_party) diff --git a/src/aktualizr_primary/main.cc b/src/aktualizr_primary/main.cc index 5738740a7a..d73c54d025 100644 --- a/src/aktualizr_primary/main.cc +++ b/src/aktualizr_primary/main.cc @@ -124,7 +124,12 @@ int main(int argc, char *argv[]) { conn = aktualizr.SetSignalHandler(f_cb); if (!config.uptane.secondary_config_file.empty()) { - Primary::initSecondaries(aktualizr, config.uptane.secondary_config_file); + if (boost::filesystem::exists(config.uptane.secondary_config_file)) { + Primary::initSecondaries(aktualizr, config.uptane.secondary_config_file); + } else { + LOG_WARNING << "The specified secondary config file does not exist: " << config.uptane.secondary_config_file + << "\nProceed further without secondary(ies)"; + } } aktualizr.Initialize(); diff --git a/src/aktualizr_primary/secondary.cc b/src/aktualizr_primary/secondary.cc index c1b5699d98..8ae3a470ce 100644 --- a/src/aktualizr_primary/secondary.cc +++ b/src/aktualizr_primary/secondary.cc @@ -24,6 +24,11 @@ static SecondaryFactoryRegistry sec_factory_registry = { auto ip_sec_cgf = dynamic_cast(config); return createIPSecondaries(ip_sec_cgf); }}, + {VirtualSecondaryConfig::Type, + [](const SecondaryConfig& config) { + auto virtual_sec_cgf = dynamic_cast(config); + return Secondaries({std::make_shared(virtual_sec_cgf)}); + }}, // { // Add another secondary factory here // } diff --git a/src/aktualizr_primary/secondary_config.cc b/src/aktualizr_primary/secondary_config.cc index 25ad6a42c5..a65d041f2c 100644 --- a/src/aktualizr_primary/secondary_config.cc +++ b/src/aktualizr_primary/secondary_config.cc @@ -89,7 +89,7 @@ static std::pair getIPAndPort(const std::string& addr) { return std::make_pair(ip, port); } -void JsonConfigParser::createIPSecondariesCfg(Configs& configs, Json::Value& json_ip_sec_cfg) { +void JsonConfigParser::createIPSecondariesCfg(Configs& configs, const Json::Value& json_ip_sec_cfg) { auto resultant_cfg = std::make_shared( static_cast(json_ip_sec_cfg[IPSecondariesConfig::PortField].asUInt()), json_ip_sec_cfg[IPSecondariesConfig::TimeoutField].asUInt()); @@ -108,4 +108,11 @@ void JsonConfigParser::createIPSecondariesCfg(Configs& configs, Json::Value& jso configs.push_back(resultant_cfg); } +void JsonConfigParser::createVirtualSecondariesCfg(Configs& configs, const Json::Value& json_virtual_sec_cfg) { + for (const auto& json_config : json_virtual_sec_cfg) { + auto virtual_config = std::make_shared(json_config); + configs.push_back(virtual_config); + } +} + } // namespace Primary diff --git a/src/aktualizr_primary/secondary_config.h b/src/aktualizr_primary/secondary_config.h index d3e13233a8..b9d993ef88 100644 --- a/src/aktualizr_primary/secondary_config.h +++ b/src/aktualizr_primary/secondary_config.h @@ -5,17 +5,10 @@ #include #include -namespace Primary { - -class SecondaryConfig { - public: - SecondaryConfig(const char* type) : type_(type) {} - virtual const char* type() const { return type_; } - virtual ~SecondaryConfig() = default; +#include "primary/secondary_config.h" +#include "virtualsecondary.h" - private: - const char* const type_; -}; +namespace Primary { class IPSecondaryConfig { public: @@ -72,14 +65,15 @@ class JsonConfigParser : public SecondaryConfigParser { Configs parse() override; private: - static void createIPSecondariesCfg(Configs& configs, Json::Value& json_ip_sec_cfg); + static void createIPSecondariesCfg(Configs& configs, const Json::Value& json_ip_sec_cfg); + static void createVirtualSecondariesCfg(Configs& configs, const Json::Value& json_virtual_sec_cfg); // add here a factory method for another type of secondary config private: using SecondaryConfigFactoryRegistry = std::unordered_map>; SecondaryConfigFactoryRegistry sec_cfg_factory_registry_ = { - {IPSecondariesConfig::Type, createIPSecondariesCfg} + {IPSecondariesConfig::Type, createIPSecondariesCfg}, {VirtualSecondaryConfig::Type, createVirtualSecondariesCfg} // add here factory method for another type of secondary config }; diff --git a/src/aktualizr_secondary/CMakeLists.txt b/src/aktualizr_secondary/CMakeLists.txt index e19f8d9dec..ee2ae55365 100644 --- a/src/aktualizr_secondary/CMakeLists.txt +++ b/src/aktualizr_secondary/CMakeLists.txt @@ -62,6 +62,7 @@ if(BUILD_OSTREE) SOURCES uptane_test.cc LIBRARIES aktualizr-posix ARGS ${PROJECT_BINARY_DIR}/ostree_repo PROJECT_WORKING_DIRECTORY) + target_link_libraries(t_aktualizr_secondary_uptane virtual_secondary) else(BUILD_OSTREE) list(APPEND TEST_SOURCES uptane_test.cc) endif(BUILD_OSTREE) diff --git a/src/aktualizr_secondary/uptane_test.cc b/src/aktualizr_secondary/uptane_test.cc index 9e2967ab77..989e5a06ae 100644 --- a/src/aktualizr_secondary/uptane_test.cc +++ b/src/aktualizr_secondary/uptane_test.cc @@ -6,6 +6,7 @@ #include "config/config.h" #include "httpfake.h" +#include "uptane_test_common.h" std::shared_ptr test_storage; AktualizrSecondaryConfig test_config; @@ -47,7 +48,7 @@ TEST(aktualizr_secondary_uptane, credentialsPassing) { auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); std::string arch = sota_client->secondaryTreehubCredentials(); diff --git a/src/libaktualizr/CMakeLists.txt b/src/libaktualizr/CMakeLists.txt index 95d7cb4c74..f6163b9383 100644 --- a/src/libaktualizr/CMakeLists.txt +++ b/src/libaktualizr/CMakeLists.txt @@ -41,6 +41,3 @@ endif (BUILD_ISOTP) target_include_directories(aktualizr_static_lib PUBLIC $) - -# To be removed once the refactoring is completed -target_link_libraries(aktualizr_static_lib virtual_secondary) diff --git a/src/libaktualizr/config/config.cc b/src/libaktualizr/config/config.cc index 447abe3a75..771d7e1e0b 100644 --- a/src/libaktualizr/config/config.cc +++ b/src/libaktualizr/config/config.cc @@ -58,8 +58,6 @@ void UptaneConfig::updateFromPropertyTree(const boost::property_tree::ptree& pt) CopyFromConfig(key_type, "key_type", pt); CopyFromConfig(force_install_completion, "force_install_completion", pt); CopyFromConfig(secondary_config_file, "secondary_config_file", pt); - CopyFromConfig(secondary_configs_dir, "secondary_configs_dir", pt); - // uptane.secondary_configs is populated by processing secondary_configs_dir } void UptaneConfig::writeToStream(std::ostream& out_stream) const { @@ -70,7 +68,6 @@ void UptaneConfig::writeToStream(std::ostream& out_stream) const { writeOption(out_stream, key_type, "key_type"); writeOption(out_stream, force_install_completion, "force_install_completion"); writeOption(out_stream, secondary_config_file, "secondary_config_file"); - writeOption(out_stream, secondary_configs_dir, "secondary_configs_dir"); } /** @@ -166,10 +163,6 @@ void Config::postUpdateValues() { } } - if (!uptane.secondary_configs_dir.empty()) { - readSecondaryConfigs(uptane.secondary_configs_dir); - } - LOG_TRACE << "Final configuration that will be used: \n" << (*this); } @@ -226,20 +219,6 @@ void Config::updateFromCommandLine(const boost::program_options::variables_map& if (cmd.count("secondary-config-file") != 0) { uptane.secondary_config_file = cmd["secondary_config_file"].as(); } - if (cmd.count("secondary-configs-dir") != 0) { - uptane.secondary_configs_dir = cmd["secondary-configs-dir"].as(); - } -} - -void Config::readSecondaryConfigs(const boost::filesystem::path& sconfigs_dir) { - if (!boost::filesystem::is_directory(sconfigs_dir)) { - LOG_ERROR << "Could not read secondary configs from " << sconfigs_dir << ": not a directory"; - return; - } - for (const auto& config_file : Utils::getDirEntriesByExt(sconfigs_dir, ".json")) { - LOG_INFO << "Parsing secondary config: " << config_file; - uptane.secondary_configs.emplace_back(config_file); - } } void Config::writeToStream(std::ostream& sink) const { diff --git a/src/libaktualizr/config/config.h b/src/libaktualizr/config/config.h index 2e017dfd9c..1591145542 100644 --- a/src/libaktualizr/config/config.h +++ b/src/libaktualizr/config/config.h @@ -16,7 +16,6 @@ #include "package_manager/packagemanagerconfig.h" #include "storage/storage_config.h" #include "telemetry/telemetryconfig.h" -#include "uptane/secondaryconfig.h" #include "utilities/config_utils.h" #include "utilities/types.h" @@ -58,9 +57,7 @@ struct UptaneConfig { CryptoSource key_source{CryptoSource::kFile}; KeyType key_type{KeyType::kRSA2048}; bool force_install_completion{false}; - boost::filesystem::path secondary_configs_dir; boost::filesystem::path secondary_config_file; - std::vector secondary_configs{}; void updateFromPropertyTree(const boost::property_tree::ptree& pt); void writeToStream(std::ostream& out_stream) const; @@ -102,7 +99,6 @@ class Config : public BaseConfig { private: void updateFromPropertyTree(const boost::property_tree::ptree& pt) override; void updateFromCommandLine(const boost::program_options::variables_map& cmd); - void readSecondaryConfigs(const boost::filesystem::path& sconfigs_dir); std::vector config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"}; bool loglevel_from_cmdline{false}; diff --git a/src/libaktualizr/config/config_test.cc b/src/libaktualizr/config/config_test.cc index e1a59d3e40..54a155a213 100644 --- a/src/libaktualizr/config/config_test.cc +++ b/src/libaktualizr/config/config_test.cc @@ -95,33 +95,6 @@ TEST(config, ExtractCredentials) { "D27E3E56BEF02AAA6D6FFEFDA5357458C477A8E891C5EADF4F04CE67BB5866A4"); } -/* - * Parse secondary config files in JSON format. - */ -TEST(config, SecondaryConfig) { - TemporaryDirectory temp_dir; - const std::string conf_path_str = (temp_dir.Path() / "config.toml").string(); - TestUtils::writePathToConfig("tests/config/minimal.toml", conf_path_str, temp_dir.Path()); - - bpo::variables_map cmd; - bpo::options_description description("some text"); - // clang-format off - description.add_options() - ("secondary-configs-dir", bpo::value(), "directory containing secondary ECU configuration files") - ("config,c", bpo::value >()->composing(), "configuration file or directory"); - - // clang-format on - const char *argv[] = {"aktualizr", "--secondary-configs-dir", "config/secondary", "-c", conf_path_str.c_str()}; - bpo::store(bpo::parse_command_line(5, argv, description), cmd); - - Config conf(cmd); - EXPECT_EQ(conf.uptane.secondary_configs.size(), 1); - EXPECT_EQ(conf.uptane.secondary_configs[0].secondary_type, Uptane::SecondaryType::kVirtual); - EXPECT_EQ(conf.uptane.secondary_configs[0].ecu_hardware_id, "demo-virtual"); - // If not provided, serial is not generated until SotaUptaneClient is initialized. - EXPECT_TRUE(conf.uptane.secondary_configs[0].ecu_serial.empty()); -} - /** * Start in device credential provisioning mode. */ diff --git a/src/libaktualizr/primary/CMakeLists.txt b/src/libaktualizr/primary/CMakeLists.txt index 6d00e0d53e..42f856ab97 100644 --- a/src/libaktualizr/primary/CMakeLists.txt +++ b/src/libaktualizr/primary/CMakeLists.txt @@ -3,7 +3,8 @@ set(SOURCES aktualizr.cc reportqueue.cc sotauptaneclient.cc) -set(HEADERS aktualizr.h +set(HEADERS secondary_config.h + aktualizr.h events.h initializer.h reportqueue.h @@ -12,7 +13,6 @@ set(HEADERS aktualizr.h add_library(primary OBJECT ${SOURCES}) -target_include_directories(primary PUBLIC ${PROJECT_SOURCE_DIR}/src/virtual_secondary) add_aktualizr_test(NAME aktualizr SOURCES aktualizr_test.cc PROJECT_WORKING_DIRECTORY ARGS ${PROJECT_BINARY_DIR}/uptane_repos) add_dependencies(t_aktualizr uptane_repo_full_no_correlation_id) @@ -23,6 +23,7 @@ if (BUILD_OSTREE) set_target_properties(t_aktualizr_fullostree PROPERTIES LINK_FLAGS -Wl,--export-dynamic) add_dependencies(t_aktualizr_fullostree ostree_mock aktualizr-repo make_ostree_sysroot) set_tests_properties(test_aktualizr_fullostree PROPERTIES ENVIRONMENT LD_PRELOAD=$) + target_link_libraries(t_aktualizr_fullostree virtual_secondary) else (BUILD_OSTREE) aktualizr_source_file_checks(aktualizr_fullostree_test.cc) endif (BUILD_OSTREE) @@ -30,6 +31,8 @@ endif (BUILD_OSTREE) add_aktualizr_test(NAME reportqueue SOURCES reportqueue_test.cc PROJECT_WORKING_DIRECTORY) add_aktualizr_test(NAME emptytargets SOURCES empty_targets_test.cc PROJECT_WORKING_DIRECTORY ARGS "$") +target_link_libraries(t_emptytargets virtual_secondary) + add_aktualizr_test(NAME device_cred_prov SOURCES device_cred_prov_test.cc PROJECT_WORKING_DIRECTORY) set_tests_properties(test_device_cred_prov PROPERTIES LABELS "crypto") diff --git a/src/libaktualizr/primary/aktualizr.cc b/src/libaktualizr/primary/aktualizr.cc index 09836ce2eb..683048628c 100644 --- a/src/libaktualizr/primary/aktualizr.cc +++ b/src/libaktualizr/primary/aktualizr.cc @@ -6,6 +6,7 @@ #include #include +#include "primary/events.h" #include "utilities/timer.h" using std::make_shared; @@ -24,7 +25,10 @@ Aktualizr::Aktualizr(Config &config, std::shared_ptr storage_in, std systemSetup(); sig_ = make_shared)>>(); storage_ = std::move(storage_in); - uptane_client_ = SotaUptaneClient::newTestClient(config_, storage_, std::move(http_in), sig_); + std::shared_ptr bootloader_in = std::make_shared(config_.bootloader, *storage_); + std::shared_ptr report_queue_in = std::make_shared(config_, http_in); + + uptane_client_ = std::make_shared(config_, storage_, http_in, bootloader_in, report_queue_in, sig_); } void Aktualizr::systemSetup() { diff --git a/src/libaktualizr/primary/aktualizr.h b/src/libaktualizr/primary/aktualizr.h index 38749e89e8..c0348c84d4 100644 --- a/src/libaktualizr/primary/aktualizr.h +++ b/src/libaktualizr/primary/aktualizr.h @@ -158,43 +158,16 @@ class Aktualizr { */ boost::signals2::connection SetSignalHandler(const std::function)>& handler); - private: - FRIEND_TEST(Aktualizr, FullNoUpdates); - FRIEND_TEST(Aktualizr, DeviceInstallationResult); - FRIEND_TEST(Aktualizr, FullWithUpdates); - FRIEND_TEST(Aktualizr, FullWithUpdatesNeedReboot); - FRIEND_TEST(Aktualizr, FinalizationFailure); - FRIEND_TEST(Aktualizr, InstallationFailure); - FRIEND_TEST(Aktualizr, AutoRebootAfterUpdate); - FRIEND_TEST(Aktualizr, FullMultipleSecondaries); - FRIEND_TEST(Aktualizr, CheckNoUpdates); - FRIEND_TEST(Aktualizr, DownloadWithUpdates); - FRIEND_TEST(Aktualizr, DownloadFailures); - FRIEND_TEST(Aktualizr, InstallWithUpdates); - FRIEND_TEST(Aktualizr, ReportDownloadProgress); - FRIEND_TEST(Aktualizr, CampaignCheckAndControl); - FRIEND_TEST(Aktualizr, FullNoCorrelationId); - FRIEND_TEST(Aktualizr, ManifestCustom); - FRIEND_TEST(Aktualizr, APICheck); - FRIEND_TEST(Aktualizr, UpdateCheckCompleteError); - FRIEND_TEST(Aktualizr, PauseResumeQueue); - FRIEND_TEST(Aktualizr, AddSecondary); - FRIEND_TEST(Aktualizr, EmptyTargets); - FRIEND_TEST(Aktualizr, EmptyTargetsAfterInstall); - FRIEND_TEST(Aktualizr, FullOstreeUpdate); - FRIEND_TEST(Delegation, Basic); - FRIEND_TEST(Delegation, RevokeAfterCheckUpdates); - FRIEND_TEST(Delegation, RevokeAfterInstall); - FRIEND_TEST(Delegation, RevokeAfterDownload); - FRIEND_TEST(Delegation, IterateAll); - - // This constructor is only used for tests + protected: Aktualizr(Config& config, std::shared_ptr storage_in, std::shared_ptr http_in); + + std::shared_ptr uptane_client_; + + private: static void systemSetup(); Config& config_; std::shared_ptr storage_; - std::shared_ptr uptane_client_; std::shared_ptr sig_; api::CommandQueue api_queue_; }; diff --git a/src/libaktualizr/primary/aktualizr_fullostree_test.cc b/src/libaktualizr/primary/aktualizr_fullostree_test.cc index bcba49fa81..22eed5b5e2 100644 --- a/src/libaktualizr/primary/aktualizr_fullostree_test.cc +++ b/src/libaktualizr/primary/aktualizr_fullostree_test.cc @@ -81,15 +81,14 @@ TEST(Aktualizr, FullOstreeUpdate) { boost::filesystem::remove(conf.bootloader.reboot_sentinel_dir / conf.bootloader.reboot_sentinel_name); { - Aktualizr aktualizr(conf); - + UptaneTestCommon::TestAktualizr aktualizr(conf); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); ASSERT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable); // check new version - const auto target = aktualizr.uptane_client_->package_manager_->getCurrent(); + const auto target = aktualizr.uptane_client()->package_manager_->getCurrent(); EXPECT_EQ(target.sha256Hash(), new_rev); } } diff --git a/src/libaktualizr/primary/aktualizr_test.cc b/src/libaktualizr/primary/aktualizr_test.cc index 5a2ec0a451..80a97477e7 100644 --- a/src/libaktualizr/primary/aktualizr_test.cc +++ b/src/libaktualizr/primary/aktualizr_test.cc @@ -43,10 +43,9 @@ void verifyNothingInstalled(const Json::Value& manifest) { "noimage"); } -static Uptane::SecondaryConfig virtual_configuration(const boost::filesystem::path& client_dir) { - Uptane::SecondaryConfig ecu_config; +static Primary::VirtualSecondaryConfig virtual_configuration(const boost::filesystem::path& client_dir) { + Primary::VirtualSecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; ecu_config.partial_verifying = false; ecu_config.full_client_dir = client_dir; ecu_config.ecu_serial = "ecuserial3"; @@ -106,7 +105,8 @@ TEST(Aktualizr, FullNoUpdates) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + std::function event)> f_cb = process_events_FullNoUpdates; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -120,7 +120,7 @@ TEST(Aktualizr, FullNoUpdates) { FAIL() << "Timed out waiting for metadata to be fetched."; } - verifyNothingInstalled(aktualizr.uptane_client_->AssembleManifest()); + verifyNothingInstalled(aktualizr.uptane_client()->AssembleManifest()); } /* @@ -132,11 +132,11 @@ TEST(Aktualizr, AddSecondary) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); - Uptane::SecondaryConfig ecu_config = virtual_configuration(temp_dir.Path()); + Primary::VirtualSecondaryConfig ecu_config = virtual_configuration(temp_dir.Path()); - aktualizr.AddSecondary(std::make_shared(ecu_config)); + aktualizr.AddSecondary(std::make_shared(ecu_config)); aktualizr.Initialize(); @@ -156,7 +156,7 @@ TEST(Aktualizr, AddSecondary) { EXPECT_EQ(expected_ecus.size(), 0); ecu_config.ecu_serial = "ecuserial4"; - auto sec4 = std::make_shared(ecu_config); + auto sec4 = std::make_shared(ecu_config); EXPECT_THROW(aktualizr.AddSecondary(sec4), std::logic_error); } @@ -176,11 +176,11 @@ TEST(Aktualizr, DeviceInstallationResult) { }; storage->storeEcuSerials(serials); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); - Uptane::SecondaryConfig ecu_config = virtual_configuration(temp_dir.Path()); + Primary::VirtualSecondaryConfig ecu_config = virtual_configuration(temp_dir.Path()); - aktualizr.AddSecondary(std::make_shared(ecu_config)); + aktualizr.AddSecondary(std::make_shared(ecu_config)); aktualizr.Initialize(); @@ -190,7 +190,7 @@ TEST(Aktualizr, DeviceInstallationResult) { data::InstallationResult(data::ResultCode::Numeric::kInstallFailed, "")); data::InstallationResult result; - aktualizr.uptane_client_->computeDeviceInstallationResult(&result, "correlation_id"); + aktualizr.uptane_client()->computeDeviceInstallationResult(&result, "correlation_id"); auto res_json = result.toJson(); EXPECT_EQ(res_json["code"].asString(), "primary_hw:INSTALL_FAILED"); EXPECT_EQ(res_json["success"], false); @@ -198,7 +198,7 @@ TEST(Aktualizr, DeviceInstallationResult) { storage->saveEcuInstallationResult( Uptane::EcuSerial("ecuserial3"), data::InstallationResult(data::ResultCode(data::ResultCode::Numeric::kInstallFailed, "SECOND_FAIL"), "")); - aktualizr.uptane_client_->computeDeviceInstallationResult(&result, "correlation_id"); + aktualizr.uptane_client()->computeDeviceInstallationResult(&result, "correlation_id"); res_json = result.toJson(); EXPECT_EQ(res_json["code"].asString(), "primary_hw:INSTALL_FAILED|hw_id3:SECOND_FAIL"); EXPECT_EQ(res_json["success"], false); @@ -340,7 +340,8 @@ TEST(Aktualizr, FullWithUpdates) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + std::function event)> f_cb = process_events_FullWithUpdates; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -413,8 +414,7 @@ TEST(Aktualizr, FullWithUpdatesNeedReboot) { { // first run: do the install auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); - + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); aktualizr.UptaneCycle(); @@ -435,8 +435,7 @@ TEST(Aktualizr, FullWithUpdatesNeedReboot) { { // second run: before reboot, re-use the storage auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); - + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); // check that everything is still pending @@ -459,8 +458,7 @@ TEST(Aktualizr, FullWithUpdatesNeedReboot) { { // third run: after reboot, re-use the storage auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); - + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_res = aktualizr.CheckUpdates().get(); @@ -617,7 +615,7 @@ TEST(Aktualizr, FinalizationFailure) { { // get update, install them and emulate reboot - Aktualizr aktualizr(conf, storage, http_server_mock); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http_server_mock); EventHandler event_hdlr(aktualizr); aktualizr.Initialize(); @@ -638,9 +636,9 @@ TEST(Aktualizr, FinalizationFailure) { auto aktualizr_cycle_thread_status = aktualizr_cycle_thread.wait_for(std::chrono::seconds(20)); ASSERT_EQ(aktualizr_cycle_thread_status, std::future_status::ready); - EXPECT_TRUE(aktualizr.uptane_client_->bootloader->rebootDetected()); + EXPECT_TRUE(aktualizr.uptane_client()->bootloader->rebootDetected()); EXPECT_TRUE(event_hdlr.checkReceivedEvents(expected_event_order)); - EXPECT_TRUE(aktualizr.uptane_client_->hasPendingUpdates()); + EXPECT_TRUE(aktualizr.uptane_client()->hasPendingUpdates()); EXPECT_TRUE(http_server_mock->checkReceivedReports(expected_report_order)); // Aktualizr reports to a server that installation was successfull for the secondary // checkReceivedReports() verifies whether EcuInstallationApplied was reported for the primary @@ -704,13 +702,13 @@ TEST(Aktualizr, FinalizationFailure) { fiu_enable("fake_install_finalization_failure", 1, nullptr, 0); http_server_mock->clearReportEvents(); - Aktualizr aktualizr(conf, storage, http_server_mock); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http_server_mock); EventHandler event_hdlr(aktualizr); aktualizr.Initialize(); fiu_disable("fake_install_finalization_failure"); - EXPECT_FALSE(aktualizr.uptane_client_->hasPendingUpdates()); + EXPECT_FALSE(aktualizr.uptane_client()->hasPendingUpdates()); EXPECT_TRUE(http_server_mock->checkReceivedReports({"EcuInstallationCompleted"})); EXPECT_FALSE(http_server_mock->wasInstallSuccessful(primary_ecu_id)); @@ -784,7 +782,7 @@ TEST(Aktualizr, InstallationFailure) { fiu_init(0); fiu_enable("fake_package_install", 1, nullptr, 0); - Aktualizr aktualizr(conf, storage, http_server_mock); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http_server_mock); EventHandler event_hdlr(aktualizr); aktualizr.Initialize(); @@ -801,10 +799,10 @@ TEST(Aktualizr, InstallationFailure) { EXPECT_EQ(current_version, SIZE_MAX); aktualizr.UptaneCycle(); - aktualizr.uptane_client_->completeInstall(); + aktualizr.uptane_client()->completeInstall(); EXPECT_TRUE(event_hdlr.checkReceivedEvents(expected_event_order)); - EXPECT_FALSE(aktualizr.uptane_client_->hasPendingUpdates()); + EXPECT_FALSE(aktualizr.uptane_client()->hasPendingUpdates()); EXPECT_TRUE(http_server_mock->checkReceivedReports(expected_report_order)); EXPECT_FALSE(http_server_mock->wasInstallSuccessful(primary_ecu_id)); EXPECT_TRUE(http_server_mock->wasInstallSuccessful(secondary_ecu_id)); @@ -845,7 +843,7 @@ TEST(Aktualizr, InstallationFailure) { fault_injection_enable("fake_package_install", 1, "PRIMFAIL", 0); fault_injection_enable(sec_fault_name.c_str(), 1, "SECFAIL", 0); - Aktualizr aktualizr(conf, storage, http_server_mock); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http_server_mock); EventHandler event_hdlr(aktualizr); aktualizr.Initialize(); @@ -862,10 +860,10 @@ TEST(Aktualizr, InstallationFailure) { EXPECT_EQ(current_version, SIZE_MAX); aktualizr.UptaneCycle(); - aktualizr.uptane_client_->completeInstall(); + aktualizr.uptane_client()->completeInstall(); EXPECT_TRUE(event_hdlr.checkReceivedEvents(expected_event_order)); - EXPECT_FALSE(aktualizr.uptane_client_->hasPendingUpdates()); + EXPECT_FALSE(aktualizr.uptane_client()->hasPendingUpdates()); EXPECT_TRUE(http_server_mock->checkReceivedReports(expected_report_order)); EXPECT_FALSE(http_server_mock->wasInstallSuccessful(primary_ecu_id)); EXPECT_FALSE(http_server_mock->wasInstallSuccessful(secondary_ecu_id)); @@ -927,20 +925,20 @@ TEST(Aktualizr, AutoRebootAfterUpdate) { { // first run: do the install, exit UptaneCycle and emulate reboot since force_install_completion is set auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); auto aktualizr_cycle_thread = aktualizr.RunForever(); auto aktualizr_cycle_thread_status = aktualizr_cycle_thread.wait_for(std::chrono::seconds(20)); EXPECT_EQ(aktualizr_cycle_thread_status, std::future_status::ready); - EXPECT_TRUE(aktualizr.uptane_client_->bootloader->rebootDetected()); + EXPECT_TRUE(aktualizr.uptane_client()->bootloader->rebootDetected()); } { // second run: after emulated reboot, check if the update was applied auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); @@ -1002,10 +1000,13 @@ TEST(Aktualizr, FullMultipleSecondaries) { TemporaryDirectory temp_dir2; UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "sec_serial1", "sec_hwid1"); - UptaneTestCommon::addDefaultSecondary(conf, temp_dir2, "sec_serial2", "sec_hwid2"); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); + UptaneTestCommon::addDefaultSecondary(conf, temp_dir2, "sec_serial2", "sec_hwid2"); + ASSERT_NO_THROW(aktualizr.AddSecondary(std::make_shared( + Primary::VirtualSecondaryConfig::create_from_file(conf.uptane.secondary_config_file)))); + std::function event)> f_cb = process_events_FullMultipleSecondaries; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -1085,7 +1086,7 @@ TEST(Aktualizr, CheckNoUpdates) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); std::function event)> f_cb = process_events_CheckNoUpdates; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -1106,7 +1107,7 @@ TEST(Aktualizr, CheckNoUpdates) { FAIL() << "Timed out waiting for metadata to be fetched."; } - verifyNothingInstalled(aktualizr.uptane_client_->AssembleManifest()); + verifyNothingInstalled(aktualizr.uptane_client()->AssembleManifest()); } int num_events_DownloadWithUpdates = 0; @@ -1179,7 +1180,7 @@ TEST(Aktualizr, DownloadWithUpdates) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); std::function event)> f_cb = process_events_DownloadWithUpdates; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -1196,7 +1197,7 @@ TEST(Aktualizr, DownloadWithUpdates) { FAIL() << "Timed out waiting for downloads to complete."; } - verifyNothingInstalled(aktualizr.uptane_client_->AssembleManifest()); + verifyNothingInstalled(aktualizr.uptane_client()->AssembleManifest()); } class HttpDownloadFailure : public HttpFake { @@ -1299,7 +1300,7 @@ TEST(Aktualizr, DownloadFailures) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); DownloadEventHandler event_hdlr{aktualizr}; aktualizr.Initialize(); @@ -1428,7 +1429,7 @@ TEST(Aktualizr, InstallWithUpdates) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); std::function event)> f_cb = process_events_InstallWithUpdates; boost::signals2::connection conn = aktualizr.SetSignalHandler(f_cb); @@ -1488,7 +1489,7 @@ TEST(Aktualizr, ReportDownloadProgress) { auto http = std::make_shared(temp_dir.Path(), "hasupdates"); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); unsigned int report_counter = {0}; std::shared_ptr lastProgressReport{nullptr}; @@ -1608,7 +1609,7 @@ TEST(Aktualizr, CampaignCheckAndControl) { { auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.SetSignalHandler(std::bind(&CampaignEvents::handler, &campaign_events, std::placeholders::_1)); aktualizr.Initialize(); @@ -1664,7 +1665,7 @@ TEST(Aktualizr, FullNoCorrelationId) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -1690,7 +1691,7 @@ TEST(Aktualizr, ManifestCustom) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); Json::Value custom = Utils::parseJSON(R"({"test_field":"test_value"})"); @@ -1739,7 +1740,7 @@ TEST(Aktualizr, APICheck) { CountUpdateCheckEvents counter; { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); boost::signals2::connection conn = aktualizr.SetSignalHandler(counter.Signal()); aktualizr.Initialize(); std::vector> futures; @@ -1757,7 +1758,7 @@ TEST(Aktualizr, APICheck) { // try again, but shutdown before it finished all calls CountUpdateCheckEvents counter2; { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); boost::signals2::connection conn = aktualizr.SetSignalHandler(counter2.Signal()); aktualizr.Initialize(); @@ -1790,7 +1791,7 @@ TEST(Aktualizr, UpdateCheckCompleteError) { auto storage = INvStorage::newStorage(conf.storage); CountUpdateCheckEvents counter; - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); boost::signals2::connection conn = aktualizr.SetSignalHandler(counter.Signal()); aktualizr.Initialize(); auto result = aktualizr.CheckUpdates().get(); @@ -1837,7 +1838,7 @@ TEST(Aktualizr, PauseResumeQueue) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); std::mutex mutex; diff --git a/src/libaktualizr/primary/empty_targets_test.cc b/src/libaktualizr/primary/empty_targets_test.cc index a0e39c53fa..a48f573461 100644 --- a/src/libaktualizr/primary/empty_targets_test.cc +++ b/src/libaktualizr/primary/empty_targets_test.cc @@ -55,7 +55,7 @@ TEST(Aktualizr, EmptyTargets) { auto storage = INvStorage::newStorage(conf.storage); { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -75,10 +75,10 @@ TEST(Aktualizr, EmptyTargets) { EXPECT_EQ(install_result.ecu_reports[0].install_res.result_code.num_code, data::ResultCode::Numeric::kNeedCompletion); - aktualizr.uptane_client_->package_manager_->completeInstall(); + aktualizr.uptane_client()->package_manager_->completeInstall(); } { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); const Json::Value manifest = http->last_manifest["signed"]; @@ -121,7 +121,7 @@ TEST(Aktualizr, EmptyTargetsAfterInstall) { // failing install auto storage = INvStorage::newStorage(conf.storage); { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -146,7 +146,7 @@ TEST(Aktualizr, EmptyTargetsAfterInstall) { // check that no update is available { - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); diff --git a/src/libaktualizr/primary/secondary_config.h b/src/libaktualizr/primary/secondary_config.h new file mode 100644 index 0000000000..f92457d4c8 --- /dev/null +++ b/src/libaktualizr/primary/secondary_config.h @@ -0,0 +1,18 @@ +#ifndef PRIMARY_SECONDARY_CONFIG_H_ +#define PRIMARY_SECONDARY_CONFIG_H_ + +namespace Primary { + +class SecondaryConfig { + public: + explicit SecondaryConfig(const char* type) : type_(type) {} + virtual const char* type() const { return type_; } + virtual ~SecondaryConfig() = default; + + private: + const char* const type_; +}; + +} // namespace Primary + +#endif // PRIMARY_SECONDARY_CONFIG_H_ diff --git a/src/libaktualizr/primary/sotauptaneclient.cc b/src/libaktualizr/primary/sotauptaneclient.cc index 231670f946..57f5d7e91e 100644 --- a/src/libaktualizr/primary/sotauptaneclient.cc +++ b/src/libaktualizr/primary/sotauptaneclient.cc @@ -12,10 +12,9 @@ #include "logging/logging.h" #include "package_manager/packagemanagerfactory.h" #include "uptane/exceptions.h" -#include "uptane/secondaryconfig.h" + #include "utilities/fault_injection.h" #include "utilities/utils.h" -#include "virtualsecondary.h" static void report_progress_cb(event::Channel *channel, const Uptane::Target &target, const std::string &description, unsigned int progress) { @@ -32,46 +31,18 @@ std::shared_ptr SotaUptaneClient::newDefaultClient( std::shared_ptr bootloader_in = std::make_shared(config_in.bootloader, *storage_in); std::shared_ptr report_queue_in = std::make_shared(config_in, http_client_in); - auto sota_uptane_client = std::make_shared(config_in, storage_in, http_client_in, bootloader_in, - report_queue_in, events_channel_in); - - // This factory method is used just for manual testing of VirtualSecondaries, so calling VirtualSecondary ctor - // directly here is fine for now until the virtual secondary implementation is moved out of libaktualizr - std::vector::const_iterator it; - for (it = config_in.uptane.secondary_configs.begin(); it != config_in.uptane.secondary_configs.end(); ++it) { - sota_uptane_client->addSecondary(std::make_shared(*it)); - } - - return sota_uptane_client; + return std::make_shared(config_in, storage_in, http_client_in, bootloader_in, report_queue_in, + events_channel_in); } -std::shared_ptr SotaUptaneClient::newTestClient(Config &config_in, - std::shared_ptr storage_in, - std::shared_ptr http_client_in, - std::shared_ptr events_channel_in) { - std::shared_ptr bootloader_in = std::make_shared(config_in.bootloader, *storage_in); - std::shared_ptr report_queue_in = std::make_shared(config_in, http_client_in); - auto sota_uptane_client = std::make_shared(config_in, storage_in, http_client_in, bootloader_in, - report_queue_in, events_channel_in); - - // This factory method is used just for automated tests, so calling VirtualSecondary ctor directly here is fine - // for now until the virtual secondary implementation is moved out of libaktualizr - std::vector::const_iterator it; - for (it = config_in.uptane.secondary_configs.begin(); it != config_in.uptane.secondary_configs.end(); ++it) { - sota_uptane_client->addSecondary(std::make_shared(*it)); - } - - return sota_uptane_client; -} - -SotaUptaneClient::SotaUptaneClient(Config &config_in, std::shared_ptr storage_in, +SotaUptaneClient::SotaUptaneClient(Config &config_in, const std::shared_ptr &storage_in, std::shared_ptr http_client, std::shared_ptr bootloader_in, std::shared_ptr report_queue_in, std::shared_ptr events_channel_in) : config(config_in), uptane_manifest(config, storage_in), - storage(std::move(storage_in)), + storage(storage_in), http(std::move(http_client)), bootloader(std::move(bootloader_in)), report_queue(std::move(report_queue_in)), @@ -240,7 +211,6 @@ void SotaUptaneClient::reportNetworkInfo() { Json::Value SotaUptaneClient::AssembleManifest() { Json::Value manifest; // signed top-level Uptane::EcuSerial primary_ecu_serial = uptane_manifest.getPrimaryEcuSerial(); - manifest["primary_ecu_serial"] = primary_ecu_serial.ToString(); // first part: report current version/state of all ecus @@ -963,6 +933,7 @@ void SotaUptaneClient::verifySecondaries() { misconfigured_ecus.emplace_back(not_registered.first, not_registered.second, EcuState::kOld); } } + storage->storeMisconfiguredEcus(misconfigured_ecus); } diff --git a/src/libaktualizr/primary/sotauptaneclient.h b/src/libaktualizr/primary/sotauptaneclient.h index a7b7d1d559..529b8477d5 100644 --- a/src/libaktualizr/primary/sotauptaneclient.h +++ b/src/libaktualizr/primary/sotauptaneclient.h @@ -32,10 +32,8 @@ class SotaUptaneClient { static std::shared_ptr newDefaultClient( Config &config_in, std::shared_ptr storage_in, std::shared_ptr events_channel_in = nullptr); - static std::shared_ptr newTestClient(Config &config_in, std::shared_ptr storage_in, - std::shared_ptr http_client_in, - std::shared_ptr events_channel_in = nullptr); - SotaUptaneClient(Config &config_in, std::shared_ptr storage_in, + + SotaUptaneClient(Config &config_in, const std::shared_ptr &storage_in, std::shared_ptr http_client, std::shared_ptr bootloader_in, std::shared_ptr report_queue_in, std::shared_ptr events_channel_in = nullptr); @@ -65,6 +63,9 @@ class SotaUptaneClient { bool checkImagesMetaOffline(); data::InstallationResult PackageInstall(const Uptane::Target &target); + protected: + void addSecondary(const std::shared_ptr &sec); + private: FRIEND_TEST(Aktualizr, FullNoUpdates); FRIEND_TEST(Aktualizr, DeviceInstallationResult); @@ -108,7 +109,6 @@ class SotaUptaneClient { void reportHwInfo(); void reportInstalledPackages(); void reportNetworkInfo(); - void addSecondary(const std::shared_ptr &sec); void verifySecondaries(); void sendMetadataToEcus(const std::vector &targets); std::future sendFirmwareAsync(Uptane::SecondaryInterface &secondary, const std::shared_ptr &data); @@ -143,7 +143,7 @@ class SotaUptaneClient { std::shared_ptr package_manager_; std::shared_ptr http; std::shared_ptr uptane_fetcher; - const std::shared_ptr bootloader; + std::shared_ptr bootloader; std::shared_ptr report_queue; Json::Value last_network_info_reported; std::map hw_ids; diff --git a/src/libaktualizr/primary/uptane_key_test.cc b/src/libaktualizr/primary/uptane_key_test.cc index 92c271930a..1342f723d3 100644 --- a/src/libaktualizr/primary/uptane_key_test.cc +++ b/src/libaktualizr/primary/uptane_key_test.cc @@ -14,11 +14,12 @@ #include "primary/reportqueue.h" #include "primary/sotauptaneclient.h" #include "storage/invstorage.h" -#include "uptane/secondaryconfig.h" #include "uptane/uptanerepository.h" +#include "uptane_test_common.h" -void initKeyTests(Config& config, Uptane::SecondaryConfig& ecu_config1, Uptane::SecondaryConfig& ecu_config2, - TemporaryDirectory& temp_dir, const std::string& tls_server) { +void initKeyTests(Config& config, Primary::VirtualSecondaryConfig& ecu_config1, + Primary::VirtualSecondaryConfig& ecu_config2, TemporaryDirectory& temp_dir, + const std::string& tls_server) { boost::filesystem::copy_file("tests/test_data/cred.zip", temp_dir / "cred.zip"); config.provision.primary_ecu_serial = "testecuserial"; config.provision.provision_path = temp_dir / "cred.zip"; @@ -29,7 +30,6 @@ void initKeyTests(Config& config, Uptane::SecondaryConfig& ecu_config1, Uptane:: config.storage.path = temp_dir.Path(); config.pacman.type = PackageManager::kNone; - ecu_config1.secondary_type = Uptane::SecondaryType::kVirtual; ecu_config1.partial_verifying = false; ecu_config1.full_client_dir = temp_dir.Path(); ecu_config1.ecu_serial = "secondary_ecu_serial1"; @@ -39,9 +39,7 @@ void initKeyTests(Config& config, Uptane::SecondaryConfig& ecu_config1, Uptane:: ecu_config1.firmware_path = temp_dir / "firmware1.txt"; ecu_config1.target_name_path = temp_dir / "firmware1_name.txt"; ecu_config1.metadata_path = temp_dir / "secondary1_metadata"; - config.uptane.secondary_configs.push_back(ecu_config1); - ecu_config2.secondary_type = Uptane::SecondaryType::kVirtual; ecu_config2.partial_verifying = false; ecu_config2.full_client_dir = temp_dir.Path(); ecu_config2.ecu_serial = "secondary_ecu_serial2"; @@ -51,7 +49,6 @@ void initKeyTests(Config& config, Uptane::SecondaryConfig& ecu_config1, Uptane:: ecu_config2.firmware_path = temp_dir / "firmware2.txt"; ecu_config2.target_name_path = temp_dir / "firmware2_name.txt"; ecu_config2.metadata_path = temp_dir / "secondary2_metadata"; - config.uptane.secondary_configs.push_back(ecu_config2); } // This is a class solely for the purpose of being a FRIEND_TEST to @@ -87,8 +84,8 @@ class UptaneKey_Check_Test { // Verify that each secondary has valid keys. std::map >::iterator it; for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) { - std::shared_ptr managed = - boost::polymorphic_pointer_downcast(it->second); + std::shared_ptr managed = + boost::polymorphic_pointer_downcast(it->second); std::string public_key; std::string private_key; EXPECT_TRUE(managed->loadKeys(&public_key, &private_key)); @@ -115,12 +112,13 @@ TEST(UptaneKey, CheckAllKeys) { TemporaryDirectory temp_dir; auto http = std::make_shared(temp_dir.Path()); Config config; - Uptane::SecondaryConfig ecu_config1; - Uptane::SecondaryConfig ecu_config2; + Primary::VirtualSecondaryConfig ecu_config1; + Primary::VirtualSecondaryConfig ecu_config2; initKeyTests(config, ecu_config1, ecu_config2, temp_dir, http->tls_server); - auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); + sota_client->addNewSecondary(std::make_shared(ecu_config1)); + sota_client->addNewSecondary(std::make_shared(ecu_config2)); EXPECT_NO_THROW(sota_client->initialize()); UptaneKey_Check_Test::checkKeyTests(storage, sota_client); } @@ -133,15 +131,17 @@ TEST(UptaneKey, RecoverWithoutKeys) { TemporaryDirectory temp_dir; auto http = std::make_shared(temp_dir.Path()); Config config; - Uptane::SecondaryConfig ecu_config1; - Uptane::SecondaryConfig ecu_config2; + Primary::VirtualSecondaryConfig ecu_config1; + Primary::VirtualSecondaryConfig ecu_config2; + initKeyTests(config, ecu_config1, ecu_config2, temp_dir, http->tls_server); // Initialize. { auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); - + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); + sota_client->addNewSecondary(std::make_shared(ecu_config1)); + sota_client->addNewSecondary(std::make_shared(ecu_config2)); EXPECT_NO_THROW(sota_client->initialize()); UptaneKey_Check_Test::checkKeyTests(storage, sota_client); @@ -151,7 +151,7 @@ TEST(UptaneKey, RecoverWithoutKeys) { { auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); UptaneKey_Check_Test::checkKeyTests(storage, sota_client); @@ -167,7 +167,7 @@ TEST(UptaneKey, RecoverWithoutKeys) { { auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); UptaneKey_Check_Test::checkKeyTests(storage, sota_client); diff --git a/src/libaktualizr/uptane/CMakeLists.txt b/src/libaktualizr/uptane/CMakeLists.txt index 15c99548ff..80b145ac67 100644 --- a/src/libaktualizr/uptane/CMakeLists.txt +++ b/src/libaktualizr/uptane/CMakeLists.txt @@ -2,7 +2,6 @@ set(SOURCES fetcher.cc iterator.cc metawithkeys.cc - partialverificationsecondary.cc role.cc root.cc tuf.cc @@ -14,8 +13,6 @@ set(HEADERS exceptions.h fetcher.h iterator.h - partialverificationsecondary.h - secondaryconfig.h secondaryinterface.h tuf.h uptanerepository.h @@ -24,7 +21,6 @@ set(HEADERS add_library(uptane OBJECT ${SOURCES}) -target_sources(config PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/secondaryconfig.cc) if (BUILD_ISOTP) target_sources(uptane PRIVATE isotpsecondary.cc) @@ -38,6 +34,7 @@ if(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS) add_aktualizr_test(NAME uptane_ci SOURCES uptane_ci_test.cc PROJECT_WORKING_DIRECTORY NO_VALGRIND ARGS ${SOTA_PACKED_CREDENTIALS} ${PROJECT_BINARY_DIR}/ostree_repo) set_tests_properties(test_uptane_ci PROPERTIES LABELS "credentials") + target_link_libraries(t_uptane_ci virtual_secondary) else(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS) list(APPEND TEST_SOURCES uptane_ci_test.cc) endif(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS) @@ -45,21 +42,25 @@ endif(BUILD_OSTREE AND SOTA_PACKED_CREDENTIALS) add_aktualizr_test(NAME uptane SOURCES uptane_test.cc PROJECT_WORKING_DIRECTORY) set_tests_properties(test_uptane PROPERTIES LABELS "crypto") +target_link_libraries(t_uptane virtual_secondary) add_aktualizr_test(NAME uptane_delegation SOURCES uptane_delegation_test.cc PROJECT_WORKING_DIRECTORY ARGS "$") add_dependencies(t_uptane_delegation aktualizr-repo) +target_link_libraries(t_uptane_delegation virtual_secondary) set_tests_properties(test_uptane_delegation 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") +target_link_libraries(t_uptane_network virtual_secondary) add_aktualizr_test(NAME uptane_serial SOURCES uptane_serial_test.cc ARGS ${PROJECT_BINARY_DIR} PROJECT_WORKING_DIRECTORY) +target_link_libraries(t_uptane_serial virtual_secondary) add_aktualizr_test(NAME uptane_init SOURCES uptane_init_test.cc PROJECT_WORKING_DIRECTORY) add_aktualizr_test(NAME director SOURCES director_test.cc PROJECT_WORKING_DIRECTORY ARGS "$") -aktualizr_source_file_checks(${SOURCES} ${HEADERS} secondaryconfig.cc isotpsecondary.cc isotpsecondary.h ${TEST_SOURCES}) +aktualizr_source_file_checks(${SOURCES} ${HEADERS} isotpsecondary.cc isotpsecondary.h ${TEST_SOURCES}) diff --git a/src/libaktualizr/uptane/isotpsecondary.cc b/src/libaktualizr/uptane/isotpsecondary.cc index ffc259c100..27c8db483d 100644 --- a/src/libaktualizr/uptane/isotpsecondary.cc +++ b/src/libaktualizr/uptane/isotpsecondary.cc @@ -32,8 +32,8 @@ enum class IsoTpUptaneMesType { namespace Uptane { -IsoTpSecondary::IsoTpSecondary(const SecondaryConfig& sconfig_in) - : conn(sconfig_in.can_iface, LIBUPTINY_ISOTP_PRIMARY_CANID, sconfig_in.can_id) {} +IsoTpSecondary::IsoTpSecondary(const std::string& can_iface, uint16_t can_id) + : conn(can_iface, LIBUPTINY_ISOTP_PRIMARY_CANID, can_id) {} EcuSerial IsoTpSecondary::getSerial() { std::string out; diff --git a/src/libaktualizr/uptane/isotpsecondary.h b/src/libaktualizr/uptane/isotpsecondary.h index cb85100a11..0bb59121c3 100644 --- a/src/libaktualizr/uptane/isotpsecondary.h +++ b/src/libaktualizr/uptane/isotpsecondary.h @@ -2,14 +2,14 @@ #define UPTANE_ISOTPSECONDARY_H_ #include "isotp_conn/isotp_conn.h" -#include "secondaryconfig.h" #include "secondaryinterface.h" namespace Uptane { class IsoTpSecondary : public SecondaryInterface { public: - explicit IsoTpSecondary(const SecondaryConfig& sconfig_in); + explicit IsoTpSecondary(const std::string& can_iface, uint16_t can_id); + EcuSerial getSerial() override; HardwareIdentifier getHwId() override; PublicKey getPublicKey() override; diff --git a/src/libaktualizr/uptane/secondaryconfig.cc b/src/libaktualizr/uptane/secondaryconfig.cc deleted file mode 100644 index 776517cf9f..0000000000 --- a/src/libaktualizr/uptane/secondaryconfig.cc +++ /dev/null @@ -1,53 +0,0 @@ -#include "uptane/secondaryconfig.h" - -namespace Uptane { -SecondaryConfig::SecondaryConfig(const boost::filesystem::path &config_file) { - if (!boost::filesystem::exists(config_file)) { - throw FatalException(config_file.string() + " does not exist!"); - } - Json::Value config_json = Utils::parseJSONFile(config_file); - - std::string stype = config_json["secondary_type"].asString(); - if (stype == "virtual") { - secondary_type = Uptane::SecondaryType::kVirtual; - } else if (stype == "legacy") { - throw FatalException("Legacy secondaries are deprecated."); - } else if (stype == "ip_uptane") { - secondary_type = Uptane::SecondaryType::kIpUptane; - } else if (stype == "isotp_uptane") { - secondary_type = Uptane::SecondaryType::kIsoTpUptane; - } else { - LOG_ERROR << "Unrecognized secondary type: " << stype; - } - ecu_serial = config_json["ecu_serial"].asString(); - ecu_hardware_id = config_json["ecu_hardware_id"].asString(); - partial_verifying = config_json["partial_verifying"].asBool(); - ecu_private_key = config_json["ecu_private_key"].asString(); - ecu_public_key = config_json["ecu_public_key"].asString(); - - full_client_dir = boost::filesystem::path(config_json["full_client_dir"].asString()); - firmware_path = boost::filesystem::path(config_json["firmware_path"].asString()); - metadata_path = boost::filesystem::path(config_json["metadata_path"].asString()); - target_name_path = boost::filesystem::path(config_json["target_name_path"].asString()); - - std::string key_type_str = config_json["key_type"].asString(); - if (key_type_str.size() != 0u) { - if (key_type_str == "RSA2048") { - key_type = KeyType::kRSA2048; - } else if (key_type_str == "RSA3072") { - key_type = KeyType::kRSA3072; - } else if (key_type_str == "RSA4096") { - key_type = KeyType::kRSA4096; - } else if (key_type_str == "ED25519") { - key_type = KeyType::kED25519; - } - } - - try { - can_id = static_cast(stoi(config_json["can_id"].asString(), nullptr, 16)); - } catch (...) { - can_id = 0; - } - can_iface = config_json["can_interface"].asString(); -} -} // namespace Uptane diff --git a/src/libaktualizr/uptane/secondaryconfig.h b/src/libaktualizr/uptane/secondaryconfig.h deleted file mode 100644 index 39afaef320..0000000000 --- a/src/libaktualizr/uptane/secondaryconfig.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef UPTANE_SECONDARYCONFIG_H_ -#define UPTANE_SECONDARYCONFIG_H_ - -#include - -#include -#include -#include "logging/logging.h" -#include "utilities/exceptions.h" -#include "utilities/types.h" -#include "utilities/utils.h" - -namespace Uptane { - -enum class SecondaryType { - - kVirtual, // Virtual secondary (in-process fake implementation). - - kIpUptane, // Custom Uptane protocol over TCP/IP network - - kVirtualUptane, // Partial UPTANE secondary implemented inside primary - kIsoTpUptane, // Custom Uptane protocol over ISO/TP network - -}; - -class SecondaryConfig { - public: - SecondaryConfig() = default; - SecondaryConfig(const boost::filesystem::path &config_file); - SecondaryType secondary_type{}; - std::string ecu_serial; - std::string ecu_hardware_id; - bool partial_verifying{}; - std::string ecu_private_key; - std::string ecu_public_key; - KeyType key_type{KeyType::kRSA2048}; - - // TODO: secondary config contains secondary specific params - // so introduction of any new type of secondary will require changes here/libaktualizr - // what we would like to avoid - boost::filesystem::path full_client_dir; // SecondaryType::kVirtual - boost::filesystem::path firmware_path; // SecondaryType::kVirtual - boost::filesystem::path metadata_path; // SecondaryType::kVirtual - boost::filesystem::path target_name_path; // SecondaryType::kVirtual - - uint16_t can_id{0x0000}; // SecondaryType::kIsoTpUptane; - std::string can_iface; // SecondaryType::kIsoTpUptane; -}; - -} // namespace Uptane - -#endif diff --git a/src/libaktualizr/uptane/uptane_ci_test.cc b/src/libaktualizr/uptane/uptane_ci_test.cc index d21544e2ee..3ecd9cb98a 100644 --- a/src/libaktualizr/uptane/uptane_ci_test.cc +++ b/src/libaktualizr/uptane/uptane_ci_test.cc @@ -15,8 +15,8 @@ #include "primary/reportqueue.h" #include "primary/sotauptaneclient.h" #include "storage/invstorage.h" -//#include "managedsecondary.h" #include "uptane/uptanerepository.h" +#include "uptane_test_common.h" #include "utilities/utils.h" boost::filesystem::path credentials; @@ -37,7 +37,7 @@ TEST(UptaneCI, ProvisionAndPutManifest) { auto http = std::make_shared(); Uptane::Manifest uptane_manifest{config, storage}; - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); EXPECT_TRUE(sota_client->putManifestSimple()); } @@ -53,23 +53,11 @@ TEST(UptaneCI, CheckKeys) { config.postUpdateValues(); // re-run copy of urls boost::filesystem::remove_all(config.storage.path); - Uptane::SecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; - ecu_config.partial_verifying = false; - ecu_config.full_client_dir = temp_dir.Path(); - ecu_config.ecu_serial = ""; - ecu_config.ecu_hardware_id = "secondary_hardware"; - ecu_config.ecu_private_key = "sec.priv"; - ecu_config.ecu_public_key = "sec.pub"; - ecu_config.firmware_path = (temp_dir / "firmware.txt").string(); - ecu_config.target_name_path = (temp_dir / "firmware_name.txt").string(); - ecu_config.metadata_path = (temp_dir / "secondary_metadata").string(); - config.uptane.secondary_configs.push_back(ecu_config); - auto storage = INvStorage::newStorage(config.storage); auto http = std::make_shared(); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + UptaneTestCommon::addDefaultSecondary(config, temp_dir, "", "secondary_hardware"); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); std::string ca; @@ -86,20 +74,19 @@ TEST(UptaneCI, CheckKeys) { EXPECT_TRUE(primary_public.size() > 0); EXPECT_TRUE(primary_private.size() > 0); - // TODO: either remove or move to the virtual secondary folder as it's effectively test of - // the test code (managed secondary is used just for test) - // std::map >::iterator it; - // for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) { - // EXPECT_EQ(it->second->sconfig.secondary_type, Uptane::SecondaryType::kVirtual); - // std::shared_ptr managed = - // boost::polymorphic_pointer_downcast(it->second); - // std::string public_key; - // std::string private_key; - // EXPECT_TRUE(managed->loadKeys(&public_key, &private_key)); - // EXPECT_TRUE(public_key.size() > 0); - // EXPECT_TRUE(private_key.size() > 0); - // EXPECT_NE(public_key, private_key); - // } + std::map >::iterator it; + for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) { + std::shared_ptr managed_secondary = + std::dynamic_pointer_cast(it->second); + EXPECT_TRUE(managed_secondary); + + std::string public_key; + std::string private_key; + EXPECT_TRUE(managed_secondary->loadKeys(&public_key, &private_key)); + EXPECT_TRUE(public_key.size() > 0); + EXPECT_TRUE(private_key.size() > 0); + EXPECT_NE(public_key, private_key); + } } #ifndef __NO_MAIN__ diff --git a/src/libaktualizr/uptane/uptane_delegation_test.cc b/src/libaktualizr/uptane/uptane_delegation_test.cc index cc8c91af78..96b43296f3 100644 --- a/src/libaktualizr/uptane/uptane_delegation_test.cc +++ b/src/libaktualizr/uptane/uptane_delegation_test.cc @@ -71,7 +71,7 @@ TEST(Delegation, Basic) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -99,7 +99,7 @@ TEST(Delegation, RevokeAfterCheckUpdates) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -112,7 +112,7 @@ TEST(Delegation, RevokeAfterCheckUpdates) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); auto update_result = aktualizr.CheckUpdates().get(); @@ -139,7 +139,7 @@ TEST(Delegation, RevokeAfterDownload) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -155,7 +155,7 @@ TEST(Delegation, RevokeAfterDownload) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); auto update_result = aktualizr.CheckUpdates().get(); @@ -182,7 +182,7 @@ TEST(Delegation, RevokeAfterInstall) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -203,7 +203,7 @@ TEST(Delegation, RevokeAfterInstall) { auto http = std::make_shared(temp_dir.Path()); Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); auto update_result = aktualizr.CheckUpdates().get(); @@ -222,7 +222,7 @@ TEST(Delegation, IterateAll) { Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server); auto storage = INvStorage::newStorage(conf.storage); - Aktualizr aktualizr(conf, storage, http); + UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http); aktualizr.Initialize(); result::UpdateCheck update_result = aktualizr.CheckUpdates().get(); @@ -231,7 +231,7 @@ TEST(Delegation, IterateAll) { std::vector expected_target_names = {"primary.txt", "abracadabra", "abc/secondary.txt", "abc/target0", "abc/target1", "abc/target2", "bcd/target0", "cde/target0", "cde/target1", "def/target0"}; - for (auto& target : aktualizr.uptane_client_->allTargets()) { + for (auto& target : aktualizr.uptane_client()->allTargets()) { EXPECT_EQ(target.filename(), expected_target_names[0]); expected_target_names.erase(expected_target_names.begin()); } diff --git a/src/libaktualizr/uptane/uptane_network_test.cc b/src/libaktualizr/uptane/uptane_network_test.cc index e2be88608a..6cf7e6eed8 100644 --- a/src/libaktualizr/uptane/uptane_network_test.cc +++ b/src/libaktualizr/uptane/uptane_network_test.cc @@ -125,7 +125,7 @@ TEST(UptaneNetwork, DownloadFailure) { auto storage = INvStorage::newStorage(conf.storage); auto http = std::make_shared(); - auto up = SotaUptaneClient::newTestClient(conf, storage, http); + auto up = newTestClient(conf, storage, http); EXPECT_NO_THROW(up->initialize()); Json::Value ot_json; diff --git a/src/libaktualizr/uptane/uptane_serial_test.cc b/src/libaktualizr/uptane/uptane_serial_test.cc index 1047aa4d4f..a229b90d19 100644 --- a/src/libaktualizr/uptane/uptane_serial_test.cc +++ b/src/libaktualizr/uptane/uptane_serial_test.cc @@ -16,6 +16,7 @@ #include "test_utils.h" #include "uptane/tuf.h" #include "uptane/uptanerepository.h" +#include "uptane_test_common.h" #include "utilities/utils.h" namespace bpo = boost::program_options; @@ -38,24 +39,8 @@ TEST(Uptane, RandomSerial) { conf_1.provision.primary_ecu_serial = ""; conf_2.provision.primary_ecu_serial = ""; - // Add a secondary to each config, again not specifying serials. - Uptane::SecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; - ecu_config.partial_verifying = false; - ecu_config.full_client_dir = temp_dir1.Path() / "sec_1"; - ecu_config.ecu_serial = ""; - ecu_config.ecu_hardware_id = "secondary_hardware"; - ecu_config.ecu_private_key = "sec.priv"; - ecu_config.ecu_public_key = "sec.pub"; - ecu_config.firmware_path = temp_dir1.Path() / "firmware.txt"; - ecu_config.target_name_path = temp_dir1.Path() / "firmware_name.txt"; - ecu_config.metadata_path = temp_dir1.Path() / "secondary_metadata"; - conf_1.uptane.secondary_configs.push_back(ecu_config); - ecu_config.full_client_dir = temp_dir2.Path() / "sec_2"; - ecu_config.firmware_path = temp_dir2.Path() / "firmware.txt"; - ecu_config.target_name_path = temp_dir2.Path() / "firmware_name.txt"; - ecu_config.metadata_path = temp_dir2.Path() / "secondary_metadata"; - conf_2.uptane.secondary_configs.push_back(ecu_config); + UptaneTestCommon::addDefaultSecondary(conf_1, temp_dir1, "", "secondary_hardware", false); + UptaneTestCommon::addDefaultSecondary(conf_2, temp_dir2, "", "secondary_hardware", false); // Initialize. auto storage_1 = INvStorage::newStorage(conf_1.storage); @@ -63,10 +48,10 @@ TEST(Uptane, RandomSerial) { auto http1 = std::make_shared(temp_dir1.Path()); auto http2 = std::make_shared(temp_dir2.Path()); - auto uptane_client1 = SotaUptaneClient::newTestClient(conf_1, storage_1, http1); + auto uptane_client1 = UptaneTestCommon::newTestClient(conf_1, storage_1, http1); EXPECT_NO_THROW(uptane_client1->initialize()); - auto uptane_client2 = SotaUptaneClient::newTestClient(conf_2, storage_2, http2); + auto uptane_client2 = UptaneTestCommon::newTestClient(conf_2, storage_2, http2); EXPECT_NO_THROW(uptane_client2->initialize()); // Verify that none of the serials match. @@ -97,28 +82,18 @@ TEST(Uptane, ReloadSerial) { EcuSerials ecu_serials_1; EcuSerials ecu_serials_2; - Uptane::SecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; - ecu_config.partial_verifying = false; - ecu_config.full_client_dir = temp_dir.Path() / "sec"; - ecu_config.ecu_serial = ""; - ecu_config.ecu_hardware_id = "secondary_hardware"; - ecu_config.ecu_private_key = "sec.priv"; - ecu_config.ecu_public_key = "sec.pub"; - ecu_config.firmware_path = temp_dir.Path() / "firmware.txt"; - ecu_config.target_name_path = temp_dir.Path() / "firmware_name.txt"; - ecu_config.metadata_path = temp_dir.Path() / "secondary_metadata"; - // Initialize. Should store new serials. { Config conf("tests/config/basic.toml"); conf.storage.path = temp_dir.Path(); conf.provision.primary_ecu_serial = ""; - conf.uptane.secondary_configs.push_back(ecu_config); auto storage = INvStorage::newStorage(conf.storage); auto http = std::make_shared(temp_dir.Path()); - auto uptane_client = SotaUptaneClient::newTestClient(conf, storage, http); + + UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "", "secondary_hardware", false); + auto uptane_client = UptaneTestCommon::newTestClient(conf, storage, http); + EXPECT_NO_THROW(uptane_client->initialize()); EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials_1)); EXPECT_EQ(ecu_serials_1.size(), 2); @@ -132,11 +107,12 @@ TEST(Uptane, ReloadSerial) { Config conf("tests/config/basic.toml"); conf.storage.path = temp_dir.Path(); conf.provision.primary_ecu_serial = ""; - conf.uptane.secondary_configs.push_back(ecu_config); auto storage = INvStorage::newStorage(conf.storage); auto http = std::make_shared(temp_dir.Path()); - auto uptane_client = SotaUptaneClient::newTestClient(conf, storage, http); + UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "", "secondary_hardware", false); + auto uptane_client = UptaneTestCommon::newTestClient(conf, storage, http); + EXPECT_NO_THROW(uptane_client->initialize()); EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials_2)); EXPECT_EQ(ecu_serials_2.size(), 2); diff --git a/src/libaktualizr/uptane/uptane_test.cc b/src/libaktualizr/uptane/uptane_test.cc index a73615b253..07ab944656 100644 --- a/src/libaktualizr/uptane/uptane_test.cc +++ b/src/libaktualizr/uptane/uptane_test.cc @@ -145,7 +145,7 @@ TEST(Uptane, AssembleManifestGood) { UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); Json::Value manifest = sota_client->AssembleManifest()["ecu_version_manifests"]; @@ -172,7 +172,7 @@ TEST(Uptane, AssembleManifestBad) { config.uptane.repo_server = http->tls_server + "/repo"; config.provision.primary_ecu_serial = "testecuserial"; config.pacman.type = PackageManager::kNone; - Uptane::SecondaryConfig ecu_config = + Primary::VirtualSecondaryConfig ecu_config = UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); /* Overwrite the secondary's keys on disk. */ @@ -183,7 +183,7 @@ TEST(Uptane, AssembleManifestBad) { Utils::writeFile(ecu_config.full_client_dir / ecu_config.ecu_public_key, public_key); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); Json::Value manifest = sota_client->AssembleManifest()["ecu_version_manifests"]; @@ -215,7 +215,7 @@ TEST(Uptane, PutManifest) { auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); EXPECT_TRUE(sota_client->putManifestSimple()); @@ -268,7 +268,7 @@ TEST(Uptane, PutManifestError) { std::function event)> f_cb = process_events_PutManifestError; events_channel->connect(f_cb); num_events_PutManifestError = 0; - auto sota_client = SotaUptaneClient::newTestClient(conf, storage, http, events_channel); + auto sota_client = UptaneTestCommon::newTestClient(conf, storage, http, events_channel); EXPECT_NO_THROW(sota_client->initialize()); auto result = sota_client->putManifest(); EXPECT_FALSE(result); @@ -293,7 +293,7 @@ TEST(Uptane, FetchMetaFail) { conf.tls.server = http->tls_server; auto storage = INvStorage::newStorage(conf.storage); - auto up = SotaUptaneClient::newTestClient(conf, storage, http); + auto up = UptaneTestCommon::newTestClient(conf, storage, http); EXPECT_NO_THROW(up->initialize()); result::UpdateCheck result = up->fetchMeta(); @@ -338,7 +338,7 @@ TEST(Uptane, InstallFake) { auto events_channel = std::make_shared(); std::function event)> f_cb = process_events_Install; events_channel->connect(f_cb); - auto up = SotaUptaneClient::newTestClient(conf, storage, http, events_channel); + auto up = UptaneTestCommon::newTestClient(conf, storage, http, events_channel); EXPECT_NO_THROW(up->initialize()); std::vector packages_to_install = UptaneTestCommon::makePackage("testecuserial", "testecuhwid"); up->uptaneInstall(packages_to_install); @@ -390,7 +390,7 @@ class HttpFakeEvents : public HttpFake { class SecondaryInterfaceMock : public Uptane::SecondaryInterface { public: - explicit SecondaryInterfaceMock(Uptane::SecondaryConfig sconfig_in) : sconfig(std::move(sconfig_in)) { + explicit SecondaryInterfaceMock(Primary::VirtualSecondaryConfig &sconfig_in) : sconfig(std::move(sconfig_in)) { std::string private_key, public_key; Crypto::generateKeyPair(sconfig.key_type, &public_key, &private_key); public_key_ = PublicKey(public_key, sconfig.key_type); @@ -427,7 +427,7 @@ class SecondaryInterfaceMock : public Uptane::SecondaryInterface { PublicKey public_key_; Json::Value manifest_; - const Uptane::SecondaryConfig sconfig; + Primary::VirtualSecondaryConfig sconfig; }; MATCHER_P(matchMeta, meta, "") { @@ -451,8 +451,7 @@ TEST(Uptane, SendMetadataToSeconadry) { conf.storage.path = temp_dir.Path(); conf.tls.server = http->tls_server; - Uptane::SecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; + Primary::VirtualSecondaryConfig ecu_config; ecu_config.partial_verifying = false; ecu_config.full_client_dir = temp_dir.Path(); ecu_config.ecu_serial = "secondary_ecu_serial"; @@ -464,10 +463,8 @@ TEST(Uptane, SendMetadataToSeconadry) { ecu_config.metadata_path = temp_dir / "secondary_metadata"; auto sec = std::make_shared(ecu_config); - auto storage = INvStorage::newStorage(conf.storage); - auto up = SotaUptaneClient::newTestClient(conf, storage, http); - + auto up = UptaneTestCommon::newTestClient(conf, storage, http); up->addNewSecondary(sec); EXPECT_NO_THROW(up->initialize()); up->fetchMeta(); @@ -504,7 +501,7 @@ TEST(Uptane, UptaneSecondaryAdd) { UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); /* Verify the correctness of the metadata sent to the server about the @@ -530,10 +527,13 @@ TEST(Uptane, UptaneSecondaryAddSameSerial) { config.storage.path = temp_dir.Path(); UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); - UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "second_secondary_hardware"); auto storage = INvStorage::newStorage(config.storage); - EXPECT_THROW(SotaUptaneClient::newTestClient(config, storage, http), std::runtime_error); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); + UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware_new"); + EXPECT_THROW(sota_client->addNewSecondary(std::make_shared( + Primary::VirtualSecondaryConfig::create_from_file(config.uptane.secondary_config_file))), + std::runtime_error); } /* @@ -553,7 +553,7 @@ TEST(Uptane, UptaneSecondaryMisconfigured) { UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); std::vector ecus; @@ -568,7 +568,7 @@ TEST(Uptane, UptaneSecondaryMisconfigured) { config.storage.path = temp_dir.Path(); auto storage = INvStorage::newStorage(config.storage); UptaneTestCommon::addDefaultSecondary(config, temp_dir, "new_secondary_ecu_serial", "new_secondary_hardware"); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); std::vector ecus; @@ -594,7 +594,7 @@ TEST(Uptane, UptaneSecondaryMisconfigured) { config.storage.path = temp_dir.Path(); auto storage = INvStorage::newStorage(config.storage); UptaneTestCommon::addDefaultSecondary(config, temp_dir, "secondary_ecu_serial", "secondary_hardware"); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); std::vector ecus; @@ -753,7 +753,7 @@ TEST(Uptane, ProvisionOnServer) { auto events_channel = std::make_shared(); std::function event)> f_cb = process_events_ProvisionOnServer; events_channel->connect(f_cb); - auto up = SotaUptaneClient::newTestClient(config, storage, http, events_channel); + auto up = UptaneTestCommon::newTestClient(config, storage, http, events_channel); EXPECT_EQ(http->devices_count, 0); EXPECT_EQ(http->ecus_count, 0); @@ -1043,7 +1043,7 @@ TEST(Uptane, restoreVerify) { config.postUpdateValues(); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); sota_client->AssembleManifest(); @@ -1101,7 +1101,7 @@ TEST(Uptane, offlineIteration) { config.postUpdateValues(); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); sota_client->AssembleManifest(); @@ -1132,7 +1132,7 @@ TEST(Uptane, IgnoreUnknownUpdate) { config.postUpdateValues(); auto storage = INvStorage::newStorage(config.storage); - auto sota_client = SotaUptaneClient::newTestClient(config, storage, http); + auto sota_client = UptaneTestCommon::newTestClient(config, storage, http); EXPECT_NO_THROW(sota_client->initialize()); sota_client->AssembleManifest(); diff --git a/src/virtual_secondary/CMakeLists.txt b/src/virtual_secondary/CMakeLists.txt index 101bd97e17..dfc8cbbd7e 100644 --- a/src/virtual_secondary/CMakeLists.txt +++ b/src/virtual_secondary/CMakeLists.txt @@ -1,6 +1,6 @@ -set(SOURCES managedsecondary.cc virtualsecondary.cc) +set(SOURCES managedsecondary.cc virtualsecondary.cc partialverificationsecondary.cc) -set(HEADERS managedsecondary.h virtualsecondary.h) +set(HEADERS managedsecondary.h virtualsecondary.h partialverificationsecondary.h) set(TARGET virtual_secondary) @@ -8,7 +8,9 @@ add_library(${TARGET} STATIC ${SOURCES} ) -target_include_directories(${TARGET} PUBLIC ${PROJECT_SOURCE_DIR}/src/virtual_secondary) +target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + add_aktualizr_test(NAME virtual_secondary SOURCES virtual_secondary_test.cc PROJECT_WORKING_DIRECTORY) +target_link_libraries(t_virtual_secondary virtual_secondary) aktualizr_source_file_checks(${HEADERS} ${SOURCES} ${TEST_SOURCES}) diff --git a/src/virtual_secondary/managedsecondary.cc b/src/virtual_secondary/managedsecondary.cc index ffdd7a6b00..b25c27bd53 100644 --- a/src/virtual_secondary/managedsecondary.cc +++ b/src/virtual_secondary/managedsecondary.cc @@ -12,8 +12,9 @@ #include -namespace Uptane { -ManagedSecondary::ManagedSecondary(SecondaryConfig sconfig_in) : sconfig(std::move(sconfig_in)) { +namespace Primary { + +ManagedSecondary::ManagedSecondary(Primary::ManagedSecondaryConfig sconfig_in) : sconfig(std::move(sconfig_in)) { // TODO: FIX // loadMetadata(meta_pack); std::string public_key_string; @@ -60,15 +61,16 @@ void ManagedSecondary::Initialize() { void ManagedSecondary::rawToMeta() { // raw meta is trusted current_meta.director_root = - Uptane::Root(RepositoryType::Director(), Utils::parseJSON(current_raw_meta.director_root)); + Uptane::Root(Uptane::RepositoryType::Director(), Utils::parseJSON(current_raw_meta.director_root)); current_meta.director_targets = Uptane::Targets(Utils::parseJSON(current_raw_meta.director_targets)); - current_meta.image_root = Uptane::Root(RepositoryType::Image(), Utils::parseJSON(current_raw_meta.image_root)); + current_meta.image_root = + Uptane::Root(Uptane::RepositoryType::Image(), Utils::parseJSON(current_raw_meta.image_root)); current_meta.image_targets = Uptane::Targets(Utils::parseJSON(current_raw_meta.image_targets)); current_meta.image_timestamp = Uptane::TimestampMeta(Utils::parseJSON(current_raw_meta.image_timestamp)); current_meta.image_snapshot = Uptane::Snapshot(Utils::parseJSON(current_raw_meta.image_snapshot)); } -bool ManagedSecondary::putMetadata(const RawMetaPack &meta_pack) { +bool ManagedSecondary::putMetadata(const Uptane::RawMetaPack &meta_pack) { // No verification is currently performed, we can add verification in future for testing purposes detected_attack = ""; @@ -117,8 +119,8 @@ int ManagedSecondary::getRootVersion(const bool director) { bool ManagedSecondary::putRoot(const std::string &root, const bool director) { Uptane::Root &prev_root = (director) ? current_meta.director_root : current_meta.image_root; std::string &prev_raw_root = (director) ? current_raw_meta.director_root : current_raw_meta.image_root; - Uptane::Root new_root = - Uptane::Root((director) ? RepositoryType::Director() : RepositoryType::Image(), Utils::parseJSON(root)); + Uptane::Root new_root = Uptane::Root( + (director) ? Uptane::RepositoryType::Director() : Uptane::RepositoryType::Image(), Utils::parseJSON(root)); // No verification is currently performed, we can add verification in future for testing purposes if (new_root.version() == prev_root.version() + 1) { @@ -151,7 +153,7 @@ bool ManagedSecondary::sendFirmware(const std::shared_ptr &data) { return false; } - std::vector::const_iterator it; + std::vector::const_iterator it; for (it = expected_target_hashes.begin(); it != expected_target_hashes.end(); it++) { if (it->TypeString() == "sha256") { if (boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(*data))) != @@ -227,4 +229,5 @@ bool ManagedSecondary::loadKeys(std::string *pub_key, std::string *priv_key) { *pub_key = Utils::readFile(public_key_path.string()); return true; } -} // namespace Uptane + +} // namespace Primary diff --git a/src/virtual_secondary/managedsecondary.h b/src/virtual_secondary/managedsecondary.h index cfb45dd93d..2e24b610f4 100644 --- a/src/virtual_secondary/managedsecondary.h +++ b/src/virtual_secondary/managedsecondary.h @@ -1,5 +1,5 @@ -#ifndef UPTANE_MANAGEDSECONDARY_H_ -#define UPTANE_MANAGEDSECONDARY_H_ +#ifndef PRIMARY_MANAGEDSECONDARY_H_ +#define PRIMARY_MANAGEDSECONDARY_H_ #include #include @@ -8,32 +8,52 @@ #include #include "json/json.h" -#include "uptane/secondaryconfig.h" +#include "primary/secondary_config.h" #include "uptane/secondaryinterface.h" #include "utilities/types.h" -namespace Uptane { +namespace Primary { + +class ManagedSecondaryConfig : public SecondaryConfig { + public: + ManagedSecondaryConfig(const char* type = Type) : SecondaryConfig(type) {} + + public: + constexpr static const char* const Type = "managed"; + + public: + bool partial_verifying{false}; + std::string ecu_serial; + std::string ecu_hardware_id; + boost::filesystem::path full_client_dir; + std::string ecu_private_key; + std::string ecu_public_key; + boost::filesystem::path firmware_path; + boost::filesystem::path target_name_path; + boost::filesystem::path metadata_path; + KeyType key_type{KeyType::kRSA2048}; +}; // Managed secondary is an abstraction over virtual and other types of legacy // (non-UPTANE) secondaries. They require all the UPTANE-related functionality // to be implemented in aktualizr itself, so there's some shared code. -class ManagedSecondary : public SecondaryInterface { +class ManagedSecondary : public Uptane::SecondaryInterface { public: - explicit ManagedSecondary(SecondaryConfig sconfig_in); + explicit ManagedSecondary(Primary::ManagedSecondaryConfig sconfig_in); ~ManagedSecondary() override = default; void Initialize(); - EcuSerial getSerial() override { + Uptane::EcuSerial getSerial() override { if (!sconfig.ecu_serial.empty()) { - return EcuSerial(sconfig.ecu_serial); + return Uptane::EcuSerial(sconfig.ecu_serial); } - return EcuSerial(public_key_.KeyId()); + return Uptane::EcuSerial(public_key_.KeyId()); } Uptane::HardwareIdentifier getHwId() override { return Uptane::HardwareIdentifier(sconfig.ecu_hardware_id); } PublicKey getPublicKey() override { return public_key_; } - bool putMetadata(const RawMetaPack& meta_pack) override; + bool putMetadata(const Uptane::RawMetaPack& meta_pack) override; int getRootVersion(bool director) override; bool putRoot(const std::string& root, bool director) override; @@ -43,7 +63,7 @@ class ManagedSecondary : public SecondaryInterface { bool loadKeys(std::string* pub_key, std::string* priv_key); protected: - SecondaryConfig sconfig; + Primary::ManagedSecondaryConfig sconfig; private: PublicKey public_key_; @@ -51,11 +71,11 @@ class ManagedSecondary : public SecondaryInterface { std::string detected_attack; std::string expected_target_name; - std::vector expected_target_hashes; + std::vector expected_target_hashes; uint64_t expected_target_length{}; - MetaPack current_meta; - RawMetaPack current_raw_meta; + Uptane::MetaPack current_meta; + Uptane::RawMetaPack current_raw_meta; std::mutex install_mutex; virtual bool storeFirmware(const std::string& target_name, const std::string& content) = 0; @@ -65,9 +85,10 @@ class ManagedSecondary : public SecondaryInterface { void rawToMeta(); // TODO: implement - void storeMetadata(const RawMetaPack& meta_pack) { (void)meta_pack; } - bool loadMetadata(RawMetaPack* meta_pack); + void storeMetadata(const Uptane::RawMetaPack& meta_pack) { (void)meta_pack; } + bool loadMetadata(Uptane::RawMetaPack* meta_pack); }; -} // namespace Uptane -#endif // UPTANE_MANAGEDSECONDARY_H_ +} // namespace Primary + +#endif // PRIMARY_MANAGEDSECONDARY_H_ diff --git a/src/libaktualizr/uptane/partialverificationsecondary.cc b/src/virtual_secondary/partialverificationsecondary.cc similarity index 95% rename from src/libaktualizr/uptane/partialverificationsecondary.cc rename to src/virtual_secondary/partialverificationsecondary.cc index 1a739e0e9c..17e0b4da39 100644 --- a/src/libaktualizr/uptane/partialverificationsecondary.cc +++ b/src/virtual_secondary/partialverificationsecondary.cc @@ -1,4 +1,4 @@ -#include "uptane/partialverificationsecondary.h" +#include "partialverificationsecondary.h" #include #include @@ -7,14 +7,13 @@ #include "json/json.h" #include "logging/logging.h" -#include "uptane/secondaryconfig.h" #include "uptane/secondaryinterface.h" #include "utilities/exceptions.h" #include "utilities/types.h" namespace Uptane { -PartialVerificationSecondary::PartialVerificationSecondary(SecondaryConfig sconfig_in) +PartialVerificationSecondary::PartialVerificationSecondary(Primary::PartialVerificationSecondaryConfig sconfig_in) : sconfig(std::move(sconfig_in)), root_(Root::Policy::kAcceptAll) { boost::filesystem::create_directories(sconfig.metadata_path); diff --git a/src/libaktualizr/uptane/partialverificationsecondary.h b/src/virtual_secondary/partialverificationsecondary.h similarity index 74% rename from src/libaktualizr/uptane/partialverificationsecondary.h rename to src/virtual_secondary/partialverificationsecondary.h index 51012d8deb..f633435b21 100644 --- a/src/libaktualizr/uptane/partialverificationsecondary.h +++ b/src/virtual_secondary/partialverificationsecondary.h @@ -7,15 +7,28 @@ #include #include "json/json.h" -#include "uptane/secondaryconfig.h" #include "uptane/secondaryinterface.h" #include "utilities/types.h" +#include "managedsecondary.h" + +namespace Primary { + +class PartialVerificationSecondaryConfig : public ManagedSecondaryConfig { + public: + PartialVerificationSecondaryConfig() : ManagedSecondaryConfig(Type) {} + + public: + constexpr static const char* const Type = "partial-verification"; +}; + +} // namespace Primary + namespace Uptane { class PartialVerificationSecondary : public SecondaryInterface { public: - explicit PartialVerificationSecondary(SecondaryConfig sconfig_in); + explicit PartialVerificationSecondary(Primary::PartialVerificationSecondaryConfig sconfig_in); EcuSerial getSerial() override { if (!sconfig.ecu_serial.empty()) { @@ -37,7 +50,7 @@ class PartialVerificationSecondary : public SecondaryInterface { void storeKeys(const std::string& public_key, const std::string& private_key); bool loadKeys(std::string* public_key, std::string* private_key); - SecondaryConfig sconfig; + Primary::PartialVerificationSecondaryConfig sconfig; Uptane::Root root_; PublicKey public_key_; std::string private_key_; diff --git a/src/virtual_secondary/virtual_secondary_test.cc b/src/virtual_secondary/virtual_secondary_test.cc index 4a86842849..a881492257 100644 --- a/src/virtual_secondary/virtual_secondary_test.cc +++ b/src/virtual_secondary/virtual_secondary_test.cc @@ -1,64 +1,69 @@ #include -#include "uptane/partialverificationsecondary.h" -#include "uptane/secondaryconfig.h" +#include "partialverificationsecondary.h" #include "uptane/secondaryinterface.h" #include "virtualsecondary.h" +class VirtualSecondaryTest : public ::testing::Test { + protected: + VirtualSecondaryTest() { + config_.partial_verifying = false; + config_.full_client_dir = temp_dir_.Path(); + config_.ecu_serial = ""; + config_.ecu_hardware_id = "secondary_hardware"; + config_.ecu_private_key = "sec.priv"; + config_.ecu_public_key = "sec.pub"; + config_.firmware_path = temp_dir_.Path() / "firmware.txt"; + config_.target_name_path = temp_dir_.Path() / "firmware_name.txt"; + config_.metadata_path = temp_dir_.Path() / "metadata"; + } + + virtual void SetUp() {} + virtual void TearDown() {} + + protected: + TemporaryDirectory temp_dir_; + Primary::VirtualSecondaryConfig config_; +}; + +class PartialVerificationSecondaryTest : public ::testing::Test { + protected: + PartialVerificationSecondaryTest() { + config_.partial_verifying = true; + config_.full_client_dir = temp_dir_.Path(); + config_.ecu_serial = ""; + config_.ecu_hardware_id = "secondary_hardware"; + config_.ecu_private_key = "sec.priv"; + config_.ecu_public_key = "sec.pub"; + config_.firmware_path = temp_dir_.Path() / "firmware.txt"; + config_.target_name_path = temp_dir_.Path() / "firmware_name.txt"; + config_.metadata_path = temp_dir_.Path() / "metadata"; + } + + virtual void SetUp() {} + virtual void TearDown() {} + + protected: + TemporaryDirectory temp_dir_; + Primary::PartialVerificationSecondaryConfig config_; +}; + /* Create a virtual secondary for testing. */ -TEST(VirtualSecondary, Instantiation) { - TemporaryDirectory temp_dir; - Uptane::SecondaryConfig sconfig; - sconfig.secondary_type = Uptane::SecondaryType::kVirtual; - sconfig.partial_verifying = false; - sconfig.full_client_dir = temp_dir.Path(); - sconfig.ecu_serial = ""; - sconfig.ecu_hardware_id = "secondary_hardware"; - sconfig.ecu_private_key = "sec.priv"; - sconfig.ecu_public_key = "sec.pub"; - sconfig.firmware_path = temp_dir.Path() / "firmware.txt"; - sconfig.target_name_path = temp_dir.Path() / "firmware_name.txt"; - sconfig.metadata_path = temp_dir.Path() / "metadata"; - EXPECT_NO_THROW(Uptane::VirtualSecondary virtual_sec(sconfig)); -} +TEST_F(VirtualSecondaryTest, Instantiation) { EXPECT_NO_THROW(Primary::VirtualSecondary virtual_sec(config_)); } /* Partial verification secondaries generate and store public keys. */ -TEST(PartialVerificationSecondary, Uptane_get_key) { - TemporaryDirectory temp_dir; - Uptane::SecondaryConfig sconfig; - sconfig.secondary_type = Uptane::SecondaryType::kVirtualUptane; - sconfig.partial_verifying = true; - sconfig.full_client_dir = temp_dir.Path(); - sconfig.ecu_serial = ""; - sconfig.ecu_hardware_id = "secondary_hardware"; - sconfig.ecu_private_key = "sec.priv"; - sconfig.ecu_public_key = "sec.pub"; - sconfig.firmware_path = temp_dir.Path() / "firmware.txt"; - sconfig.target_name_path = temp_dir.Path() / "firmware_name.txt"; - sconfig.metadata_path = temp_dir.Path() / "metadata"; - Uptane::PartialVerificationSecondary sec1(sconfig); +TEST_F(PartialVerificationSecondaryTest, Uptane_get_key) { + Uptane::PartialVerificationSecondary sec1(config_); PublicKey key1 = sec1.getPublicKey(); - Uptane::PartialVerificationSecondary sec2(sconfig); + Uptane::PartialVerificationSecondary sec2(config_); PublicKey key2 = sec2.getPublicKey(); // Verify that we store keys EXPECT_EQ(key1, key2); } /* Partial verification secondaries can verify Uptane metadata. */ -TEST(PartialVerificationSecondary, Uptane_putMetadata_good) { - TemporaryDirectory temp_dir; - Uptane::SecondaryConfig sconfig; - sconfig.secondary_type = Uptane::SecondaryType::kVirtualUptane; - sconfig.partial_verifying = true; - sconfig.full_client_dir = temp_dir.Path(); - sconfig.ecu_serial = ""; - sconfig.ecu_hardware_id = "secondary_hardware"; - sconfig.ecu_private_key = "sec.priv"; - sconfig.ecu_public_key = "sec.pub"; - sconfig.firmware_path = temp_dir.Path() / "firmware.txt"; - sconfig.target_name_path = temp_dir.Path() / "firmware_name.txt"; - sconfig.metadata_path = temp_dir.Path() / "metadata"; - Uptane::PartialVerificationSecondary sec(sconfig); +TEST_F(PartialVerificationSecondaryTest, Uptane_putMetadata_good) { + Uptane::PartialVerificationSecondary sec(config_); Uptane::RawMetaPack metadata; metadata.director_root = Utils::readFile("tests/test_data/repo/repo/director/root.json"); @@ -67,20 +72,8 @@ TEST(PartialVerificationSecondary, Uptane_putMetadata_good) { } /* Partial verification secondaries reject invalid Uptane metadata. */ -TEST(PartialVerificationSecondary, Uptane_putMetadata_bad) { - TemporaryDirectory temp_dir; - Uptane::SecondaryConfig sconfig; - sconfig.secondary_type = Uptane::SecondaryType::kVirtualUptane; - sconfig.partial_verifying = true; - sconfig.full_client_dir = temp_dir.Path(); - sconfig.ecu_serial = ""; - sconfig.ecu_hardware_id = "secondary_hardware"; - sconfig.ecu_private_key = "sec.priv"; - sconfig.ecu_public_key = "sec.pub"; - sconfig.firmware_path = temp_dir.Path() / "firmware.txt"; - sconfig.target_name_path = temp_dir.Path() / "firmware_name.txt"; - sconfig.metadata_path = temp_dir.Path() / "metadata"; - Uptane::PartialVerificationSecondary sec(sconfig); +TEST_F(PartialVerificationSecondaryTest, Uptane_putMetadata_bad) { + Uptane::PartialVerificationSecondary sec(config_); Uptane::RawMetaPack metadata; metadata.director_root = Utils::readFile("tests/test_data/repo/repo/director/root.json"); diff --git a/src/virtual_secondary/virtualsecondary.cc b/src/virtual_secondary/virtualsecondary.cc index 36c1452ead..f71db00a56 100644 --- a/src/virtual_secondary/virtualsecondary.cc +++ b/src/virtual_secondary/virtualsecondary.cc @@ -1,14 +1,63 @@ -#include "virtualsecondary.h" - #include #include +#include #include "crypto/crypto.h" #include "utilities/fault_injection.h" #include "utilities/utils.h" +#include "virtualsecondary.h" + +namespace Primary { + +const char* const VirtualSecondaryConfig::Type = "virtual"; + +VirtualSecondaryConfig::VirtualSecondaryConfig(const Json::Value& json_config) : ManagedSecondaryConfig(Type) { + partial_verifying = json_config["partial_verifying"].asBool(); + ecu_serial = json_config["ecu_serial"].asString(); + ecu_hardware_id = json_config["ecu_hardware_id"].asString(); + full_client_dir = json_config["full_client_dir"].asString(); + ecu_private_key = json_config["ecu_private_key"].asString(); + ecu_public_key = json_config["ecu_public_key"].asString(); + firmware_path = json_config["firmware_path"].asString(); + target_name_path = json_config["target_name_path"].asString(); + metadata_path = json_config["metadata_path"].asString(); +} + +VirtualSecondaryConfig VirtualSecondaryConfig::create_from_file(const boost::filesystem::path& file_full_path) { + Json::Value json_config; + Json::Reader reader; + std::ifstream json_file(file_full_path.string()); -namespace Uptane { -VirtualSecondary::VirtualSecondary(const SecondaryConfig& sconfig_in) : ManagedSecondary(sconfig_in) {} + reader.parse(json_file, json_config); + json_file.close(); + return VirtualSecondaryConfig(json_config[Type][0]); +} + +void VirtualSecondaryConfig::dump(const boost::filesystem::path& file_full_path) const { + Json::Value json_config; + + json_config["partial_verifying"] = partial_verifying; + json_config["ecu_serial"] = ecu_serial; + json_config["ecu_hardware_id"] = ecu_hardware_id; + json_config["full_client_dir"] = full_client_dir.string(); + json_config["ecu_private_key"] = ecu_private_key; + json_config["ecu_public_key"] = ecu_public_key; + json_config["firmware_path"] = firmware_path.string(); + json_config["target_name_path"] = target_name_path.string(); + json_config["metadata_path"] = metadata_path.string(); + + Json::Value root; + root[Type].append(json_config); + + Json::StyledStreamWriter json_writer; + boost::filesystem::create_directories(file_full_path.parent_path()); + std::ofstream json_file(file_full_path.string()); + json_writer.write(json_file, root); + json_file.close(); +} + +VirtualSecondary::VirtualSecondary(Primary::VirtualSecondaryConfig sconfig_in) + : ManagedSecondary(std::move(sconfig_in)) {} bool VirtualSecondary::storeFirmware(const std::string& target_name, const std::string& content) { if (fiu_fail((std::string("secondary_install_") + getSerial().ToString()).c_str()) != 0) { @@ -35,4 +84,5 @@ bool VirtualSecondary::getFirmwareInfo(std::string* target_name, size_t& target_ return true; } -} // namespace Uptane + +} // namespace Primary diff --git a/src/virtual_secondary/virtualsecondary.h b/src/virtual_secondary/virtualsecondary.h index 296ae2cfa3..60b080347f 100644 --- a/src/virtual_secondary/virtualsecondary.h +++ b/src/virtual_secondary/virtualsecondary.h @@ -1,21 +1,35 @@ -#ifndef UPTANE_VIRTUALSECONDARY_H_ -#define UPTANE_VIRTUALSECONDARY_H_ +#ifndef PRIMARY_VIRTUALSECONDARY_H_ +#define PRIMARY_VIRTUALSECONDARY_H_ #include #include "managedsecondary.h" #include "utilities/types.h" -namespace Uptane { +namespace Primary { + +class VirtualSecondaryConfig : public ManagedSecondaryConfig { + public: + VirtualSecondaryConfig() : ManagedSecondaryConfig(Type) {} + VirtualSecondaryConfig(const Json::Value& json_config); + + static VirtualSecondaryConfig create_from_file(const boost::filesystem::path& file_full_path); + void dump(const boost::filesystem::path& file_full_path) const; + + public: + static const char* const Type; +}; + class VirtualSecondary : public ManagedSecondary { public: - explicit VirtualSecondary(const SecondaryConfig& sconfig_in); + explicit VirtualSecondary(Primary::VirtualSecondaryConfig sconfig_in); ~VirtualSecondary() override = default; private: bool storeFirmware(const std::string& target_name, const std::string& content) override; bool getFirmwareInfo(std::string* target_name, size_t& target_len, std::string* sha256hash) override; }; -} // namespace Uptane -#endif // UPTANE_VIRTUALSECONDARY_H_ +} // namespace Primary + +#endif // PRIMARY_VIRTUALSECONDARY_H_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bb4b148e3b..cb36c45ca0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -157,12 +157,6 @@ add_test(NAME test-help-with-nonexistent-options set_tests_properties(test-help-with-nonexistent-options PROPERTIES PASS_REGULAR_EXPRESSION "aktualizr command line options") -add_test(NAME test-secondary-config-with-nonexisting-dir - COMMAND aktualizr -c ${PROJECT_SOURCE_DIR}/config/sota-shared-cred.toml - --tls-server fake --secondary-configs-dir nonexistingdir check) -set_tests_properties(test-secondary-config-with-nonexisting-dir - PROPERTIES PASS_REGULAR_EXPRESSION "\"nonexistingdir\": not a directory") - # run `aktualizr check` for these tests, as they can run forever if a global # configuration is present diff --git a/tests/uptane_test_common.h b/tests/uptane_test_common.h index 8db77dc91f..4bbd1f292d 100644 --- a/tests/uptane_test_common.h +++ b/tests/uptane_test_common.h @@ -7,19 +7,80 @@ #include "json/json.h" #include "config/config.h" -#include "uptane/secondaryconfig.h" #include "uptane/tuf.h" #include "utilities/utils.h" +#include "virtualsecondary.h" +#include "primary/sotauptaneclient.h" +#include "primary/aktualizr.h" static const char* sec_public_key = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyjUeAzozBEccaGFAJ2Q3\n9QBfItH5i5O7yLRjZlKcEnWnFsxAWHUn5W/msRgZN/pXUrlax0wvrvMvHHLwZA2J\nz+UQApzSqj53HPVAcCH6kB9x0r9PM/0vVTKtmcrdSHj7jJ2yAW2T4Vo/eKlpvz3w\n9kTPAj0j1f5LvUgX5VIjUnsQK1LGzMwnleHk2dkWeWnt3OqomnO7V5C0jkDi58tG\nJ6fnyCYWcMUbpMaldXVXqmQ+iBkWxBjZ99+XJSRjdsskC7x8u8t+sA146VDB977r\nN8D+i+P1tAe810crciUqpYNenDYx47aAm6gaDWr7oeDzp3HyCjx4dZi9Z85rVE36\n8wIDAQAB\n-----END PUBLIC KEY-----\n"; static const char* sec_private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAyjUeAzozBEccaGFAJ2Q39QBfItH5i5O7yLRjZlKcEnWnFsxA\nWHUn5W/msRgZN/pXUrlax0wvrvMvHHLwZA2Jz+UQApzSqj53HPVAcCH6kB9x0r9P\nM/0vVTKtmcrdSHj7jJ2yAW2T4Vo/eKlpvz3w9kTPAj0j1f5LvUgX5VIjUnsQK1LG\nzMwnleHk2dkWeWnt3OqomnO7V5C0jkDi58tGJ6fnyCYWcMUbpMaldXVXqmQ+iBkW\nxBjZ99+XJSRjdsskC7x8u8t+sA146VDB977rN8D+i+P1tAe810crciUqpYNenDYx\n47aAm6gaDWr7oeDzp3HyCjx4dZi9Z85rVE368wIDAQABAoIBAA0WlxS6Zab3O11+\nPfrOv9h5566HTNG+BD+ffXeYDUYcm24cVmXjX2u4bIQ1/RvkdlaCbN/NjKCUWQ5M\nWkb/oVX1i62/nNssI+WZ8kvPxzog7usnOucwkim/mAEGYoBYZF/brTPudc32W3lh\n7dhVGA24snWAo5ssVJax3eoYAPVLqFK5Pb8VUxpHtjERMDDUxM3w6WGXLxuBdA5s\n5vIdv+XrdiQhdPn1HMYEBBInkkYK8w4UytOCAS1/3xfVi2QwX5H9bHkduFpjLSQt\n2StWR9Kh4I80xXp7FwGpfkdUn+3qj5WwneuGY/JnD7AzjDlAThj0AE9iaYjkzXKJ\nVD4ULmECgYEA+UGQ1aglftFuTO427Xmi7tHhooo9U1pKMrg5CkCLkA+MudFzMEgj\npRtDdj8lTTWHEIYQXo5hhZfhk63j89RAKRz1MDFOvgknE8yJa9rSyCAEcwzRzXcY\n3WtWozEZ+5u4KYFHhGjJCSqVFdwyXmjP9ldb35Uxh06OuTbdNkSbiUsCgYEAz62t\nJ1EftTkd/YA/9Poq1deil5g0btPXnMJMj7C99dexNAXuVhS10Rz1Hi74wCFEbkcV\nGL/8U80pER9YYYeFUmqs1pYu7zwcYBT+iNrvFaPifid8FqlJEJ727swnWdpzXpwv\n/6q0h3JXU2odrEMNaGqiPycHQ/45EWMbCtpSs/kCgYEAwjMgWicA17bqvkuXRhzQ\nIkwqBU65ixi82JmJ73/sfNhwp1IV8hcylnAQdq+qK2a6Ddi2JkW+m6yDF2GTSiUj\nvCSQr/SqygsthBKHOx4pvbycWtsxF2lkWRdJUCpweQWRTd0o0HQntdmUgIyoPcBh\nzyevMBr4lNhTAOFLJv37RNMCgYAQq+ODjXqbJKuopvv7YX3Azt+phbln0C+10M8u\nlcSaEKeUAongdScnU0jGFIU5fzIsHB6wbvEFlSmfy0FgCu4D8LZRP5si71Njzyuj\ntteMiCxtbiQC+bH42JoAD3l1OBkc1jLwNjbpzJ7//jvFkVhpMm413Z8ysRzJrYgF\nNgN/mQKBgQDNT2nFoqanlQPkZekqNQNcVMHPyOWP40z4HC5JD1Z5F18Kg3El4EdS\nNfwaFGRT5qiFJBmmzl+6EFmUrrBNtV01zQ6rO+xgy2Y7qUQMNAUMjh1cCpWwUlN0\ng4aT/RawS5WpWN3+lEs4Ouxpgg4ZStXNZRJkSDHwZpkXtFfKzsEXaA==\n-----END RSA PRIVATE KEY-----\n"; +std::shared_ptr newTestClient(Config &config_in, + std::shared_ptr storage_in, + std::shared_ptr http_client_in, + std::shared_ptr events_channel_in = nullptr) { + std::shared_ptr bootloader_in = std::make_shared(config_in.bootloader, *storage_in); + std::shared_ptr report_queue_in = std::make_shared(config_in, http_client_in); + return std::make_shared(config_in, storage_in, http_client_in, bootloader_in, + report_queue_in, events_channel_in); +} + struct UptaneTestCommon { - static Uptane::SecondaryConfig addDefaultSecondary(Config& config, const TemporaryDirectory& temp_dir, - const std::string& serial, const std::string& hw_id) { - Uptane::SecondaryConfig ecu_config; - ecu_config.secondary_type = Uptane::SecondaryType::kVirtual; + class TestAktualizr: public Aktualizr { + public: + TestAktualizr(Config& config): Aktualizr(config) {} + + TestAktualizr(Config& config, + std::shared_ptr storage, + std::shared_ptr http) + : Aktualizr(config, storage, http) { + + // add secondary(ies) if its/their configuration is specified in the config file + if (boost::filesystem::exists(config.uptane.secondary_config_file)) { + AddSecondary(std::make_shared(Primary::VirtualSecondaryConfig::create_from_file(config.uptane.secondary_config_file))); + } + } + + std::shared_ptr& uptane_client() { return uptane_client_; } + }; + + class TestUptaneClient: public SotaUptaneClient + { + public: + TestUptaneClient(Config &config_in, + std::shared_ptr storage_in, + std::shared_ptr http_client, + std::shared_ptr bootloader_in, + std::shared_ptr report_queue_in, + std::shared_ptr events_channel_in = nullptr): + SotaUptaneClient(config_in, storage_in, http_client, bootloader_in, report_queue_in, events_channel_in) { + + // add secondary(ies) if its/their configuration is specified in the config file + if (boost::filesystem::exists(config_in.uptane.secondary_config_file)) { + addSecondary(std::make_shared(Primary::VirtualSecondaryConfig::create_from_file(config_in.uptane.secondary_config_file))); + } + } + }; + + static std::shared_ptr newTestClient(Config &config_in, + std::shared_ptr storage_in, + std::shared_ptr http_client_in, + std::shared_ptr events_channel_in = nullptr) { + + std::shared_ptr bootloader_in = std::make_shared(config_in.bootloader, *storage_in); + std::shared_ptr report_queue_in = std::make_shared(config_in, http_client_in); + + return std::make_shared(config_in, storage_in, http_client_in, bootloader_in, + report_queue_in, events_channel_in); + } + + static Primary::VirtualSecondaryConfig addDefaultSecondary(Config& config, const TemporaryDirectory& temp_dir, + const std::string& serial, const std::string& hw_id, + bool hardcoded_keys = true) { + Primary::VirtualSecondaryConfig ecu_config; + ecu_config.partial_verifying = false; ecu_config.full_client_dir = temp_dir.Path(); ecu_config.ecu_serial = serial; @@ -29,10 +90,15 @@ struct UptaneTestCommon { ecu_config.firmware_path = temp_dir / "firmware.txt"; ecu_config.target_name_path = temp_dir / "firmware_name.txt"; ecu_config.metadata_path = temp_dir / "secondary_metadata"; - // store hard-coded keys to make the tests run WAY faster - Utils::writeFile((ecu_config.full_client_dir / ecu_config.ecu_private_key), std::string(sec_private_key)); - Utils::writeFile((ecu_config.full_client_dir / ecu_config.ecu_public_key), std::string(sec_public_key)); - config.uptane.secondary_configs.push_back(ecu_config); + + config.uptane.secondary_config_file = temp_dir / boost::filesystem::unique_path() / "virtual_secondary_conf.json"; + ecu_config.dump(config.uptane.secondary_config_file); + + if (hardcoded_keys) { + // store hard-coded keys to make the tests run WAY faster + Utils::writeFile((ecu_config.full_client_dir / ecu_config.ecu_private_key), std::string(sec_private_key)); + Utils::writeFile((ecu_config.full_client_dir / ecu_config.ecu_public_key), std::string(sec_public_key)); + } return ecu_config; }