1 #include <gtest/gtest.h>
5 #include <boost/filesystem.hpp>
8 #include "primary/initializer.h"
9 #include "primary/sotauptaneclient.h"
10 #include "storage/invstorage.h"
11 #include "utilities/utils.h"
17 RecordProperty(
"zephyr_key",
"OTA-983,TST-153");
19 auto http = std::make_shared<HttpFake>(temp_dir.Path());
20 Config conf(
"tests/config/basic.toml");
21 conf.uptane.director_server = http->tls_server +
"/director";
22 conf.uptane.repo_server = http->tls_server +
"/repo";
23 conf.tls.server = http->tls_server;
24 conf.storage.path = temp_dir.Path();
25 conf.provision.primary_ecu_serial =
"testecuserial";
28 auto storage = INvStorage::newStorage(conf.storage);
32 EXPECT_FALSE(storage->loadTlsCreds(&ca, &cert, &pkey));
33 std::string public_key;
34 std::string private_key;
35 EXPECT_FALSE(storage->loadPrimaryKeys(&public_key, &private_key));
38 KeyManager keys(storage, conf.keymanagerConfig());
39 Initializer initializer(conf.provision, storage, http, keys, {});
40 EXPECT_TRUE(initializer.isSuccessful());
43 EXPECT_TRUE(storage->loadTlsCreds(&ca, &cert, &pkey));
47 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key, &private_key));
48 EXPECT_NE(public_key,
"");
49 EXPECT_NE(private_key,
"");
51 const Json::Value ecu_data = Utils::parseJSONFile(temp_dir.Path() /
"post.json");
52 EXPECT_EQ(ecu_data[
"ecus"].size(), 1);
53 EXPECT_EQ(ecu_data[
"ecus"][0][
"clientKey"][
"keyval"][
"public"].asString(), public_key);
54 EXPECT_EQ(ecu_data[
"ecus"][0][
"ecu_serial"].asString(), conf.provision.primary_ecu_serial);
55 EXPECT_NE(ecu_data[
"ecus"][0][
"hardware_identifier"].asString(),
"");
56 EXPECT_EQ(ecu_data[
"primary_ecu_serial"].asString(), conf.provision.primary_ecu_serial);
63 TEST(
Uptane, InitializeTwice) {
64 RecordProperty(
"zephyr_key",
"OTA-983,TST-154");
66 auto http = std::make_shared<HttpFake>(temp_dir.Path());
67 Config conf(
"tests/config/basic.toml");
68 conf.storage.path = temp_dir.Path();
69 conf.provision.primary_ecu_serial =
"testecuserial";
72 auto storage = INvStorage::newStorage(conf.storage);
76 EXPECT_FALSE(storage->loadTlsCreds(&ca1, &cert1, &pkey1));
77 std::string public_key1;
78 std::string private_key1;
79 EXPECT_FALSE(storage->loadPrimaryKeys(&public_key1, &private_key1));
83 KeyManager keys(storage, conf.keymanagerConfig());
84 Initializer initializer(conf.provision, storage, http, keys, {});
85 EXPECT_TRUE(initializer.isSuccessful());
87 EXPECT_TRUE(storage->loadTlsCreds(&ca1, &cert1, &pkey1));
91 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key1, &private_key1));
92 EXPECT_NE(public_key1,
"");
93 EXPECT_NE(private_key1,
"");
98 KeyManager keys(storage, conf.keymanagerConfig());
99 Initializer initializer(conf.provision, storage, http, keys, {});
100 EXPECT_TRUE(initializer.isSuccessful());
105 EXPECT_TRUE(storage->loadTlsCreds(&ca2, &cert2, &pkey2));
106 std::string public_key2;
107 std::string private_key2;
108 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key2, &private_key2));
110 EXPECT_EQ(cert1, cert2);
112 EXPECT_EQ(pkey1, pkey2);
113 EXPECT_EQ(public_key1, public_key2);
114 EXPECT_EQ(private_key1, private_key2);
122 TEST(
Uptane, PetNameProvided) {
123 RecordProperty(
"zephyr_key",
"OTA-985,TST-146");
125 const std::string test_name =
"test-name-123";
128 Config conf(
"tests/config/device_id.toml");
129 conf.storage.path = temp_dir.Path();
130 conf.provision.primary_ecu_serial =
"testecuserial";
132 auto storage = INvStorage::newStorage(conf.storage);
133 auto http = std::make_shared<HttpFake>(temp_dir.Path());
134 KeyManager keys(storage, conf.keymanagerConfig());
135 Initializer initializer(conf.provision, storage, http, keys, {});
136 EXPECT_TRUE(initializer.isSuccessful());
139 EXPECT_EQ(conf.provision.device_id, test_name);
141 EXPECT_TRUE(storage->loadDeviceId(&devid));
142 EXPECT_EQ(devid, test_name);
147 conf.postUpdateValues();
148 EXPECT_EQ(conf.provision.device_id, test_name);
150 EXPECT_TRUE(storage->loadDeviceId(&devid));
151 EXPECT_EQ(devid, test_name);
158 TEST(
Uptane, PetNameCreation) {
159 RecordProperty(
"zephyr_key",
"OTA-985,TST-145");
163 Config conf(
"tests/config/basic.toml");
164 conf.storage.path = temp_dir.Path();
165 conf.provision.primary_ecu_serial =
"testecuserial";
166 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
167 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
169 std::string test_name1, test_name2;
171 auto storage = INvStorage::newStorage(conf.storage);
172 auto http = std::make_shared<HttpFake>(temp_dir.Path());
173 KeyManager keys(storage, conf.keymanagerConfig());
174 Initializer initializer(conf.provision, storage, http, keys, {});
175 EXPECT_TRUE(initializer.isSuccessful());
177 EXPECT_TRUE(storage->loadDeviceId(&test_name1));
178 EXPECT_NE(test_name1,
"");
185 conf.storage.path = temp_dir2.Path();
186 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir2.Path() /
"cred.zip");
187 conf.provision.device_id =
"";
189 auto storage = INvStorage::newStorage(conf.storage);
190 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
191 KeyManager keys(storage, conf.keymanagerConfig());
192 Initializer initializer(conf.provision, storage, http, keys, {});
193 EXPECT_TRUE(initializer.isSuccessful());
195 EXPECT_TRUE(storage->loadDeviceId(&test_name2));
196 EXPECT_NE(test_name2, test_name1);
202 conf.provision.device_id =
"";
203 auto storage = INvStorage::newStorage(conf.storage);
204 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
205 KeyManager keys(storage, conf.keymanagerConfig());
206 Initializer initializer(conf.provision, storage, http, keys, {});
207 EXPECT_TRUE(initializer.isSuccessful());
210 EXPECT_TRUE(storage->loadDeviceId(&devid));
211 EXPECT_EQ(devid, test_name2);
219 conf.storage.path = temp_dir3.Path();
220 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir3.Path() /
"cred.zip");
221 conf.provision.device_id = test_name2;
223 auto storage = INvStorage::newStorage(conf.storage);
224 auto http = std::make_shared<HttpFake>(temp_dir3.Path());
225 KeyManager keys(storage, conf.keymanagerConfig());
226 Initializer initializer(conf.provision, storage, http, keys, {});
227 EXPECT_TRUE(initializer.isSuccessful());
230 EXPECT_TRUE(storage->loadDeviceId(&devid));
231 EXPECT_EQ(devid, test_name2);
236 TEST(
Uptane, InitializeFail) {
238 auto http = std::make_shared<HttpFake>(temp_dir.Path());
239 Config conf(
"tests/config/basic.toml");
240 conf.uptane.director_server = http->tls_server +
"/director";
241 conf.uptane.repo_server = http->tls_server +
"/repo";
242 conf.tls.server = http->tls_server;
243 conf.storage.path = temp_dir.Path();
244 conf.provision.primary_ecu_serial =
"testecuserial";
246 auto storage = INvStorage::newStorage(conf.storage);
247 KeyManager keys(storage, conf.keymanagerConfig());
251 http->provisioningResponse = ProvisioningResult::kFailure;
252 Initializer initializer(conf.provision, storage, http, keys, {});
253 EXPECT_FALSE(initializer.isSuccessful());
258 http->provisioningResponse = ProvisioningResult::kOK;
259 Initializer initializer(conf.provision, storage, http, keys, {});
260 EXPECT_TRUE(initializer.isSuccessful());
272 TEST(
Uptane, HostnameAsHardwareID) {
274 Config conf(
"tests/config/basic.toml");
275 conf.storage.path = temp_dir.Path();
277 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
278 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
281 auto storage = INvStorage::newStorage(conf.storage);
282 auto http = std::make_shared<HttpFake>(temp_dir.Path());
283 KeyManager keys(storage, conf.keymanagerConfig());
285 EXPECT_TRUE(conf.provision.primary_ecu_hardware_id.empty());
286 Initializer initializer(conf.provision, storage, http, keys, {});
287 EXPECT_TRUE(initializer.isSuccessful());
289 EcuSerials ecu_serials;
290 EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials));
291 EXPECT_GE(ecu_serials.size(), 1);
298 auto primaryHardwareID = ecu_serials[0].second;
299 auto hostname = Utils::getHostname();
305 int main(
int argc,
char** argv) {
306 ::testing::InitGoogleTest(&argc, argv);
308 logger_set_threshold(boost::log::trivial::trace);
309 return RUN_ALL_TESTS();