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  Initializer initializer(config.provision, storage, http, keys, {});
36  EXPECT_FALSE(initializer.isSuccessful());
37 }
38 
39 /**
40  * Verfiy that aktualizr halts when provided incomplete device provisioning
41  * credentials.
42  */
43 TEST(DeviceCredProv, Incomplete) {
44  RecordProperty("zephyr_key", "OTA-1209,TST-187");
45  TemporaryDirectory temp_dir;
46  Config config;
47  // Set device_id to prevent trying to read it from the certificate.
48  config.provision.device_id = "device_id";
49  config.storage.path = temp_dir.Path();
50  config.import.base_path = temp_dir / "import";
51  EXPECT_EQ(config.provision.mode, ProvisionMode::kDeviceCred);
52 
53  auto http = std::make_shared<HttpFake>(temp_dir.Path());
54 
55  {
56  config.import.tls_cacert_path = BasedPath("ca.pem");
57  config.import.tls_clientcert_path = BasedPath("");
58  config.import.tls_pkey_path = BasedPath("");
59  Utils::createDirectories(temp_dir / "import", S_IRWXU);
60  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
61  auto storage = INvStorage::newStorage(config.storage);
62  storage->importData(config.import);
63  KeyManager keys(storage, config.keymanagerConfig());
64 
65  Initializer initializer(config.provision, storage, http, keys, {});
66  EXPECT_FALSE(initializer.isSuccessful());
67  }
68 
69  {
70  config.import.tls_cacert_path = BasedPath("");
71  config.import.tls_clientcert_path = BasedPath("client.pem");
72  config.import.tls_pkey_path = BasedPath("");
73  boost::filesystem::remove_all(temp_dir.Path());
74  Utils::createDirectories(temp_dir / "import", S_IRWXU);
75  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
76  auto storage = INvStorage::newStorage(config.storage);
77  storage->importData(config.import);
78  KeyManager keys(storage, config.keymanagerConfig());
79 
80  Initializer initializer(config.provision, storage, http, keys, {});
81  EXPECT_FALSE(initializer.isSuccessful());
82  }
83 
84  {
85  config.import.tls_cacert_path = BasedPath("");
86  config.import.tls_clientcert_path = BasedPath("");
87  config.import.tls_pkey_path = BasedPath("pkey.pem");
88  boost::filesystem::remove_all(temp_dir.Path());
89  Utils::createDirectories(temp_dir / "import", S_IRWXU);
90  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
91  auto storage = INvStorage::newStorage(config.storage);
92  storage->importData(config.import);
93  KeyManager keys(storage, config.keymanagerConfig());
94 
95  Initializer initializer(config.provision, storage, http, keys, {});
96  EXPECT_FALSE(initializer.isSuccessful());
97  }
98 
99  {
100  config.import.tls_cacert_path = BasedPath("ca.pem");
101  config.import.tls_clientcert_path = BasedPath("client.pem");
102  config.import.tls_pkey_path = BasedPath("");
103  boost::filesystem::remove_all(temp_dir.Path());
104  Utils::createDirectories(temp_dir / "import", S_IRWXU);
105  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
106  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
107  auto storage = INvStorage::newStorage(config.storage);
108  storage->importData(config.import);
109  KeyManager keys(storage, config.keymanagerConfig());
110 
111  Initializer initializer(config.provision, storage, http, keys, {});
112  EXPECT_FALSE(initializer.isSuccessful());
113  }
114 
115  {
116  config.import.tls_cacert_path = BasedPath("ca.pem");
117  config.import.tls_clientcert_path = BasedPath("");
118  config.import.tls_pkey_path = BasedPath("pkey.pem");
119  boost::filesystem::remove_all(temp_dir.Path());
120  Utils::createDirectories(temp_dir / "import", S_IRWXU);
121  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
122  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
123  auto storage = INvStorage::newStorage(config.storage);
124  storage->importData(config.import);
125  KeyManager keys(storage, config.keymanagerConfig());
126 
127  Initializer initializer(config.provision, storage, http, keys, {});
128  EXPECT_FALSE(initializer.isSuccessful());
129  }
130 
131  {
132  config.import.tls_cacert_path = BasedPath("");
133  config.import.tls_clientcert_path = BasedPath("client.pem");
134  config.import.tls_pkey_path = BasedPath("pkey.pem");
135  boost::filesystem::remove_all(temp_dir.Path());
136  Utils::createDirectories(temp_dir / "import", S_IRWXU);
137  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
138  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
139  auto storage = INvStorage::newStorage(config.storage);
140  storage->importData(config.import);
141  KeyManager keys(storage, config.keymanagerConfig());
142 
143  Initializer initializer(config.provision, storage, http, keys, {});
144  EXPECT_FALSE(initializer.isSuccessful());
145  }
146 
147  // Do one last round with all three files to make sure it actually works as
148  // expected.
149  config.import.tls_cacert_path = BasedPath("ca.pem");
150  config.import.tls_clientcert_path = BasedPath("client.pem");
151  config.import.tls_pkey_path = BasedPath("pkey.pem");
152  boost::filesystem::remove_all(temp_dir.Path());
153  Utils::createDirectories(temp_dir / "import", S_IRWXU);
154  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
155  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
156  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
157  auto storage = INvStorage::newStorage(config.storage);
158  storage->importData(config.import);
159  KeyManager keys(storage, config.keymanagerConfig());
160 
161  Initializer initializer(config.provision, storage, http, keys, {});
162  EXPECT_TRUE(initializer.isSuccessful());
163 }
164 
165 /**
166  * Verify that aktualizr can provision with provided device credentials.
167  */
168 TEST(DeviceCredProv, Success) {
169  RecordProperty("zephyr_key", "OTA-996,OTA-1210,TST-186");
170  TemporaryDirectory temp_dir;
171  Config config;
172  Utils::createDirectories(temp_dir / "import", S_IRWXU);
173  boost::filesystem::copy_file("tests/test_data/device_cred_prov/ca.pem", temp_dir / "import/ca.pem");
174  boost::filesystem::copy_file("tests/test_data/device_cred_prov/client.pem", temp_dir / "import/client.pem");
175  boost::filesystem::copy_file("tests/test_data/device_cred_prov/pkey.pem", temp_dir / "import/pkey.pem");
176  config.storage.path = temp_dir.Path();
177  config.import.base_path = temp_dir / "import";
178  config.import.tls_cacert_path = BasedPath("ca.pem");
179  config.import.tls_clientcert_path = BasedPath("client.pem");
180  config.import.tls_pkey_path = BasedPath("pkey.pem");
181  EXPECT_EQ(config.provision.mode, ProvisionMode::kDeviceCred);
182 
183  auto storage = INvStorage::newStorage(config.storage);
184  storage->importData(config.import);
185  auto http = std::make_shared<HttpFake>(temp_dir.Path());
186  KeyManager keys(storage, config.keymanagerConfig());
187 
188  Initializer initializer(config.provision, storage, http, keys, {});
189  EXPECT_TRUE(initializer.isSuccessful());
190 }
191 
192 #ifndef __NO_MAIN__
193 int main(int argc, char** argv) {
194  ::testing::InitGoogleTest(&argc, argv);
195  logger_set_threshold(boost::log::trivial::trace);
196  return RUN_ALL_TESTS();
197 }
198 #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:13