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