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