Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
uptane_serial_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <memory>
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 #include <boost/filesystem.hpp>
9 #include <boost/program_options.hpp>
10 
11 #include "httpfake.h"
12 #include "logging/logging.h"
13 #include "primary/reportqueue.h"
14 #include "primary/sotauptaneclient.h"
15 #include "storage/invstorage.h"
16 #include "test_utils.h"
17 #include "uptane/tuf.h"
18 #include "uptane/uptanerepository.h"
19 #include "uptane_test_common.h"
20 #include "utilities/utils.h"
21 
22 namespace bpo = boost::program_options;
23 
24 boost::filesystem::path build_dir;
25 
26 /**
27  * Check that aktualizr generates random ecu_serial for primary and all
28  * secondaries.
29  */
30 TEST(Uptane, RandomSerial) {
31  RecordProperty("zephyr_key", "OTA-989,TST-155");
32  // Make two configs, neither of which specify a primary serial.
33  TemporaryDirectory temp_dir1, temp_dir2;
34  Config conf_1("tests/config/basic.toml");
35  conf_1.storage.path = temp_dir1.Path();
36  Config conf_2("tests/config/basic.toml");
37  conf_2.storage.path = temp_dir2.Path();
38 
39  conf_1.provision.primary_ecu_serial = "";
40  conf_2.provision.primary_ecu_serial = "";
41 
42  UptaneTestCommon::addDefaultSecondary(conf_1, temp_dir1, "", "secondary_hardware", false);
43  UptaneTestCommon::addDefaultSecondary(conf_2, temp_dir2, "", "secondary_hardware", false);
44 
45  // Initialize.
46  auto storage_1 = INvStorage::newStorage(conf_1.storage);
47  auto storage_2 = INvStorage::newStorage(conf_2.storage);
48  auto http1 = std::make_shared<HttpFake>(temp_dir1.Path());
49  auto http2 = std::make_shared<HttpFake>(temp_dir2.Path());
50 
51  auto uptane_client1 = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf_1, storage_1, http1);
52  ASSERT_NO_THROW(uptane_client1->initialize());
53 
54  auto uptane_client2 = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf_2, storage_2, http2);
55  ASSERT_NO_THROW(uptane_client2->initialize());
56 
57  // Verify that none of the serials match.
58  EcuSerials ecu_serials_1;
59  EcuSerials ecu_serials_2;
60  ASSERT_TRUE(storage_1->loadEcuSerials(&ecu_serials_1));
61  ASSERT_TRUE(storage_2->loadEcuSerials(&ecu_serials_2));
62  ASSERT_EQ(ecu_serials_1.size(), 2);
63  ASSERT_EQ(ecu_serials_2.size(), 2);
64  EXPECT_FALSE(ecu_serials_1[0].first.ToString().empty());
65  EXPECT_FALSE(ecu_serials_1[1].first.ToString().empty());
66  EXPECT_FALSE(ecu_serials_2[0].first.ToString().empty());
67  EXPECT_FALSE(ecu_serials_2[1].first.ToString().empty());
68  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_2[0].first);
69  EXPECT_NE(ecu_serials_1[1].first, ecu_serials_2[1].first);
70  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_1[1].first);
71  EXPECT_NE(ecu_serials_2[0].first, ecu_serials_2[1].first);
72 }
73 
74 /**
75  * Check that aktualizr saves random ecu_serial for primary and all secondaries.
76  *
77  * Test with a virtual secondary.
78  */
79 TEST(Uptane, ReloadSerial) {
80  RecordProperty("zephyr_key", "OTA-989,TST-156");
81  TemporaryDirectory temp_dir;
82  EcuSerials ecu_serials_1;
83  EcuSerials ecu_serials_2;
84 
85  // Initialize. Should store new serials.
86  {
87  Config conf("tests/config/basic.toml");
88  conf.storage.path = temp_dir.Path();
89  conf.provision.primary_ecu_serial = "";
90 
91  auto storage = INvStorage::newStorage(conf.storage);
92  auto http = std::make_shared<HttpFake>(temp_dir.Path());
93 
94  UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "", "secondary_hardware", false);
95  auto uptane_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf, storage, http);
96 
97  ASSERT_NO_THROW(uptane_client->initialize());
98  ASSERT_TRUE(storage->loadEcuSerials(&ecu_serials_1));
99  ASSERT_EQ(ecu_serials_1.size(), 2);
100  EXPECT_FALSE(ecu_serials_1[0].first.ToString().empty());
101  EXPECT_FALSE(ecu_serials_1[1].first.ToString().empty());
102  }
103 
104  // Keep storage directory, but initialize new objects. Should load existing
105  // serials.
106  {
107  Config conf("tests/config/basic.toml");
108  conf.storage.path = temp_dir.Path();
109  conf.provision.primary_ecu_serial = "";
110 
111  auto storage = INvStorage::newStorage(conf.storage);
112  auto http = std::make_shared<HttpFake>(temp_dir.Path());
113  UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "", "secondary_hardware", false);
114  auto uptane_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf, storage, http);
115 
116  ASSERT_NO_THROW(uptane_client->initialize());
117  ASSERT_TRUE(storage->loadEcuSerials(&ecu_serials_2));
118  ASSERT_EQ(ecu_serials_2.size(), 2);
119  EXPECT_FALSE(ecu_serials_2[0].first.ToString().empty());
120  EXPECT_FALSE(ecu_serials_2[1].first.ToString().empty());
121  }
122 
123  // Verify that serials match across initializations.
124  EXPECT_EQ(ecu_serials_1[0].first, ecu_serials_2[0].first);
125  EXPECT_EQ(ecu_serials_1[1].first, ecu_serials_2[1].first);
126  // Sanity check that primary and secondary serials do not match.
127  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_1[1].first);
128  EXPECT_NE(ecu_serials_2[0].first, ecu_serials_2[1].first);
129 }
130 
131 #ifndef __NO_MAIN__
132 int main(int argc, char** argv) {
133  ::testing::InitGoogleTest(&argc, argv);
134  logger_set_threshold(boost::log::trivial::trace);
135 
136  if (argc != 2) {
137  std::cerr << "Error: " << argv[0] << " requires the path to the build directory as an input argument.\n";
138  return EXIT_FAILURE;
139  }
140  build_dir = argv[1];
141  return RUN_ALL_TESTS();
142 }
143 #endif
Config
Configuration object for an aktualizr instance running on a primary ECU.
Definition: config.h:74
TemporaryDirectory
Definition: utils.h:82
Uptane
Base data types that are used in The Update Framework (TUF), part of UPTANE.
Definition: secondary_tcp_server.h:8