Aktualizr
C++ SOTA Client
custom_url_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 
5 #include "httpfake.h"
6 #include "libaktualizr/aktualizr.h"
7 #include "test_utils.h"
8 #include "uptane_test_common.h"
9 
10 boost::filesystem::path uptane_generator_path;
11 
12 class HttpCheckUrl : public HttpFake {
13  public:
14  HttpCheckUrl(const boost::filesystem::path &test_dir_in, const boost::filesystem::path &meta_dir_in)
15  : HttpFake(test_dir_in, "", meta_dir_in) {}
16  HttpResponse download(const std::string &url, curl_write_callback write_cb, curl_xferinfo_callback progress_cb,
17  void *userp, curl_off_t from) override {
18  (void)write_cb;
19  (void)progress_cb;
20  (void)userp;
21  (void)from;
22 
23  url_ = url;
24  return HttpResponse("", 200, CURLE_OK, "");
25  }
26 
27  std::string url_;
28 };
29 
30 /*
31  * If the URL from the Director is unset, but the URL from the Image repo is
32  * set, use that.
33  */
34 TEST(Aktualizr, ImageCustomUrl) {
35  TemporaryDirectory temp_dir;
36  TemporaryDirectory meta_dir;
37  auto http = std::make_shared<HttpCheckUrl>(temp_dir.Path(), meta_dir.Path() / "repo");
38  Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
39  logger_set_threshold(boost::log::trivial::trace);
40 
41  Process uptane_gen(uptane_generator_path.string());
42  uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
43  uptane_gen.run({"image", "--path", meta_dir.PathString(), "--filename", "tests/test_data/firmware.txt",
44  "--targetname", "firmware.txt", "--hwid", "primary_hw", "--url", "test-url"});
45  uptane_gen.run({"addtarget", "--path", meta_dir.PathString(), "--targetname", "firmware.txt", "--hwid", "primary_hw",
46  "--serial", "CA:FE:A6:D2:84:9D"});
47  uptane_gen.run({"signtargets", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
48 
49  auto storage = INvStorage::newStorage(conf.storage);
50  UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
51  aktualizr.Initialize();
52  EXPECT_EQ(http->url_, "");
53 
54  result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
55  EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
56 
57  result::Download download_result = aktualizr.Download(update_result.updates).get();
58  EXPECT_EQ(http->url_, "test-url");
59 }
60 
61 /*
62  * If the URL is set by both the Director and Image repo, use the version from
63  * the Director.
64  */
65 TEST(Aktualizr, BothCustomUrl) {
66  TemporaryDirectory temp_dir;
67  TemporaryDirectory meta_dir;
68  auto http = std::make_shared<HttpCheckUrl>(temp_dir.Path(), meta_dir.Path() / "repo");
69  Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
70  logger_set_threshold(boost::log::trivial::trace);
71 
72  Process uptane_gen(uptane_generator_path.string());
73  uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
74  uptane_gen.run({"image", "--path", meta_dir.PathString(), "--filename", "tests/test_data/firmware.txt",
75  "--targetname", "firmware.txt", "--hwid", "primary_hw", "--url", "test-url2"});
76  uptane_gen.run({"addtarget", "--path", meta_dir.PathString(), "--targetname", "firmware.txt", "--hwid", "primary_hw",
77  "--serial", "CA:FE:A6:D2:84:9D", "--url", "test-url3"});
78  uptane_gen.run({"signtargets", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
79 
80  auto storage = INvStorage::newStorage(conf.storage);
81  UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
82  aktualizr.Initialize();
83  EXPECT_EQ(http->url_, "");
84 
85  result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
86  EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
87 
88  result::Download download_result = aktualizr.Download(update_result.updates).get();
89  EXPECT_EQ(http->url_, "test-url3");
90 }
91 
92 #ifndef __NO_MAIN__
93 int main(int argc, char **argv) {
94  ::testing::InitGoogleTest(&argc, argv);
95  if (argc != 2) {
96  std::cerr << "Error: " << argv[0] << " requires the path to the uptane-generator utility\n";
97  return EXIT_FAILURE;
98  }
99  uptane_generator_path = argv[1];
100 
101  logger_init();
102  logger_set_threshold(boost::log::trivial::trace);
103 
104  return RUN_ALL_TESTS();
105 }
106 #endif
107 
108 // vim: set tabstop=2 shiftwidth=2 expandtab:
HttpFake
Definition: httpfake.h:20
UptaneTestCommon::TestAktualizr
Definition: uptane_test_common.h:21
result::UpdateCheck
Container for information about available updates.
Definition: results.h:37
HttpCheckUrl
Definition: custom_url_test.cc:12
HttpResponse
Definition: httpinterface.h:17
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
Aktualizr
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:24
result::Download
Container for information about downloading an update.
Definition: results.h:116
Process
Definition: test_utils.h:19
TemporaryDirectory
Definition: utils.h:82