Aktualizr
C++ SOTA Client
download_nonostree_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <boost/process.hpp>
4 
5 #include "libaktualizr/aktualizr.h"
6 #include "libaktualizr/config.h"
7 #include "logging/logging.h"
8 #include "package_manager/ostreemanager.h"
9 #include "storage/sqlstorage.h"
10 #include "test_utils.h"
11 #include "uptane_test_common.h"
12 
13 static std::string server = "http://127.0.0.1:";
14 static std::string treehub_server = "http://127.0.0.1:";
15 static boost::filesystem::path sysroot;
16 
17 /*
18  * Reject non-OSTree binaries.
19  */
20 TEST(Aktualizr, DownloadNonOstreeBin) {
21  TemporaryDirectory temp_dir;
22  Config conf = UptaneTestCommon::makeTestConfig(temp_dir, server);
23  conf.pacman.type = PACKAGE_MANAGER_OSTREE;
24  conf.pacman.sysroot = sysroot.string();
25  conf.pacman.ostree_server = treehub_server;
26  conf.pacman.os = "dummy-os";
27  conf.provision.device_id = "device_id";
28  conf.provision.ecu_registration_endpoint = server + "/director/ecus";
29  conf.tls.server = server;
30 
31  {
32  std::shared_ptr<INvStorage> storage = INvStorage::newStorage(conf.storage);
33  auto uptane_client = std_::make_unique<SotaUptaneClient>(conf, storage);
34  uptane_client->initialize();
35  EXPECT_THROW(uptane_client->uptaneIteration(nullptr, nullptr), Uptane::InvalidTarget);
36  }
37 }
38 
39 #ifndef __NO_MAIN__
40 int main(int argc, char **argv) {
41  ::testing::InitGoogleTest(&argc, argv);
42 
43  logger_init();
44 
45  if (argc != 3) {
46  std::cerr << "Error: " << argv[0] << " requires the path to the uptane-generator utility "
47  << "and an OSTree sysroot\n";
48  return EXIT_FAILURE;
49  }
50 
51  boost::filesystem::path uptane_generator_path;
52  uptane_generator_path = argv[1];
53 
54  TemporaryDirectory meta_dir;
55  TemporaryDirectory temp_sysroot;
56  sysroot = temp_sysroot / "sysroot";
57 
58  // uses cp, as boost doesn't like to copy bad symlinks
59  int ret = system((std::string("cp -r ") + argv[2] + std::string(" ") + sysroot.string()).c_str());
60  if (ret != 0) {
61  return -1;
62  }
63 
64  Process ostree("ostree");
65 
66  std::string new_rev = ostree.lastStdOut();
67  boost::trim_if(new_rev, boost::is_any_of(" \t\r\n"));
68 
69  std::string port = TestUtils::getFreePort();
70  server += port;
71  boost::process::child http_server_process("tests/fake_http_server/fake_test_server.py", port, "-m", meta_dir.Path());
72  TestUtils::waitForServer(server + "/");
73  std::string treehub_port = TestUtils::getFreePort();
74  treehub_server += treehub_port;
75  TemporaryDirectory treehub_dir;
76  boost::process::child ostree_server_process("tests/sota_tools/treehub_server.py", std::string("-p"), treehub_port,
77  std::string("-d"), treehub_dir.PathString(), std::string("-s0.5"),
78  std::string("--create"));
79  TestUtils::waitForServer(treehub_server + "/");
80 
81  Process uptane_gen(uptane_generator_path.string());
82  uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
83  uptane_gen.run({"image", "--path", meta_dir.PathString(), "--targetname", "update_1.0", "--targetsha256", new_rev,
84  "--targetlength", "0", "--targetformat", "BINARY", "--hwid", "primary_hw"});
85  uptane_gen.run({"addtarget", "--path", meta_dir.PathString(), "--targetname", "update_1.0", "--hwid", "primary_hw",
86  "--serial", "CA:FE:A6:D2:84:9D"});
87  uptane_gen.run({"signtargets", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
88 
89  return RUN_ALL_TESTS();
90 }
91 #endif // __NO_MAIN__
Uptane::InvalidTarget
Definition: exceptions.h:151
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
Process
Definition: test_utils.h:19
TemporaryDirectory
Definition: utils.h:82