Aktualizr
C++ SOTA Client
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 /**
25  * Check that aktualizr generates random ecu_serial for Primary and all
26  * Secondaries.
27  */
28 TEST(Uptane, RandomSerial) {
29  RecordProperty("zephyr_key", "OTA-989,TST-155");
30  // Make two configs, neither of which specify a Primary serial.
31  TemporaryDirectory temp_dir1, temp_dir2;
32  Config conf_1("tests/config/basic.toml");
33  conf_1.storage.path = temp_dir1.Path();
34  Config conf_2("tests/config/basic.toml");
35  conf_2.storage.path = temp_dir2.Path();
36 
37  conf_1.provision.primary_ecu_serial = "";
38  conf_2.provision.primary_ecu_serial = "";
39 
40  UptaneTestCommon::addDefaultSecondary(conf_1, temp_dir1, "", "secondary_hardware", false);
41  UptaneTestCommon::addDefaultSecondary(conf_2, temp_dir2, "", "secondary_hardware", false);
42 
43  // Initialize.
44  auto storage_1 = INvStorage::newStorage(conf_1.storage);
45  auto storage_2 = INvStorage::newStorage(conf_2.storage);
46  auto http1 = std::make_shared<HttpFake>(temp_dir1.Path());
47  auto http2 = std::make_shared<HttpFake>(temp_dir2.Path());
48 
49  auto uptane_client1 = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf_1, storage_1, http1);
50  ASSERT_NO_THROW(uptane_client1->initialize());
51 
52  auto uptane_client2 = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf_2, storage_2, http2);
53  ASSERT_NO_THROW(uptane_client2->initialize());
54 
55  // Verify that none of the serials match.
56  EcuSerials ecu_serials_1;
57  EcuSerials ecu_serials_2;
58  ASSERT_TRUE(storage_1->loadEcuSerials(&ecu_serials_1));
59  ASSERT_TRUE(storage_2->loadEcuSerials(&ecu_serials_2));
60  ASSERT_EQ(ecu_serials_1.size(), 2);
61  ASSERT_EQ(ecu_serials_2.size(), 2);
62  EXPECT_FALSE(ecu_serials_1[0].first.ToString().empty());
63  EXPECT_FALSE(ecu_serials_1[1].first.ToString().empty());
64  EXPECT_FALSE(ecu_serials_2[0].first.ToString().empty());
65  EXPECT_FALSE(ecu_serials_2[1].first.ToString().empty());
66  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_2[0].first);
67  EXPECT_NE(ecu_serials_1[1].first, ecu_serials_2[1].first);
68  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_1[1].first);
69  EXPECT_NE(ecu_serials_2[0].first, ecu_serials_2[1].first);
70 }
71 
72 /**
73  * Check that aktualizr saves random ecu_serial for Primary and all Secondaries.
74  *
75  * Test with a virtual Secondary.
76  */
77 TEST(Uptane, ReloadSerial) {
78  RecordProperty("zephyr_key", "OTA-989,TST-156");
79  TemporaryDirectory temp_dir;
80  EcuSerials ecu_serials_1;
81  EcuSerials ecu_serials_2;
82 
83  Config conf("tests/config/basic.toml");
84  conf.storage.path = temp_dir.Path();
85  conf.provision.primary_ecu_serial = "";
86  UptaneTestCommon::addDefaultSecondary(conf, temp_dir, "", "secondary_hardware", false);
87 
88  // Initialize. Should store new serials.
89  {
90  auto storage = INvStorage::newStorage(conf.storage);
91  auto http = std::make_shared<HttpFake>(temp_dir.Path());
92  auto uptane_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf, storage, http);
93 
94  ASSERT_NO_THROW(uptane_client->initialize());
95  ASSERT_TRUE(storage->loadEcuSerials(&ecu_serials_1));
96  ASSERT_EQ(ecu_serials_1.size(), 2);
97  EXPECT_FALSE(ecu_serials_1[0].first.ToString().empty());
98  EXPECT_FALSE(ecu_serials_1[1].first.ToString().empty());
99  }
100 
101  // Keep storage directory, but initialize new objects. Should load existing
102  // serials.
103  {
104  auto storage = INvStorage::newStorage(conf.storage);
105  auto http = std::make_shared<HttpFake>(temp_dir.Path());
106  auto uptane_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(conf, storage, http);
107 
108  ASSERT_NO_THROW(uptane_client->initialize());
109  ASSERT_TRUE(storage->loadEcuSerials(&ecu_serials_2));
110  ASSERT_EQ(ecu_serials_2.size(), 2);
111  EXPECT_FALSE(ecu_serials_2[0].first.ToString().empty());
112  EXPECT_FALSE(ecu_serials_2[1].first.ToString().empty());
113  }
114 
115  // Verify that serials match across initializations.
116  EXPECT_EQ(ecu_serials_1[0].first, ecu_serials_2[0].first);
117  EXPECT_EQ(ecu_serials_1[1].first, ecu_serials_2[1].first);
118  // Sanity check that Primary and Secondary serials do not match.
119  EXPECT_NE(ecu_serials_1[0].first, ecu_serials_1[1].first);
120  EXPECT_NE(ecu_serials_2[0].first, ecu_serials_2[1].first);
121 }
122 
123 #ifndef __NO_MAIN__
124 int main(int argc, char** argv) {
125  ::testing::InitGoogleTest(&argc, argv);
126  logger_set_threshold(boost::log::trivial::trace);
127 
128  return RUN_ALL_TESTS();
129 }
130 #endif
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
TemporaryDirectory
Definition: utils.h:82
Uptane
Base data types that are used in The Update Framework (TUF), part of Uptane.
Definition: packagemanagerinterface.h:18