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 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
42 EXPECT_TRUE(storage->loadTlsCreds(&ca, &cert, &pkey));
46 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key, &private_key));
47 EXPECT_NE(public_key,
"");
48 EXPECT_NE(private_key,
"");
50 const Json::Value ecu_data = Utils::parseJSONFile(temp_dir.Path() /
"post.json");
51 EXPECT_EQ(ecu_data[
"ecus"].size(), 1);
52 EXPECT_EQ(ecu_data[
"ecus"][0][
"clientKey"][
"keyval"][
"public"].asString(), public_key);
53 EXPECT_EQ(ecu_data[
"ecus"][0][
"ecu_serial"].asString(), conf.provision.primary_ecu_serial);
54 EXPECT_NE(ecu_data[
"ecus"][0][
"hardware_identifier"].asString(),
"");
55 EXPECT_EQ(ecu_data[
"primary_ecu_serial"].asString(), conf.provision.primary_ecu_serial);
63 RecordProperty(
"zephyr_key",
"OTA-983,TST-154");
65 auto http = std::make_shared<HttpFake>(temp_dir.Path());
66 Config conf(
"tests/config/basic.toml");
67 conf.storage.path = temp_dir.Path();
68 conf.provision.primary_ecu_serial =
"testecuserial";
71 auto storage = INvStorage::newStorage(conf.storage);
75 EXPECT_FALSE(storage->loadTlsCreds(&ca1, &cert1, &pkey1));
76 std::string public_key1;
77 std::string private_key1;
78 EXPECT_FALSE(storage->loadPrimaryKeys(&public_key1, &private_key1));
82 KeyManager keys(storage, conf.keymanagerConfig());
83 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
85 EXPECT_TRUE(storage->loadTlsCreds(&ca1, &cert1, &pkey1));
89 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key1, &private_key1));
90 EXPECT_NE(public_key1,
"");
91 EXPECT_NE(private_key1,
"");
96 KeyManager keys(storage, conf.keymanagerConfig());
97 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
102 EXPECT_TRUE(storage->loadTlsCreds(&ca2, &cert2, &pkey2));
103 std::string public_key2;
104 std::string private_key2;
105 EXPECT_TRUE(storage->loadPrimaryKeys(&public_key2, &private_key2));
107 EXPECT_EQ(cert1, cert2);
109 EXPECT_EQ(pkey1, pkey2);
110 EXPECT_EQ(public_key1, public_key2);
111 EXPECT_EQ(private_key1, private_key2);
120 RecordProperty(
"zephyr_key",
"OTA-985,TST-146");
122 const std::string test_name =
"test-name-123";
125 Config conf(
"tests/config/device_id.toml");
126 conf.storage.path = temp_dir.Path();
127 conf.provision.primary_ecu_serial =
"testecuserial";
129 auto storage = INvStorage::newStorage(conf.storage);
130 auto http = std::make_shared<HttpFake>(temp_dir.Path());
131 KeyManager keys(storage, conf.keymanagerConfig());
132 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
135 EXPECT_EQ(conf.provision.device_id, test_name);
137 EXPECT_TRUE(storage->loadDeviceId(&devid));
138 EXPECT_EQ(devid, test_name);
143 conf.postUpdateValues();
144 EXPECT_EQ(conf.provision.device_id, test_name);
146 EXPECT_TRUE(storage->loadDeviceId(&devid));
147 EXPECT_EQ(devid, test_name);
157 std::string test_name;
158 Config conf(
"tests/config/basic.toml");
162 auto http = std::make_shared<HttpFake>(temp_dir.Path());
163 conf.storage.path = temp_dir.Path();
164 auto storage = INvStorage::newStorage(conf.storage);
165 KeyManager keys(storage, conf.keymanagerConfig());
166 storage->storeTlsCert(Utils::readFile(
"tests/test_data/prov/client.pem"));
167 test_name = keys.getCN();
168 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
170 EXPECT_TRUE(storage->loadDeviceId(&devid));
171 EXPECT_EQ(devid, test_name);
178 auto http = std::make_shared<HttpFake>(temp_dir.Path());
179 conf.storage.path = temp_dir.Path();
180 auto storage = INvStorage::newStorage(conf.storage);
181 KeyManager keys(storage, conf.keymanagerConfig());
182 storage->storeTlsCert(Utils::readFile(
"tests/test_data/prov/client.pem"));
183 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
185 EXPECT_TRUE(storage->loadDeviceId(&devid));
186 EXPECT_EQ(devid, test_name);
194 RecordProperty(
"zephyr_key",
"OTA-985,TST-145");
198 Config conf(
"tests/config/basic.toml");
199 conf.storage.path = temp_dir.Path();
200 conf.provision.primary_ecu_serial =
"testecuserial";
201 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
202 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
204 std::string test_name1, test_name2;
206 auto storage = INvStorage::newStorage(conf.storage);
207 auto http = std::make_shared<HttpFake>(temp_dir.Path());
208 KeyManager keys(storage, conf.keymanagerConfig());
209 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
211 EXPECT_TRUE(storage->loadDeviceId(&test_name1));
212 EXPECT_NE(test_name1,
"");
219 conf.storage.path = temp_dir2.Path();
220 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir2.Path() /
"cred.zip");
221 conf.provision.device_id =
"";
223 auto storage = INvStorage::newStorage(conf.storage);
224 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
225 KeyManager keys(storage, conf.keymanagerConfig());
226 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
228 EXPECT_TRUE(storage->loadDeviceId(&test_name2));
229 EXPECT_NE(test_name2, test_name1);
235 conf.provision.device_id =
"";
236 auto storage = INvStorage::newStorage(conf.storage);
237 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
238 KeyManager keys(storage, conf.keymanagerConfig());
239 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
242 EXPECT_TRUE(storage->loadDeviceId(&devid));
243 EXPECT_EQ(devid, test_name2);
251 conf.storage.path = temp_dir3.Path();
252 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir3.Path() /
"cred.zip");
253 conf.provision.device_id = test_name2;
255 auto storage = INvStorage::newStorage(conf.storage);
256 auto http = std::make_shared<HttpFake>(temp_dir3.Path());
257 KeyManager keys(storage, conf.keymanagerConfig());
258 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
261 EXPECT_TRUE(storage->loadDeviceId(&devid));
262 EXPECT_EQ(devid, test_name2);
266 enum class InitRetCode { kOk, kOccupied, kServerFailure, kStorageFailure, kSecondaryFailure, kBadP12, kPkcs11Failure };
272 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
273 if (url.find(
"/devices") != std::string::npos) {
274 if (retcode == InitRetCode::kOk) {
275 return HttpResponse(Utils::readFile(
"tests/test_data/cred.p12"), 200, CURLE_OK,
"");
276 }
else if (retcode == InitRetCode::kOccupied) {
277 Json::Value response;
278 response[
"code"] =
"device_already_registered";
279 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
284 return HttpFake::post(url,
data);
287 InitRetCode retcode{InitRetCode::kOk};
293 auto http = std::make_shared<HttpFakeDeviceRegistration>(temp_dir.Path());
294 Config conf(
"tests/config/basic.toml");
295 conf.uptane.director_server = http->tls_server +
"/director";
296 conf.uptane.repo_server = http->tls_server +
"/repo";
297 conf.tls.server = http->tls_server;
298 conf.storage.path = temp_dir.Path();
299 conf.provision.primary_ecu_serial =
"testecuserial";
301 auto storage = INvStorage::newStorage(conf.storage);
302 KeyManager keys(storage, conf.keymanagerConfig());
306 http->retcode = InitRetCode::kOccupied;
312 http->retcode = InitRetCode::kServerFailure;
318 http->retcode = InitRetCode::kOk;
319 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
327 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
328 if (url.find(
"/director/ecus") != std::string::npos) {
329 if (retcode == InitRetCode::kOk) {
331 }
else if (retcode == InitRetCode::kOccupied) {
332 Json::Value response;
333 response[
"code"] =
"ecu_already_registered";
334 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
339 return HttpFake::post(url,
data);
342 InitRetCode retcode{InitRetCode::kOk};
348 auto http = std::make_shared<HttpFakeEcuRegistration>(temp_dir.Path());
349 Config conf(
"tests/config/basic.toml");
350 conf.uptane.director_server = http->tls_server +
"/director";
351 conf.uptane.repo_server = http->tls_server +
"/repo";
352 conf.tls.server = http->tls_server;
353 conf.storage.path = temp_dir.Path();
354 conf.provision.primary_ecu_serial =
"testecuserial";
356 auto storage = INvStorage::newStorage(conf.storage);
357 KeyManager keys(storage, conf.keymanagerConfig());
361 http->retcode = InitRetCode::kOccupied;
367 http->retcode = InitRetCode::kServerFailure;
373 http->retcode = InitRetCode::kOk;
374 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
381 Config conf(
"tests/config/basic.toml");
382 conf.storage.path = temp_dir.Path();
384 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
385 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
388 auto storage = INvStorage::newStorage(conf.storage);
389 auto http = std::make_shared<HttpFake>(temp_dir.Path());
390 KeyManager keys(storage, conf.keymanagerConfig());
392 EXPECT_TRUE(conf.provision.primary_ecu_hardware_id.empty());
393 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
395 EcuSerials ecu_serials;
396 EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials));
397 EXPECT_GE(ecu_serials.size(), 1);
399 auto primaryHardwareID = ecu_serials[0].second;
400 auto hostname = Utils::getHostname();
406 int main(
int argc,
char** argv) {
407 ::testing::InitGoogleTest(&argc, argv);
409 logger_set_threshold(boost::log::trivial::trace);
410 return RUN_ALL_TESTS();