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 = 20; // 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 pause_and_die(const Uptane::Target& target) {
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  token.setPause(true);
79  std::this_thread::sleep_for(std::chrono::seconds(pause_duration));
80  std::raise(SIGKILL);
81 }
82 
83 TEST(FetcherDeathTest, TestResumeBinary) {
84  TemporaryDirectory temp_dir;
85  config.storage.path = temp_dir.Path();
86 
87  Json::Value target_json;
88  target_json["hashes"]["sha256"] = "dd7bd1c37a3226e520b8d6939c30991b1c08772d5dab62b381c3a63541dc629a";
89  target_json["length"] = 100 * (1 << 20);
90 
91  Uptane::Target target("large_file", target_json);
92  ASSERT_DEATH(pause_and_die(target), "");
93  std::cout << "Fetcher died, retrying" << std::endl;
94  resume(target);
95 }
96 
97 #ifndef __NO_MAIN__
98 int main(int argc, char** argv) {
99  ::testing::InitGoogleTest(&argc, argv);
100 
101  logger_init();
102  logger_set_threshold(boost::log::trivial::trace);
103 
104  std::string port = TestUtils::getFreePort();
105  server = "http://127.0.0.1:" + port;
106  config.uptane.repo_server = server;
107  boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
108  TestUtils::waitForServer(server + "/");
109  return RUN_ALL_TESTS();
110 }
111 #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:73
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