blob: b7876ab21fd26705b845b6e736837f2a521c50a4 [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 Liu5e708b82017-11-13 13:59:12 -0800175TEST_F(ImageLoaderTest, CleanupAll) {
176 Keys keys;
177 keys.push_back(
178 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
179
180 auto helper_mock = std::make_unique<MockHelperProcess>();
181 EXPECT_CALL(*helper_mock, SendUnmountAllCommand(_, _, _))
182 .Times(1);
183 ON_CALL(*helper_mock, SendUnmountAllCommand(_, _, _))
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 base::FilePath rootpath("/");
194 std::vector<std::string> paths;
195 EXPECT_EQ(loader.CleanupAll(true, rootpath, &paths, helper_mock.get()), true);
196}
197
198TEST_F(ImageLoaderTest, Cleanup) {
199 Keys keys;
200 keys.push_back(
201 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
202
203 auto helper_mock = std::make_unique<MockHelperProcess>();
204 EXPECT_CALL(*helper_mock, SendUnmountCommand(_)).Times(1);
205 ON_CALL(*helper_mock, SendUnmountCommand(_))
206 .WillByDefault(testing::Return(true));
207
208 base::ScopedTempDir scoped_mount_dir;
209 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
210
211 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
212 scoped_mount_dir.path().value().c_str());
213 ImageLoaderImpl loader(std::move(config));
214
215 base::FilePath path("/");
216 EXPECT_EQ(loader.Cleanup(path, helper_mock.get()), true);
217}
218
Xiaochu Liuc2264342017-08-14 16:37:42 -0700219TEST_F(ImageLoaderTest, LoadExt4Image) {
Greg Kerre8704202017-07-27 12:54:31 -0700220 Keys keys;
Xiaochu Liuc2264342017-08-14 16:37:42 -0700221 keys.push_back(
222 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
Greg Kerre8704202017-07-27 12:54:31 -0700223
Ben Chanea104dd2017-09-29 00:43:04 -0700224 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700225 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kExt4, _))
226 .Times(1);
227 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
228 .WillByDefault(testing::Return(true));
229
230 base::ScopedTempDir scoped_mount_dir;
231 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
232
233 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
234 scoped_mount_dir.path().value().c_str());
235 ImageLoaderImpl loader(std::move(config));
236
237 const std::string expected_path =
Xiaochu Liu1e5dc142017-10-11 17:33:33 -0700238 scoped_mount_dir.path().value() + "/ext4/9824.0.4";
Xiaochu Liuc2264342017-08-14 16:37:42 -0700239 const std::string mnt_path = loader.LoadComponentAtPath(
240 "ext4", GetTestDataPath("ext4_component"), helper_mock.get());
241 EXPECT_EQ(expected_path, mnt_path);
242}
243
Xiaochu Liu7a224d92017-10-06 17:33:41 -0700244TEST_F(ImageLoaderTest, RemoveImageAtPathRemovable) {
245 Keys keys;
246 keys.push_back(
247 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
248
249 base::ScopedTempDir scoped_mount_dir;
250 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
251 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
252 scoped_mount_dir.path().value().c_str());
253 ImageLoaderImpl loader(std::move(config));
254
255 // Make a copy to avoid permanent loss of test data.
256 base::ScopedTempDir component_root;
257 ASSERT_TRUE(component_root.CreateUniqueTempDir());
258 base::FilePath component_path = component_root.path().Append("9824.0.4");
259 ASSERT_TRUE(base::CreateDirectory(component_path));
260 std::unique_ptr<Component> component =
261 Component::Create(base::FilePath(GetTestDataPath("ext4_component")),
262 keys);
263 ASSERT_TRUE(component->CopyTo(component_path));
264
265 // Remove the component.
266 EXPECT_TRUE(loader.RemoveComponentAtPath(
267 "ext4", component_root.path(), component_path));
268 EXPECT_FALSE(base::PathExists(component_root.path()));
269}
270
271TEST_F(ImageLoaderTest, RemoveImageAtPathNotRemovable) {
272 Keys keys;
273 keys.push_back(
274 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
275
276 base::ScopedTempDir scoped_mount_dir;
277 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
278 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
279 scoped_mount_dir.path().value().c_str());
280 ImageLoaderImpl loader(std::move(config));
281
282 // Make a copy to avoid permanent loss of test data.
283 base::ScopedTempDir component_root;
284 ASSERT_TRUE(component_root.CreateUniqueTempDir());
285 base::FilePath component_path = component_root.path().Append("9824.0.4");
286 ASSERT_TRUE(base::CreateDirectory(component_path));
287 std::unique_ptr<Component> component =
288 Component::Create(base::FilePath(GetTestComponentPath()),
289 keys);
290 ASSERT_TRUE(component->CopyTo(component_path));
291
292 // Remove the component.
293 EXPECT_FALSE(loader.RemoveComponentAtPath(
294 kTestComponentName, component_root.path(), component_path));
295 EXPECT_TRUE(base::PathExists(component_root.path()));
296}
297
Xiaochu Liuc2264342017-08-14 16:37:42 -0700298TEST_F(ImageLoaderTest, MountInvalidImage) {
299 Keys keys;
300 keys.push_back(
301 std::vector<uint8_t>(std::begin(kDevPublicKey), std::end(kDevPublicKey)));
302
Ben Chanea104dd2017-09-29 00:43:04 -0700303 auto helper_mock = std::make_unique<MockHelperProcess>();
Xiaochu Liuc2264342017-08-14 16:37:42 -0700304 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, FileSystem::kSquashFS, _))
305 .Times(0);
306 ON_CALL(*helper_mock, SendMountCommand(_, _, _, _))
Greg Kerr9944e242017-01-26 15:09:31 -0800307 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700308
Greg Kerr89be05f2016-07-27 10:40:32 -0700309 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700310 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
311
Eric Caruso0b79bc82017-03-21 13:44:34 -0700312 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Greg Kerr9944e242017-01-26 15:09:31 -0800313 scoped_mount_dir.path().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700314 ImageLoaderImpl loader(std::move(config));
315
316 // We previously tested RegisterComponent, so assume this works if it reports
317 // true.
318 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800319 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700320
Greg Kerr019d59c2016-11-17 14:28:49 -0800321 base::FilePath table = temp_dir_.Append(kTestComponentName)
Greg Kerr89be05f2016-07-27 10:40:32 -0700322 .Append(kTestDataVersion)
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700323 .Append("table");
Greg Kerr89be05f2016-07-27 10:40:32 -0700324 std::string contents = "corrupt";
325 ASSERT_EQ(static_cast<int>(contents.size()),
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700326 base::WriteFile(table, contents.data(), contents.size()));
Greg Kerr9944e242017-01-26 15:09:31 -0800327 ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700328}
329
Greg Kerr2f76fde2016-08-29 16:39:45 -0700330TEST_F(ImageLoaderTest, SetupTable) {
331 std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV "
332 "hashstart=40 alg=sha256 root_hexdigest="
333 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
334 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2";
335
336 // Make sure excess newlines are rejected.
337 std::string bad_table = base_table + "\n\n";
338 EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6"));
339
340 // Make sure it does the right replacements on a simple base table.
341 std::string good_table = base_table;
342 EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6"));
343
344 std::string known_good_table =
345 "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 "
346 "hashstart=40 alg=sha256 root_hexdigest="
347 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
348 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 "
349 "error_behavior=eio";
350 EXPECT_EQ(known_good_table, good_table);
351
352 // Make sure the newline is stripped.
353 std::string good_table_newline = base_table + "\n";
354 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6"));
355 EXPECT_EQ(known_good_table, good_table_newline);
356
357 // Make sure error_behavior isn't appended twice.
358 std::string good_table_error = base_table + " error_behavior=eio\n";
359 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6"));
360 EXPECT_EQ(known_good_table, good_table_error);
361}
362
Eric Caruso0b79bc82017-03-21 13:44:34 -0700363TEST_F(ImageLoaderTest, SecondKey) {
364 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
365 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
366 kTestOciComponentVersion,
367 GetTestOciComponentPath().value()));
368
369 base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName);
370 ASSERT_TRUE(base::DirectoryExists(comp_dir));
371
372 base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion);
373 ASSERT_TRUE(base::DirectoryExists(version_dir));
374}
375
Eric Caruso26a91442017-10-25 16:05:40 -0700376TEST_F(ImageLoaderTest, GetMetadata) {
377 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
378 ASSERT_TRUE(loader.RegisterComponent(kMetadataComponentName,
379 kTestOciComponentVersion,
380 GetMetadataComponentPath().value()));
381
382 // We shouldn't need to load the component to get the metadata.
383 std::map<std::string, std::string> metadata;
384 ASSERT_TRUE(loader.GetComponentMetadata(kMetadataComponentName, &metadata));
385 std::map<std::string, std::string> expected_metadata{
386 {"foo", "bar"},
387 {"baz", "quux"},
388 };
389 ASSERT_EQ(expected_metadata, metadata);
390}
391
392TEST_F(ImageLoaderTest, GetEmptyMetadata) {
393 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
394 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
395 kTestOciComponentVersion,
396 GetTestOciComponentPath().value()));
397
398 // If there's no metadata, we should get nothing.
399 std::map<std::string, std::string> metadata;
400 ASSERT_TRUE(loader.GetComponentMetadata(kTestOciComponentName, &metadata));
401 ASSERT_TRUE(metadata.empty());
402}
403
404TEST_F(ImageLoaderTest, MetadataFailure) {
405 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
406 // Metadata is optional, but malformed metadata should not be present in the
407 // manifest. If it is, fail to load the component.
408 ASSERT_FALSE(loader.RegisterComponent(kBadMetadataComponentName,
409 kTestOciComponentVersion,
410 GetBadMetadataComponentPath().value()));
411
412 ASSERT_FALSE(loader.RegisterComponent(
413 kNonDictMetadataComponentName,
414 kTestOciComponentVersion,
415 GetNonDictMetadataComponentPath().value()));
416
417}
418
Greg Kerra6c0c522016-07-25 11:15:31 -0700419} // namespace imageloader