Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
packagemanagerfake.cc
1 #include "packagemanagerfake.h"
2 
3 #include "utilities/fault_injection.h"
4 
5 Json::Value PackageManagerFake::getInstalledPackages() const {
6  Json::Value packages(Json::arrayValue);
7  Json::Value package;
8  package["name"] = "fake-package";
9  package["version"] = "1.0";
10  packages.append(package);
11  return packages;
12 }
13 
14 Uptane::Target PackageManagerFake::getCurrent() const {
15  boost::optional<Uptane::Target> current_version;
16  storage_->loadPrimaryInstalledVersions(&current_version, nullptr);
17 
18  if (!!current_version) {
19  return *current_version;
20  }
21 
22  return Uptane::Target::Unknown();
23 }
24 
25 data::InstallationResult PackageManagerFake::install(const Uptane::Target& target) const {
26  (void)target;
27 
28  // fault injection: only enabled with FIU_ENABLE defined
29  if (fiu_fail("fake_package_install") != 0) {
30  std::string failure_cause = fault_injection_last_info();
31  if (failure_cause.empty()) {
33  }
34  LOG_DEBUG << "Causing installation failure with message: " << failure_cause;
36  }
37 
38  if (config.fake_need_reboot) {
39  // set reboot flag to be notified later
40  if (bootloader_ != nullptr) {
41  bootloader_->rebootFlagSet();
42  }
43  return data::InstallationResult(data::ResultCode::Numeric::kNeedCompletion, "Application successful, need reboot");
44  }
45 
46  return data::InstallationResult(data::ResultCode::Numeric::kOk, "Installing package was successful");
47 }
48 
49 void PackageManagerFake::completeInstall() const {
50  LOG_INFO << "Emulating a system reboot";
51  bootloader_->reboot(true);
52 }
53 
54 data::InstallationResult PackageManagerFake::finalizeInstall(const Uptane::Target& target) const {
55  boost::optional<Uptane::Target> pending_version;
56  storage_->loadPrimaryInstalledVersions(nullptr, &pending_version);
57 
58  if (!pending_version) {
59  throw std::runtime_error("No pending update, nothing to finalize");
60  }
61 
62  data::InstallationResult install_res;
63 
64  if (target.MatchTarget(*pending_version)) {
65  if (fiu_fail("fake_install_finalization_failure") != 0) {
66  const std::string failure_cause = fault_injection_last_info();
67  if (failure_cause.empty()) {
69  } else {
70  install_res =
72  "Failed to finalize the pending update installation");
73  }
74  } else {
75  install_res = data::InstallationResult(data::ResultCode::Numeric::kOk, "Installing fake package was successful");
76  }
77 
78  } else {
79  install_res =
80  data::InstallationResult(data::ResultCode::Numeric::kInternalError, "Pending and new target do not match");
81  }
82 
83  return install_res;
84 }
85 
86 bool PackageManagerFake::fetchTarget(const Uptane::Target& target, Uptane::Fetcher& fetcher, const KeyManager& keys,
87  FetcherProgressCb progress_cb, const api::FlowControlToken* token) {
88  // fault injection: only enabled with FIU_ENABLE defined. Note that all
89  // exceptions thrown in PackageManagerInterface::fetchTarget are caught by a
90  // try in the same function, so we can only emulate the warning and return
91  // value.
92  if (fiu_fail("fake_package_download") != 0) {
93  const std::string failure_cause = fault_injection_last_info();
94  if (!failure_cause.empty()) {
95  LOG_WARNING << "Error while downloading a target: " << failure_cause;
96  } else {
97  LOG_WARNING << "Error while downloading a target: forced failure";
98  }
99  return false;
100  }
101 
102  return PackageManagerInterface::fetchTarget(target, fetcher, keys, progress_cb, token);
103 }
Uptane::Fetcher
Definition: fetcher.h:33
data::ResultCode
Definition: types.h:122
KeyManager
Definition: keymanager.h:13
data::InstallationResult
Definition: types.h:179
api::FlowControlToken
Provides a thread-safe way to pause and terminate task execution.
Definition: apiqueue.h:19
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric::kInstallFailed
Package installation failed.