blob: 8e6e45d275ef2e3776a1079eee33104b4b288ae8 [file] [log] [blame]
Greg Kerr3e750f42016-06-29 15:20:21 -07001// 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
Greg Kerra6c0c522016-07-25 11:15:31 -07005#include "imageloader_impl.h"
6
Greg Kerr6a5ee862016-10-19 11:32:43 -07007#include <stdint.h>
Greg Kerr3e750f42016-06-29 15:20:21 -07008
Greg Kerr3e750f42016-06-29 15:20:21 -07009#include <list>
Ben Chanea104dd2017-09-29 00:43:04 -070010#include <memory>
Greg Kerr019d59c2016-11-17 14:28:49 -080011#include <string>
Greg Kerr3e750f42016-06-29 15:20:21 -070012#include <vector>
13
Greg Kerr019d59c2016-11-17 14:28:49 -080014#include "component.h"
Greg Kerr9944e242017-01-26 15:09:31 -080015#include "mock_helper_process.h"
Greg Kerr019d59c2016-11-17 14:28:49 -080016#include "test_utilities.h"
Greg Kerr2f76fde2016-08-29 16:39:45 -070017#include "verity_mounter.h"
Greg Kerr3e750f42016-06-29 15:20:21 -070018
Greg Kerr3e750f42016-06-29 15:20:21 -070019#include <base/files/file_path.h>
20#include <base/files/file_util.h>
21#include <base/files/scoped_temp_dir.h>
Greg Kerr89be05f2016-07-27 10:40:32 -070022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
Greg Kerr3e750f42016-06-29 15:20:21 -070024
25namespace imageloader {
26
Greg Kerr89be05f2016-07-27 10:40:32 -070027using testing::_;
28
Greg Kerr3e750f42016-06-29 15:20:21 -070029class ImageLoaderTest : public testing::Test {
30 public:
Greg Kerr019d59c2016-11-17 14:28:49 -080031 ImageLoaderTest() {
32 CHECK(scoped_temp_dir_.CreateUniqueTempDir());
33 temp_dir_ = scoped_temp_dir_.path();
34 CHECK(base::SetPosixFilePermissions(temp_dir_, kComponentDirPerms));
35 }
36
Greg Kerra6c0c522016-07-25 11:15:31 -070037 ImageLoaderConfig GetConfig(const char* path) {
Eric Caruso0b79bc82017-03-21 13:44:34 -070038 Keys keys;
39 keys.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey),
40 std::end(kDevPublicKey)));
41 keys.push_back(std::vector<uint8_t>(std::begin(kOciDevPublicKey),
42 std::end(kOciDevPublicKey)));
43 ImageLoaderConfig config(keys, path, "/foo");
Greg Kerra6c0c522016-07-25 11:15:31 -070044 return config;
45 }
46
Greg Kerr019d59c2016-11-17 14:28:49 -080047 base::ScopedTempDir scoped_temp_dir_;
48 base::FilePath temp_dir_;
Greg Kerr3e750f42016-06-29 15:20:21 -070049};
50
Greg Kerra6c0c522016-07-25 11:15:31 -070051// Test the RegisterComponent public interface.
52TEST_F(ImageLoaderTest, RegisterComponentAndGetVersion) {
Greg Kerr019d59c2016-11-17 14:28:49 -080053 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
Greg Kerra6c0c522016-07-25 11:15:31 -070054 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -080055 GetTestComponentPath().value()));
Greg Kerra6c0c522016-07-25 11:15:31 -070056
Greg Kerr019d59c2016-11-17 14:28:49 -080057 base::FilePath comp_dir = temp_dir_.Append(kTestComponentName);
Greg Kerra6c0c522016-07-25 11:15:31 -070058 ASSERT_TRUE(base::DirectoryExists(comp_dir));
59
Greg Kerr89be05f2016-07-27 10:40:32 -070060 base::FilePath hint_file = comp_dir.Append("latest-version");
Greg Kerra6c0c522016-07-25 11:15:31 -070061 ASSERT_TRUE(base::PathExists(hint_file));
62
63 std::string hint_file_contents;
64 ASSERT_TRUE(
65 base::ReadFileToStringWithMaxSize(hint_file, &hint_file_contents, 4096));
66 EXPECT_EQ(kTestDataVersion, hint_file_contents);
67
68 base::FilePath version_dir = comp_dir.Append(kTestDataVersion);
69 ASSERT_TRUE(base::DirectoryExists(version_dir));
70
Greg Kerrf50e24a2017-01-06 17:12:32 -080071 // Make sure it actually checks the reported version against the real version.
72 EXPECT_FALSE(loader.RegisterComponent(kTestComponentName, kTestUpdatedVersion,
73 GetTestComponentPath().value()));
74
Greg Kerra6c0c522016-07-25 11:15:31 -070075 // Now copy a new version into place.
Greg Kerr89be05f2016-07-27 10:40:32 -070076 EXPECT_TRUE(
77 loader.RegisterComponent(kTestComponentName, kTestUpdatedVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -080078 GetTestComponentPath(kTestUpdatedVersion).value()));
Greg Kerra6c0c522016-07-25 11:15:31 -070079
80 std::string hint_file_contents2;
81 ASSERT_TRUE(
82 base::ReadFileToStringWithMaxSize(hint_file, &hint_file_contents2, 4096));
83 EXPECT_EQ(kTestUpdatedVersion, hint_file_contents2);
84
85 base::FilePath version_dir2 = comp_dir.Append(kTestUpdatedVersion);
86 ASSERT_TRUE(base::DirectoryExists(version_dir2));
87
Greg Kerr89be05f2016-07-27 10:40:32 -070088 EXPECT_EQ(kTestUpdatedVersion,
89 loader.GetComponentVersion(kTestComponentName));
Greg Kerra6c0c522016-07-25 11:15:31 -070090
91 // Reject rollback to an older version.
92 EXPECT_FALSE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -080093 GetTestComponentPath().value()));
Greg Kerra6c0c522016-07-25 11:15:31 -070094
Greg Kerr89be05f2016-07-27 10:40:32 -070095 EXPECT_EQ(kTestUpdatedVersion,
96 loader.GetComponentVersion(kTestComponentName));
Greg Kerra6c0c522016-07-25 11:15:31 -070097}
98
Greg Kerr1c7403c2016-11-11 11:57:44 -080099// Pretend ImageLoader crashed, by creating an incomplete installation, and then
100// attempt registration with ImageLoader.
101TEST_F(ImageLoaderTest, RegisterComponentAfterCrash) {
Greg Kerr1c7403c2016-11-11 11:57:44 -0800102 // Now create the junk there.
103 const std::string junk_contents ="Bad file contents";
104 const base::FilePath junk_path =
Greg Kerr019d59c2016-11-17 14:28:49 -0800105 temp_dir_.Append(kTestComponentName).Append(kTestDataVersion);
Greg Kerr1c7403c2016-11-11 11:57:44 -0800106 ASSERT_TRUE(base::CreateDirectory(junk_path));
107 ASSERT_EQ(static_cast<int>(junk_contents.size()),
108 base::WriteFile(junk_path.Append("junkfile"), junk_contents.data(),
109 junk_contents.size()));
Greg Kerr019d59c2016-11-17 14:28:49 -0800110 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
Greg Kerr1c7403c2016-11-11 11:57:44 -0800111 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800112 GetTestComponentPath().value()));
Greg Kerr4bd78132016-07-19 11:51:16 -0700113}
114
Greg Kerr89be05f2016-07-27 10:40:32 -0700115TEST_F(ImageLoaderTest, MountValidImage) {
Eric Caruso0b79bc82017-03-21 13:44:34 -0700116 Keys keys;
117 keys.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey),
118 std::end(kDevPublicKey)));
119
Ben Chanea104dd2017-09-29 00:43:04 -0700120 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700121 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _))
122 .Times(2);
123 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800124 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700125
Greg Kerr89be05f2016-07-27 10:40:32 -0700126 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700127 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
128
Eric Caruso0b79bc82017-03-21 13:44:34 -0700129 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Greg Kerr9944e242017-01-26 15:09:31 -0800130 scoped_mount_dir.path().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700131 ImageLoaderImpl loader(std::move(config));
132
133 // We previously tested RegisterComponent, so assume this works if it reports
134 // true.
135 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800136 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700137
138 const std::string expected_path =
139 scoped_mount_dir.path().value() + "/PepperFlashPlayer/22.0.0.158";
Greg Kerr9944e242017-01-26 15:09:31 -0800140 EXPECT_EQ(expected_path,
141 loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerrc5b91692016-09-14 12:09:22 -0700142
143 // Let's also test mounting the component at a fixed point.
144 const std::string expected_path2 =
145 scoped_mount_dir.path().value() + "/FixedMountPoint";
Greg Kerr9944e242017-01-26 15:09:31 -0800146 EXPECT_TRUE(loader.LoadComponent(kTestComponentName, expected_path2,
147 helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700148}
149
Greg Kerr772abab2017-06-16 14:51:01 -0700150TEST_F(ImageLoaderTest, LoadComponentAtPath) {
151 Keys keys;
152 keys.push_back(
153 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
154
Ben Chanea104dd2017-09-29 00:43:04 -0700155 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700156 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _))
157 .Times(1);
158 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr772abab2017-06-16 14:51:01 -0700159 .WillByDefault(testing::Return(true));
160
161 base::ScopedTempDir scoped_mount_dir;
162 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
163
164 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
165 scoped_mount_dir.path().value().c_str());
166 ImageLoaderImpl loader(std::move(config));
167
168 const std::string expected_path =
169 scoped_mount_dir.path().value() + "/PepperFlashPlayer/22.0.0.158";
170 const std::string mnt_path = loader.LoadComponentAtPath(
171 kTestComponentName, GetTestComponentPath(), helper_mock.get());
172 EXPECT_EQ(expected_path, mnt_path);
173}
174
Xiaochu Liuc2264342017-08-14 16:37:42 -0700175TEST_F(ImageLoaderTest, LoadExt4Image) {
Greg Kerre8704202017-07-27 12:54:31 -0700176 Keys keys;
Xiaochu Liuc2264342017-08-14 16:37:42 -0700177 keys.push_back(
178 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
Greg Kerre8704202017-07-27 12:54:31 -0700179
Ben Chanea104dd2017-09-29 00:43:04 -0700180 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700181 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kExt4, _))
182 .Times(1);
183 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
184 .WillByDefault(testing::Return(true));
185
186 base::ScopedTempDir scoped_mount_dir;
187 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
188
189 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
190 scoped_mount_dir.path().value().c_str());
191 ImageLoaderImpl loader(std::move(config));
192
193 const std::string expected_path =
194 scoped_mount_dir.path().value() + "/ext4/1.6.10.10";
195 const std::string mnt_path = loader.LoadComponentAtPath(
196 "ext4", GetTestDataPath("ext4_component"), helper_mock.get());
197 EXPECT_EQ(expected_path, mnt_path);
198}
199
200TEST_F(ImageLoaderTest, MountInvalidImage) {
201 Keys keys;
202 keys.push_back(
203 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
204
Ben Chanea104dd2017-09-29 00:43:04 -0700205 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700206 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _))
207 .Times(0);
208 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800209 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700210
Greg Kerr89be05f2016-07-27 10:40:32 -0700211 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700212 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
213
Eric Caruso0b79bc82017-03-21 13:44:34 -0700214 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Greg Kerr9944e242017-01-26 15:09:31 -0800215 scoped_mount_dir.path().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700216 ImageLoaderImpl loader(std::move(config));
217
218 // We previously tested RegisterComponent, so assume this works if it reports
219 // true.
220 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800221 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700222
Greg Kerr019d59c2016-11-17 14:28:49 -0800223 base::FilePath table = temp_dir_.Append(kTestComponentName)
Greg Kerr89be05f2016-07-27 10:40:32 -0700224 .Append(kTestDataVersion)
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700225 .Append("table");
Greg Kerr89be05f2016-07-27 10:40:32 -0700226 std::string contents = "corrupt";
227 ASSERT_EQ(static_cast<int>(contents.size()),
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700228 base::WriteFile(table, contents.data(), contents.size()));
Greg Kerr9944e242017-01-26 15:09:31 -0800229 ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700230}
231
Greg Kerr2f76fde2016-08-29 16:39:45 -0700232TEST_F(ImageLoaderTest, SetupTable) {
233 std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV "
234 "hashstart=40 alg=sha256 root_hexdigest="
235 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
236 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2";
237
238 // Make sure excess newlines are rejected.
239 std::string bad_table = base_table + "\n\n";
240 EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6"));
241
242 // Make sure it does the right replacements on a simple base table.
243 std::string good_table = base_table;
244 EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6"));
245
246 std::string known_good_table =
247 "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 "
248 "hashstart=40 alg=sha256 root_hexdigest="
249 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
250 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 "
251 "error_behavior=eio";
252 EXPECT_EQ(known_good_table, good_table);
253
254 // Make sure the newline is stripped.
255 std::string good_table_newline = base_table + "\n";
256 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6"));
257 EXPECT_EQ(known_good_table, good_table_newline);
258
259 // Make sure error_behavior isn't appended twice.
260 std::string good_table_error = base_table + " error_behavior=eio\n";
261 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6"));
262 EXPECT_EQ(known_good_table, good_table_error);
263}
264
Eric Caruso0b79bc82017-03-21 13:44:34 -0700265TEST_F(ImageLoaderTest, SecondKey) {
266 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
267 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
268 kTestOciComponentVersion,
269 GetTestOciComponentPath().value()));
270
271 base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName);
272 ASSERT_TRUE(base::DirectoryExists(comp_dir));
273
274 base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion);
275 ASSERT_TRUE(base::DirectoryExists(version_dir));
276}
277
Greg Kerra6c0c522016-07-25 11:15:31 -0700278} // namespace imageloader