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

Commit

Permalink
Rebased on latest master
Browse files Browse the repository at this point in the history
Signed-off-by: Zee314159 <252806294@qq.com>
  • Loading branch information
Zee314159 committed Aug 16, 2019
1 parent b12990e commit 702c9aa
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/libaktualizr/primary/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ if (BUILD_OSTREE)
ENVIRONMENT LD_PRELOAD=$<TARGET_FILE:ostree_mock>
LABELS "noptest")
target_link_libraries(t_aktualizr_fullostree virtual_secondary)
add_aktualizr_test(NAME download_nonostree SOURCES download_nonostree_test.cc PROJECT_WORKING_DIRECTORY ARGS $<TARGET_FILE:uptane-generator> ${PROJECT_BINARY_DIR}/ostree_repo)
add_dependencies(t_download_nonostree uptane-generator make_ostree_sysroot)
set_tests_properties(test_download_nonostree PROPERTIES
LABELS "noptest")
target_link_libraries(t_download_nonostree virtual_secondary)
else (BUILD_OSTREE)
aktualizr_source_file_checks(aktualizr_fullostree_test.cc)
aktualizr_source_file_checks(download_nonostree_test.cc)
endif (BUILD_OSTREE)

add_aktualizr_test(NAME reportqueue SOURCES reportqueue_test.cc PROJECT_WORKING_DIRECTORY LIBRARIES PUBLIC uptane_generator_lib)
Expand Down
111 changes: 111 additions & 0 deletions src/libaktualizr/primary/download_nonostree_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <gtest/gtest.h>

#include <boost/process.hpp>

#include "uptane_test_common.h"

#include "config/config.h"
#include "logging/logging.h"
#include "package_manager/ostreemanager.h"
#include "primary/aktualizr.h"
#include "storage/sqlstorage.h"
#include "test_utils.h"

static std::string server = "http://127.0.0.1:";
static std::string treehub_server = "http://127.0.0.1:";
static boost::filesystem::path sysroot;

static std::string new_rev;

/*
* Reject non-OSTree binaries.
*/
TEST(Aktualizr, DownloadNonOstreeBin) {
TemporaryDirectory temp_dir;
Config conf = UptaneTestCommon::makeTestConfig(temp_dir, server);
conf.pacman.type = PackageManager::kOstree;
conf.pacman.sysroot = sysroot.string();
conf.pacman.ostree_server = treehub_server;
conf.pacman.os = "dummy-os";
conf.provision.device_id = "device_id";
conf.provision.ecu_registration_endpoint = server + "/director/ecus";
conf.tls.server = server;

{
std::shared_ptr<INvStorage> storage = INvStorage::newStorage(conf.storage);
OstreeManager ostree(conf.pacman, storage, nullptr, nullptr);

Aktualizr aktualizr(conf);

aktualizr.Initialize();

result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
ASSERT_EQ(update_result.status, result::UpdateStatus::kError);
}
}

#ifndef __NO_MAIN__
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

logger_init();

if (argc != 3) {
std::cerr << "Error: " << argv[0] << " requires the path to the uptane-generator utility "
<< "and an OStree sysroot\n";
return EXIT_FAILURE;
}

boost::filesystem::path uptane_generator_path;
uptane_generator_path = argv[1];

Process ostree("ostree");

TemporaryDirectory meta_dir;
TemporaryDirectory temp_sysroot;
sysroot = temp_sysroot / "sysroot";
// uses cp, as boost doesn't like to copy bad symlinks
int ret = system((std::string("cp -r ") + argv[2] + std::string(" ") + sysroot.string()).c_str());
if (ret != 0) {
return -1;
}
auto r = ostree.run(
{"rev-parse", std::string("--repo"), (sysroot / "/ostree/repo").string(), "generate-remote/generated"});
if (std::get<0>(r) != 0) {
return -1;
}

std::string port = TestUtils::getFreePort();
server += port;
boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-m", meta_dir.Path());
TestUtils::waitForServer(server + "/");

std::string treehub_port = TestUtils::getFreePort();
treehub_server += treehub_port;
TemporaryDirectory treehub_dir;
boost::process::child ostree_server_process("tests/sota_tools/treehub_server.py", std::string("-p"), treehub_port,
std::string("-d"), treehub_dir.PathString(), std::string("-s0.5"),
std::string("--create"));
TestUtils::waitForServer(treehub_server + "/");
r = ostree.run({"rev-parse", std::string("--repo"), treehub_dir.PathString(), "master"});
if (std::get<0>(r) != 0) {
return -1;
}
new_rev = ostree.lastStdOut();
boost::trim_if(new_rev, boost::is_any_of(" \t\r\n"));
LOG_INFO << "DEST: " << new_rev;

