Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 5 | #include "imageloader/imageloader_impl.h" |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 6 | |
Greg Kerr | 6a5ee86 | 2016-10-19 11:32:43 -0700 | [diff] [blame] | 7 | #include <stdint.h> |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 8 | |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 9 | #include <list> |
Ben Chan | ea104dd | 2017-09-29 00:43:04 -0700 | [diff] [blame] | 10 | #include <memory> |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 11 | #include <string> |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
| 15 | #include <base/files/file_util.h> |
| 16 | #include <base/files/scoped_temp_dir.h> |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 19 | |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 20 | #include "imageloader/component.h" |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 21 | #include "imageloader/mock_helper_process_proxy.h" |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 22 | #include "imageloader/test_utilities.h" |
| 23 | #include "imageloader/verity_mounter.h" |
| 24 | |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 25 | namespace imageloader { |
| 26 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 27 | using testing::_; |
| 28 | |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 29 | class ImageLoaderTest : public testing::Test { |
| 30 | public: |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 31 | ImageLoaderTest() { |
| 32 | CHECK(scoped_temp_dir_.CreateUniqueTempDir()); |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 33 | temp_dir_ = scoped_temp_dir_.GetPath(); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 34 | CHECK(base::SetPosixFilePermissions(temp_dir_, kComponentDirPerms)); |
| 35 | } |
| 36 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 37 | ImageLoaderConfig GetConfig(const char* path) { |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 38 | Keys keys; |
| 39 | keys.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey), |
| 40 | std::end(kDevPublicKey))); |
| 41 | keys.push_back(std::vector<uint8_t>(std::begin(kOciDevPublicKey), |
| 42 | std::end(kOciDevPublicKey))); |
| 43 | ImageLoaderConfig config(keys, path, "/foo"); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 44 | return config; |
| 45 | } |
| 46 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 47 | base::ScopedTempDir scoped_temp_dir_; |
| 48 | base::FilePath temp_dir_; |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 51 | // Test the RegisterComponent public interface. |
| 52 | TEST_F(ImageLoaderTest, RegisterComponentAndGetVersion) { |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 53 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 55 | GetTestComponentPath().value())); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 56 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 57 | base::FilePath comp_dir = temp_dir_.Append(kTestComponentName); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(base::DirectoryExists(comp_dir)); |
| 59 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 60 | base::FilePath hint_file = comp_dir.Append("latest-version"); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(base::PathExists(hint_file)); |
| 62 | |
| 63 | std::string hint_file_contents; |
| 64 | ASSERT_TRUE( |
| 65 | base::ReadFileToStringWithMaxSize(hint_file, &hint_file_contents, 4096)); |
| 66 | EXPECT_EQ(kTestDataVersion, hint_file_contents); |
| 67 | |
| 68 | base::FilePath version_dir = comp_dir.Append(kTestDataVersion); |
| 69 | ASSERT_TRUE(base::DirectoryExists(version_dir)); |
| 70 | |
Greg Kerr | f50e24a | 2017-01-06 17:12:32 -0800 | [diff] [blame] | 71 | // Make sure it actually checks the reported version against the real version. |
| 72 | EXPECT_FALSE(loader.RegisterComponent(kTestComponentName, kTestUpdatedVersion, |
| 73 | GetTestComponentPath().value())); |
| 74 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 75 | // Now copy a new version into place. |
Ben Chan | a92a9f0 | 2017-12-18 17:47:23 -0800 | [diff] [blame] | 76 | EXPECT_TRUE(loader.RegisterComponent( |
| 77 | kTestComponentName, kTestUpdatedVersion, |
| 78 | GetTestComponentPath(kTestUpdatedVersion).value())); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 79 | |
| 80 | std::string hint_file_contents2; |
| 81 | ASSERT_TRUE( |
| 82 | base::ReadFileToStringWithMaxSize(hint_file, &hint_file_contents2, 4096)); |
| 83 | EXPECT_EQ(kTestUpdatedVersion, hint_file_contents2); |
| 84 | |
| 85 | base::FilePath version_dir2 = comp_dir.Append(kTestUpdatedVersion); |
| 86 | ASSERT_TRUE(base::DirectoryExists(version_dir2)); |
| 87 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 88 | EXPECT_EQ(kTestUpdatedVersion, |
| 89 | loader.GetComponentVersion(kTestComponentName)); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 90 | |
| 91 | // Reject rollback to an older version. |
| 92 | EXPECT_FALSE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 93 | GetTestComponentPath().value())); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 94 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 95 | EXPECT_EQ(kTestUpdatedVersion, |
| 96 | loader.GetComponentVersion(kTestComponentName)); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Greg Kerr | 1c7403c | 2016-11-11 11:57:44 -0800 | [diff] [blame] | 99 | // Pretend ImageLoader crashed, by creating an incomplete installation, and then |
| 100 | // attempt registration with ImageLoader. |
| 101 | TEST_F(ImageLoaderTest, RegisterComponentAfterCrash) { |
Greg Kerr | 1c7403c | 2016-11-11 11:57:44 -0800 | [diff] [blame] | 102 | // Now create the junk there. |
| 103 | const std::string junk_contents ="Bad file contents"; |
| 104 | const base::FilePath junk_path = |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 105 | temp_dir_.Append(kTestComponentName).Append(kTestDataVersion); |
Greg Kerr | 1c7403c | 2016-11-11 11:57:44 -0800 | [diff] [blame] | 106 | ASSERT_TRUE(base::CreateDirectory(junk_path)); |
| 107 | ASSERT_EQ(static_cast<int>(junk_contents.size()), |
| 108 | base::WriteFile(junk_path.Append("junkfile"), junk_contents.data(), |
| 109 | junk_contents.size())); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 110 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
Greg Kerr | 1c7403c | 2016-11-11 11:57:44 -0800 | [diff] [blame] | 111 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 112 | GetTestComponentPath().value())); |
Greg Kerr | 4bd7813 | 2016-07-19 11:51:16 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 115 | TEST_F(ImageLoaderTest, MountValidImage) { |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 116 | Keys keys; |
| 117 | keys.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey), |
| 118 | std::end(kDevPublicKey))); |
| 119 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 120 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 646339e | 2018-07-23 17:41:36 -0700 | [diff] [blame] | 121 | EXPECT_CALL( |
| 122 | *helper_mock, |
| 123 | SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 124 | .Times(2); |
| 125 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 126 | .WillByDefault(testing::Return(true)); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 127 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 128 | base::ScopedTempDir scoped_mount_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 129 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 130 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 131 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 132 | scoped_mount_dir.GetPath().value().c_str()); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 133 | ImageLoaderImpl loader(std::move(config)); |
| 134 | |
| 135 | // We previously tested RegisterComponent, so assume this works if it reports |
| 136 | // true. |
| 137 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 138 | GetTestComponentPath().value())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 139 | |
| 140 | const std::string expected_path = |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 141 | scoped_mount_dir.GetPath().value() + "/PepperFlashPlayer/22.0.0.158"; |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 142 | EXPECT_EQ(expected_path, |
| 143 | loader.LoadComponent(kTestComponentName, helper_mock.get())); |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 144 | |
| 145 | // Let's also test mounting the component at a fixed point. |
| 146 | const std::string expected_path2 = |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 147 | scoped_mount_dir.GetPath().value() + "/FixedMountPoint"; |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 148 | EXPECT_TRUE(loader.LoadComponent(kTestComponentName, expected_path2, |
| 149 | helper_mock.get())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 152 | TEST_F(ImageLoaderTest, LoadComponentAtPath) { |
| 153 | Keys keys; |
| 154 | keys.push_back( |
| 155 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 156 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 157 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 646339e | 2018-07-23 17:41:36 -0700 | [diff] [blame] | 158 | EXPECT_CALL( |
| 159 | *helper_mock, |
| 160 | SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 161 | .Times(1); |
| 162 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 163 | .WillByDefault(testing::Return(true)); |
| 164 | |
| 165 | base::ScopedTempDir scoped_mount_dir; |
| 166 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 167 | |
| 168 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 169 | scoped_mount_dir.GetPath().value().c_str()); |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 170 | ImageLoaderImpl loader(std::move(config)); |
| 171 | |
| 172 | const std::string expected_path = |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 173 | scoped_mount_dir.GetPath().value() + "/PepperFlashPlayer/22.0.0.158"; |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 174 | const std::string mnt_path = loader.LoadComponentAtPath( |
| 175 | kTestComponentName, GetTestComponentPath(), helper_mock.get()); |
| 176 | EXPECT_EQ(expected_path, mnt_path); |
| 177 | } |
| 178 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 179 | TEST_F(ImageLoaderTest, CleanupAll) { |
| 180 | Keys keys; |
| 181 | keys.push_back( |
| 182 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 183 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 184 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 185 | EXPECT_CALL(*helper_mock, SendUnmountAllCommand(_, _, _)) |
| 186 | .Times(1); |
| 187 | ON_CALL(*helper_mock, SendUnmountAllCommand(_, _, _)) |
| 188 | .WillByDefault(testing::Return(true)); |
| 189 | |
| 190 | base::ScopedTempDir scoped_mount_dir; |
| 191 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 192 | |
| 193 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 194 | scoped_mount_dir.GetPath().value().c_str()); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 195 | ImageLoaderImpl loader(std::move(config)); |
| 196 | |
| 197 | base::FilePath rootpath("/"); |
| 198 | std::vector<std::string> paths; |
| 199 | EXPECT_EQ(loader.CleanupAll(true, rootpath, &paths, helper_mock.get()), true); |
| 200 | } |
| 201 | |
| 202 | TEST_F(ImageLoaderTest, Cleanup) { |
| 203 | Keys keys; |
| 204 | keys.push_back( |
| 205 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 206 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 207 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 208 | EXPECT_CALL(*helper_mock, SendUnmountCommand(_)).Times(1); |
| 209 | ON_CALL(*helper_mock, SendUnmountCommand(_)) |
| 210 | .WillByDefault(testing::Return(true)); |
| 211 | |
| 212 | base::ScopedTempDir scoped_mount_dir; |
| 213 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 214 | |
| 215 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 216 | scoped_mount_dir.GetPath().value().c_str()); |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame] | 217 | ImageLoaderImpl loader(std::move(config)); |
| 218 | |
| 219 | base::FilePath path("/"); |
| 220 | EXPECT_EQ(loader.Cleanup(path, helper_mock.get()), true); |
| 221 | } |
| 222 | |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 223 | TEST_F(ImageLoaderTest, LoadExt4Image) { |
Greg Kerr | e870420 | 2017-07-27 12:54:31 -0700 | [diff] [blame] | 224 | Keys keys; |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 225 | keys.push_back( |
| 226 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
Greg Kerr | e870420 | 2017-07-27 12:54:31 -0700 | [diff] [blame] | 227 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 228 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 646339e | 2018-07-23 17:41:36 -0700 | [diff] [blame] | 229 | EXPECT_CALL(*helper_mock, |
| 230 | SendMountCommand(_, _, brillo::imageloader::FileSystem::kExt4, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 231 | .Times(1); |
| 232 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
| 233 | .WillByDefault(testing::Return(true)); |
| 234 | |
| 235 | base::ScopedTempDir scoped_mount_dir; |
| 236 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 237 | |
| 238 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 239 | scoped_mount_dir.GetPath().value().c_str()); |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 240 | ImageLoaderImpl loader(std::move(config)); |
| 241 | |
| 242 | const std::string expected_path = |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 243 | scoped_mount_dir.GetPath().value() + "/ext4/9824.0.4"; |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 244 | const std::string mnt_path = loader.LoadComponentAtPath( |
| 245 | "ext4", GetTestDataPath("ext4_component"), helper_mock.get()); |
| 246 | EXPECT_EQ(expected_path, mnt_path); |
| 247 | } |
| 248 | |
Xiaochu Liu | f6106e5 | 2018-08-10 13:09:00 -0700 | [diff] [blame^] | 249 | TEST_F(ImageLoaderTest, UnloadDlcImage) { |
| 250 | Keys keys; |
| 251 | keys.push_back( |
| 252 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 253 | |
| 254 | base::ScopedTempDir scoped_mount_dir; |
| 255 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 256 | |
| 257 | const std::string dlc_id = "dlc_id"; |
| 258 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
| 259 | EXPECT_CALL(*helper_mock, |
| 260 | SendUnmountCommand( |
| 261 | scoped_mount_dir.GetPath().Append(dlc_id).value().c_str())) |
| 262 | .Times(1); |
| 263 | ON_CALL(*helper_mock, SendUnmountCommand(_)) |
| 264 | .WillByDefault(testing::Return(true)); |
| 265 | |
| 266 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 267 | scoped_mount_dir.GetPath().value().c_str()); |
| 268 | ImageLoaderImpl loader(std::move(config)); |
| 269 | |
| 270 | loader.UnloadDlcImage(dlc_id, helper_mock.get()); |
| 271 | } |
| 272 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 273 | TEST_F(ImageLoaderTest, RemoveImageAtPathRemovable) { |
| 274 | Keys keys; |
| 275 | keys.push_back( |
| 276 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 277 | |
| 278 | base::ScopedTempDir scoped_mount_dir; |
| 279 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 280 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 281 | scoped_mount_dir.GetPath().value().c_str()); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 282 | ImageLoaderImpl loader(std::move(config)); |
| 283 | |
| 284 | // Make a copy to avoid permanent loss of test data. |
| 285 | base::ScopedTempDir component_root; |
| 286 | ASSERT_TRUE(component_root.CreateUniqueTempDir()); |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 287 | base::FilePath component_path = component_root.GetPath().Append("9824.0.4"); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 288 | ASSERT_TRUE(base::CreateDirectory(component_path)); |
| 289 | std::unique_ptr<Component> component = |
| 290 | Component::Create(base::FilePath(GetTestDataPath("ext4_component")), |
| 291 | keys); |
| 292 | ASSERT_TRUE(component->CopyTo(component_path)); |
| 293 | |
| 294 | // Remove the component. |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 295 | EXPECT_TRUE(loader.RemoveComponentAtPath("ext4", component_root.GetPath(), |
| 296 | component_path)); |
| 297 | EXPECT_FALSE(base::PathExists(component_root.GetPath())); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(ImageLoaderTest, RemoveImageAtPathNotRemovable) { |
| 301 | Keys keys; |
| 302 | keys.push_back( |
| 303 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 304 | |
| 305 | base::ScopedTempDir scoped_mount_dir; |
| 306 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 307 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 308 | scoped_mount_dir.GetPath().value().c_str()); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 309 | ImageLoaderImpl loader(std::move(config)); |
| 310 | |
| 311 | // Make a copy to avoid permanent loss of test data. |
| 312 | base::ScopedTempDir component_root; |
| 313 | ASSERT_TRUE(component_root.CreateUniqueTempDir()); |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 314 | base::FilePath component_path = component_root.GetPath().Append("9824.0.4"); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 315 | ASSERT_TRUE(base::CreateDirectory(component_path)); |
| 316 | std::unique_ptr<Component> component = |
| 317 | Component::Create(base::FilePath(GetTestComponentPath()), |
| 318 | keys); |
| 319 | ASSERT_TRUE(component->CopyTo(component_path)); |
| 320 | |
| 321 | // Remove the component. |
| 322 | EXPECT_FALSE(loader.RemoveComponentAtPath( |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 323 | kTestComponentName, component_root.GetPath(), component_path)); |
| 324 | EXPECT_TRUE(base::PathExists(component_root.GetPath())); |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 327 | TEST_F(ImageLoaderTest, MountInvalidImage) { |
| 328 | Keys keys; |
| 329 | keys.push_back( |
| 330 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 331 | |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 332 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | 646339e | 2018-07-23 17:41:36 -0700 | [diff] [blame] | 333 | EXPECT_CALL( |
| 334 | *helper_mock, |
| 335 | SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 336 | .Times(0); |
| 337 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 338 | .WillByDefault(testing::Return(true)); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 339 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 340 | base::ScopedTempDir scoped_mount_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 341 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 342 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 343 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 344 | scoped_mount_dir.GetPath().value().c_str()); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 345 | ImageLoaderImpl loader(std::move(config)); |
| 346 | |
| 347 | // We previously tested RegisterComponent, so assume this works if it reports |
| 348 | // true. |
| 349 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 350 | GetTestComponentPath().value())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 351 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 352 | base::FilePath table = temp_dir_.Append(kTestComponentName) |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 353 | .Append(kTestDataVersion) |
Greg Kerr | 30cd5fb | 2016-09-29 12:37:02 -0700 | [diff] [blame] | 354 | .Append("table"); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 355 | std::string contents = "corrupt"; |
| 356 | ASSERT_EQ(static_cast<int>(contents.size()), |
Greg Kerr | 30cd5fb | 2016-09-29 12:37:02 -0700 | [diff] [blame] | 357 | base::WriteFile(table, contents.data(), contents.size())); |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 358 | ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 361 | TEST_F(ImageLoaderTest, SetupTable) { |
| 362 | std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV " |
| 363 | "hashstart=40 alg=sha256 root_hexdigest=" |
| 364 | "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 " |
| 365 | "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2"; |
| 366 | |
| 367 | // Make sure excess newlines are rejected. |
| 368 | std::string bad_table = base_table + "\n\n"; |
| 369 | EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6")); |
| 370 | |
| 371 | // Make sure it does the right replacements on a simple base table. |
| 372 | std::string good_table = base_table; |
| 373 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6")); |
| 374 | |
| 375 | std::string known_good_table = |
| 376 | "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 " |
| 377 | "hashstart=40 alg=sha256 root_hexdigest=" |
| 378 | "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 " |
| 379 | "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 " |
| 380 | "error_behavior=eio"; |
| 381 | EXPECT_EQ(known_good_table, good_table); |
| 382 | |
| 383 | // Make sure the newline is stripped. |
| 384 | std::string good_table_newline = base_table + "\n"; |
| 385 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6")); |
| 386 | EXPECT_EQ(known_good_table, good_table_newline); |
| 387 | |
| 388 | // Make sure error_behavior isn't appended twice. |
| 389 | std::string good_table_error = base_table + " error_behavior=eio\n"; |
| 390 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6")); |
| 391 | EXPECT_EQ(known_good_table, good_table_error); |
| 392 | } |
| 393 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 394 | TEST_F(ImageLoaderTest, SecondKey) { |
| 395 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 396 | ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName, |
| 397 | kTestOciComponentVersion, |
| 398 | GetTestOciComponentPath().value())); |
| 399 | |
| 400 | base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName); |
| 401 | ASSERT_TRUE(base::DirectoryExists(comp_dir)); |
| 402 | |
| 403 | base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion); |
| 404 | ASSERT_TRUE(base::DirectoryExists(version_dir)); |
| 405 | } |
| 406 | |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 407 | TEST_F(ImageLoaderTest, GetMetadata) { |
| 408 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 409 | ASSERT_TRUE(loader.RegisterComponent(kMetadataComponentName, |
| 410 | kTestOciComponentVersion, |
| 411 | GetMetadataComponentPath().value())); |
| 412 | |
| 413 | // We shouldn't need to load the component to get the metadata. |
| 414 | std::map<std::string, std::string> metadata; |
| 415 | ASSERT_TRUE(loader.GetComponentMetadata(kMetadataComponentName, &metadata)); |
| 416 | std::map<std::string, std::string> expected_metadata{ |
| 417 | {"foo", "bar"}, |
| 418 | {"baz", "quux"}, |
| 419 | }; |
| 420 | ASSERT_EQ(expected_metadata, metadata); |
| 421 | } |
| 422 | |
| 423 | TEST_F(ImageLoaderTest, GetEmptyMetadata) { |
| 424 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 425 | ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName, |
| 426 | kTestOciComponentVersion, |
| 427 | GetTestOciComponentPath().value())); |
| 428 | |
| 429 | // If there's no metadata, we should get nothing. |
| 430 | std::map<std::string, std::string> metadata; |
| 431 | ASSERT_TRUE(loader.GetComponentMetadata(kTestOciComponentName, &metadata)); |
| 432 | ASSERT_TRUE(metadata.empty()); |
| 433 | } |
| 434 | |
| 435 | TEST_F(ImageLoaderTest, MetadataFailure) { |
| 436 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 437 | // Metadata is optional, but malformed metadata should not be present in the |
| 438 | // manifest. If it is, fail to load the component. |
| 439 | ASSERT_FALSE(loader.RegisterComponent(kBadMetadataComponentName, |
| 440 | kTestOciComponentVersion, |
| 441 | GetBadMetadataComponentPath().value())); |
| 442 | |
| 443 | ASSERT_FALSE(loader.RegisterComponent( |
| 444 | kNonDictMetadataComponentName, |
| 445 | kTestOciComponentVersion, |
| 446 | GetNonDictMetadataComponentPath().value())); |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 449 | } // namespace imageloader |