Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
fetcher_death_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <boost/process.hpp>
4 #include <chrono>
5 #include <csignal>
6 #include <future>
7 #include <iostream>
8 #include <string>
9 #include <thread>
10 
11 #include "config/config.h"
12 #include "http/httpclient.h"
13 #include "logging/logging.h"
14 #include "package_manager/packagemanagerfake.h"
15 #include "storage/sqlstorage.h"
16 #include "test_utils.h"
17 #include "uptane/tuf.h"
18 
19 static const int die_after = 50; // percent
20 static const int pause_duration = 2; // seconds
21 
22 std::string server;
23 
24 static std::mutex pause_m;
25 static std::condition_variable cv;
26 static bool die = false;
27 static bool resumed = false;
28 
29 Config config;
30 
31 static void progress_cb(const Uptane::Target& target, const std::string& description, unsigned int progress) {
32  (void)target;
33  (void)description;
34  std::cout << "progress: " << progress << std::endl;
35  if (progress >= die_after) {
36  std::lock_guard<std::mutex> lk(pause_m);
37  die = true;
38  cv.notify_all();
39  }
40  if (resumed) {
41  EXPECT_GE(progress, die_after);
42  }
43 }
44 
45 void resume(const Uptane::Target& target) {
46  std::shared_ptr<INvStorage> storage(new SQLStorage(config.storage, false));
47  auto http = std::make_shared<HttpClient>();
48  auto pacman = std::make_shared<PackageManagerFake>(config.pacman, config.bootloader, storage, http);
50  KeyManager keys(storage, config.keymanagerConfig());
51  Uptane::Fetcher fetcher(config, http);
52 
53  resumed = true;
54  bool res = pacman->fetchTarget(target, fetcher, keys, progress_cb, &token);
55 
56  EXPECT_TRUE(res);
57 }
58 
59 void try_and_die(const Uptane::Target& target, bool graceful) {
60  std::shared_ptr<INvStorage> storage(new SQLStorage(config.storage, false));
61  auto http = std::make_shared<HttpClient>();
63  auto pacman = std::make_shared<PackageManagerFake>(config.pacman, config.bootloader, storage, http);
64  KeyManager keys(storage, config.keymanagerConfig());
65  Uptane::Fetcher fetcher(config, http);
66 
67  std::promise<bool> download_promise;
68  auto result = download_promise.get_future();
69 
70  std::thread([&target, &fetcher, &download_promise, &token, pacman, &keys]() {
71  bool res = pacman->fetchTarget(target, fetcher, keys, progress_cb, &token);
72  download_promise.set_value(res);
73  })
74  .detach();
75 
76  std::unique_lock<std::mutex> lk(pause_m);
77  cv.wait(lk, [] { return die; });
78  if (graceful) {
79  token.setPause(true);
80  std::this_thread::sleep_for(std::chrono::seconds(pause_duration));
81  std::raise(SIGTERM);
82  } else {
83  std::raise(SIGKILL);
84  }
85 }
86 
87 TEST(FetcherDeathTest, TestResumeAfterPause) {
88  TemporaryDirectory temp_dir;
89  config.storage.path = temp_dir.Path();
90 
91  Json::Value target_json;
92  target_json["hashes"]["sha256"] = "dd7bd1c37a3226e520b8d6939c30991b1c08772d5dab62b381c3a63541dc629a";
93  target_json["length"] = 100 * (1 << 20);
94 
95  Uptane::Target target("large_file", target_json);
96  die = false;
97  resumed = false;
98  ASSERT_DEATH(try_and_die(target, true), "");
99  std::cout << "Fetcher died, retrying" << std::endl;
100  resume(target);
101 }
102 
103 TEST(FetcherDeathTest, TestResumeAfterSigkill) {
104  TemporaryDirectory temp_dir;
105  config.storage.path = temp_dir.Path();
106 
107  Json::Value target_json;
108  target_json["hashes"]["sha256"] = "dd7bd1c37a3226e520b8d6939c30991b1c08772d5dab62b381c3a63541dc629a";
109  target_json["length"] = 100 * (1 << 20);
110 
111  Uptane::Target target("large_file", target_json);
112  die = false;
113  resumed = false;
114  ASSERT_DEATH(try_and_die(target, false), "");
115  std::cout << "Fetcher died, retrying" << std::endl;
116  resume(target);
117 }
118 
119 #ifndef __NO_MAIN__
120 int main(int argc, char** argv) {
121  ::testing::InitGoogleTest(&argc, argv);
122 
123  logger_init();
124  logger_set_threshold(boost::log::trivial::trace);
125 
126  std::string port = TestUtils::getFreePort();
127  server = "http://127.0.0.1:" + port;
128  config.uptane.repo_server = server;
129  boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
130  TestUtils::waitForServer(server + "/");
131  return RUN_ALL_TESTS();
132 }
133 #endif // __NO_MAIN__
Uptane::Fetcher
Definition: fetcher.h:33
KeyManager
Definition: keymanager.h:13
Config
Configuration object for an aktualizr instance running on a primary ECU.
Definition: config.h:74
api::FlowControlToken
Provides a thread-safe way to pause and terminate task execution.
Definition: apiqueue.h:19
TemporaryDirectory
Definition: utils.h:82
result
Results of libaktualizr API calls.
Definition: results.h:13
Uptane::Target
Definition: tuf.h:238
api::FlowControlToken::setPause
bool setPause(bool set_paused)
Called by the controlling thread to request the task to pause or resume.
Definition: apiqueue.cc:6
SQLStorage
Definition: sqlstorage.h:18