blob: 9d27f0eb40abf904a0df6956a01a14aa62442caf [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
Ben Chan045849f2017-12-18 17:27:07 -08005#include "imageloader/imageloader_impl.h"
Greg Kerra6c0c522016-07-25 11:15:31 -07006
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 Kerr3e750f42016-06-29 15:20:21 -070014#include <base/files/file_path.h>
15#include <base/files/file_util.h>
16#include <base/files/scoped_temp_dir.h>
Greg Kerr89be05f2016-07-27 10:40:32 -070017#include <gmock/gmock.h>
18#include <gtest/gtest.h>
Greg Kerr3e750f42016-06-29 15:20:21 -070019
Ben Chan045849f2017-12-18 17:27:07 -080020#include "imageloader/component.h"
Greg Kerr09f06de2018-02-16 15:32:07 -080021#include "imageloader/mock_helper_process_proxy.h"
Ben Chan045849f2017-12-18 17:27:07 -080022#include "imageloader/test_utilities.h"
23#include "imageloader/verity_mounter.h"
24
Greg Kerr3e750f42016-06-29 15:20:21 -070025namespace 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());
Eric Carusoa5dfc942018-01-22 15:44:45 -080033 temp_dir_ = scoped_temp_dir_.GetPath();
Greg Kerr019d59c2016-11-17 14:28:49 -080034 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.
Ben Chana92a9f02017-12-18 17:47:23 -080076 EXPECT_TRUE(loader.RegisterComponent(
77 kTestComponentName, kTestUpdatedVersion,
78 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
Greg Kerr09f06de2018-02-16 15:32:07 -0800120 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu646339e2018-07-23 17:41:36 -0700121 EXPECT_CALL(
122 *helper_mock,
123 SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700124 .Times(2);
125 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800126 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700127
Greg Kerr89be05f2016-07-27 10:40:32 -0700128 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700129 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
130
Eric Caruso0b79bc82017-03-21 13:44:34 -0700131 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800132 scoped_mount_dir.GetPath().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700133 ImageLoaderImpl loader(std::move(config));
134
135 // We previously tested RegisterComponent, so assume this works if it reports
136 // true.
137 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800138 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700139
140 const std::string expected_path =
Eric Carusoa5dfc942018-01-22 15:44:45 -0800141 scoped_mount_dir.GetPath().value() + "/PepperFlashPlayer/22.0.0.158";
Greg Kerr9944e242017-01-26 15:09:31 -0800142 EXPECT_EQ(expected_path,
143 loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerrc5b91692016-09-14 12:09:22 -0700144
145 // Let's also test mounting the component at a fixed point.
146 const std::string expected_path2 =
Eric Carusoa5dfc942018-01-22 15:44:45 -0800147 scoped_mount_dir.GetPath().value() + "/FixedMountPoint";
Greg Kerr9944e242017-01-26 15:09:31 -0800148 EXPECT_TRUE(loader.LoadComponent(kTestComponentName, expected_path2,
149 helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700150}
151
Greg Kerr772abab2017-06-16 14:51:01 -0700152TEST_F(ImageLoaderTest, LoadComponentAtPath) {
153 Keys keys;
154 keys.push_back(
155 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
156
Greg Kerr09f06de2018-02-16 15:32:07 -0800157 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu646339e2018-07-23 17:41:36 -0700158 EXPECT_CALL(
159 *helper_mock,
160 SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700161 .Times(1);
162 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr772abab2017-06-16 14:51:01 -0700163 .WillByDefault(testing::Return(true));
164
165 base::ScopedTempDir scoped_mount_dir;
166 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
167
168 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800169 scoped_mount_dir.GetPath().value().c_str());
Greg Kerr772abab2017-06-16 14:51:01 -0700170 ImageLoaderImpl loader(std::move(config));
171
172 const std::string expected_path =
Eric Carusoa5dfc942018-01-22 15:44:45 -0800173 scoped_mount_dir.GetPath().value() + "/PepperFlashPlayer/22.0.0.158";
Greg Kerr772abab2017-06-16 14:51:01 -0700174 const std::string mnt_path = loader.LoadComponentAtPath(
175 kTestComponentName, GetTestComponentPath(), helper_mock.get());
176 EXPECT_EQ(expected_path, mnt_path);
177}
178
Xiaochu Liu5e708b82017-11-13 13:59:12 -0800179TEST_F(ImageLoaderTest, CleanupAll) {
180 Keys keys;
181 keys.push_back(
182 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
183
Greg Kerr09f06de2018-02-16 15:32:07 -0800184 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu5e708b82017-11-13 13:59:12 -0800185 EXPECT_CALL(*helper_mock, SendUnmountAllCommand(_, _, _))
186 .Times(1);
187 ON_CALL(*helper_mock, SendUnmountAllCommand(_, _, _))
188 .WillByDefault(testing::Return(true));
189
190 base::ScopedTempDir scoped_mount_dir;
191 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
192
193 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800194 scoped_mount_dir.GetPath().value().c_str());
Xiaochu Liu5e708b82017-11-13 13:59:12 -0800195 ImageLoaderImpl loader(std::move(config));
196
197 base::FilePath rootpath("/");
198 std::vector<std::string> paths;
199 EXPECT_EQ(loader.CleanupAll(true, rootpath, &paths, helper_mock.get()), true);
200}
201
202TEST_F(ImageLoaderTest, Cleanup) {
203 Keys keys;
204 keys.push_back(
205 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
206
Greg Kerr09f06de2018-02-16 15:32:07 -0800207 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu5e708b82017-11-13 13:59:12 -0800208 EXPECT_CALL(*helper_mock, SendUnmountCommand(_)).Times(1);
209 ON_CALL(*helper_mock, SendUnmountCommand(_))
210 .WillByDefault(testing::Return(true));
211
212 base::ScopedTempDir scoped_mount_dir;
213 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
214
215 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800216 scoped_mount_dir.GetPath().value().c_str());
Xiaochu Liu5e708b82017-11-13 13:59:12 -0800217 ImageLoaderImpl loader(std::move(config));
218
219 base::FilePath path("/");
220 EXPECT_EQ(loader.Cleanup(path, helper_mock.get()), true);
221}
222
Xiaochu Liuc2264342017-08-14 16:37:42 -0700223TEST_F(ImageLoaderTest, LoadExt4Image) {
Greg Kerre8704202017-07-27 12:54:31 -0700224 Keys keys;
Xiaochu Liuc2264342017-08-14 16:37:42 -0700225 keys.push_back(
226 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
Greg Kerre8704202017-07-27 12:54:31 -0700227
Greg Kerr09f06de2018-02-16 15:32:07 -0800228 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu646339e2018-07-23 17:41:36 -0700229 EXPECT_CALL(*helper_mock,
230 SendMountCommand(_, _, brillo::imageloader::FileSystem::kExt4, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700231 .Times(1);
232 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
233 .WillByDefault(testing::Return(true));
234
235 base::ScopedTempDir scoped_mount_dir;
236 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
237
238 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800239 scoped_mount_dir.GetPath().value().c_str());
Xiaochu Liuc2264342017-08-14 16:37:42 -0700240 ImageLoaderImpl loader(std::move(config));
241
242 const std::string expected_path =
Eric Carusoa5dfc942018-01-22 15:44:45 -0800243 scoped_mount_dir.GetPath().value() + "/ext4/9824.0.4";
Xiaochu Liuc2264342017-08-14 16:37:42 -0700244 const std::string mnt_path = loader.LoadComponentAtPath(
245 "ext4", GetTestDataPath("ext4_component"), helper_mock.get());
246 EXPECT_EQ(expected_path, mnt_path);
247}
248
Xiaochu Liuf6106e52018-08-10 13:09:00 -0700249TEST_F(ImageLoaderTest, UnloadDlcImage) {
250 Keys keys;
251 keys.push_back(
252 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
253
254 base::ScopedTempDir scoped_mount_dir;
255 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
256
257 const std::string dlc_id = "dlc_id";
258 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
259 EXPECT_CALL(*helper_mock,
260 SendUnmountCommand(
261 scoped_mount_dir.GetPath().Append(dlc_id).value().c_str()))
262 .Times(1);
263 ON_CALL(*helper_mock, SendUnmountCommand(_))
264 .WillByDefault(testing::Return(true));
265
266 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
267 scoped_mount_dir.GetPath().value().c_str());
268 ImageLoaderImpl loader(std::move(config));
269
270 loader.UnloadDlcImage(dlc_id, helper_mock.get());
271}
272
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700273TEST_F(ImageLoaderTest, RemoveImageAtPathRemovable) {
274 Keys keys;
275 keys.push_back(
276 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
277
278 base::ScopedTempDir scoped_mount_dir;
279 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
280 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800281 scoped_mount_dir.GetPath().value().c_str());
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700282 ImageLoaderImpl loader(std::move(config));
283
284 // Make a copy to avoid permanent loss of test data.
285 base::ScopedTempDir component_root;
286 ASSERT_TRUE(component_root.CreateUniqueTempDir());
Eric Carusoa5dfc942018-01-22 15:44:45 -0800287 base::FilePath component_path = component_root.GetPath().Append("9824.0.4");
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700288 ASSERT_TRUE(base::CreateDirectory(component_path));
289 std::unique_ptr<Component> component =
290 Component::Create(base::FilePath(GetTestDataPath("ext4_component")),
291 keys);
292 ASSERT_TRUE(component->CopyTo(component_path));
293
294 // Remove the component.
Eric Carusoa5dfc942018-01-22 15:44:45 -0800295 EXPECT_TRUE(loader.RemoveComponentAtPath("ext4", component_root.GetPath(),
296 component_path));
297 EXPECT_FALSE(base::PathExists(component_root.GetPath()));
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700298}
299
300TEST_F(ImageLoaderTest, RemoveImageAtPathNotRemovable) {
301 Keys keys;
302 keys.push_back(
303 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
304
305 base::ScopedTempDir scoped_mount_dir;
306 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
307 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800308 scoped_mount_dir.GetPath().value().c_str());
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700309 ImageLoaderImpl loader(std::move(config));
310
311 // Make a copy to avoid permanent loss of test data.
312 base::ScopedTempDir component_root;
313 ASSERT_TRUE(component_root.CreateUniqueTempDir());
Eric Carusoa5dfc942018-01-22 15:44:45 -0800314 base::FilePath component_path = component_root.GetPath().Append("9824.0.4");
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700315 ASSERT_TRUE(base::CreateDirectory(component_path));
316 std::unique_ptr<Component> component =
317 Component::Create(base::FilePath(GetTestComponentPath()),
318 keys);
319 ASSERT_TRUE(component->CopyTo(component_path));
320
321 // Remove the component.
322 EXPECT_FALSE(loader.RemoveComponentAtPath(
Eric Carusoa5dfc942018-01-22 15:44:45 -0800323 kTestComponentName, component_root.GetPath(), component_path));
324 EXPECT_TRUE(base::PathExists(component_root.GetPath()));
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700325}
326
Xiaochu Liuc2264342017-08-14 16:37:42 -0700327TEST_F(ImageLoaderTest, MountInvalidImage) {
328 Keys keys;
329 keys.push_back(
330 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
331
Greg Kerr09f06de2018-02-16 15:32:07 -0800332 auto helper_mock = std::make_unique<MockHelperProcessProxy>();
Xiaochu Liu646339e2018-07-23 17:41:36 -0700333 EXPECT_CALL(
334 *helper_mock,
335 SendMountCommand(_, _, brillo::imageloader::FileSystem::kSquashFS, _))
Xiaochu Liuc2264342017-08-14 16:37:42 -0700336 .Times(0);
337 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800338 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700339
Greg Kerr89be05f2016-07-27 10:40:32 -0700340 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700341 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
342
Eric Caruso0b79bc82017-03-21 13:44:34 -0700343 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Eric Carusoa5dfc942018-01-22 15:44:45 -0800344 scoped_mount_dir.GetPath().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700345 ImageLoaderImpl loader(std::move(config));
346
347 // We previously tested RegisterComponent, so assume this works if it reports
348 // true.
349 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800350 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700351
Greg Kerr019d59c2016-11-17 14:28:49 -0800352 base::FilePath table = temp_dir_.Append(kTestComponentName)
Greg Kerr89be05f2016-07-27 10:40:32 -0700353 .Append(kTestDataVersion)
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700354 .Append("table");
Greg Kerr89be05f2016-07-27 10:40:32 -0700355 std::string contents = "corrupt";
356 ASSERT_EQ(static_cast<int>(contents.size()),
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700357 base::WriteFile(table, contents.data(), contents.size()));
Greg Kerr9944e242017-01-26 15:09:31 -0800358 ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700359}
360
Greg Kerr2f76fde2016-08-29 16:39:45 -0700361TEST_F(ImageLoaderTest, SetupTable) {
362 std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV "
363 "hashstart=40 alg=sha256 root_hexdigest="
364 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
365 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2";
366
367 // Make sure excess newlines are rejected.
368 std::string bad_table = base_table + "\n\n";
369 EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6"));
370
371 // Make sure it does the right replacements on a simple base table.
372 std::string good_table = base_table;
373 EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6"));
374
375 std::string known_good_table =
376 "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 "
377 "hashstart=40 alg=sha256 root_hexdigest="
378 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
379 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 "
380 "error_behavior=eio";
381 EXPECT_EQ(known_good_table, good_table);
382
383 // Make sure the newline is stripped.
384 std::string good_table_newline = base_table + "\n";
385 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6"));
386 EXPECT_EQ(known_good_table, good_table_newline);
387
388 // Make sure error_behavior isn't appended twice.
389 std::string good_table_error = base_table + " error_behavior=eio\n";
390 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6"));
391 EXPECT_EQ(known_good_table, good_table_error);
392}
393
Eric Caruso0b79bc82017-03-21 13:44:34 -0700394TEST_F(ImageLoaderTest, SecondKey) {
395 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
396 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
397 kTestOciComponentVersion,
398 GetTestOciComponentPath().value()));
399
400 base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName);
401 ASSERT_TRUE(base::DirectoryExists(comp_dir));
402
403 base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion);
404 ASSERT_TRUE(base::DirectoryExists(version_dir));
405}
406
Eric Caruso26a91442017-10-25 16:05:40 -0700407TEST_F(ImageLoaderTest, GetMetadata) {
408 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
409 ASSERT_TRUE(loader.RegisterComponent(kMetadataComponentName,
410 kTestOciComponentVersion,
411 GetMetadataComponentPath().value()));
412
413 // We shouldn't need to load the component to get the metadata.
414 std::map<std::string, std::string> metadata;
415 ASSERT_TRUE(loader.GetComponentMetadata(kMetadataComponentName, &metadata));
416 std::map<std::string, std::string> expected_metadata{
417 {"foo", "bar"},
418 {"baz", "quux"},
419 };
420 ASSERT_EQ(expected_metadata, metadata);
421}
422
423TEST_F(ImageLoaderTest, GetEmptyMetadata) {
424 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
425 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
426 kTestOciComponentVersion,
427 GetTestOciComponentPath().value()));
428
429 // If there's no metadata, we should get nothing.
430 std::map<std::string, std::string> metadata;
431 ASSERT_TRUE(loader.GetComponentMetadata(kTestOciComponentName, &metadata));
432 ASSERT_TRUE(metadata.empty());
433}
434
435TEST_F(ImageLoaderTest, MetadataFailure) {
436 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
437 // Metadata is optional, but malformed metadata should not be present in the
438 // manifest. If it is, fail to load the component.
439 ASSERT_FALSE(loader.RegisterComponent(kBadMetadataComponentName,
440 kTestOciComponentVersion,
441 GetBadMetadataComponentPath().value()));
442
443 ASSERT_FALSE(loader.RegisterComponent(
444 kNonDictMetadataComponentName,
445 kTestOciComponentVersion,
446 GetNonDictMetadataComponentPath().value()));
Eric Caruso26a91442017-10-25 16:05:40 -0700447}
448
Greg Kerra6c0c522016-07-25 11:15:31 -0700449} // namespace imageloader