blob: 8aaf5556021b31da8788757804fd376696a38c3d [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>
Greg Kerr019d59c2016-11-17 14:28:49 -080010#include <string>
Greg Kerr3e750f42016-06-29 15:20:21 -070011#include <vector>
12
Greg Kerr019d59c2016-11-17 14:28:49 -080013#include "component.h"
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 Kerr2f76fde2016-08-29 16:39:45 -070016#include "verity_mounter.h"
Greg Kerr3e750f42016-06-29 15:20:21 -070017
Greg Kerr3e750f42016-06-29 15:20:21 -070018#include <base/files/file_path.h>
19#include <base/files/file_util.h>
20#include <base/files/scoped_temp_dir.h>
Greg Kerr89be05f2016-07-27 10:40:32 -070021#include <base/memory/ptr_util.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
Greg Kerr9944e242017-01-26 15:09:31 -0800120 auto helper_mock = base::MakeUnique<MockHelperProcess>();
121 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, _)).Times(2);
122 ON_CALL(*helper_mock, SendMountCommand(_, _, _))
123 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700124
Greg Kerr89be05f2016-07-27 10:40:32 -0700125 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700126 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
127
Eric Caruso0b79bc82017-03-21 13:44:34 -0700128 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Greg Kerr9944e242017-01-26 15:09:31 -0800129 scoped_mount_dir.path().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700130 ImageLoaderImpl loader(std::move(config));
131
132 // We previously tested RegisterComponent, so assume this works if it reports
133 // true.
134 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800135 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700136
137 const std::string expected_path =
138 scoped_mount_dir.path().value() + "/PepperFlashPlayer/22.0.0.158";
Greg Kerr9944e242017-01-26 15:09:31 -0800139 EXPECT_EQ(expected_path,
140 loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerrc5b91692016-09-14 12:09:22 -0700141
142 // Let's also test mounting the component at a fixed point.
143 const std::string expected_path2 =
144 scoped_mount_dir.path().value() + "/FixedMountPoint";
Greg Kerr9944e242017-01-26 15:09:31 -0800145 EXPECT_TRUE(loader.LoadComponent(kTestComponentName, expected_path2,
146 helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700147}
148
149TEST_F(ImageLoaderTest, MountInvalidImage) {
Eric Caruso0b79bc82017-03-21 13:44:34 -0700150 Keys keys;
151 keys.push_back(std::vector<uint8_t>(std::begin(kDevPublicKey),
152 std::end(kDevPublicKey)));
153
Greg Kerr9944e242017-01-26 15:09:31 -0800154 auto helper_mock = base::MakeUnique<MockHelperProcess>();
155 EXPECT_CALL(*helper_mock, SendMountCommand(_, _, _)).Times(0);
156 ON_CALL(*helper_mock, SendMountCommand(_, _, _))
157 .WillByDefault(testing::Return(true));
Greg Kerr89be05f2016-07-27 10:40:32 -0700158
Greg Kerr89be05f2016-07-27 10:40:32 -0700159 base::ScopedTempDir scoped_mount_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -0700160 ASSERT_TRUE(scoped_mount_dir.CreateUniqueTempDir());
161
Eric Caruso0b79bc82017-03-21 13:44:34 -0700162 ImageLoaderConfig config(keys, temp_dir_.value().c_str(),
Greg Kerr9944e242017-01-26 15:09:31 -0800163 scoped_mount_dir.path().value().c_str());
Greg Kerr89be05f2016-07-27 10:40:32 -0700164 ImageLoaderImpl loader(std::move(config));
165
166 // We previously tested RegisterComponent, so assume this works if it reports
167 // true.
168 ASSERT_TRUE(loader.RegisterComponent(kTestComponentName, kTestDataVersion,
Greg Kerr019d59c2016-11-17 14:28:49 -0800169 GetTestComponentPath().value()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700170
Greg Kerr019d59c2016-11-17 14:28:49 -0800171 base::FilePath table = temp_dir_.Append(kTestComponentName)
Greg Kerr89be05f2016-07-27 10:40:32 -0700172 .Append(kTestDataVersion)
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700173 .Append("table");
Greg Kerr89be05f2016-07-27 10:40:32 -0700174 std::string contents = "corrupt";
175 ASSERT_EQ(static_cast<int>(contents.size()),
Greg Kerr30cd5fb2016-09-29 12:37:02 -0700176 base::WriteFile(table, contents.data(), contents.size()));
Greg Kerr9944e242017-01-26 15:09:31 -0800177 ASSERT_EQ("", loader.LoadComponent(kTestComponentName, helper_mock.get()));
Greg Kerr89be05f2016-07-27 10:40:32 -0700178}
179
Greg Kerr2f76fde2016-08-29 16:39:45 -0700180TEST_F(ImageLoaderTest, SetupTable) {
181 std::string base_table = "0 40 verity payload=ROOT_DEV hashtree=HASH_DEV "
182 "hashstart=40 alg=sha256 root_hexdigest="
183 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
184 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2";
185
186 // Make sure excess newlines are rejected.
187 std::string bad_table = base_table + "\n\n";
188 EXPECT_FALSE(VerityMounter::SetupTable(&bad_table, "/dev/loop6"));
189
190 // Make sure it does the right replacements on a simple base table.
191 std::string good_table = base_table;
192 EXPECT_TRUE(VerityMounter::SetupTable(&good_table, "/dev/loop6"));
193
194 std::string known_good_table =
195 "0 40 verity payload=/dev/loop6 hashtree=/dev/loop6 "
196 "hashstart=40 alg=sha256 root_hexdigest="
197 "34663b9920632778d38a0943a5472cae196bd4bf1d7dfa191506e7a8e7ec84d2 "
198 "salt=fcfc9b5a329e44be73a323188ae75ca644122d920161f672f6935623831d07e2 "
199 "error_behavior=eio";
200 EXPECT_EQ(known_good_table, good_table);
201
202 // Make sure the newline is stripped.
203 std::string good_table_newline = base_table + "\n";
204 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_newline, "/dev/loop6"));
205 EXPECT_EQ(known_good_table, good_table_newline);
206
207 // Make sure error_behavior isn't appended twice.
208 std::string good_table_error = base_table + " error_behavior=eio\n";
209 EXPECT_TRUE(VerityMounter::SetupTable(&good_table_error, "/dev/loop6"));
210 EXPECT_EQ(known_good_table, good_table_error);
211}
212
Eric Caruso0b79bc82017-03-21 13:44:34 -0700213TEST_F(ImageLoaderTest, SecondKey) {
214 ImageLoaderImpl loader(GetConfig(temp_dir_.value().c_str()));
215 ASSERT_TRUE(loader.RegisterComponent(kTestOciComponentName,
216 kTestOciComponentVersion,
217 GetTestOciComponentPath().value()));
218
219 base::FilePath comp_dir = temp_dir_.Append(kTestOciComponentName);
220 ASSERT_TRUE(base::DirectoryExists(comp_dir));
221
222 base::FilePath version_dir = comp_dir.Append(kTestOciComponentVersion);
223 ASSERT_TRUE(base::DirectoryExists(version_dir));
224}
225
Greg Kerra6c0c522016-07-25 11:15:31 -0700226} // namespace imageloader