Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dlcservice/test_utils.h" |
| 6 | |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 7 | #include <string> |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 8 | #include <utility> |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 9 | #include <vector> |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 10 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 11 | #include <base/check.h> |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
| 13 | #include <base/files/file_util.h> |
| 14 | #include <base/files/scoped_temp_dir.h> |
| 15 | #include <dbus/dlcservice/dbus-constants.h> |
| 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <imageloader/dbus-proxy-mocks.h> |
Andrew | 4d45400 | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 19 | #include <metrics/metrics_library_mock.h> |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 20 | #include <update_engine/dbus-constants.h> |
| 21 | #include <update_engine/dbus-proxy-mocks.h> |
| 22 | |
| 23 | #include "dlcservice/boot/boot_slot.h" |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 24 | #include "dlcservice/dlc.h" |
Andrew | 4d45400 | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 25 | #include "dlcservice/metrics.h" |
Jae Hoon Kim | 0d057fb | 2020-04-13 17:44:38 -0700 | [diff] [blame] | 26 | #include "dlcservice/system_state.h" |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 27 | #include "dlcservice/utils.h" |
| 28 | |
| 29 | using std::string; |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 30 | using std::vector; |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 31 | using testing::_; |
Mike Frysinger | 0cc1856 | 2021-01-04 02:16:54 -0500 | [diff] [blame] | 32 | using testing::DoAll; |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 33 | using testing::Return; |
| 34 | using testing::SetArgPointee; |
| 35 | using testing::StrictMock; |
| 36 | |
| 37 | namespace dlcservice { |
| 38 | |
Amin Hassani | c0208d7 | 2020-04-23 22:49:31 -0700 | [diff] [blame] | 39 | const char kFirstDlc[] = "first-dlc"; |
| 40 | const char kSecondDlc[] = "second-dlc"; |
| 41 | const char kThirdDlc[] = "third-dlc"; |
| 42 | const char kPackage[] = "package"; |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 43 | const char kDefaultOmahaUrl[] = "http://foo-url"; |
| 44 | |
| 45 | BaseTest::BaseTest() { |
| 46 | // Create mocks with default behaviors. |
| 47 | mock_image_loader_proxy_ = |
| 48 | std::make_unique<StrictMock<ImageLoaderProxyMock>>(); |
| 49 | mock_image_loader_proxy_ptr_ = mock_image_loader_proxy_.get(); |
| 50 | |
| 51 | mock_update_engine_proxy_ = |
| 52 | std::make_unique<StrictMock<UpdateEngineProxyMock>>(); |
| 53 | mock_update_engine_proxy_ptr_ = mock_update_engine_proxy_.get(); |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 54 | |
| 55 | mock_session_manager_proxy_ = |
| 56 | std::make_unique<StrictMock<SessionManagerProxyMock>>(); |
| 57 | mock_session_manager_proxy_ptr_ = mock_session_manager_proxy_.get(); |
Jae Hoon Kim | 23c7f49 | 2020-06-26 10:30:09 -0700 | [diff] [blame] | 58 | |
| 59 | mock_boot_device_ = std::make_unique<MockBootDevice>(); |
| 60 | mock_boot_device_ptr_ = mock_boot_device_.get(); |
| 61 | EXPECT_CALL(*mock_boot_device_, GetBootDevice()) |
| 62 | .WillOnce(Return("/dev/sdb5")); |
| 63 | ON_CALL(*mock_boot_device_, IsRemovableDevice(_)) |
| 64 | .WillByDefault(Return(false)); |
| 65 | EXPECT_CALL(*mock_boot_device_, IsRemovableDevice(_)).Times(1); |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void BaseTest::SetUp() { |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 69 | loop_.SetAsCurrent(); |
| 70 | |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 71 | SetUpFilesAndDirectories(); |
| 72 | |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 73 | auto mock_metrics = std::make_unique<testing::StrictMock<MockMetrics>>(); |
| 74 | mock_metrics_ = mock_metrics.get(); |
Andrew | 4d45400 | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 75 | |
Jae Hoon Kim | 9318f34 | 2020-09-10 14:38:13 -0700 | [diff] [blame] | 76 | auto mock_system_properties = |
| 77 | std::make_unique<testing::StrictMock<MockSystemProperties>>(); |
| 78 | mock_system_properties_ = mock_system_properties.get(); |
| 79 | |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 80 | SystemState::Initialize( |
| 81 | std::move(mock_image_loader_proxy_), std::move(mock_update_engine_proxy_), |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 82 | std::move(mock_session_manager_proxy_), &mock_state_change_reporter_, |
Jae Hoon Kim | 23c7f49 | 2020-06-26 10:30:09 -0700 | [diff] [blame] | 83 | std::make_unique<BootSlot>(std::move(mock_boot_device_)), |
Jae Hoon Kim | 9318f34 | 2020-09-10 14:38:13 -0700 | [diff] [blame] | 84 | std::move(mock_metrics), std::move(mock_system_properties), |
| 85 | manifest_path_, preloaded_content_path_, content_path_, prefs_path_, |
| 86 | users_path_, &clock_, |
| 87 | /*for_test=*/true); |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void BaseTest::SetUpFilesAndDirectories() { |
| 91 | // Initialize DLC path. |
| 92 | CHECK(scoped_temp_dir_.CreateUniqueTempDir()); |
| 93 | manifest_path_ = JoinPaths(scoped_temp_dir_.GetPath(), "rootfs"); |
| 94 | preloaded_content_path_ = |
| 95 | JoinPaths(scoped_temp_dir_.GetPath(), "preloaded_stateful"); |
| 96 | content_path_ = JoinPaths(scoped_temp_dir_.GetPath(), "stateful"); |
Amin Hassani | fe63fc2 | 2020-04-07 11:34:34 -0700 | [diff] [blame] | 97 | prefs_path_ = JoinPaths(scoped_temp_dir_.GetPath(), "var_lib_dlcservice"); |
Amin Hassani | bfad7b8 | 2020-04-13 12:33:47 -0700 | [diff] [blame] | 98 | users_path_ = JoinPaths(scoped_temp_dir_.GetPath(), "users"); |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 99 | mount_path_ = JoinPaths(scoped_temp_dir_.GetPath(), "mount"); |
| 100 | base::FilePath mount_root_path = JoinPaths(mount_path_, "root"); |
| 101 | base::CreateDirectory(manifest_path_); |
| 102 | base::CreateDirectory(preloaded_content_path_); |
| 103 | base::CreateDirectory(content_path_); |
Amin Hassani | fe63fc2 | 2020-04-07 11:34:34 -0700 | [diff] [blame] | 104 | base::CreateDirectory(prefs_path_); |
Amin Hassani | bfad7b8 | 2020-04-13 12:33:47 -0700 | [diff] [blame] | 105 | base::CreateDirectory(users_path_); |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 106 | base::CreateDirectory(mount_root_path); |
| 107 | testdata_path_ = JoinPaths(getenv("SRC"), "testdata"); |
| 108 | |
| 109 | // Create DLC manifest sub-directories. |
| 110 | for (auto&& id : {kFirstDlc, kSecondDlc, kThirdDlc}) { |
| 111 | base::CreateDirectory(JoinPaths(manifest_path_, id, kPackage)); |
| 112 | base::CopyFile(JoinPaths(testdata_path_, id, kPackage, kManifestName), |
| 113 | JoinPaths(manifest_path_, id, kPackage, kManifestName)); |
| 114 | } |
| 115 | } |
| 116 | |
Amin Hassani | c0867b6 | 2020-04-16 09:44:34 -0700 | [diff] [blame] | 117 | int64_t GetFileSize(const base::FilePath& path) { |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 118 | int64_t file_size; |
| 119 | EXPECT_TRUE(base::GetFileSize(path, &file_size)); |
| 120 | return file_size; |
| 121 | } |
| 122 | |
Amin Hassani | 28ed900 | 2020-05-28 12:37:01 -0700 | [diff] [blame] | 123 | base::FilePath BaseTest::SetUpDlcPreloadedImage(const DlcId& id) { |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 124 | imageloader::Manifest manifest; |
| 125 | dlcservice::GetDlcManifest(manifest_path_, id, kPackage, &manifest); |
| 126 | |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 127 | base::FilePath image_path = |
| 128 | JoinPaths(preloaded_content_path_, id, kPackage, kDlcImageFileName); |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 129 | CreateFile(image_path, manifest.size()); |
Amin Hassani | 28ed900 | 2020-05-28 12:37:01 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(base::PathExists(image_path)); |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 131 | |
| 132 | string data(manifest.size(), '1'); |
Amin Hassani | 7c6543f | 2020-06-02 16:16:43 -0700 | [diff] [blame] | 133 | WriteToImage(image_path, data); |
Amin Hassani | 28ed900 | 2020-05-28 12:37:01 -0700 | [diff] [blame] | 134 | |
| 135 | return image_path; |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Will create |path/|id|/|package|/dlc_[a|b]/dlc.img files. |
| 139 | void BaseTest::SetUpDlcWithSlots(const DlcId& id) { |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 140 | imageloader::Manifest manifest; |
| 141 | dlcservice::GetDlcManifest(manifest_path_, id, kPackage, &manifest); |
| 142 | |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 143 | // Create DLC content sub-directories and empty images. |
| 144 | for (const auto& slot : {BootSlot::Slot::A, BootSlot::Slot::B}) { |
| 145 | base::FilePath image_path = |
| 146 | GetDlcImagePath(content_path_, id, kPackage, slot); |
Amin Hassani | f2efc5a | 2020-05-25 21:22:57 -0700 | [diff] [blame] | 147 | CreateFile(image_path, manifest.preallocated_size()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void BaseTest::InstallWithUpdateEngine(const vector<string>& ids) { |
| 152 | for (const auto& id : ids) { |
| 153 | imageloader::Manifest manifest; |
| 154 | dlcservice::GetDlcManifest(manifest_path_, id, kPackage, &manifest); |
| 155 | base::FilePath image_path = GetDlcImagePath( |
| 156 | content_path_, id, kPackage, SystemState::Get()->active_boot_slot()); |
| 157 | |
| 158 | string data(manifest.size(), '1'); |
Amin Hassani | 7c6543f | 2020-06-02 16:16:43 -0700 | [diff] [blame] | 159 | WriteToImage(image_path, data); |
Amin Hassani | 10c8c6c | 2020-04-06 12:28:59 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void BaseTest::SetMountPath(const string& mount_path_expected) { |
| 164 | ON_CALL(*mock_image_loader_proxy_ptr_, LoadDlcImage(_, _, _, _, _, _)) |
| 165 | .WillByDefault( |
| 166 | DoAll(SetArgPointee<3>(mount_path_expected), Return(true))); |
| 167 | } |
| 168 | |
| 169 | } // namespace dlcservice |