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);
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);
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);
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);
239 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
240 if (url.find(
"/devices") != std::string::npos) {
241 if (retcode == InitRetCode::kOk) {
242 return HttpResponse(Utils::readFile(
"tests/test_data/cred.p12"), 200, CURLE_OK,
"");
243 }
else if (retcode == InitRetCode::kOccupied) {
244 Json::Value response;
245 response[
"code"] =
"device_already_registered";
246 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
251 return HttpFake::post(url,
data);
254 InitRetCode retcode{InitRetCode::kOk};
260 auto http = std::make_shared<HttpFakeDeviceRegistration>(temp_dir.Path());
261 Config conf(
"tests/config/basic.toml");
262 conf.uptane.director_server = http->tls_server +
"/director";
263 conf.uptane.repo_server = http->tls_server +
"/repo";
264 conf.tls.server = http->tls_server;
265 conf.storage.path = temp_dir.Path();
266 conf.provision.primary_ecu_serial =
"testecuserial";
268 auto storage = INvStorage::newStorage(conf.storage);
269 KeyManager keys(storage, conf.keymanagerConfig());
273 http->retcode = InitRetCode::kOccupied;
274 Initializer initializer(conf.provision, storage, http, keys, {});
275 EXPECT_FALSE(initializer.isSuccessful());
280 http->retcode = InitRetCode::kServerFailure;
281 Initializer initializer(conf.provision, storage, http, keys, {});
282 EXPECT_FALSE(initializer.isSuccessful());
287 http->retcode = InitRetCode::kOk;
288 Initializer initializer(conf.provision, storage, http, keys, {});
289 EXPECT_TRUE(initializer.isSuccessful());
297 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
298 if (url.find(
"/director/ecus") != std::string::npos) {
299 if (retcode == InitRetCode::kOk) {
301 }
else if (retcode == InitRetCode::kOccupied) {
302 Json::Value response;
303 response[
"code"] =
"ecu_already_registered";
304 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
309 return HttpFake::post(url,
data);
312 InitRetCode retcode{InitRetCode::kOk};
318 auto http = std::make_shared<HttpFakeEcuRegistration>(temp_dir.Path());
319 Config conf(
"tests/config/basic.toml");
320 conf.uptane.director_server = http->tls_server +
"/director";
321 conf.uptane.repo_server = http->tls_server +
"/repo";
322 conf.tls.server = http->tls_server;
323 conf.storage.path = temp_dir.Path();
324 conf.provision.primary_ecu_serial =
"testecuserial";
326 auto storage = INvStorage::newStorage(conf.storage);
327 KeyManager keys(storage, conf.keymanagerConfig());
331 http->retcode = InitRetCode::kOccupied;
332 Initializer initializer(conf.provision, storage, http, keys, {});
333 EXPECT_FALSE(initializer.isSuccessful());
338 http->retcode = InitRetCode::kServerFailure;
339 Initializer initializer(conf.provision, storage, http, keys, {});
340 EXPECT_FALSE(initializer.isSuccessful());
345 http->retcode = InitRetCode::kOk;
346 Initializer initializer(conf.provision, storage, http, keys, {});
347 EXPECT_TRUE(initializer.isSuccessful());
361 Config conf(
"tests/config/basic.toml");
362 conf.storage.path = temp_dir.Path();
364 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
365 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
368 auto storage = INvStorage::newStorage(conf.storage);
369 auto http = std::make_shared<HttpFake>(temp_dir.Path());
370 KeyManager keys(storage, conf.keymanagerConfig());
372 EXPECT_TRUE(conf.provision.primary_ecu_hardware_id.empty());
373 Initializer initializer(conf.provision, storage, http, keys, {});
374 EXPECT_TRUE(initializer.isSuccessful());
376 EcuSerials ecu_serials;
377 EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials));
378 EXPECT_GE(ecu_serials.size(), 1);
385 auto primaryHardwareID = ecu_serials[0].second;
386 auto hostname = Utils::getHostname();
392 int main(
int argc,
char** argv) {
393 ::testing::InitGoogleTest(&argc, argv);
395 logger_set_threshold(boost::log::trivial::trace);
396 return RUN_ALL_TESTS();