1 #include <gtest/gtest.h>
5 #include <boost/filesystem.hpp>
8 #include "config/config.h"
10 #include "primary/aktualizr.h"
12 #include "uptane_test_common.h"
14 boost::filesystem::path uptane_generator_path;
16 void delegation_basic(
const boost::filesystem::path& delegation_path,
bool revoke) {
18 std::string cmd =
"tests/uptane_repo_generation/delegation_basic.sh " + uptane_generator_path.string() +
" " +
19 delegation_path.string();
23 int retval = Utils::shell(cmd, &output,
true);
24 EXPECT_EQ(retval, EXIT_SUCCESS) << output;
27 void delegation_nested(
const boost::filesystem::path& delegation_path,
bool revoke) {
29 std::string cmd =
"tests/uptane_repo_generation/delegation_nested.sh " + uptane_generator_path.string() +
" " +
30 delegation_path.string();
34 int retval = Utils::shell(cmd, &output,
true);
35 EXPECT_EQ(retval, EXIT_SUCCESS) << output;
41 :
HttpFake(test_dir_in,
"", test_dir_in /
"delegation_test/repo") {}
43 HttpResponse handle_event(
const std::string& url,
const Json::Value&
data)
override {
47 EXPECT_EQ(
event[
"event"][
"correlationId"].asString(),
"");
52 unsigned int events_seen{0};
59 for (
auto generate_fun : {delegation_basic, delegation_nested}) {
61 auto delegation_path = temp_dir.Path() /
"delegation_test";
62 generate_fun(delegation_path,
false);
63 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
68 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
70 auto storage = INvStorage::newStorage(conf.storage);
73 aktualizr.Initialize();
75 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
77 result::Download download_result = aktualizr.Download(update_result.updates).get();
78 EXPECT_EQ(download_result.status, result::DownloadStatus::kSuccess);
80 result::Install install_result = aktualizr.Install(download_result.updates).get();
81 for (
const auto& r : install_result.ecu_reports) {
82 EXPECT_EQ(r.install_res.result_code.num_code, data::ResultCode::Numeric::kOk);
86 EXPECT_EQ(http->events_seen, 8);
91 for (
auto generate_fun : {delegation_basic, delegation_nested}) {
93 auto delegation_path = temp_dir.Path() /
"delegation_test";
94 generate_fun(delegation_path,
false);
96 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
97 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
98 auto storage = INvStorage::newStorage(conf.storage);
100 aktualizr.Initialize();
103 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
104 EXPECT_EQ(update_result.updates.size(), 2);
108 generate_fun(delegation_path,
true);
109 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
110 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
111 auto storage = INvStorage::newStorage(conf.storage);
113 aktualizr.Initialize();
115 auto update_result = aktualizr.CheckUpdates().get();
116 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
117 EXPECT_EQ(update_result.updates.size(), 1);
119 result::Download download_result = aktualizr.Download(update_result.updates).get();
120 EXPECT_EQ(download_result.status, result::DownloadStatus::kSuccess);
122 result::Install install_result = aktualizr.Install(download_result.updates).get();
123 for (
const auto& r : install_result.ecu_reports) {
124 EXPECT_EQ(r.install_res.result_code.num_code, data::ResultCode::Numeric::kOk);
131 for (
auto generate_fun : {delegation_basic, delegation_nested}) {
133 auto delegation_path = temp_dir.Path() /
"delegation_test";
134 generate_fun(delegation_path,
false);
136 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
137 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
138 auto storage = INvStorage::newStorage(conf.storage);
140 aktualizr.Initialize();
143 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
145 result::Download download_result = aktualizr.Download(update_result.updates).get();
146 EXPECT_EQ(download_result.status, result::DownloadStatus::kSuccess);
150 generate_fun(delegation_path,
true);
152 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
153 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
154 auto storage = INvStorage::newStorage(conf.storage);
156 aktualizr.Initialize();
158 auto update_result = aktualizr.CheckUpdates().get();
159 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
160 EXPECT_EQ(update_result.updates.size(), 1);
162 result::Download download_result = aktualizr.Download(update_result.updates).get();
163 EXPECT_EQ(download_result.status, result::DownloadStatus::kSuccess);
165 result::Install install_result = aktualizr.Install(download_result.updates).get();
166 for (
const auto& r : install_result.ecu_reports) {
167 EXPECT_EQ(r.install_res.result_code.num_code, data::ResultCode::Numeric::kOk);
174 for (
auto generate_fun : {delegation_basic, delegation_nested}) {
176 auto delegation_path = temp_dir.Path() /
"delegation_test";
177 generate_fun(delegation_path,
false);
179 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
180 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
181 auto storage = INvStorage::newStorage(conf.storage);
183 aktualizr.Initialize();
186 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
188 result::Download download_result = aktualizr.Download(update_result.updates).get();
189 EXPECT_EQ(download_result.status, result::DownloadStatus::kSuccess);
191 result::Install install_result = aktualizr.Install(download_result.updates).get();
192 for (
const auto& r : install_result.ecu_reports) {
193 EXPECT_EQ(r.install_res.result_code.num_code, data::ResultCode::Numeric::kOk);
198 generate_fun(delegation_path,
true);
200 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
201 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
202 auto storage = INvStorage::newStorage(conf.storage);
204 aktualizr.Initialize();
206 auto update_result = aktualizr.CheckUpdates().get();
207 EXPECT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable);
215 auto delegation_path = temp_dir.Path() /
"delegation_test";
216 delegation_nested(delegation_path,
false);
217 auto http = std::make_shared<HttpFakeDelegation>(temp_dir.Path());
219 Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
221 auto storage = INvStorage::newStorage(conf.storage);
224 aktualizr.Initialize();
226 EXPECT_EQ(update_result.status, result::UpdateStatus::kUpdatesAvailable);
228 std::vector<std::string> expected_target_names = {
"primary.txt",
"abracadabra",
"abc/secondary.txt",
"abc/target0",
229 "abc/target1",
"abc/target2",
"bcd/target0",
"cde/target0",
230 "cde/target1",
"def/target0"};
231 for (
auto& target : aktualizr.uptane_client()->allTargets()) {
232 EXPECT_EQ(target.filename(), expected_target_names[0]);
233 expected_target_names.erase(expected_target_names.begin());
236 EXPECT_TRUE(expected_target_names.empty());
240 int main(
int argc,
char** argv) {
241 ::testing::InitGoogleTest(&argc, argv);
243 std::cerr <<
"Error: " << argv[0] <<
" requires the path to the uptane-generator utility\n";
246 uptane_generator_path = argv[1];
249 logger_set_threshold(boost::log::trivial::trace);
251 return RUN_ALL_TESTS();