1 #include <gtest/gtest.h>
6 #include "primary/aktualizr.h"
7 #include "test_utils.h"
8 #include "uptane_test_common.h"
10 boost::filesystem::path uptane_generator_path;
15 Process uptane_gen(uptane_generator_path.string());
16 uptane_gen.run({
"generate",
"--path", meta_dir_.PathString()});
18 auto http = std::make_shared<HttpFake>(temp_dir_.Path(),
"", meta_dir_.Path() /
"repo");
19 Config conf = UptaneTestCommon::makeTestConfig(temp_dir_, http->tls_server);
20 conf.pacman.fake_need_reboot =
true;
21 conf.uptane.force_install_completion =
true;
22 conf.bootloader.reboot_sentinel_dir = temp_dir_.Path();
24 logger_set_threshold(boost::log::trivial::trace);
26 storage_ = INvStorage::newStorage(conf.storage);
27 aktualizr_ = std::make_shared<UptaneTestCommon::TestAktualizr>(conf, storage_, http);
31 uptane_gen_.run({
"image",
"--path", meta_dir_.PathString(),
"--filename",
"tests/test_data/firmware.txt",
32 "--targetname", target_filename_,
"--hwid",
"primary_hw"});
34 target_image_hash_ = boost::algorithm::to_lower_copy(
35 boost::algorithm::hex(Crypto::sha256digest(Utils::readFile(
"tests/test_data/firmware.txt"))));
38 void addTarget(
const std::string& target_filename,
int expiration_delta = 0) {
39 if (expiration_delta != 0) {
40 time_t new_expiration_time;
41 std::time(&new_expiration_time);
42 new_expiration_time += expiration_delta;
43 struct tm new_expiration_time_str {};
44 gmtime_r(&new_expiration_time, &new_expiration_time_str);
46 uptane_gen_.run({
"addtarget",
"--path", meta_dir_.PathString(),
"--targetname", target_filename,
"--hwid",
47 "primary_hw",
"--serial",
"CA:FE:A6:D2:84:9D",
"--expires",
48 TimeStamp(new_expiration_time_str).ToString()});
51 uptane_gen_.run({
"addtarget",
"--path", meta_dir_.PathString(),
"--targetname", target_filename,
"--hwid",
52 "primary_hw",
"--serial",
"CA:FE:A6:D2:84:9D"});
56 void addTargetAndSign(
const std::string& target_filename,
int expiration_delta = 0) {
57 addTarget(target_filename, expiration_delta);
58 uptane_gen_.run({
"signtargets",
"--path", meta_dir_.PathString()});
61 void refreshTargetsMetadata() {
63 uptane_gen_.run({
"refresh",
"--path", meta_dir_.PathString(),
"--repotype",
"director",
"--keyname",
"targets"});
66 void addTargetToInstall(
int expiration_delta = 0) {
68 addTargetAndSign(target_filename_, expiration_delta);
73 const std::string target_filename_ =
"firmware.txt";
74 std::string target_image_hash_;
78 std::shared_ptr<INvStorage> storage_;
79 std::shared_ptr<UptaneTestCommon::TestAktualizr> aktualizr_;
83 aktualizr_->Initialize();
85 EXPECT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable);
87 addTargetToInstall(-1);
90 aktualizr_->UptaneCycle();
93 auto& client = aktualizr_->uptane_client();
94 ASSERT_FALSE(client->hasPendingUpdates());
95 ASSERT_FALSE(client->isInstallCompletionRequired());
97 auto currently_installed_target = client->getCurrent();
99 EXPECT_NE(target_image_hash_, currently_installed_target.sha256Hash());
100 EXPECT_NE(target_filename_, currently_installed_target.filename());
102 refreshTargetsMetadata();
105 aktualizr_->UptaneCycle();
108 ASSERT_TRUE(client->hasPendingUpdates());
109 ASSERT_TRUE(client->isInstallCompletionRequired());
112 client->completeInstall();
115 aktualizr_->Initialize();
116 aktualizr_->UptaneCycle();
118 ASSERT_FALSE(client->hasPendingUpdates());
119 ASSERT_FALSE(client->isInstallCompletionRequired());
121 currently_installed_target = client->getCurrent();
122 EXPECT_EQ(target_image_hash_, currently_installed_target.sha256Hash());
123 EXPECT_EQ(target_filename_, currently_installed_target.filename());
127 aktualizr_->Initialize();
130 EXPECT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable);
132 const int expiration_in_sec = 5;
133 addTargetToInstall(expiration_in_sec);
134 auto target_init_time = std::chrono::system_clock::now();
137 aktualizr_->UptaneCycle();
140 auto& client = aktualizr_->uptane_client();
141 ASSERT_TRUE(client->hasPendingUpdates());
142 ASSERT_TRUE(client->isInstallCompletionRequired());
145 std::this_thread::sleep_for(std::chrono::seconds(expiration_in_sec) -
146 (std::chrono::system_clock::now() - target_init_time));
147 aktualizr_->UptaneCycle();
151 ASSERT_TRUE(client->hasPendingUpdates());
152 ASSERT_TRUE(client->isInstallCompletionRequired());
155 client->completeInstall();
158 aktualizr_->Initialize();
159 aktualizr_->UptaneCycle();
163 auto currently_installed_target = client->getCurrent();
164 EXPECT_EQ(target_image_hash_, currently_installed_target.sha256Hash());
165 EXPECT_EQ(target_filename_, currently_installed_target.filename());
169 aktualizr_->Initialize();
172 EXPECT_EQ(update_result.status, result::UpdateStatus::kNoUpdatesAvailable);
174 const int expiration_in_sec = 5;
175 addTargetToInstall(expiration_in_sec);
176 auto target_init_time = std::chrono::system_clock::now();
179 aktualizr_->UptaneCycle();
182 auto& client = aktualizr_->uptane_client();
183 ASSERT_TRUE(client->hasPendingUpdates());
184 ASSERT_TRUE(client->isInstallCompletionRequired());
188 std::this_thread::sleep_for(std::chrono::seconds(expiration_in_sec) -
189 (std::chrono::system_clock::now() - target_init_time));
192 client->completeInstall();
195 aktualizr_->Initialize();
196 aktualizr_->UptaneCycle();
200 auto currently_installed_target = client->getCurrent();
201 EXPECT_EQ(target_image_hash_, currently_installed_target.sha256Hash());
202 EXPECT_EQ(target_filename_, currently_installed_target.filename());
205 int main(
int argc,
char** argv) {
206 ::testing::InitGoogleTest(&argc, argv);
208 std::cerr <<
"Error: " << argv[0] <<
" requires the path to the uptane-generator utility\n";
211 uptane_generator_path = argv[1];
214 logger_set_threshold(boost::log::trivial::trace);
216 return RUN_ALL_TESTS();