Process uptane_gen(uptane_generator_path.string());
uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
uptane_gen.run({"image", "--path", meta_dir.PathString(), "--targetname", "update_1.0", "--targetsha256", new_rev,
"--targetlength", "0", "--targetformat", "BINARY", "--hwid", "primary_hw"});
uptane_gen.run({"addtarget", "--path", meta_dir.PathString(), "--targetname", "update_1.0", "--hwid", "primary_hw",
"--serial", "CA:FE:A6:D2:84:9D"});
uptane_gen.run({"signtargets", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
LOG_INFO << uptane_gen.lastStdOut();
// Work around inconsistent directory naming.
Utils::copyDir(meta_dir.Path() / "repo/image", meta_dir.Path() / "repo/repo");

return RUN_ALL_TESTS();
}
#endif // __NO_MAIN__
30 changes: 17 additions & 13 deletions src/libaktualizr/primary/sotauptaneclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,13 @@ void SotaUptaneClient::finalizeAfterReboot() {
data::InstallationResult SotaUptaneClient::PackageInstallSetResult(const Uptane::Target &target) {
data::InstallationResult result;
Uptane::EcuSerial ecu_serial = uptane_manifest.getPrimaryEcuSerial();
if (!target.IsOstree() &&
(config.pacman.type == PackageManager::kOstree || config.pacman.type == PackageManager::kOstreeDockerApp)) {
result = data::InstallationResult(data::ResultCode::Numeric::kValidationFailed,
"Cannot install a non-OSTree package on an OSTree system");
} else {
result = PackageInstall(target);
if (result.result_code.num_code == data::ResultCode::Numeric::kOk) {
// simple case: update already completed
storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);
} else if (result.result_code.num_code == data::ResultCode::Numeric::kNeedCompletion) {
// ostree case: need reboot
storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kPending);
}
result = PackageInstall(target);
if (result.result_code.num_code == data::ResultCode::Numeric::kOk) {
// simple case: update already completed
storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);
} else if (result.result_code.num_code == data::ResultCode::Numeric::kNeedCompletion) {
// ostree case: need reboot
storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kPending);
}
storage->saveEcuInstallationResult(ecu_serial, result);
return result;
Expand Down Expand Up @@ -411,6 +405,7 @@ void SotaUptaneClient::computeDeviceInstallationResult(data::InstallationResult

bool SotaUptaneClient::getNewTargets(std::vector<Uptane::Target> *new_targets, unsigned int *ecus_count) {
std::vector<Uptane::Target> targets = director_repo.getTargets();
Uptane::EcuSerial primary_ecu_serial = uptane_manifest.getPrimaryEcuSerial();
if (ecus_count != nullptr) {
*ecus_count = 0;
}
Expand Down Expand Up @@ -447,6 +442,15 @@ bool SotaUptaneClient::getNewTargets(std::vector<Uptane::Target> *new_targets, u
is_new = true;
}

if (primary_ecu_serial == ecu_serial) {
if (!target.IsOstree() &&
(config.pacman.type == PackageManager::kOstree || config.pacman.type == PackageManager::kOstreeDockerApp)) {
LOG_ERROR << "Cannot install a non-OSTree package on an OSTree system";
last_exception = Uptane::InvalidTarget(target.filename());
return false;
}
}

if (is_new && ecus_count != nullptr) {
(*ecus_count)++;
}
Expand Down
1 change: 1 addition & 0 deletions src/libaktualizr/primary/sotauptaneclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class SotaUptaneClient {
FRIEND_TEST(Aktualizr, AutoRebootAfterUpdate);
FRIEND_TEST(Aktualizr, EmptyTargets);
FRIEND_TEST(Aktualizr, FullOstreeUpdate);
FRIEND_TEST(Aktualizr, DownloadNonOstreeBin);
FRIEND_TEST(DockerAppManager, DockerApp_Fetch);
FRIEND_TEST(Uptane, AssembleManifestGood);
FRIEND_TEST(Uptane, AssembleManifestBad);
Expand Down
7 changes: 7 additions & 0 deletions src/libaktualizr/uptane/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class DelegationMissing : public Exception {
~DelegationMissing() noexcept override = default;
};

class InvalidTarget : public Exception {
public:
InvalidTarget(const std::string& reponame)
: Exception(reponame, "The target had a non-OSTree package that can not be installed on an OSTree system.") {}
~InvalidTarget() noexcept override = default;
};

} // namespace Uptane

#endif

0 comments on commit 702c9aa

Please sign in to comment.