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 | |
| 5 | #include "component.h" |
| 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> |
| 18 | #include <base/memory/ptr_util.h> |
| 19 | #include <crypto/secure_hash.h> |
| 20 | #include <crypto/sha2.h> |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 24 | #include "imageloader_impl.h" |
| 25 | #include "mock_helper_process.h" |
| 26 | #include "test_utilities.h" |
| 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()); |
| 38 | temp_dir_ = scoped_temp_dir_.path(); |
| 39 | CHECK(base::SetPosixFilePermissions(temp_dir_, kComponentDirPerms)); |
| 40 | } |
| 41 | |
| 42 | bool TestCopyWithCorruptFile(const std::string& component_name, |
| 43 | const std::string& file_name) { |
| 44 | base::FilePath bad_component_dir = temp_dir_.Append(component_name); |
| 45 | if (!base::CreateDirectory(bad_component_dir)) return false; |
| 46 | if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms)) |
| 47 | return false; |
| 48 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 49 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 50 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 51 | if (!component || !component->CopyTo(bad_component_dir)) |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 52 | return false; |
| 53 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 54 | std::unique_ptr<Component> bad_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 55 | Component::Create(bad_component_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 56 | if (!bad_component) return false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 57 | |
| 58 | base::FilePath file = bad_component_dir.Append(file_name); |
| 59 | const char data[] = "c"; |
| 60 | if (!base::AppendToFile(file, data, sizeof(data))) return false; |
| 61 | |
| 62 | base::FilePath bad_component_dest = |
| 63 | temp_dir_.Append(component_name + "invalid"); |
| 64 | if (!base::CreateDirectory(bad_component_dest)) return false; |
| 65 | |
| 66 | if (!base::SetPosixFilePermissions(bad_component_dest, kComponentDirPerms)) |
| 67 | return false; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 68 | return bad_component->CopyTo(bad_component_dest) == false; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool TestInitComponentWithCorruptFile(const std::string& component_name, |
| 72 | const std::string& file_name) { |
| 73 | base::FilePath bad_component_dir = temp_dir_.Append(component_name); |
| 74 | if (!base::CreateDirectory(bad_component_dir)) return false; |
| 75 | if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms)) |
| 76 | return false; |
| 77 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 78 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 79 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 80 | if (!component || !component->CopyTo(bad_component_dir)) |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 81 | return false; |
| 82 | |
| 83 | base::FilePath file = bad_component_dir.Append(file_name); |
| 84 | // Read the file out and change the last byte. |
| 85 | std::string file_contents; |
| 86 | if (!base::ReadFileToString(file, &file_contents)) return false; |
| 87 | file_contents[file_contents.size() - 1] = |
| 88 | ~file_contents[file_contents.size() - 1]; |
| 89 | |
| 90 | if (!base::WriteFile(file, file_contents.data(), file_contents.size())) { |
| 91 | return false; |
| 92 | } |
| 93 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 94 | std::unique_ptr<Component> bad_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 95 | Component::Create(bad_component_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 96 | return bad_component == nullptr; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool CompareFileContents(const base::FilePath& src, |
| 100 | const base::FilePath& dest, |
| 101 | const std::list<std::string>& filenames) { |
| 102 | for (const std::string& name : filenames) { |
| 103 | std::string source_file_contents; |
| 104 | std::string dest_file_contents; |
| 105 | if (!base::ReadFileToString(src.Append(name), &source_file_contents)) |
| 106 | return false; |
| 107 | if (!base::ReadFileToString(dest.Append(name), &dest_file_contents)) |
| 108 | return false; |
| 109 | if (source_file_contents != dest_file_contents) { |
| 110 | LOG(ERROR) << "File contents does not match for file: " << name; |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 117 | Keys keys_; |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 118 | base::ScopedTempDir scoped_temp_dir_; |
| 119 | base::FilePath temp_dir_; |
| 120 | }; |
| 121 | |
| 122 | TEST_F(ComponentTest, InitComponentAndCheckManifest) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 123 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 124 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 125 | ASSERT_NE(nullptr, component); |
| 126 | |
| 127 | EXPECT_EQ(1, component->manifest().manifest_version); |
| 128 | EXPECT_EQ(kTestDataVersion, component->manifest().version); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 129 | // Don't hardcode the sha256 hashes, but run some sanity checks. |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 130 | EXPECT_EQ(crypto::kSHA256Length, component->manifest().image_sha256.size()); |
| 131 | EXPECT_EQ(crypto::kSHA256Length, component->manifest().table_sha256.size()); |
| 132 | EXPECT_NE(component->manifest().image_sha256, |
| 133 | component->manifest().table_sha256); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(ComponentTest, TestCopyAndMountComponent) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 137 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 138 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 139 | ASSERT_NE(nullptr, component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 140 | |
| 141 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 142 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 143 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 144 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 145 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 146 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 147 | std::unique_ptr<Component> copied_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 148 | Component::Create(copied_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 149 | ASSERT_NE(nullptr, copied_component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 150 | |
| 151 | const base::FilePath mount_dir = temp_dir_.Append("mount"); |
| 152 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 153 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 154 | |
Greg Kerr | 9944e24 | 2017-01-26 15:09:31 -0800 | [diff] [blame] | 155 | // Note: this fails to test the actual mounting process since it is just a |
| 156 | // mock here. The platform_ImageLoader autotest tests the real helper |
| 157 | // process running as a dbus service. |
| 158 | auto helper_mock = base::MakeUnique<MockHelperProcess>(); |
| 159 | EXPECT_CALL(*helper_mock, SendMountCommand(_, _, _)).Times(1); |
| 160 | ON_CALL(*helper_mock, SendMountCommand(_, _, _)) |
| 161 | .WillByDefault(testing::Return(true)); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 162 | ASSERT_TRUE(copied_component->Mount(helper_mock.get(), mount_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(ComponentTest, CheckFilesAfterCopy) { |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 166 | std::unique_ptr<Component> component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 167 | Component::Create(GetTestComponentPath(), keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 168 | ASSERT_NE(nullptr, component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 169 | |
| 170 | const base::FilePath copied_dir = temp_dir_.Append("dest"); |
| 171 | ASSERT_TRUE(base::CreateDirectory(copied_dir)); |
| 172 | ASSERT_TRUE(base::SetPosixFilePermissions(copied_dir, kComponentDirPerms)); |
| 173 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 174 | ASSERT_TRUE(component->CopyTo(copied_dir)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 175 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 176 | std::unique_ptr<Component> copied_component = |
Eric Caruso | 0b79bc8 | 2017-03-21 13:44:34 -0700 | [diff] [blame^] | 177 | Component::Create(copied_dir, keys_); |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 178 | ASSERT_NE(nullptr, copied_component); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 179 | |
| 180 | // Check that all the files are present, except for the manifest.json which |
| 181 | // should be discarded. |
| 182 | std::list<std::string> original_files; |
| 183 | std::list<std::string> copied_files; |
| 184 | GetFilesInDir(GetTestComponentPath(), &original_files); |
| 185 | GetFilesInDir(copied_dir, &copied_files); |
| 186 | |
| 187 | EXPECT_THAT(original_files, |
| 188 | testing::UnorderedElementsAre( |
| 189 | "imageloader.json", "imageloader.sig.1", "manifest.json", |
| 190 | "table", "image.squash", "manifest.fingerprint")); |
| 191 | ASSERT_THAT(copied_files, |
| 192 | testing::UnorderedElementsAre( |
| 193 | "imageloader.json", "imageloader.sig.1", "table", |
| 194 | "image.squash", "manifest.fingerprint")); |
| 195 | EXPECT_TRUE( |
| 196 | CompareFileContents(GetTestComponentPath(), copied_dir, copied_files)); |
| 197 | } |
| 198 | |
| 199 | TEST_F(ComponentTest, IsValidFingerprintFile) { |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 200 | const std::string valid_manifest = |
| 201 | "1.3464353b1ed78574e05f3ffe84b52582572b2fe7202f3824a3761e54ace8bb1"; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 202 | EXPECT_TRUE(Component::IsValidFingerprintFile(valid_manifest)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 203 | |
| 204 | const std::string invalid_unicode_manifest = "Ё Ђ Ѓ Є Ѕ І Ї Ј Љ "; |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 205 | EXPECT_FALSE(Component::IsValidFingerprintFile(invalid_unicode_manifest)); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 206 | |
Eric Caruso | cbe1c5c | 2017-03-15 14:21:08 -0700 | [diff] [blame] | 207 | EXPECT_FALSE(Component::IsValidFingerprintFile("\x49\x34\x19-43.*+abc")); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | TEST_F(ComponentTest, InitComponentWithBadFiles) { |
| 211 | EXPECT_TRUE( |
| 212 | TestInitComponentWithCorruptFile("bad-component1", "imageloader.json")); |
| 213 | EXPECT_TRUE( |
| 214 | TestInitComponentWithCorruptFile("bad-component2", "imageloader.sig.1")); |
| 215 | } |
| 216 | |
| 217 | // Now corrupt the manifest of an already initialized component to verify that |
| 218 | // the copy operation fails. |
| 219 | TEST_F(ComponentTest, CopyWithBadFiles) { |
| 220 | EXPECT_TRUE(TestCopyWithCorruptFile("bad-component1", "image.squash")); |
| 221 | EXPECT_TRUE(TestCopyWithCorruptFile("bad-component2", "table")); |
| 222 | EXPECT_TRUE( |
| 223 | TestCopyWithCorruptFile("bad-component3", "manifest.fingerprint")); |
| 224 | } |
| 225 | |
| 226 | TEST_F(ComponentTest, CopyValidImage) { |
| 227 | const int image_size = 4096 * 4; |
| 228 | |
| 229 | base::FilePath image_path = temp_dir_.Append("image"); |
| 230 | std::vector<char> image(image_size, |
| 231 | 0xBB); // large enough to test streaming read. |
| 232 | ASSERT_EQ(image_size, |
| 233 | base::WriteFile(image_path, image.data(), image.size())); |
| 234 | |
| 235 | std::vector<uint8_t> hash(crypto::kSHA256Length); |
| 236 | |
| 237 | std::unique_ptr<crypto::SecureHash> sha256( |
| 238 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 239 | sha256->Update(image.data(), image.size()); |
| 240 | sha256->Finish(hash.data(), hash.size()); |
| 241 | |
Eric Caruso | 089bbff | 2017-03-21 11:34:15 -0700 | [diff] [blame] | 242 | Component component(GetTestComponentPath(), 1); |
Greg Kerr | 019d59c | 2016-11-17 14:28:49 -0800 | [diff] [blame] | 243 | base::FilePath image_dest = temp_dir_.Append("image.copied"); |
| 244 | ASSERT_TRUE(component.CopyComponentFile(image_path, image_dest, hash)); |
| 245 | |
| 246 | // Check if the image file actually exists and has the correct contents. |
| 247 | std::string resulting_image; |
| 248 | ASSERT_TRUE(base::ReadFileToStringWithMaxSize(image_dest, &resulting_image, |
| 249 | image_size)); |
| 250 | |
| 251 | EXPECT_TRUE(memcmp(image.data(), resulting_image.data(), image_size) == 0); |
| 252 | } |
| 253 | |
| 254 | } // namespace imageloader |