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);
155 RecordProperty(
"zephyr_key",
"OTA-985,TST-145");
159 Config conf(
"tests/config/basic.toml");
160 conf.storage.path = temp_dir.Path();
161 conf.provision.primary_ecu_serial =
"testecuserial";
162 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
163 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
165 std::string test_name1, test_name2;
167 auto storage = INvStorage::newStorage(conf.storage);
168 auto http = std::make_shared<HttpFake>(temp_dir.Path());
169 KeyManager keys(storage, conf.keymanagerConfig());
170 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
172 EXPECT_TRUE(storage->loadDeviceId(&test_name1));
173 EXPECT_NE(test_name1,
"");
180 conf.storage.path = temp_dir2.Path();
181 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir2.Path() /
"cred.zip");
182 conf.provision.device_id =
"";
184 auto storage = INvStorage::newStorage(conf.storage);
185 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
186 KeyManager keys(storage, conf.keymanagerConfig());
187 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
189 EXPECT_TRUE(storage->loadDeviceId(&test_name2));
190 EXPECT_NE(test_name2, test_name1);
196 conf.provision.device_id =
"";
197 auto storage = INvStorage::newStorage(conf.storage);
198 auto http = std::make_shared<HttpFake>(temp_dir2.Path());
199 KeyManager keys(storage, conf.keymanagerConfig());
200 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
203 EXPECT_TRUE(storage->loadDeviceId(&devid));
204 EXPECT_EQ(devid, test_name2);
212 conf.storage.path = temp_dir3.Path();
213 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir3.Path() /
"cred.zip");
214 conf.provision.device_id = test_name2;
216 auto storage = INvStorage::newStorage(conf.storage);
217 auto http = std::make_shared<HttpFake>(temp_dir3.Path());
218 KeyManager keys(storage, conf.keymanagerConfig());
219 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
222 EXPECT_TRUE(storage->loadDeviceId(&devid));
223 EXPECT_EQ(devid, test_name2);
227 enum class InitRetCode { kOk, kOccupied, kServerFailure, kStorageFailure, kSecondaryFailure, kBadP12, kPkcs11Failure };
233 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
234 if (url.find(
"/devices") != std::string::npos) {
235 if (retcode == InitRetCode::kOk) {
236 return HttpResponse(Utils::readFile(
"tests/test_data/cred.p12"), 200, CURLE_OK,
"");
237 }
else if (retcode == InitRetCode::kOccupied) {
238 Json::Value response;
239 response[
"code"] =
"device_already_registered";
240 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
245 return HttpFake::post(url, data);
248 InitRetCode retcode{InitRetCode::kOk};
254 auto http = std::make_shared<HttpFakeDeviceRegistration>(temp_dir.Path());
255 Config conf(
"tests/config/basic.toml");
256 conf.uptane.director_server = http->tls_server +
"/director";
257 conf.uptane.repo_server = http->tls_server +
"/repo";
258 conf.tls.server = http->tls_server;
259 conf.storage.path = temp_dir.Path();
260 conf.provision.primary_ecu_serial =
"testecuserial";
262 auto storage = INvStorage::newStorage(conf.storage);
263 KeyManager keys(storage, conf.keymanagerConfig());
267 http->retcode = InitRetCode::kOccupied;
273 http->retcode = InitRetCode::kServerFailure;
279 http->retcode = InitRetCode::kOk;
280 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
288 HttpResponse post(
const std::string& url,
const Json::Value&
data)
override {
289 if (url.find(
"/director/ecus") != std::string::npos) {
290 if (retcode == InitRetCode::kOk) {
292 }
else if (retcode == InitRetCode::kOccupied) {
293 Json::Value response;
294 response[
"code"] =
"ecu_already_registered";
295 return HttpResponse(Utils::jsonToStr(response), 400, CURLE_OK,
"");
300 return HttpFake::post(url, data);
303 InitRetCode retcode{InitRetCode::kOk};
309 auto http = std::make_shared<HttpFakeEcuRegistration>(temp_dir.Path());
310 Config conf(
"tests/config/basic.toml");
311 conf.uptane.director_server = http->tls_server +
"/director";
312 conf.uptane.repo_server = http->tls_server +
"/repo";
313 conf.tls.server = http->tls_server;
314 conf.storage.path = temp_dir.Path();
315 conf.provision.primary_ecu_serial =
"testecuserial";
317 auto storage = INvStorage::newStorage(conf.storage);
318 KeyManager keys(storage, conf.keymanagerConfig());
322 http->retcode = InitRetCode::kOccupied;
328 http->retcode = InitRetCode::kServerFailure;
334 http->retcode = InitRetCode::kOk;
335 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
349 Config conf(
"tests/config/basic.toml");
350 conf.storage.path = temp_dir.Path();
352 boost::filesystem::copy_file(
"tests/test_data/cred.zip", temp_dir.Path() /
"cred.zip");
353 conf.provision.provision_path = temp_dir.Path() /
"cred.zip";
356 auto storage = INvStorage::newStorage(conf.storage);
357 auto http = std::make_shared<HttpFake>(temp_dir.Path());
358 KeyManager keys(storage, conf.keymanagerConfig());
360 EXPECT_TRUE(conf.provision.primary_ecu_hardware_id.empty());
361 EXPECT_NO_THROW(
Initializer(conf.provision, storage, http, keys, {}));
363 EcuSerials ecu_serials;
364 EXPECT_TRUE(storage->loadEcuSerials(&ecu_serials));
365 EXPECT_GE(ecu_serials.size(), 1);
372 auto primaryHardwareID = ecu_serials[0].second;
373 auto hostname = Utils::getHostname();
379 int main(
int argc,
char** argv) {
380 ::testing::InitGoogleTest(&argc, argv);
382 logger_set_threshold(boost::log::trivial::trace);
383 return RUN_ALL_TESTS();
Configuration object for an aktualizr instance running on a Primary ECU.