Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
primary_secondary_registration_test.cc
1 #include <gtest/gtest.h>
2 
3 #include "httpfake.h"
4 #include "primary/aktualizr.h"
5 #include "secondary.h"
6 #include "uptane_test_common.h"
7 #include "utilities/utils.h"
8 
9 boost::filesystem::path uptane_repos_dir;
10 boost::filesystem::path fake_meta_dir;
11 
12 TEST(PrimarySecondaryReg, SecondariesMigration) {
13  // This tests that a device that had an ip secondary will still find it after
14  // recent changes, even if it does not connect when the device starts
15 
16  TemporaryDirectory temp_dir;
17  auto http = std::make_shared<HttpFake>(temp_dir.Path(), "noupdates", fake_meta_dir);
18 
19  const auto& url = http->tls_server;
20  Config conf("tests/config/basic.toml");
21  conf.uptane.director_server = url + "/director";
22  conf.uptane.repo_server = url + "/repo";
23  conf.provision.server = url;
24  conf.provision.primary_ecu_serial = "CA:FE:A6:D2:84:9D";
25  conf.provision.primary_ecu_hardware_id = "primary_hw";
26  conf.storage.path = temp_dir.Path();
27  conf.import.base_path = temp_dir.Path() / "import";
28  conf.tls.server = url;
29  conf.bootloader.reboot_sentinel_dir = temp_dir.Path();
30  const boost::filesystem::path sec_conf_path = temp_dir / "s_config.json";
31  conf.uptane.secondary_config_file = sec_conf_path;
32 
33  Json::Value sec_conf;
34 
35  auto storage = INvStorage::newStorage(conf.storage);
36 
37  const Uptane::EcuSerial primary_serial{"p_serial"};
38  const Uptane::EcuSerial secondary_serial{"s_serial"};
39  const Uptane::HardwareIdentifier primary_hwid{"p_hwid"};
40  const Uptane::HardwareIdentifier secondary_hwid{"s_hwid"};
41 
42  {
43  // prepare storage the "old" way:
44  storage->storeDeviceId("device");
45  storage->storeEcuSerials({{primary_serial, primary_hwid}, {secondary_serial, secondary_hwid}});
46  storage->storeEcuRegistered();
47 
48  sec_conf["IP"]["secondary_wait_port"] = 9030;
49  sec_conf["IP"]["secondary_wait_timeout"] = 1;
50  sec_conf["IP"]["secondaries"] = Json::arrayValue;
51  sec_conf["IP"]["secondaries"][0]["addr"] = "127.0.0.1:9061";
52  Utils::writeFile(sec_conf_path, sec_conf);
53  }
54 
55  {
56  // verifies that aktualizr can still start if it can't connect to its
57  // secondary
58 
59  UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
60  Primary::initSecondaries(aktualizr, sec_conf_path);
61  aktualizr.Initialize();
62  aktualizr.CheckUpdates().get();
63 
64  std::vector<SecondaryInfo> secs_info;
65  storage->loadSecondariesInfo(&secs_info);
66  EXPECT_EQ(secs_info[0].serial.ToString(), secondary_serial.ToString());
67  EXPECT_EQ(secs_info[0].type, "IP");
68  EXPECT_EQ(secs_info[0].extra, R"({"ip":"127.0.0.1","port":9061})");
69  }
70 
71  const Uptane::EcuSerial secondary_serial2{"s_serial2"};
72  const Uptane::HardwareIdentifier secondary_hwid2{"s_hwid2"};
73  {
74  // prepare the storage the "old" way with two secondaries
75  storage->clearEcuSerials();
76  storage->storeEcuSerials({
77  {primary_serial, primary_hwid},
78  {secondary_serial, secondary_hwid},
79  {secondary_serial2, secondary_hwid2},
80  });
81 
82  sec_conf["IP"]["secondaries"][1]["addr"] = "127.0.0.1:9062";
83  Utils::writeFile(sec_conf_path, sec_conf);
84  }
85 
86  {
87  UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
88 
89  // this will fail to actually register the secondaries as there is no way to
90  // tell them apart (since they haven't connected)
91  // however, we still allow aktualizr to go through in case we would like to
92  // update the primary, to maybe fix this situation
93  Primary::initSecondaries(aktualizr, sec_conf_path);
94  aktualizr.Initialize();
95  aktualizr.CheckUpdates().get();
96 
97  // there was no way to correlate info here
98  std::vector<SecondaryInfo> secs_info;
99  storage->loadSecondariesInfo(&secs_info);
100  EXPECT_EQ(secs_info[0].serial.ToString(), secondary_serial.ToString());
101  EXPECT_EQ(secs_info[0].type, "");
102  EXPECT_EQ(secs_info[0].extra, "");
103  EXPECT_EQ(secs_info[1].serial.ToString(), secondary_serial2.ToString());
104  EXPECT_EQ(secs_info[1].type, "");
105  EXPECT_EQ(secs_info[1].extra, "");
106  }
107 }
108 
109 #ifndef __NO_MAIN__
110 int main(int argc, char** argv) {
111  ::testing::InitGoogleTest(&argc, argv);
112  if (argc != 2) {
113  std::cerr << "Error: " << argv[0] << " requires the path to the base directory of Uptane repos.\n";
114  return EXIT_FAILURE;
115  }
116  uptane_repos_dir = argv[1];
117 
118  logger_init();
119  logger_set_threshold(boost::log::trivial::trace);
120 
121  TemporaryDirectory tmp_dir;
122  fake_meta_dir = tmp_dir.Path();
123  MetaFake meta_fake(fake_meta_dir);
124 
125  return RUN_ALL_TESTS();
126 }
127 #endif
UptaneTestCommon::TestAktualizr
Definition: uptane_test_common.h:21
Uptane::HardwareIdentifier
Definition: tuf.h:146
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:74
Uptane::EcuSerial
Definition: tuf.h:177
TemporaryDirectory
Definition: utils.h:82
MetaFake
Definition: metafake.h:14