Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [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/component.h" |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <list> |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 10 | #include <memory> |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
| 15 | #include <base/files/file_util.h> |
| 16 | #include <base/files/scoped_temp_dir.h> |
| 17 | #include <base/logging.h> |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 18 | #include <crypto/secure_hash.h> |
| 19 | #include <crypto/sha2.h> |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 23 | #include "imageloader/imageloader_impl.h" |
Amin Hassani | 17a185b | 2021-02-10 12:07:57 -0800 | [diff] [blame] | 24 | #include "imageloader/mock_global_context.h" |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 25 | #include "imageloader/mock_helper_process_proxy.h" |
Ben Chan | 045849f | 2017-12-18 17:27:07 -0800 | [diff] [blame] | 26 | #include "imageloader/test_utilities.h" |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 27 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 28 | namespace imageloader { |
| 29 | |
| 30 | using testing::_; |
| 31 | |
| 32 | class ComponentTest : public testing::Test { |
| 33 | public: |
| 34 | ComponentTest() { |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 35 | keys_.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey), |
| 36 | std::end(kDevPublicKey))); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 37 | CHECK(scoped_temp_dir_.CreateUniqueTempDir()); |
Eric Caruso | a5dfc94 | 2018-01-22 15:44:45 -0800 | [diff] [blame] | 38 | temp_dir_ = scoped_temp_dir_.GetPath(); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 39 | CHECK(base::SetPosixFilePermissions(temp_dir_, kComponentDirPerms)); |
| 40 | } |
| 41 | |
Amin Hassani | 17a185b | 2021-02-10 12:07:57 -0800 | [diff] [blame] | 42 | void SetUp() override { |
| 43 | g_ctx_.SetAsCurrent(); |
| 44 | ON_CALL(g_ctx_, IsOfficialBuild()).WillByDefault(testing::Return(true)); |
| 45 | } |
| 46 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 47 | bool TestCopyWithCorruptFile(const std::string& component_name, |
| 48 | const std::string& file_name) { |
| 49 | base::FilePath bad_component_dir = temp_dir_.Append(component_name); |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 50 | if (!base::CreateDirectory(bad_component_dir)) |
| 51 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 52 | if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms)) |
| 53 | return false; |
| 54 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 55 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 56 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 57 | if (!component || !component->CopyTo(bad_component_dir)) |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 58 | return false; |
| 59 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 60 | std::unique_ptr<Component> bad_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 61 | Component::Create(bad_component_dir, keys_); |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 62 | if (!bad_component) |
| 63 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 64 | |
| 65 | base::FilePath file = bad_component_dir.Append(file_name); |
| 66 | const char data[] = "c"; |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 67 | if (!base::AppendToFile(file, data, sizeof(data))) |
| 68 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 69 | |
| 70 | base::FilePath bad_component_dest = |
| 71 | temp_dir_.Append(component_name + "invalid"); |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 72 | if (!base::CreateDirectory(bad_component_dest)) |
| 73 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 74 | |
| 75 | if (!base::SetPosixFilePermissions(bad_component_dest, kComponentDirPerms)) |
| 76 | return false; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 77 | return bad_component->CopyTo(bad_component_dest) == false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool TestInitComponentWithCorruptFile(const std::string& component_name, |
| 81 | const std::string& file_name) { |
| 82 | base::FilePath bad_component_dir = temp_dir_.Append(component_name); |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 83 | if (!base::CreateDirectory(bad_component_dir)) |
| 84 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 85 | if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms)) |
| 86 | return false; |
| 87 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 88 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 89 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 90 | if (!component || !component->CopyTo(bad_component_dir)) |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 91 | return false; |
| 92 | |
| 93 | base::FilePath file = bad_component_dir.Append(file_name); |
| 94 | // Read the file out and change the last byte. |
| 95 | std::string file_contents; |
Colin Howes | ad6271a | 2018-11-21 15:36:05 -0800 | [diff] [blame] | 96 | if (!base::ReadFileToString(file, &file_contents)) |
| 97 | return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 98 | file_contents[file_contents.size() - 1] = |
| 99 | ~file_contents[file_contents.size() - 1]; |
| 100 | |
| 101 | if (!base::WriteFile(file, file_contents.data(), file_contents.size())) { |
| 102 | return false; |
| 103 | } |
| 104 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 105 | std::unique_ptr<Component> bad_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 106 | Component::Create(bad_component_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 107 | return bad_component == nullptr; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool CompareFileContents(const base::FilePath& src, |
| 111 | const base::FilePath& dest, |
| 112 | const std::list<std::string>& filenames) { |
| 113 | for (const std::string& name : filenames) { |
| 114 | std::string source_file_contents; |
| 115 | std::string dest_file_contents; |
| 116 | if (!base::ReadFileToString(src.Append(name), &source_file_contents)) |
| 117 | return false; |
| 118 | if (!base::ReadFileToString(dest.Append(name), &dest_file_contents)) |
| 119 | return false; |
| 120 | if (source_file_contents != dest_file_contents) { |
| 121 | LOG(ERROR) << "File contents does not match for file: " << name; |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 128 | Keys keys_; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 129 | base::ScopedTempDir scoped_temp_dir_; |
| 130 | base::FilePath temp_dir_; |
Amin Hassani | 17a185b | 2021-02-10 12:07:57 -0800 | [diff] [blame] | 131 | |
| 132 | MockGlobalContext g_ctx_; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | TEST_F(ComponentTest, InitComponentAndCheckManifest) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 136 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 137 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 138 | ASSERT_NE(nullptr, component); |
| 139 | |
Xiaochu Liu | c209aab | 2018-06-19 13:42:15 -0700 | [diff] [blame] | 140 | EXPECT_EQ(1, component->manifest().manifest_version()); |
| 141 | EXPECT_EQ(kTestDataVersion, component->manifest().version()); |
Greg Kerr | 04c1cee | 2020-10-15 14:08:44 +0000 | [diff] [blame] | 142 | // Don't hardcode the sha256 hashes, but run some validity checks. |
Xiaochu Liu | c209aab | 2018-06-19 13:42:15 -0700 | [diff] [blame] | 143 | EXPECT_EQ(crypto::kSHA256Length, component->manifest().image_sha256().size()); |
| 144 | EXPECT_EQ(crypto::kSHA256Length, component->manifest().table_sha256().size()); |
| 145 | EXPECT_NE(component->manifest().image_sha256(), |
| 146 | component->manifest().table_sha256()); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 149 | TEST_F(ComponentTest, TestCopyAndMountComponentExt4) { |
| 150 | std::unique_ptr<Component> component = |
| 151 | Component::Create(GetTestDataPath("ext4_component"), keys_); |
| 152 | ASSERT_NE(nullptr, component); |
| 153 | |
| 154 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 155 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 156 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 157 | |
| 158 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
| 159 | |
| 160 | std::unique_ptr<Component> copied_component = |
| 161 | Component::Create(copied_dir, keys_); |
| 162 | ASSERT_NE(nullptr, copied_component); |
| 163 | |
| 164 | const base::FilePath mount_dir = temp_dir_.Append("mount"); |
| 165 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 166 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 167 | |
| 168 | // Note: this fails to test the actual mounting process since it is just a |
| 169 | // mock here. The platform_ImageLoader autotest tests the real helper |
| 170 | // process running as a dbus service. |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 171 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | e61e1d6 | 2018-11-12 13:20:09 -0800 | [diff] [blame] | 172 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kExt4, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 173 | .Times(1); |
| 174 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
| 175 | .WillByDefault(testing::Return(true)); |
| 176 | ASSERT_TRUE(copied_component->Mount(helper_mock.get(), mount_dir)); |
| 177 | } |
| 178 | |
| 179 | TEST_F(ComponentTest, TestCopyAndMountComponentSquashfs) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 180 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 181 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 182 | ASSERT_NE(nullptr, component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 183 | |
| 184 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 185 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 186 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 187 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 188 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 189 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 190 | std::unique_ptr<Component> copied_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 191 | Component::Create(copied_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 192 | ASSERT_NE(nullptr, copied_component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 193 | |
| 194 | const base::FilePath mount_dir = temp_dir_.Append("mount"); |
| 195 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 196 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 197 | |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 198 | // Note: this fails to test the actual mounting process since it is just a |
| 199 | // mock here. The platform_ImageLoader autotest tests the real helper |
| 200 | // process running as a dbus service. |
Greg Kerr | 09f06de | 2018-02-16 15:32:07 -0800 | [diff] [blame] | 201 | auto helper_mock = std::make_unique<MockHelperProcessProxy>(); |
Xiaochu Liu | e61e1d6 | 2018-11-12 13:20:09 -0800 | [diff] [blame] | 202 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _)) |
Xiaochu Liu | c226434 | 2017-08-14 16:37:42 -0700 | [diff] [blame] | 203 | .Times(1); |
| 204 | ON_CALL(*helper_mock, SendMountCommand(_, _, _, _)) |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 205 | .WillByDefault(testing::Return(true)); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(copied_component->Mount(helper_mock.get(), mount_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(ComponentTest, CheckFilesAfterCopy) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 210 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 211 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 212 | ASSERT_NE(nullptr, component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 213 | |
| 214 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 215 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 216 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 217 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 218 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 219 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 220 | std::unique_ptr<Component> copied_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame] | 221 | Component::Create(copied_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 222 | ASSERT_NE(nullptr, copied_component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 223 | |
| 224 | // Check that all the files are present, except for the manifest.json which |
| 225 | // should be discarded. |
| 226 | std::list<std::string> original_files; |
| 227 | std::list<std::string> copied_files; |
| 228 | GetFilesInDir(GetTestComponentPath(), &original_files); |
| 229 | GetFilesInDir(copied_dir, &copied_files); |
| 230 | |
| 231 | EXPECT_THAT(original_files, |
| 232 | testing::UnorderedElementsAre( |
| 233 | "imageloader.json", "imageloader.sig.1", "manifest.json", |
| 234 | "table", "image.squash", "manifest.fingerprint")); |
| 235 | ASSERT_THAT(copied_files, |
| 236 | testing::UnorderedElementsAre( |
| 237 | "imageloader.json", "imageloader.sig.1", "table", |
| 238 | "image.squash", "manifest.fingerprint")); |
| 239 | EXPECT_TRUE( |
| 240 | CompareFileContents(GetTestComponentPath(), copied_dir, copied_files)); |
| 241 | } |
| 242 | |
Amin Hassani | 17a185b | 2021-02-10 12:07:57 -0800 | [diff] [blame] | 243 | TEST_F(ComponentTest, CheckNoSignatureComponentFail) { |
| 244 | EXPECT_FALSE(Component::Create(GetNoSignatureComponentPath(), keys_)); |
| 245 | } |
| 246 | |
| 247 | TEST_F(ComponentTest, CheckNoSignatureFilesAfterCopy) { |
| 248 | // Make non-official build. |
| 249 | EXPECT_CALL(g_ctx_, IsOfficialBuild()).WillRepeatedly(testing::Return(false)); |
| 250 | |
| 251 | base::FilePath component_path = GetNoSignatureComponentPath(); |
| 252 | std::unique_ptr<Component> component = |
| 253 | Component::Create(component_path, keys_); |
| 254 | ASSERT_TRUE(component); |
| 255 | |
| 256 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 257 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 258 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 259 | |
| 260 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
| 261 | |
| 262 | std::unique_ptr<Component> copied_component = |
| 263 | Component::Create(copied_dir, keys_); |
| 264 | ASSERT_NE(nullptr, copied_component); |
| 265 | |
| 266 | // Check that all the files are present. The signature file should just be |
| 267 | // ignored. |
| 268 | std::list<std::string> original_files; |
| 269 | std::list<std::string> copied_files; |
| 270 | GetFilesInDir(component_path, &original_files); |
| 271 | GetFilesInDir(copied_dir, &copied_files); |
| 272 | |
| 273 | EXPECT_THAT(original_files, |
| 274 | testing::UnorderedElementsAre("imageloader.json", "manifest.json", |
| 275 | "table", "image.squash")); |
| 276 | ASSERT_THAT(copied_files, testing::UnorderedElementsAre( |
| 277 | "imageloader.json", "table", "image.squash")); |
| 278 | EXPECT_TRUE(CompareFileContents(component_path, copied_dir, copied_files)); |
| 279 | } |
| 280 | |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 281 | TEST_F(ComponentTest, IsValidFingerprintFile) { |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 282 | const std::string valid_manifest = |
| 283 | "1.3464353b1ed78574e05f3ffe84b52582572b2fe7202f3824a3761e54ace8bb1"; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 284 | EXPECT_TRUE(Component::IsValidFingerprintFile(valid_manifest)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 285 | |
| 286 | const std::string invalid_unicode_manifest = "Ё Ђ Ѓ Є Ѕ І Ї Ј Љ "; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 287 | EXPECT_FALSE(Component::IsValidFingerprintFile(invalid_unicode_manifest)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 288 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 289 | EXPECT_FALSE(Component::IsValidFingerprintFile("\x49\x34\x19-43.*+abc")); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST_F(ComponentTest, InitComponentWithBadFiles) { |
| 293 | EXPECT_TRUE( |
| 294 | TestInitComponentWithCorruptFile("bad-component1", "imageloader.json")); |
| 295 | EXPECT_TRUE( |
| 296 | TestInitComponentWithCorruptFile("bad-component2", "imageloader.sig.1")); |
| 297 | } |
| 298 | |
| 299 | // Now corrupt the manifest of an already initialized component to verify that |
| 300 | // the copy operation fails. |
| 301 | TEST_F(ComponentTest, CopyWithBadFiles) { |
| 302 | EXPECT_TRUE(TestCopyWithCorruptFile("bad-component1", "image.squash")); |
| 303 | EXPECT_TRUE(TestCopyWithCorruptFile("bad-component2", "table")); |
| 304 | EXPECT_TRUE( |
| 305 | TestCopyWithCorruptFile("bad-component3", "manifest.fingerprint")); |
| 306 | } |
| 307 | |
| 308 | TEST_F(ComponentTest, CopyValidImage) { |
| 309 | const int image_size = 4096 * 4; |
| 310 | |
| 311 | base::FilePath image_path = temp_dir_.Append("image"); |
| 312 | std::vector<char> image(image_size, |
| 313 | 0xBB); // large enough to test streaming read. |
| 314 | ASSERT_EQ(image_size, |
| 315 | base::WriteFile(image_path, image.data(), image.size())); |
| 316 | |
| 317 | std::vector<uint8_t> hash(crypto::kSHA256Length); |
| 318 | |
| 319 | std::unique_ptr<crypto::SecureHash> sha256( |
| 320 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 321 | sha256->Update(image.data(), image.size()); |
| 322 | sha256->Finish(hash.data(), hash.size()); |
| 323 | |
Eric Caruso | 089bbff | 2017-03-21 11:34:15 -0700 | [diff] [blame] | 324 | Component component(GetTestComponentPath(), 1); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 325 | base::FilePath image_dest = temp_dir_.Append("image.copied"); |
| 326 | ASSERT_TRUE(component.CopyComponentFile(image_path, image_dest, hash)); |
| 327 | |
| 328 | // Check if the image file actually exists and has the correct contents. |
| 329 | std::string resulting_image; |
| 330 | ASSERT_TRUE(base::ReadFileToStringWithMaxSize(image_dest, &resulting_image, |
| 331 | image_size)); |
| 332 | |
Ben Chan | a92a9f0 | 2017-12-18 17:47:23 -0800 | [diff] [blame] | 333 | EXPECT_EQ(0, memcmp(image.data(), resulting_image.data(), image_size)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | } // namespace imageloader |