Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
device_cred_prov_test.cc
Go to the documentation of this file.
1 /**
2  * \file
3  */
4 
5 #include <gtest/gtest.h>
6 
7 #include <boost/filesystem.hpp>
8 #include <boost/smart_ptr/make_shared.hpp>
9 
10 #include "httpfake.h"
11 #include "logging/logging.h"
12 #include "primary/initializer.h"
13 #include "primary/sotauptaneclient.h"
14 #include "storage/invstorage.h"
15 #include "uptane/uptanerepository.h"
16 #include "utilities/utils.h"
17 
18 /**
19  * Verify that when provisioning with device credentials, aktualizr halts if
20  * credentials are not available.
21  */
22 TEST(DeviceCredProv, Failure) {
23  RecordProperty("zephyr_key", "OTA-1209,TST-185");
24  TemporaryDirectory temp_dir;
25  Config config;
26  // Set device_id to prevent trying to read it from the certificate.
27  config.provision.device_id = "device_id";
28  config.storage.path = temp_dir.Path();
29  EXPECT_EQ(config.provision.mode, ProvisionMode::kDeviceCred);
30 
31  auto storage = INvStorage::newStorage(config.storage);
32  auto http = std::make_shared<HttpFake>(temp_dir.Path());
33  KeyManager keys(storage, config.keymanagerConfig());
34 
35  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
36 }
37 
38 /**
39  * Verfiy that aktualizr halts when provided incomplete device provisioning
40  * credentials.
41  */
42 TEST(DeviceCredProv, Incomplete) {
43  RecordProperty("zephyr_key", "OTA-1209,TST-187");
44  TemporaryDirectory temp_dir;
45  Config config;
46  // Set device_id to prevent trying to read it from the certificate.
47  config.provision.device_id = "device_id";
48  config.storage.path = temp_dir.Path();
49  config.import.base_path = temp_dir / "import";
50  EXPECT_EQ(config.provision.mode, ProvisionMode::kDeviceCred);
51 
52  auto http = std::make_shared<HttpFake>(temp_dir.Path());
53 
54  {
55  config.import.tls_cacert_path = BasedPath("ca.pem");
56  config.import.tls_clientcert_path = BasedPath("");
57  config.import.tls_pkey_path = BasedPath("");
58  Utils::createDirectories(temp_dir / "import", S_IRWXU);
59  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
60  auto storage = INvStorage::newStorage(config.storage);
61  storage->importData(config.import);
62  KeyManager keys(storage, config.keymanagerConfig());
63 
64  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
65  }
66 
67  {
68  config.import.tls_cacert_path = BasedPath("");
69  config.import.tls_clientcert_path = BasedPath("client.pem");
70  config.import.tls_pkey_path = BasedPath("");
71  boost::filesystem::remove_all(temp_dir.Path());
72  Utils::createDirectories(temp_dir / "import", S_IRWXU);
73  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
74  auto storage = INvStorage::newStorage(config.storage);
75  storage->importData(config.import);
76  KeyManager keys(storage, config.keymanagerConfig());
77 
78  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
79  }
80 
81  {
82  config.import.tls_cacert_path = BasedPath("");
83  config.import.tls_clientcert_path = BasedPath("");
84  config.import.tls_pkey_path = BasedPath("pkey.pem");
85  boost::filesystem::remove_all(temp_dir.Path());
86  Utils::createDirectories(temp_dir / "import", S_IRWXU);
87  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
88  auto storage = INvStorage::newStorage(config.storage);
89  storage->importData(config.import);
90  KeyManager keys(storage, config.keymanagerConfig());
91 
92  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
93  }
94 
95  {
96  config.import.tls_cacert_path = BasedPath("ca.pem");
97  config.import.tls_clientcert_path = BasedPath("client.pem");
98  config.import.tls_pkey_path = BasedPath("");
99  boost::filesystem::remove_all(temp_dir.Path());
100  Utils::createDirectories(temp_dir / "import", S_IRWXU);
101  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
102  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
103  auto storage = INvStorage::newStorage(config.storage);
104  storage->importData(config.import);
105  KeyManager keys(storage, config.keymanagerConfig());
106 
107  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
108  }
109 
110  {
111  config.import.tls_cacert_path = BasedPath("ca.pem");
112  config.import.tls_clientcert_path = BasedPath("");
113  config.import.tls_pkey_path = BasedPath("pkey.pem");
114  boost::filesystem::remove_all(temp_dir.Path());
115  Utils::createDirectories(temp_dir / "import", S_IRWXU);
116  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
117  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
118  auto storage = INvStorage::newStorage(config.storage);
119  storage->importData(config.import);
120  KeyManager keys(storage, config.keymanagerConfig());
121 
122  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
123  }
124 
125  {
126  config.import.tls_cacert_path = BasedPath("");
127  config.import.tls_clientcert_path = BasedPath("client.pem");
128  config.import.tls_pkey_path = BasedPath("pkey.pem");
129  boost::filesystem::remove_all(temp_dir.Path());
130  Utils::createDirectories(temp_dir / "import", S_IRWXU);
131  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
132  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
133  auto storage = INvStorage::newStorage(config.storage);
134  storage->importData(config.import);
135  KeyManager keys(storage, config.keymanagerConfig());
136 
137  EXPECT_THROW(Initializer(config.provision, storage, http, keys, {}), Initializer::Error);
138  }
139 
140  // Do one last round with all three files to make sure it actually works as
141  // expected.
142  config.import.tls_cacert_path = BasedPath("ca.pem");
143  config.import.tls_clientcert_path = BasedPath("client.pem");
144  config.import.tls_pkey_path = BasedPath("pkey.pem");
145  boost::filesystem::remove_all(temp_dir.Path());
146  Utils::createDirectories(temp_dir / "import", S_IRWXU);
147  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
148  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
149  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
150  auto storage = INvStorage::newStorage(config.storage);
151  storage->importData(config.import);
152  KeyManager keys(storage, config.keymanagerConfig());
153 
154  EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
155 }
156 
157 /**
158  * Verify that aktualizr can provision with provided device credentials.
159  */
160 TEST(DeviceCredProv, Success) {
161  RecordProperty("zephyr_key", "OTA-996,OTA-1210,TST-186");
162  TemporaryDirectory temp_dir;
163  Config config;
164  Utils::createDirectories(temp_dir / "import", S_IRWXU);
165  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
166  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
167  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
168  config.storage.path = temp_dir.Path();
169  config.import.base_path = temp_dir / "import";
170  config.import.tls_cacert_path = BasedPath("ca.pem");
171  config.import.tls_clientcert_path = BasedPath("client.pem");
172  config.import.tls_pkey_path = BasedPath("pkey.pem");
173  EXPECT_EQ(config.provision.mode, ProvisionMode::kDeviceCred);
174 
175  auto storage = INvStorage::newStorage(config.storage);
176  storage->importData(config.import);
177  auto http = std::make_shared<HttpFake>(temp_dir.Path());
178  KeyManager keys(storage, config.keymanagerConfig());
179 
180  EXPECT_NO_THROW(Initializer(config.provision, storage, http, keys, {}));
181 }
182 
183 #ifndef __NO_MAIN__
184 int main(int argc, char** argv) {
185  ::testing::InitGoogleTest(&argc, argv);
186  logger_set_threshold(boost::log::trivial::trace);
187  return RUN_ALL_TESTS();
188 }
189 #endif
KeyManager
Definition: keymanager.h:13
BasedPath
Definition: utils.h:101
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:74
TEST
TEST(DeviceCredProv, Failure)
Verify that when provisioning with device credentials, aktualizr halts if credentials are not availab...
Definition: device_cred_prov_test.cc:22
TemporaryDirectory
Definition: utils.h:82
Initializer
Definition: initializer.h:11
Initializer::Error
Definition: initializer.h:17