blob: 300708f276c07a91cabbc13b4ec2b1716d56a877 [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
Darin Petkov698d0412010-10-13 10:59:44 -07005#include <fcntl.h>
6
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <set>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08008#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <vector>
Darin Petkov698d0412010-10-13 10:59:44 -070010
Chris Sosafc661a12013-02-26 14:43:21 -080011#include <base/posix/eintr_wrapper.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/strings/string_util.h>
13#include <base/strings/stringprintf.h>
Darin Petkov698d0412010-10-13 10:59:44 -070014#include <glib.h>
Don Garrett83692e42013-11-08 10:11:30 -080015#include <gmock/gmock.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000016#include <gtest/gtest.h>
Darin Petkov698d0412010-10-13 10:59:44 -070017
Gilad Arnold5bb4c902014-04-10 12:32:13 -070018#include "update_engine/fake_system_state.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include "update_engine/filesystem_copier_action.h"
Don Garrett6646b442013-11-13 15:29:11 -080020#include "update_engine/mock_hardware.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include "update_engine/omaha_hash_calculator.h"
22#include "update_engine/test_utils.h"
23#include "update_engine/utils.h"
24
25using std::set;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080026using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000027using std::vector;
28
29namespace chromeos_update_engine {
30
31class FilesystemCopierActionTest : public ::testing::Test {
32 protected:
Darin Petkov3aefa862010-12-07 14:45:00 -080033 // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification,
34 // 2 -- hash verification failure.
Gilad Arnoldae236702012-05-17 09:33:20 -070035 // Returns true iff test has completed successfully.
Gilad Arnoldae236702012-05-17 09:33:20 -070036 bool DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -070037 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -080038 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -070039 int verify_hash);
adlr@google.com3defe6a2009-12-04 20:57:17 +000040 void SetUp() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000041 }
42 void TearDown() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000043 }
Don Garrett6646b442013-11-13 15:29:11 -080044
Gilad Arnold5bb4c902014-04-10 12:32:13 -070045 FakeSystemState fake_system_state_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046};
47
48class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
49 public:
Ben Chan2add7d72012-10-08 19:28:37 -070050 FilesystemCopierActionTestDelegate(GMainLoop* loop,
51 FilesystemCopierAction* action)
David Zeuthena99981f2013-04-29 13:42:47 -070052 : loop_(loop), action_(action), ran_(false), code_(kErrorCodeError) {}
Andrew de los Reyesc7020782010-04-28 10:46:04 -070053 void ExitMainLoop() {
Ben Chan2add7d72012-10-08 19:28:37 -070054 GMainContext* context = g_main_loop_get_context(loop_);
55 // We cannot use g_main_context_pending() alone to determine if it is safe
56 // to quit the main loop here becasuse g_main_context_pending() may return
57 // FALSE when g_input_stream_read_async() in FilesystemCopierAction has
58 // been cancelled but the callback has not yet been invoked.
59 while (g_main_context_pending(context) || action_->IsCleanupPending()) {
60 g_main_context_iteration(context, false);
61 g_usleep(100);
Andrew de los Reyesc7020782010-04-28 10:46:04 -070062 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 g_main_loop_quit(loop_);
64 }
David Zeuthena99981f2013-04-29 13:42:47 -070065 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070066 ExitMainLoop();
67 }
68 void ProcessingStopped(const ActionProcessor* processor) {
69 ExitMainLoop();
70 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000071 void ActionCompleted(ActionProcessor* processor,
72 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070073 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000074 if (action->Type() == FilesystemCopierAction::StaticType()) {
75 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070076 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +000077 }
78 }
Ben Chan2add7d72012-10-08 19:28:37 -070079 bool ran() const { return ran_; }
David Zeuthena99981f2013-04-29 13:42:47 -070080 ErrorCode code() const { return code_; }
adlr@google.com3defe6a2009-12-04 20:57:17 +000081 private:
82 GMainLoop* loop_;
Ben Chan2add7d72012-10-08 19:28:37 -070083 FilesystemCopierAction* action_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000084 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -070085 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000086};
87
Andrew de los Reyesc7020782010-04-28 10:46:04 -070088struct StartProcessorCallbackArgs {
89 ActionProcessor* processor;
90 FilesystemCopierAction* filesystem_copier_action;
91 bool terminate_early;
92};
93
adlr@google.com3defe6a2009-12-04 20:57:17 +000094gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070095 StartProcessorCallbackArgs* args =
96 reinterpret_cast<StartProcessorCallbackArgs*>(data);
97 ActionProcessor* processor = args->processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +000098 processor->StartProcessing();
Andrew de los Reyesc7020782010-04-28 10:46:04 -070099 if (args->terminate_early) {
100 EXPECT_TRUE(args->filesystem_copier_action);
101 args->processor->StopProcessing();
102 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103 return FALSE;
104}
105
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700106// TODO(garnold) Temporarily disabling this test, see chromium-os:31082 for
107// details; still trying to track down the root cause for these rare write
108// failures and whether or not they are due to the test setup or an inherent
109// issue with the chroot environiment, library versions we use, etc.
110TEST_F(FilesystemCopierActionTest, DISABLED_RunAsRootSimpleTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111 ASSERT_EQ(0, getuid());
Don Garrett83692e42013-11-08 10:11:30 -0800112 bool test = DoTest(false, false, true, 0);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700113 EXPECT_TRUE(test);
114 if (!test)
115 return;
Don Garrett83692e42013-11-08 10:11:30 -0800116 test = DoTest(false, false, false, 0);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700117 EXPECT_TRUE(test);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000118}
Darin Petkov3aefa862010-12-07 14:45:00 -0800119
Gilad Arnoldae236702012-05-17 09:33:20 -0700120bool FilesystemCopierActionTest::DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700121 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -0800122 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700123 int verify_hash) {
Don Garrett6646b442013-11-13 15:29:11 -0800124 // We need MockHardware to verify MarkUnbootable calls, but don't want
125 // warnings about other usages.
126 testing::NiceMock<MockHardware> mock_hardware;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700127 fake_system_state_.set_hardware(&mock_hardware);
Don Garrett83692e42013-11-08 10:11:30 -0800128
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
130
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700131 string a_loop_file;
132 string b_loop_file;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133
Gilad Arnolda6742b32014-01-11 00:18:34 -0800134 if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, NULL) &&
135 utils::MakeTempFile("b_loop_file.XXXXXX", &b_loop_file, NULL))) {
Gilad Arnoldae236702012-05-17 09:33:20 -0700136 ADD_FAILURE();
137 return false;
138 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700139 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700140 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700141
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700142 // Make random data for a, zero filled data for b.
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700143 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700144 vector<char> a_loop_data(kLoopFileSize);
145 FillWithData(&a_loop_data);
146 vector<char> b_loop_data(run_out_of_space ?
147 (kLoopFileSize - 1) :
148 kLoopFileSize,
149 '\0'); // Fill with 0s
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700151 // Write data to disk
Gilad Arnoldae236702012-05-17 09:33:20 -0700152 if (!(WriteFileVector(a_loop_file, a_loop_data) &&
153 WriteFileVector(b_loop_file, b_loop_data))) {
154 ADD_FAILURE();
155 return false;
156 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157
Don Garrett58e8b1f2012-01-31 16:38:16 -0800158 // Attach loop devices to the files
159 string a_dev;
160 string b_dev;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161
Don Garrett58e8b1f2012-01-31 16:38:16 -0800162 ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev);
163 ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev);
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700164 if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) {
165 ADD_FAILURE();
166 return false;
167 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000168
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700169 LOG(INFO) << "copying: "
170 << a_loop_file << " (" << a_dev << ") -> "
171 << b_loop_file << " (" << b_dev << ", "
172 << kLoopFileSize << " bytes";
Gilad Arnoldae236702012-05-17 09:33:20 -0700173 bool success = true;
174
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700175 // Set up the action objects
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 InstallPlan install_plan;
Darin Petkov3aefa862010-12-07 14:45:00 -0800177 if (verify_hash) {
178 if (use_kernel_partition) {
179 install_plan.kernel_install_path = a_dev;
180 install_plan.kernel_size =
181 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700182 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
183 &install_plan.kernel_hash)) {
184 ADD_FAILURE();
185 success = false;
186 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800187 } else {
188 install_plan.install_path = a_dev;
189 install_plan.rootfs_size =
190 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700191 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
192 &install_plan.rootfs_hash)) {
193 ADD_FAILURE();
194 success = false;
195 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800196 }
197 } else {
198 if (use_kernel_partition) {
199 install_plan.kernel_install_path = b_dev;
200 } else {
201 install_plan.install_path = b_dev;
202 }
203 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000204
Don Garrett6646b442013-11-13 15:29:11 -0800205 EXPECT_CALL(mock_hardware,
Don Garrett83692e42013-11-08 10:11:30 -0800206 MarkKernelUnbootable(a_dev)).Times(use_kernel_partition ? 1 : 0);
207
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208 ActionProcessor processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000209
210 ObjectFeederAction<InstallPlan> feeder_action;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700211 FilesystemCopierAction copier_action(&fake_system_state_,
Alex Deymo42432912013-07-12 20:21:15 -0700212 use_kernel_partition,
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700213 verify_hash != 0);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 ObjectCollectorAction<InstallPlan> collector_action;
215
216 BondActions(&feeder_action, &copier_action);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700217 BondActions(&copier_action, &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218
Ben Chan2add7d72012-10-08 19:28:37 -0700219 FilesystemCopierActionTestDelegate delegate(loop, &copier_action);
220 processor.set_delegate(&delegate);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 processor.EnqueueAction(&feeder_action);
222 processor.EnqueueAction(&copier_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223 processor.EnqueueAction(&collector_action);
224
Darin Petkov3aefa862010-12-07 14:45:00 -0800225 if (!verify_hash) {
226 copier_action.set_copy_source(a_dev);
227 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000228 feeder_action.set_obj(install_plan);
229
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700230 StartProcessorCallbackArgs start_callback_args;
231 start_callback_args.processor = &processor;
232 start_callback_args.filesystem_copier_action = &copier_action;
233 start_callback_args.terminate_early = terminate_early;
234
235 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000236 g_main_loop_run(loop);
237 g_main_loop_unref(loop);
238
Gilad Arnoldae236702012-05-17 09:33:20 -0700239 if (!terminate_early) {
240 bool is_delegate_ran = delegate.ran();
241 EXPECT_TRUE(is_delegate_ran);
242 success = success && is_delegate_ran;
243 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700244 if (run_out_of_space || terminate_early) {
David Zeuthena99981f2013-04-29 13:42:47 -0700245 EXPECT_EQ(kErrorCodeError, delegate.code());
246 return (kErrorCodeError == delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000247 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800248 if (verify_hash == 2) {
David Zeuthena99981f2013-04-29 13:42:47 -0700249 ErrorCode expected_exit_code =
Gilad Arnoldae236702012-05-17 09:33:20 -0700250 (use_kernel_partition ?
David Zeuthena99981f2013-04-29 13:42:47 -0700251 kErrorCodeNewKernelVerificationError :
252 kErrorCodeNewRootfsVerificationError);
Gilad Arnoldae236702012-05-17 09:33:20 -0700253 EXPECT_EQ(expected_exit_code, delegate.code());
254 return (expected_exit_code == delegate.code());
Darin Petkov3aefa862010-12-07 14:45:00 -0800255 }
David Zeuthena99981f2013-04-29 13:42:47 -0700256 EXPECT_EQ(kErrorCodeSuccess, delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000257
adlr@google.com3defe6a2009-12-04 20:57:17 +0000258 // Make sure everything in the out_image is there
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700259 vector<char> a_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700260 if (!utils::ReadFile(a_dev, &a_out)) {
261 ADD_FAILURE();
262 return false;
263 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700264 const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out);
265 EXPECT_TRUE(is_a_file_reading_eq);
266 success = success && is_a_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800267 if (!verify_hash) {
268 vector<char> b_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700269 if (!utils::ReadFile(b_dev, &b_out)) {
270 ADD_FAILURE();
271 return false;
272 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700273 const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out);
274 EXPECT_TRUE(is_b_file_reading_eq);
275 success = success && is_b_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800276 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000277
Gilad Arnoldae236702012-05-17 09:33:20 -0700278 bool is_install_plan_eq = (collector_action.object() == install_plan);
279 EXPECT_TRUE(is_install_plan_eq);
280 success = success && is_install_plan_eq;
281
Don Garrett83692e42013-11-08 10:11:30 -0800282 LOG(INFO) << "Verifying bootable flag on: " << a_dev;
283 bool bootable;
Don Garrett6646b442013-11-13 15:29:11 -0800284 EXPECT_TRUE(mock_hardware.fake().IsKernelBootable(a_dev, &bootable));
Don Garrett83692e42013-11-08 10:11:30 -0800285 // We should always mark a partition as unbootable if it's a kernel
286 // partition, but never if it's anything else.
287 EXPECT_EQ(bootable, !use_kernel_partition);
288
Gilad Arnoldae236702012-05-17 09:33:20 -0700289 return success;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000290}
291
292class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
293 public:
294 void ActionCompleted(ActionProcessor* processor,
295 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700296 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000297 if (action->Type() == FilesystemCopierAction::StaticType()) {
298 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700299 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000300 }
301 }
302 GMainLoop *loop_;
303 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -0700304 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000305};
306
307TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
308 ActionProcessor processor;
309 FilesystemCopierActionTest2Delegate delegate;
310
311 processor.set_delegate(&delegate);
312
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700313 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000314 ObjectCollectorAction<InstallPlan> collector_action;
315
316 BondActions(&copier_action, &collector_action);
317
318 processor.EnqueueAction(&copier_action);
319 processor.EnqueueAction(&collector_action);
320 processor.StartProcessing();
321 EXPECT_FALSE(processor.IsRunning());
322 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700323 EXPECT_EQ(kErrorCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000324}
325
Darin Petkov9b230572010-10-08 10:20:09 -0700326TEST_F(FilesystemCopierActionTest, ResumeTest) {
327 ActionProcessor processor;
328 FilesystemCopierActionTest2Delegate delegate;
329
330 processor.set_delegate(&delegate);
331
332 ObjectFeederAction<InstallPlan> feeder_action;
333 const char* kUrl = "http://some/url";
David Zeuthene7f89172013-10-31 10:21:04 -0700334 InstallPlan install_plan(false, true, kUrl, 0, "", 0, "", "", "", "");
Darin Petkov9b230572010-10-08 10:20:09 -0700335 feeder_action.set_obj(install_plan);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700336 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
Darin Petkov9b230572010-10-08 10:20:09 -0700337 ObjectCollectorAction<InstallPlan> collector_action;
338
339 BondActions(&feeder_action, &copier_action);
340 BondActions(&copier_action, &collector_action);
341
342 processor.EnqueueAction(&feeder_action);
343 processor.EnqueueAction(&copier_action);
344 processor.EnqueueAction(&collector_action);
345 processor.StartProcessing();
346 EXPECT_FALSE(processor.IsRunning());
347 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700348 EXPECT_EQ(kErrorCodeSuccess, delegate.code_);
Darin Petkov9b230572010-10-08 10:20:09 -0700349 EXPECT_EQ(kUrl, collector_action.object().download_url);
350}
351
adlr@google.com3defe6a2009-12-04 20:57:17 +0000352TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) {
353 ActionProcessor processor;
354 FilesystemCopierActionTest2Delegate delegate;
355
356 processor.set_delegate(&delegate);
357
358 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov0406e402010-10-06 21:33:11 -0700359 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700360 false,
Darin Petkov0406e402010-10-06 21:33:11 -0700361 "",
362 0,
363 "",
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700364 0,
365 "",
Darin Petkov0406e402010-10-06 21:33:11 -0700366 "/no/such/file",
David Zeuthene7f89172013-10-31 10:21:04 -0700367 "/no/such/file",
368 "");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000369 feeder_action.set_obj(install_plan);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700370 FilesystemCopierAction copier_action(&fake_system_state_, false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000371 ObjectCollectorAction<InstallPlan> collector_action;
372
373 BondActions(&copier_action, &collector_action);
374
375 processor.EnqueueAction(&feeder_action);
376 processor.EnqueueAction(&copier_action);
377 processor.EnqueueAction(&collector_action);
378 processor.StartProcessing();
379 EXPECT_FALSE(processor.IsRunning());
380 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700381 EXPECT_EQ(kErrorCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000382}
383
Darin Petkov3aefa862010-12-07 14:45:00 -0800384TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) {
385 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700386 EXPECT_TRUE(DoTest(false, false, false, 1));
387 EXPECT_TRUE(DoTest(false, false, true, 1));
Darin Petkov3aefa862010-12-07 14:45:00 -0800388}
389
390TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) {
391 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700392 EXPECT_TRUE(DoTest(false, false, false, 2));
393 EXPECT_TRUE(DoTest(false, false, true, 2));
Darin Petkov3aefa862010-12-07 14:45:00 -0800394}
395
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700396TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000397 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700398 EXPECT_TRUE(DoTest(true, false, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000399}
400
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700401TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000402 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700403 EXPECT_TRUE(DoTest(false, true, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000404}
405
Darin Petkov698d0412010-10-13 10:59:44 -0700406TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) {
407 string img;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800408 EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, NULL));
Darin Petkov698d0412010-10-13 10:59:44 -0700409 ScopedPathUnlinker img_unlinker(img);
410 CreateExtImageAtPath(img, NULL);
411 // Extend the "partition" holding the file system from 10MiB to 20MiB.
Alex Vakulenko75039d72014-03-25 12:36:28 -0700412 EXPECT_EQ(0, System(base::StringPrintf(
Darin Petkov698d0412010-10-13 10:59:44 -0700413 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
414 img.c_str())));
415 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
416
417 for (int i = 0; i < 2; ++i) {
418 bool is_kernel = i == 1;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700419 FilesystemCopierAction action(&fake_system_state_, is_kernel, false);
Darin Petkov698d0412010-10-13 10:59:44 -0700420 EXPECT_EQ(kint64max, action.filesystem_size_);
421 {
422 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY));
423 EXPECT_TRUE(fd > 0);
424 ScopedFdCloser fd_closer(&fd);
425 action.DetermineFilesystemSize(fd);
426 }
427 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024,
428 action.filesystem_size_);
429 }
430}
431
432
adlr@google.com3defe6a2009-12-04 20:57:17 +0000433} // namespace chromeos_update_engine