blob: 16e945c682d76b58f981aff2b2576854f1bb7670 [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
5#include "component.h"
6
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 Kerr9944e242017-01-26 15:09:31 -080014#include "mock_helper_process.h"
Greg Kerr019d59c2016-11-17 14:28:49 -080015#include "test_utilities.h"
Greg Kerr019d59c2016-11-17 14:28:49 -080016
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
27namespace imageloader {
28
29using testing::_;
30
31class 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 Carusocbe1c5c2017-03-15 14:21:08 -070048 std::unique_ptr<Component> component =
49 Component::Create(GetTestComponentPath(), key_);
50 if (!component || !component->CopyTo(bad_component_dir))
Greg Kerr019d59c2016-11-17 14:28:49 -080051 return false;
52
Eric Carusocbe1c5c2017-03-15 14:21:08 -070053 std::unique_ptr<Component> bad_component =
54 Component::Create(bad_component_dir, key_);
55 if (!bad_component) return false;
Greg Kerr019d59c2016-11-17 14:28:49 -080056
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 Carusocbe1c5c2017-03-15 14:21:08 -070067 return bad_component->CopyTo(bad_component_dest) == false;
Greg Kerr019d59c2016-11-17 14:28:49 -080068 }
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 Carusocbe1c5c2017-03-15 14:21:08 -070077 std::unique_ptr<Component> component =
78 Component::Create(GetTestComponentPath(), key_);
79 if (!component || !component->CopyTo(bad_component_dir))
Greg Kerr019d59c2016-11-17 14:28:49 -080080 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 Carusocbe1c5c2017-03-15 14:21:08 -070093 std::unique_ptr<Component> bad_component =
94 Component::Create(bad_component_dir, key_);
95 return bad_component == nullptr;
Greg Kerr019d59c2016-11-17 14:28:49 -080096 }
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
121TEST_F(ComponentTest, InitComponentAndCheckManifest) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700122 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 Kerr019d59c2016-11-17 14:28:49 -0800128 // Don't hardcode the sha256 hashes, but run some sanity checks.
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700129 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 Kerr019d59c2016-11-17 14:28:49 -0800133}
134
135TEST_F(ComponentTest, TestCopyAndMountComponent) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700136 std::unique_ptr<Component> component =
137 Component::Create(GetTestComponentPath(), key_);
138 ASSERT_NE(nullptr, component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800139
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 Carusocbe1c5c2017-03-15 14:21:08 -0700144 ASSERT_TRUE(component->CopyTo(copied_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800145
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700146 std::unique_ptr<Component> copied_component =
147 Component::Create(copied_dir, key_);
148 ASSERT_NE(nullptr, copied_component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800149
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 Kerr9944e242017-01-26 15:09:31 -0800154 // 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 Carusocbe1c5c2017-03-15 14:21:08 -0700161 ASSERT_TRUE(copied_component->Mount(helper_mock.get(), mount_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800162}
163
164TEST_F(ComponentTest, CheckFilesAfterCopy) {
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700165 std::unique_ptr<Component> component =
166 Component::Create(GetTestComponentPath(), key_);
167 ASSERT_NE(nullptr, component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800168
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 Carusocbe1c5c2017-03-15 14:21:08 -0700173 ASSERT_TRUE(component->CopyTo(copied_dir));
Greg Kerr019d59c2016-11-17 14:28:49 -0800174
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700175 std::unique_ptr<Component> copied_component =
176 Component::Create(copied_dir, key_);
177 ASSERT_NE(nullptr, copied_component);
Greg Kerr019d59c2016-11-17 14:28:49 -0800178
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
198TEST_F(ComponentTest, IsValidFingerprintFile) {
Greg Kerr019d59c2016-11-17 14:28:49 -0800199 const std::string valid_manifest =
200 "1.3464353b1ed78574e05f3ffe84b52582572b2fe7202f3824a3761e54ace8bb1";
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700201 EXPECT_TRUE(Component::IsValidFingerprintFile(valid_manifest));
Greg Kerr019d59c2016-11-17 14:28:49 -0800202
203 const std::string invalid_unicode_manifest = "Ё Ђ Ѓ Є Ѕ І Ї Ј Љ ";
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700204 EXPECT_FALSE(Component::IsValidFingerprintFile(invalid_unicode_manifest));
Greg Kerr019d59c2016-11-17 14:28:49 -0800205
Eric Carusocbe1c5c2017-03-15 14:21:08 -0700206 EXPECT_FALSE(Component::IsValidFingerprintFile("\x49\x34\x19-43.*+abc"));
Greg Kerr019d59c2016-11-17 14:28:49 -0800207}
208
209TEST_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.
218TEST_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
225TEST_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