blob: eeae540950848f976c86d5116cbe5929937d7e75 [file] [log] [blame]
Greg Kerr019d59c2016-11-17 14:28:49 -08001// 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 Chan045849f2017-12-18 17:27:07 -08005#include "imageloader/component.h"
Greg Kerr019d59c2016-11-17 14:28:49 -08006
7#include <stdint.h>
8
9#include <list>
Eric Carusocbe1c5c2017-03-15 14:21:08 -070010#include <memory>
Greg Kerr019d59c2016-11-17 14:28:49 -080011#include <string>
12#include <vector>
13
Greg Kerr019d59c2016-11-17 14:28:49 -080014#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 Kerr019d59c2016-11-17 14:28:49 -080018#include <crypto/secure_hash.h>
19#include <crypto/sha2.h>
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22
Ben Chan045849f2017-12-18 17:27:07 -080023#include "imageloader/imageloader_impl.h"
Amin Hassani17a185b2021-02-10 12:07:57 -080024#include "imageloader/mock_global_context.h"
Greg Kerr09f06de2018-02-16 15:32:07 -080025#include "imageloader/mock_helper_process_proxy.h"
Ben Chan045849f2017-12-18 17:27:07 -080026#include "imageloader/test_utilities.h"
Eric Caruso0b79bc82017-03-21 13:44:34 -070027
Greg Kerr019d59c2016-11-17 14:28:49 -080028namespace imageloader {
29
30using testing::_;
31
32class ComponentTest : public testing::Test {
33 public:
34 ComponentTest() {
Eric Caruso0b79bc82017-03-21 13:44:34 -070035 keys_.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey),
36 std::end(kDevPublicKey)));
Greg Kerr019d59c2016-11-17 14:28:49 -080037 CHECK(scoped_temp_dir_.CreateUniqueTempDir());
Eric Carusoa5dfc942018-01-22 15:44:45 -080038 temp_dir_ = scoped_temp_dir_.GetPath();
Greg Kerr019d59c2016-11-17 14:28:49 -080039 CHECK(base::SetPosixFilePermissions(temp_dir_, kComponentDirPerms));
40 }
41
Amin Hassani17a185b2021-02-10 12:07:57 -080042 void SetUp() override {
43 g_ctx_.SetAsCurrent();
44 ON_CALL(g_ctx_, IsOfficialBuild()).WillByDefault(testing::Return(true));
45 }
46
Greg Kerr019d59c2016-11-17 14:28:49 -080047 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 Howesad6271a2018-11-21 15:36:05 -080050 if (!base::CreateDirectory(bad_component_dir))
51 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080052 if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms))
53 return false;
54
Eric Carusocbe1c5c2017-03-15 14:21:08 -070055 std::unique_ptr<Component> component =
Eric Caruso0b79bc82017-03-21 13:44:34 -070056 Component::Create(GetTestComponentPath(), keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -070057 if (!component || !component->CopyTo(bad_component_dir))
Greg Kerr019d59c2016-11-17 14:28:49 -080058 return false;
59
Eric Carusocbe1c5c2017-03-15 14:21:08 -070060 std::unique_ptr<Component> bad_component =
Eric Caruso0b79bc82017-03-21 13:44:34 -070061 Component::Create(bad_component_dir, keys_);
Colin Howesad6271a2018-11-21 15:36:05 -080062 if (!bad_component)
63 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080064
65 base::FilePath file = bad_component_dir.Append(file_name);
66 const char data[] = "c";
Colin Howesad6271a2018-11-21 15:36:05 -080067 if (!base::AppendToFile(file, data, sizeof(data)))
68 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080069
70 base::FilePath bad_component_dest =
71 temp_dir_.Append(component_name + "invalid");
Colin Howesad6271a2018-11-21 15:36:05 -080072 if (!base::CreateDirectory(bad_component_dest))
73 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080074
75 if (!base::SetPosixFilePermissions(bad_component_dest, kComponentDirPerms))
76 return false;
Eric Carusocbe1c5c2017-03-15 14:21:08 -070077 return bad_component->CopyTo(bad_component_dest) == false;
Greg Kerr019d59c2016-11-17 14:28:49 -080078 }
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 Howesad6271a2018-11-21 15:36:05 -080083 if (!base::CreateDirectory(bad_component_dir))
84 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080085 if (!base::SetPosixFilePermissions(bad_component_dir, kComponentDirPerms))
86 return false;
87
Eric Carusocbe1c5c2017-03-15 14:21:08 -070088 std::unique_ptr<Component> component =
Eric Caruso0b79bc82017-03-21 13:44:34 -070089 Component::Create(GetTestComponentPath(), keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -070090 if (!component || !component->CopyTo(bad_component_dir))
Greg Kerr019d59c2016-11-17 14:28:49 -080091 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 Howesad6271a2018-11-21 15:36:05 -080096 if (!base::ReadFileToString(file, &file_contents))
97 return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080098 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 Carusocbe1c5c2017-03-15 14:21:08 -0700105 std::unique_ptr<Component> bad_component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700106 Component::Create(bad_component_dir, keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700107 return bad_component == nullptr;
Greg Kerr019d59c2016-11-17 14:28:49 -0800108 }
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 Caruso0b79bc82017-03-21 13:44:34 -0700128 Keys keys_;
Greg Kerr019d59c2016-11-17 14:28:49 -0800129 base::ScopedTempDir scoped_temp_dir_;
130 base::FilePath temp_dir_;
Amin Hassani17a185b2021-02-10 12:07:57 -0800131
132 MockGlobalContext g_ctx_;
Greg Kerr019d59c2016-11-17 14:28:49 -0800133};
134
135TEST_F(ComponentTest, InitComponentAndCheckManifest) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700136 std::unique_ptr<Component> component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700137 Component::Create(GetTestComponentPath(), keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700138 ASSERT_NE(nullptr, component);
139
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700140 EXPECT_EQ(1, component->manifest().manifest_version());
141 EXPECT_EQ(kTestDataVersion, component->manifest().version());
Greg Kerr04c1cee2020-10-15 14:08:44 +0000142 // Don't hardcode the sha256 hashes, but run some validity checks.
Xiaochu Liuc209aab2018-06-19 13:42:15 -0700143 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 Kerr019d59c2016-11-17 14:28:49 -0800147}
148
Xiaochu Liuc2264342017-08-14 16:37:42 -0700149TEST_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 Kerr09f06de2018-02-16 15:32:07 -0800171 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liue61e1d62018-11-12 13:20:09 -0800172 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kExt4, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700173 .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
179TEST_F(ComponentTest, TestCopyAndMountComponentSquashfs) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700180 std::unique_ptr<Component> component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700181 Component::Create(GetTestComponentPath(), keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700182 ASSERT_NE(nullptr, component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800183
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 Carusocbe1c5c2017-03-15 14:21:08 -0700188 ASSERT_TRUE(component->CopyTo(copied_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800189
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700190 std::unique_ptr<Component> copied_component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700191 Component::Create(copied_dir, keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700192 ASSERT_NE(nullptr, copied_component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800193
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 Kerr9944e242017-01-26 15:09:31 -0800198 // 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 Kerr09f06de2018-02-16 15:32:07 -0800201 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liue61e1d62018-11-12 13:20:09 -0800202 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700203 .Times(1);
204 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800205 .WillByDefault(testing::Return(true));
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700206 ASSERT_TRUE(copied_component->Mount(helper_mock.get(), mount_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800207}
208
209TEST_F(ComponentTest, CheckFilesAfterCopy) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700210 std::unique_ptr<Component> component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700211 Component::Create(GetTestComponentPath(), keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700212 ASSERT_NE(nullptr, component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800213
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 Carusocbe1c5c2017-03-15 14:21:08 -0700218 ASSERT_TRUE(component->CopyTo(copied_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800219
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700220 std::unique_ptr<Component> copied_component =
Eric Caruso0b79bc82017-03-21 13:44:34 -0700221 Component::Create(copied_dir, keys_);
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700222 ASSERT_NE(nullptr, copied_component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800223
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 Hassani17a185b2021-02-10 12:07:57 -0800243TEST_F(ComponentTest, CheckNoSignatureComponentFail) {
244 EXPECT_FALSE(Component::Create(GetNoSignatureComponentPath(), keys_));
245}
246
247TEST_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 Kerr019d59c2016-11-17 14:28:49 -0800281TEST_F(ComponentTest, IsValidFingerprintFile) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800282 const std::string valid_manifest =
283 "1.3464353b1ed78574e05f3ffe84b52582572b2fe7202f3824a3761e54ace8bb1";
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700284 EXPECT_TRUE(Component::IsValidFingerprintFile(valid_manifest));
Greg Kerr019d59c2016-11-17 14:28:49 -0800285
286 const std::string invalid_unicode_manifest = "Ё Ђ Ѓ Є Ѕ І Ї Ј Љ ";
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700287 EXPECT_FALSE(Component::IsValidFingerprintFile(invalid_unicode_manifest));
Greg Kerr019d59c2016-11-17 14:28:49 -0800288
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700289 EXPECT_FALSE(Component::IsValidFingerprintFile("\x49\x34\x19-43.*+abc"));
Greg Kerr019d59c2016-11-17 14:28:49 -0800290}
291
292TEST_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.
301TEST_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
308TEST_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 Caruso089bbff2017-03-21 11:34:15 -0700324 Component component(GetTestComponentPath(), 1);
Greg Kerr019d59c2016-11-17 14:28:49 -0800325 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 Chana92a9f02017-12-18 17:47:23 -0800333 EXPECT_EQ(0, memcmp(image.data(), resulting_image.data(), image_size));
Greg Kerr019d59c2016-11-17 14:28:49 -0800334}
335
336} // namespace imageloader