blob: 5973fb64256dc6a289eb93696f4867e1a2153ddf [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/filesystem_copier_action.h"
6
Darin Petkov698d0412010-10-13 10:59:44 -07007#include <fcntl.h>
8
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <set>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <vector>
Darin Petkov698d0412010-10-13 10:59:44 -070012
Chris Sosafc661a12013-02-26 14:43:21 -080013#include <base/posix/eintr_wrapper.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/strings/string_util.h>
15#include <base/strings/stringprintf.h>
Darin Petkov698d0412010-10-13 10:59:44 -070016#include <glib.h>
Don Garrett83692e42013-11-08 10:11:30 -080017#include <gmock/gmock.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include <gtest/gtest.h>
Darin Petkov698d0412010-10-13 10:59:44 -070019
Gilad Arnold5bb4c902014-04-10 12:32:13 -070020#include "update_engine/fake_system_state.h"
Don Garrett6646b442013-11-13 15:29:11 -080021#include "update_engine/mock_hardware.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000022#include "update_engine/omaha_hash_calculator.h"
23#include "update_engine/test_utils.h"
24#include "update_engine/utils.h"
25
26using std::set;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080027using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000028using std::vector;
29
30namespace chromeos_update_engine {
31
32class FilesystemCopierActionTest : public ::testing::Test {
33 protected:
Darin Petkov3aefa862010-12-07 14:45:00 -080034 // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification,
35 // 2 -- hash verification failure.
Gilad Arnoldae236702012-05-17 09:33:20 -070036 // Returns true iff test has completed successfully.
Gilad Arnoldae236702012-05-17 09:33:20 -070037 bool DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -070038 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -080039 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -070040 int verify_hash);
Don Garrett6646b442013-11-13 15:29:11 -080041
Gilad Arnold5bb4c902014-04-10 12:32:13 -070042 FakeSystemState fake_system_state_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000043};
44
45class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
46 public:
Ben Chan2add7d72012-10-08 19:28:37 -070047 FilesystemCopierActionTestDelegate(GMainLoop* loop,
48 FilesystemCopierAction* action)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070049 : loop_(loop), action_(action), ran_(false), code_(ErrorCode::kError) {}
Andrew de los Reyesc7020782010-04-28 10:46:04 -070050 void ExitMainLoop() {
Ben Chan2add7d72012-10-08 19:28:37 -070051 GMainContext* context = g_main_loop_get_context(loop_);
52 // We cannot use g_main_context_pending() alone to determine if it is safe
Alex Vakulenko072359c2014-07-18 11:41:07 -070053 // to quit the main loop here because g_main_context_pending() may return
Ben Chan2add7d72012-10-08 19:28:37 -070054 // FALSE when g_input_stream_read_async() in FilesystemCopierAction has
55 // been cancelled but the callback has not yet been invoked.
56 while (g_main_context_pending(context) || action_->IsCleanupPending()) {
57 g_main_context_iteration(context, false);
58 g_usleep(100);
Andrew de los Reyesc7020782010-04-28 10:46:04 -070059 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000060 g_main_loop_quit(loop_);
61 }
David Zeuthena99981f2013-04-29 13:42:47 -070062 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070063 ExitMainLoop();
64 }
65 void ProcessingStopped(const ActionProcessor* processor) {
66 ExitMainLoop();
67 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000068 void ActionCompleted(ActionProcessor* processor,
69 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070070 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000071 if (action->Type() == FilesystemCopierAction::StaticType()) {
72 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070073 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 }
75 }
Ben Chan2add7d72012-10-08 19:28:37 -070076 bool ran() const { return ran_; }
David Zeuthena99981f2013-04-29 13:42:47 -070077 ErrorCode code() const { return code_; }
Alex Vakulenkod2779df2014-06-16 13:19:00 -070078
adlr@google.com3defe6a2009-12-04 20:57:17 +000079 private:
80 GMainLoop* loop_;
Ben Chan2add7d72012-10-08 19:28:37 -070081 FilesystemCopierAction* action_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000082 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -070083 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000084};
85
Andrew de los Reyesc7020782010-04-28 10:46:04 -070086struct StartProcessorCallbackArgs {
87 ActionProcessor* processor;
88 FilesystemCopierAction* filesystem_copier_action;
89 bool terminate_early;
90};
91
adlr@google.com3defe6a2009-12-04 20:57:17 +000092gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070093 StartProcessorCallbackArgs* args =
94 reinterpret_cast<StartProcessorCallbackArgs*>(data);
95 ActionProcessor* processor = args->processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +000096 processor->StartProcessing();
Andrew de los Reyesc7020782010-04-28 10:46:04 -070097 if (args->terminate_early) {
98 EXPECT_TRUE(args->filesystem_copier_action);
99 args->processor->StopProcessing();
100 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000101 return FALSE;
102}
103
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700104// TODO(garnold) Temporarily disabling this test, see chromium-os:31082 for
105// details; still trying to track down the root cause for these rare write
106// failures and whether or not they are due to the test setup or an inherent
Alex Vakulenko072359c2014-07-18 11:41:07 -0700107// issue with the chroot environment, library versions we use, etc.
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700108TEST_F(FilesystemCopierActionTest, DISABLED_RunAsRootSimpleTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000109 ASSERT_EQ(0, getuid());
Don Garrett83692e42013-11-08 10:11:30 -0800110 bool test = DoTest(false, false, true, 0);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700111 EXPECT_TRUE(test);
112 if (!test)
113 return;
Don Garrett83692e42013-11-08 10:11:30 -0800114 test = DoTest(false, false, false, 0);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700115 EXPECT_TRUE(test);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000116}
Darin Petkov3aefa862010-12-07 14:45:00 -0800117
Gilad Arnoldae236702012-05-17 09:33:20 -0700118bool FilesystemCopierActionTest::DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700119 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -0800120 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700121 int verify_hash) {
Don Garrett6646b442013-11-13 15:29:11 -0800122 // We need MockHardware to verify MarkUnbootable calls, but don't want
123 // warnings about other usages.
124 testing::NiceMock<MockHardware> mock_hardware;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700125 fake_system_state_.set_hardware(&mock_hardware);
Don Garrett83692e42013-11-08 10:11:30 -0800126
adlr@google.com3defe6a2009-12-04 20:57:17 +0000127 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
128
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700129 string a_loop_file;
130 string b_loop_file;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700131
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700132 if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, nullptr) &&
133 utils::MakeTempFile("b_loop_file.XXXXXX", &b_loop_file, nullptr))) {
Gilad Arnoldae236702012-05-17 09:33:20 -0700134 ADD_FAILURE();
135 return false;
136 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700137 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700138 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700139
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700140 // Make random data for a, zero filled data for b.
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700141 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700142 vector<char> a_loop_data(kLoopFileSize);
Alex Deymo10875d92014-11-10 21:52:57 -0800143 test_utils::FillWithData(&a_loop_data);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700144 vector<char> b_loop_data(run_out_of_space ?
145 (kLoopFileSize - 1) :
146 kLoopFileSize,
147 '\0'); // Fill with 0s
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700149 // Write data to disk
Alex Deymo10875d92014-11-10 21:52:57 -0800150 if (!(test_utils::WriteFileVector(a_loop_file, a_loop_data) &&
151 test_utils::WriteFileVector(b_loop_file, b_loop_data))) {
Gilad Arnoldae236702012-05-17 09:33:20 -0700152 ADD_FAILURE();
153 return false;
154 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155
Don Garrett58e8b1f2012-01-31 16:38:16 -0800156 // Attach loop devices to the files
157 string a_dev;
158 string b_dev;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159
Alex Deymo10875d92014-11-10 21:52:57 -0800160 test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev);
161 test_utils::ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev);
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700162 if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) {
163 ADD_FAILURE();
164 return false;
165 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000166
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700167 LOG(INFO) << "copying: "
168 << a_loop_file << " (" << a_dev << ") -> "
169 << b_loop_file << " (" << b_dev << ", "
170 << kLoopFileSize << " bytes";
Gilad Arnoldae236702012-05-17 09:33:20 -0700171 bool success = true;
172
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700173 // Set up the action objects
adlr@google.com3defe6a2009-12-04 20:57:17 +0000174 InstallPlan install_plan;
Darin Petkov3aefa862010-12-07 14:45:00 -0800175 if (verify_hash) {
176 if (use_kernel_partition) {
177 install_plan.kernel_install_path = a_dev;
178 install_plan.kernel_size =
179 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700180 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
181 &install_plan.kernel_hash)) {
182 ADD_FAILURE();
183 success = false;
184 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800185 } else {
186 install_plan.install_path = a_dev;
187 install_plan.rootfs_size =
188 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700189 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
190 &install_plan.rootfs_hash)) {
191 ADD_FAILURE();
192 success = false;
193 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800194 }
195 } else {
196 if (use_kernel_partition) {
197 install_plan.kernel_install_path = b_dev;
198 } else {
199 install_plan.install_path = b_dev;
200 }
201 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000202
Don Garrett6646b442013-11-13 15:29:11 -0800203 EXPECT_CALL(mock_hardware,
Don Garrett83692e42013-11-08 10:11:30 -0800204 MarkKernelUnbootable(a_dev)).Times(use_kernel_partition ? 1 : 0);
205
adlr@google.com3defe6a2009-12-04 20:57:17 +0000206 ActionProcessor processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000207
208 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700209 FilesystemCopierAction copier_action(&fake_system_state_,
Alex Deymo42432912013-07-12 20:21:15 -0700210 use_kernel_partition,
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700211 verify_hash != 0);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000212 ObjectCollectorAction<InstallPlan> collector_action;
213
214 BondActions(&feeder_action, &copier_action);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700215 BondActions(&copier_action, &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216
Ben Chan2add7d72012-10-08 19:28:37 -0700217 FilesystemCopierActionTestDelegate delegate(loop, &copier_action);
218 processor.set_delegate(&delegate);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000219 processor.EnqueueAction(&feeder_action);
220 processor.EnqueueAction(&copier_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 processor.EnqueueAction(&collector_action);
222
Darin Petkov3aefa862010-12-07 14:45:00 -0800223 if (!verify_hash) {
224 copier_action.set_copy_source(a_dev);
225 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000226 feeder_action.set_obj(install_plan);
227
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700228 StartProcessorCallbackArgs start_callback_args;
229 start_callback_args.processor = &processor;
230 start_callback_args.filesystem_copier_action = &copier_action;
231 start_callback_args.terminate_early = terminate_early;
232
233 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000234 g_main_loop_run(loop);
235 g_main_loop_unref(loop);
236
Gilad Arnoldae236702012-05-17 09:33:20 -0700237 if (!terminate_early) {
238 bool is_delegate_ran = delegate.ran();
239 EXPECT_TRUE(is_delegate_ran);
240 success = success && is_delegate_ran;
241 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700242 if (run_out_of_space || terminate_early) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700243 EXPECT_EQ(ErrorCode::kError, delegate.code());
244 return (ErrorCode::kError == delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000245 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800246 if (verify_hash == 2) {
David Zeuthena99981f2013-04-29 13:42:47 -0700247 ErrorCode expected_exit_code =
Gilad Arnoldae236702012-05-17 09:33:20 -0700248 (use_kernel_partition ?
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700249 ErrorCode::kNewKernelVerificationError :
250 ErrorCode::kNewRootfsVerificationError);
Gilad Arnoldae236702012-05-17 09:33:20 -0700251 EXPECT_EQ(expected_exit_code, delegate.code());
252 return (expected_exit_code == delegate.code());
Darin Petkov3aefa862010-12-07 14:45:00 -0800253 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700254 EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000255
adlr@google.com3defe6a2009-12-04 20:57:17 +0000256 // Make sure everything in the out_image is there
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700257 vector<char> a_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700258 if (!utils::ReadFile(a_dev, &a_out)) {
259 ADD_FAILURE();
260 return false;
261 }
Alex Deymo10875d92014-11-10 21:52:57 -0800262 const bool is_a_file_reading_eq =
263 test_utils::ExpectVectorsEq(a_loop_data, a_out);
Gilad Arnold308b85e2012-06-07 15:50:16 -0700264 EXPECT_TRUE(is_a_file_reading_eq);
265 success = success && is_a_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800266 if (!verify_hash) {
267 vector<char> b_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700268 if (!utils::ReadFile(b_dev, &b_out)) {
269 ADD_FAILURE();
270 return false;
271 }
Alex Deymo10875d92014-11-10 21:52:57 -0800272 const bool is_b_file_reading_eq = test_utils::ExpectVectorsEq(a_out, b_out);
Gilad Arnold308b85e2012-06-07 15:50:16 -0700273 EXPECT_TRUE(is_b_file_reading_eq);
274 success = success && is_b_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800275 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000276
Gilad Arnoldae236702012-05-17 09:33:20 -0700277 bool is_install_plan_eq = (collector_action.object() == install_plan);
278 EXPECT_TRUE(is_install_plan_eq);
279 success = success && is_install_plan_eq;
280
Don Garrett83692e42013-11-08 10:11:30 -0800281 LOG(INFO) << "Verifying bootable flag on: " << a_dev;
282 bool bootable;
Don Garrett6646b442013-11-13 15:29:11 -0800283 EXPECT_TRUE(mock_hardware.fake().IsKernelBootable(a_dev, &bootable));
Don Garrett83692e42013-11-08 10:11:30 -0800284 // We should always mark a partition as unbootable if it's a kernel
285 // partition, but never if it's anything else.
286 EXPECT_EQ(bootable, !use_kernel_partition);
287
Gilad Arnoldae236702012-05-17 09:33:20 -0700288 return success;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000289}
290
291class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
292 public:
293 void ActionCompleted(ActionProcessor* processor,
294 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700295 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000296 if (action->Type() == FilesystemCopierAction::StaticType()) {
297 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700298 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000299 }
300 }
301 GMainLoop *loop_;
302 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -0700303 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000304};
305
306TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
307 ActionProcessor processor;
308 FilesystemCopierActionTest2Delegate delegate;
309
310 processor.set_delegate(&delegate);
311
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700312 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000313 ObjectCollectorAction<InstallPlan> collector_action;
314
315 BondActions(&copier_action, &collector_action);
316
317 processor.EnqueueAction(&copier_action);
318 processor.EnqueueAction(&collector_action);
319 processor.StartProcessing();
320 EXPECT_FALSE(processor.IsRunning());
321 EXPECT_TRUE(delegate.ran_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700322 EXPECT_EQ(ErrorCode::kError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000323}
324
Darin Petkov9b230572010-10-08 10:20:09 -0700325TEST_F(FilesystemCopierActionTest, ResumeTest) {
326 ActionProcessor processor;
327 FilesystemCopierActionTest2Delegate delegate;
328
329 processor.set_delegate(&delegate);
330
331 ObjectFeederAction<InstallPlan> feeder_action;
332 const char* kUrl = "http://some/url";
David Zeuthene7f89172013-10-31 10:21:04 -0700333 InstallPlan install_plan(false, true, kUrl, 0, "", 0, "", "", "", "");
Darin Petkov9b230572010-10-08 10:20:09 -0700334 feeder_action.set_obj(install_plan);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700335 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
Darin Petkov9b230572010-10-08 10:20:09 -0700336 ObjectCollectorAction<InstallPlan> collector_action;
337
338 BondActions(&feeder_action, &copier_action);
339 BondActions(&copier_action, &collector_action);
340
341 processor.EnqueueAction(&feeder_action);
342 processor.EnqueueAction(&copier_action);
343 processor.EnqueueAction(&collector_action);
344 processor.StartProcessing();
345 EXPECT_FALSE(processor.IsRunning());
346 EXPECT_TRUE(delegate.ran_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700347 EXPECT_EQ(ErrorCode::kSuccess, delegate.code_);
Darin Petkov9b230572010-10-08 10:20:09 -0700348 EXPECT_EQ(kUrl, collector_action.object().download_url);
349}
350
adlr@google.com3defe6a2009-12-04 20:57:17 +0000351TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) {
352 ActionProcessor processor;
353 FilesystemCopierActionTest2Delegate delegate;
354
355 processor.set_delegate(&delegate);
356
357 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov0406e402010-10-06 21:33:11 -0700358 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700359 false,
Darin Petkov0406e402010-10-06 21:33:11 -0700360 "",
361 0,
362 "",
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700363 0,
364 "",
Darin Petkov0406e402010-10-06 21:33:11 -0700365 "/no/such/file",
David Zeuthene7f89172013-10-31 10:21:04 -0700366 "/no/such/file",
367 "");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000368 feeder_action.set_obj(install_plan);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700369 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000370 ObjectCollectorAction<InstallPlan> collector_action;
371
372 BondActions(&copier_action, &collector_action);
373
374 processor.EnqueueAction(&feeder_action);
375 processor.EnqueueAction(&copier_action);
376 processor.EnqueueAction(&collector_action);
377 processor.StartProcessing();
378 EXPECT_FALSE(processor.IsRunning());
379 EXPECT_TRUE(delegate.ran_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700380 EXPECT_EQ(ErrorCode::kError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000381}
382
Darin Petkov3aefa862010-12-07 14:45:00 -0800383TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) {
384 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700385 EXPECT_TRUE(DoTest(false, false, false, 1));
386 EXPECT_TRUE(DoTest(false, false, true, 1));
Darin Petkov3aefa862010-12-07 14:45:00 -0800387}
388
389TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) {
390 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700391 EXPECT_TRUE(DoTest(false, false, false, 2));
392 EXPECT_TRUE(DoTest(false, false, true, 2));
Darin Petkov3aefa862010-12-07 14:45:00 -0800393}
394
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700395TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000396 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700397 EXPECT_TRUE(DoTest(true, false, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000398}
399
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700400TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000401 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700402 EXPECT_TRUE(DoTest(false, true, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000403}
404
Darin Petkov698d0412010-10-13 10:59:44 -0700405TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) {
406 string img;
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700407 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr));
Darin Petkov698d0412010-10-13 10:59:44 -0700408 ScopedPathUnlinker img_unlinker(img);
Alex Deymo10875d92014-11-10 21:52:57 -0800409 test_utils::CreateExtImageAtPath(img, nullptr);
Darin Petkov698d0412010-10-13 10:59:44 -0700410 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Deymo10875d92014-11-10 21:52:57 -0800411 EXPECT_EQ(0, test_utils::System(base::StringPrintf(
Darin Petkov698d0412010-10-13 10:59:44 -0700412 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
413 img.c_str())));
414 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
415
416 for (int i = 0; i < 2; ++i) {
417 bool is_kernel = i == 1;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700418 FilesystemCopierAction action(&fake_system_state_, is_kernel, false);
Darin Petkov698d0412010-10-13 10:59:44 -0700419 EXPECT_EQ(kint64max, action.filesystem_size_);
420 {
421 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700422 EXPECT_GT(fd, 0);
Darin Petkov698d0412010-10-13 10:59:44 -0700423 ScopedFdCloser fd_closer(&fd);
424 action.DetermineFilesystemSize(fd);
425 }
426 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024,
427 action.filesystem_size_);
428 }
429}
430
431
adlr@google.com3defe6a2009-12-04 20:57:17 +0000432} // namespace chromeos_update_engine