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