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 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 5 | #include "imageloader_impl.h" |
| 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 | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 14 | #include "component.h" |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 15 | #include "mock_helper_process.h" |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 16 | #include "test_utilities.h" |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 17 | #include "verity_mounter.h" |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 18 | |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 19 | #include <base/files/file_path.h> |
| 20 | #include <base/files/file_util.h> |
| 21 | #include <base/files/scoped_temp_dir.h> |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
Greg Kerr | 3e750f4 | 2016-06-29 15:20:21 -0700 | [diff] [blame] | 24 | |
| 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()); |
| 33 | temp_dir_ = scoped_temp_dir_.path(); |
| 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. |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 76 | EXPECT_TRUE( |
| 77 | loader.RegisterComponent(kTestComponentName, kTestUpdatedVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 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 | |
Ben Chan | ea104dd | 2017-09-29 00:43:04 -0700 | [diff] [blame] | 120 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 121 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _)) |
| 122 | .Times(2); |
| 123 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 124 | .WillByDefault(testing::Return(true)); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 125 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 126 | base::ScopedTempDir scoped_mount_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 127 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 128 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 129 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 130 | scoped_mount_dir.path().value().c_str()); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 131 | ImageLoaderImpl loader(std::move(config)); |
| 132 | |
| 133 | // We previously tested RegisterComponent, so assume this works if it reports |
| 134 | // true. |
| 135 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 136 | GetTestComponentPath().value())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 137 | |
| 138 | const std::string expected_path = |
| 139 | scoped_mount_dir.path().value() + "/PepperFlashPlayer/22.0.0.158"; |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 140 | EXPECT_EQ(expected_path, |
| 141 | loader.LoadComponent(kTestComponentName, helper_mock.get())); |
Greg Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame] | 142 | |
| 143 | // Let's also test mounting the component at a fixed point. |
| 144 | const std::string expected_path2 = |
| 145 | scoped_mount_dir.path().value() + "/FixedMountPoint"; |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 146 | EXPECT_TRUE(loader.LoadComponent(kTestComponentName, expected_path2, |
| 147 | helper_mock.get())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 150 | TEST_F(ImageLoaderTest, LoadComponentAtPath) { |
| 151 | Keys keys; |
| 152 | keys.push_back( |
| 153 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 154 | |
Ben Chan | ea104dd | 2017-09-29 00:43:04 -0700 | [diff] [blame] | 155 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 156 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _)) |
| 157 | .Times(1); |
| 158 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 772abab | 2017-06-16 14:51:01 -0700 | [diff] [blame] | 159 | .WillByDefault(testing::Return(true)); |
| 160 | |
| 161 | base::ScopedTempDir scoped_mount_dir; |
| 162 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 163 | |
| 164 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 165 | scoped_mount_dir.path().value().c_str()); |
| 166 | ImageLoaderImpl loader(std::move(config)); |
| 167 | |
| 168 | const std::string expected_path = |
| 169 | scoped_mount_dir.path().value() + "/PepperFlashPlayer/22.0.0.158"; |
| 170 | const std::string mnt_path = loader.LoadComponentAtPath( |
| 171 | kTestComponentName, GetTestComponentPath(), helper_mock.get()); |
| 172 | EXPECT_EQ(expected_path, mnt_path); |
| 173 | } |
| 174 | |
Xiaochu Liu | 5e708b8 | 2017-11-13 13:59:12 -0800 | [diff] [blame^] | 175 | TEST_F(ImageLoaderTest, CleanupAll) { |
| 176 | Keys keys; |
| 177 | keys.push_back( |
| 178 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 179 | |
| 180 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
| 181 | EXPECT_CALL(*helper_mock, SendUnmountAllCommand(_, _, _)) |
| 182 | .Times(1); |
| 183 | ON_CALL(*helper_mock, SendUnmountAllCommand(_, _, _)) |
| 184 | .WillByDefault(testing::Return(true)); |
| 185 | |
| 186 | base::ScopedTempDir scoped_mount_dir; |
| 187 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 188 | |
| 189 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 190 | scoped_mount_dir.path().value().c_str()); |
| 191 | ImageLoaderImpl loader(std::move(config)); |
| 192 | |
| 193 | base::FilePath rootpath("/"); |
| 194 | std::vector<std::string> paths; |
| 195 | EXPECT_EQ(loader.CleanupAll(true, rootpath, &paths, helper_mock.get()), true); |
| 196 | } |
| 197 | |
| 198 | TEST_F(ImageLoaderTest, Cleanup) { |
| 199 | Keys keys; |
| 200 | keys.push_back( |
| 201 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 202 | |
| 203 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
| 204 | EXPECT_CALL(*helper_mock, SendUnmountCommand(_)).Times(1); |
| 205 | ON_CALL(*helper_mock, SendUnmountCommand(_)) |
| 206 | .WillByDefault(testing::Return(true)); |
| 207 | |
| 208 | base::ScopedTempDir scoped_mount_dir; |
| 209 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 210 | |
| 211 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 212 | scoped_mount_dir.path().value().c_str()); |
| 213 | ImageLoaderImpl loader(std::move(config)); |
| 214 | |
| 215 | base::FilePath path("/"); |
| 216 | EXPECT_EQ(loader.Cleanup(path, helper_mock.get()), true); |
| 217 | } |
| 218 | |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 219 | TEST_F(ImageLoaderTest, LoadExt4Image) { |
Greg Kerr | e870420 | 2017-07-27 12:54:31 -0700 | [diff] [blame] | 220 | Keys keys; |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 221 | keys.push_back( |
| 222 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
Greg Kerr | e870420 | 2017-07-27 12:54:31 -0700 | [diff] [blame] | 223 | |
Ben Chan | ea104dd | 2017-09-29 00:43:04 -0700 | [diff] [blame] | 224 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 225 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kExt4, _)) |
| 226 | .Times(1); |
| 227 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
| 228 | .WillByDefault(testing::Return(true)); |
| 229 | |
| 230 | base::ScopedTempDir scoped_mount_dir; |
| 231 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 232 | |
| 233 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 234 | scoped_mount_dir.path().value().c_str()); |
| 235 | ImageLoaderImpl loader(std::move(config)); |
| 236 | |
| 237 | const std::string expected_path = |
Xiaochu Liu | 1e5dc14 | 2017-10-11 17:33:33 -0700 | [diff] [blame] | 238 | scoped_mount_dir.path().value() + "/ext4/9824.0.4"; |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 239 | const std::string mnt_path = loader.LoadComponentAtPath( |
| 240 | "ext4", GetTestDataPath("ext4_component"), helper_mock.get()); |
| 241 | EXPECT_EQ(expected_path, mnt_path); |
| 242 | } |
| 243 | |
Xiaochu Liu | 7a224d9 | 2017-10-06 17:33:41 -0700 | [diff] [blame] | 244 | TEST_F(ImageLoaderTest, RemoveImageAtPathRemovable) { |
| 245 | Keys keys; |
| 246 | keys.push_back( |
| 247 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 248 | |
| 249 | base::ScopedTempDir scoped_mount_dir; |
| 250 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 251 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 252 | scoped_mount_dir.path().value().c_str()); |
| 253 | ImageLoaderImpl loader(std::move(config)); |
| 254 | |
| 255 | // Make a copy to avoid permanent loss of test data. |
| 256 | base::ScopedTempDir component_root; |
| 257 | ASSERT_TRUE(component_root.CreateUniqueTempDir()); |
| 258 | base::FilePath component_path = component_root.path().Append("9824.0.4"); |
| 259 | ASSERT_TRUE(base::CreateDirectory(component_path)); |
| 260 | std::unique_ptr<Component> component = |
| 261 | Component::Create(base::FilePath(GetTestDataPath("ext4_component")), |
| 262 | keys); |
| 263 | ASSERT_TRUE(component->CopyTo(component_path)); |
| 264 | |
| 265 | // Remove the component. |
| 266 | EXPECT_TRUE(loader.RemoveComponentAtPath( |
| 267 | "ext4", component_root.path(), component_path)); |
| 268 | EXPECT_FALSE(base::PathExists(component_root.path())); |
| 269 | } |
| 270 | |
| 271 | TEST_F(ImageLoaderTest, RemoveImageAtPathNotRemovable) { |
| 272 | Keys keys; |
| 273 | keys.push_back( |
| 274 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 275 | |
| 276 | base::ScopedTempDir scoped_mount_dir; |
| 277 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 278 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
| 279 | scoped_mount_dir.path().value().c_str()); |
| 280 | ImageLoaderImpl loader(std::move(config)); |
| 281 | |
| 282 | // Make a copy to avoid permanent loss of test data. |
| 283 | base::ScopedTempDir component_root; |
| 284 | ASSERT_TRUE(component_root.CreateUniqueTempDir()); |
| 285 | base::FilePath component_path = component_root.path().Append("9824.0.4"); |
| 286 | ASSERT_TRUE(base::CreateDirectory(component_path)); |
| 287 | std::unique_ptr<Component> component = |
| 288 | Component::Create(base::FilePath(GetTestComponentPath()), |
| 289 | keys); |
| 290 | ASSERT_TRUE(component->CopyTo(component_path)); |
| 291 | |
| 292 | // Remove the component. |
| 293 | EXPECT_FALSE(loader.RemoveComponentAtPath( |
| 294 | kTestComponentName, component_root.path(), component_path)); |
| 295 | EXPECT_TRUE(base::PathExists(component_root.path())); |
| 296 | } |
| 297 | |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 298 | TEST_F(ImageLoaderTest, MountInvalidImage) { |
| 299 | Keys keys; |
| 300 | keys.push_back( |
| 301 | std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey))); |
| 302 | |
Ben Chan | ea104dd | 2017-09-29 00:43:04 -0700 | [diff] [blame] | 303 | auto helper_mock = std::make_unique<MockHelperProcess>(); |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 304 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _)) |
| 305 | .Times(0); |
| 306 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 307 | .WillByDefault(testing::Return(true)); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 308 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 309 | base::ScopedTempDir scoped_mount_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 310 | ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir()); |
| 311 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 312 | ImageLoaderConfig config(keys, temp_dir_.value().c_str(), |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 313 | scoped_mount_dir.path().value().c_str()); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 314 | ImageLoaderImpl loader(std::move(config)); |
| 315 | |
| 316 | // We previously tested RegisterComponent, so assume this works if it reports |
| 317 | // true. |
| 318 | ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion, |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 319 | GetTestComponentPath().value())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 320 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 321 | base::FilePath table = temp_dir_.Append(kTestComponentName) |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 322 | .Append(kTestDataVersion) |
Greg Kerr | 30cd5fb | 2016-09-29 12:37:02 -0700 | [diff] [blame] | 323 | .Append("table"); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 324 | std::string contents = "corrupt"; |
| 325 | ASSERT_EQ(static_cast<int>(contents.size()), |
Greg Kerr | 30cd5fb | 2016-09-29 12:37:02 -0700 | [diff] [blame] | 326 | base::WriteFile(table, contents.data(), contents.size())); |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 327 | ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get())); |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Greg Kerr | 2f76fde | 2016-08-29 16:39:45 -0700 | [diff] [blame] | 330 | TEST_F(ImageLoaderTest, SetupTable) { |
| 331 | std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV " |
| 332 | "hashstart=40 alg=sha256 root_hexdigest=" |
| 333 | "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 " |
| 334 | "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2"; |
| 335 | |
| 336 | // Make sure excess newlines are rejected. |
| 337 | std::string bad_table = base_table + "\n\n"; |
| 338 | EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6")); |
| 339 | |
| 340 | // Make sure it does the right replacements on a simple base table. |
| 341 | std::string good_table = base_table; |
| 342 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6")); |
| 343 | |
| 344 | std::string known_good_table = |
| 345 | "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 " |
| 346 | "hashstart=40 alg=sha256 root_hexdigest=" |
| 347 | "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 " |
| 348 | "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 " |
| 349 | "error_behavior=eio"; |
| 350 | EXPECT_EQ(known_good_table, good_table); |
| 351 | |
| 352 | // Make sure the newline is stripped. |
| 353 | std::string good_table_newline = base_table + "\n"; |
| 354 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6")); |
| 355 | EXPECT_EQ(known_good_table, good_table_newline); |
| 356 | |
| 357 | // Make sure error_behavior isn't appended twice. |
| 358 | std::string good_table_error = base_table + " error_behavior=eio\n"; |
| 359 | EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6")); |
| 360 | EXPECT_EQ(known_good_table, good_table_error); |
| 361 | } |
| 362 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 363 | TEST_F(ImageLoaderTest, SecondKey) { |
| 364 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 365 | ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName, |
| 366 | kTestOciComponentVersion, |
| 367 | GetTestOciComponentPath().value())); |
| 368 | |
| 369 | base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName); |
| 370 | ASSERT_TRUE(base::DirectoryExists(comp_dir)); |
| 371 | |
| 372 | base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion); |
| 373 | ASSERT_TRUE(base::DirectoryExists(version_dir)); |
| 374 | } |
| 375 | |
Eric Caruso | 26a9144 | 2017-10-25 16:05:40 -0700 | [diff] [blame] | 376 | TEST_F(ImageLoaderTest, GetMetadata) { |
| 377 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 378 | ASSERT_TRUE(loader.RegisterComponent(kMetadataComponentName, |
| 379 | kTestOciComponentVersion, |
| 380 | GetMetadataComponentPath().value())); |
| 381 | |
| 382 | // We shouldn't need to load the component to get the metadata. |
| 383 | std::map<std::string, std::string> metadata; |
| 384 | ASSERT_TRUE(loader.GetComponentMetadata(kMetadataComponentName, &metadata)); |
| 385 | std::map<std::string, std::string> expected_metadata{ |
| 386 | {"foo", "bar"}, |
| 387 | {"baz", "quux"}, |
| 388 | }; |
| 389 | ASSERT_EQ(expected_metadata, metadata); |
| 390 | } |
| 391 | |
| 392 | TEST_F(ImageLoaderTest, GetEmptyMetadata) { |
| 393 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 394 | ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName, |
| 395 | kTestOciComponentVersion, |
| 396 | GetTestOciComponentPath().value())); |
| 397 | |
| 398 | // If there's no metadata, we should get nothing. |
| 399 | std::map<std::string, std::string> metadata; |
| 400 | ASSERT_TRUE(loader.GetComponentMetadata(kTestOciComponentName, &metadata)); |
| 401 | ASSERT_TRUE(metadata.empty()); |
| 402 | } |
| 403 | |
| 404 | TEST_F(ImageLoaderTest, MetadataFailure) { |
| 405 | ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str())); |
| 406 | // Metadata is optional, but malformed metadata should not be present in the |
| 407 | // manifest. If it is, fail to load the component. |
| 408 | ASSERT_FALSE(loader.RegisterComponent(kBadMetadataComponentName, |
| 409 | kTestOciComponentVersion, |
| 410 | GetBadMetadataComponentPath().value())); |
| 411 | |
| 412 | ASSERT_FALSE(loader.RegisterComponent( |
| 413 | kNonDictMetadataComponentName, |
| 414 | kTestOciComponentVersion, |
| 415 | GetNonDictMetadataComponentPath().value())); |
| 416 | |
| 417 | } |
| 418 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 419 | } // namespace imageloader |