blob: 8b6e6278147532fbb5367f53c103b7524747f230 [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>
Darin Petkov698d0412010-10-13 10:59:44 -070012#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040013#include <base/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
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include "update_engine/filesystem_copier_action.h"
19#include "update_engine/filesystem_iterator.h"
Alex Deymo42432912013-07-12 20:21:15 -070020#include "update_engine/mock_system_state.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 }
44};
45
46class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
47 public:
Ben Chan2add7d72012-10-08 19:28:37 -070048 FilesystemCopierActionTestDelegate(GMainLoop* loop,
49 FilesystemCopierAction* action)
David Zeuthena99981f2013-04-29 13:42:47 -070050 : loop_(loop), action_(action), ran_(false), code_(kErrorCodeError) {}
Andrew de los Reyesc7020782010-04-28 10:46:04 -070051 void ExitMainLoop() {
Ben Chan2add7d72012-10-08 19:28:37 -070052 GMainContext* context = g_main_loop_get_context(loop_);
53 // We cannot use g_main_context_pending() alone to determine if it is safe
54 // to quit the main loop here becasuse g_main_context_pending() may return
55 // FALSE when g_input_stream_read_async() in FilesystemCopierAction has
56 // been cancelled but the callback has not yet been invoked.
57 while (g_main_context_pending(context) || action_->IsCleanupPending()) {
58 g_main_context_iteration(context, false);
59 g_usleep(100);
Andrew de los Reyesc7020782010-04-28 10:46:04 -070060 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000061 g_main_loop_quit(loop_);
62 }
David Zeuthena99981f2013-04-29 13:42:47 -070063 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070064 ExitMainLoop();
65 }
66 void ProcessingStopped(const ActionProcessor* processor) {
67 ExitMainLoop();
68 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000069 void ActionCompleted(ActionProcessor* processor,
70 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070071 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 if (action->Type() == FilesystemCopierAction::StaticType()) {
73 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +000075 }
76 }
Ben Chan2add7d72012-10-08 19:28:37 -070077 bool ran() const { return ran_; }
David Zeuthena99981f2013-04-29 13:42:47 -070078 ErrorCode code() const { return code_; }
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
107// issue with the chroot environiment, library versions we use, etc.
108TEST_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 Garrett83692e42013-11-08 10:11:30 -0800122 MockSystemState mock_system_state;
123
adlr@google.com3defe6a2009-12-04 20:57:17 +0000124 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
125
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700126 string a_loop_file;
127 string b_loop_file;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700128
Gilad Arnoldae236702012-05-17 09:33:20 -0700129 if (!(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", &a_loop_file, NULL) &&
130 utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", &b_loop_file, NULL))) {
131 ADD_FAILURE();
132 return false;
133 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700134 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700135 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700136
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700137 // Make random data for a, zero filled data for b.
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700138 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700139 vector<char> a_loop_data(kLoopFileSize);
140 FillWithData(&a_loop_data);
141 vector<char> b_loop_data(run_out_of_space ?
142 (kLoopFileSize - 1) :
143 kLoopFileSize,
144 '\0'); // Fill with 0s
adlr@google.com3defe6a2009-12-04 20:57:17 +0000145
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700146 // Write data to disk
Gilad Arnoldae236702012-05-17 09:33:20 -0700147 if (!(WriteFileVector(a_loop_file, a_loop_data) &&
148 WriteFileVector(b_loop_file, b_loop_data))) {
149 ADD_FAILURE();
150 return false;
151 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000152
Don Garrett58e8b1f2012-01-31 16:38:16 -0800153 // Attach loop devices to the files
154 string a_dev;
155 string b_dev;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156
Don Garrett58e8b1f2012-01-31 16:38:16 -0800157 ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev);
158 ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev);
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700159 if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) {
160 ADD_FAILURE();
161 return false;
162 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000163
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700164 LOG(INFO) << "copying: "
165 << a_loop_file << " (" << a_dev << ") -> "
166 << b_loop_file << " (" << b_dev << ", "
167 << kLoopFileSize << " bytes";
Gilad Arnoldae236702012-05-17 09:33:20 -0700168 bool success = true;
169
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700170 // Set up the action objects
adlr@google.com3defe6a2009-12-04 20:57:17 +0000171 InstallPlan install_plan;
Darin Petkov3aefa862010-12-07 14:45:00 -0800172 if (verify_hash) {
173 if (use_kernel_partition) {
174 install_plan.kernel_install_path = a_dev;
175 install_plan.kernel_size =
176 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700177 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
178 &install_plan.kernel_hash)) {
179 ADD_FAILURE();
180 success = false;
181 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800182 } else {
183 install_plan.install_path = a_dev;
184 install_plan.rootfs_size =
185 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700186 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
187 &install_plan.rootfs_hash)) {
188 ADD_FAILURE();
189 success = false;
190 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800191 }
192 } else {
193 if (use_kernel_partition) {
194 install_plan.kernel_install_path = b_dev;
195 } else {
196 install_plan.install_path = b_dev;
197 }
198 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199
Don Garrett83692e42013-11-08 10:11:30 -0800200 EXPECT_CALL(mock_system_state.get_mock_hardware(),
201 MarkKernelUnbootable(a_dev)).Times(use_kernel_partition ? 1 : 0);
202
adlr@google.com3defe6a2009-12-04 20:57:17 +0000203 ActionProcessor processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000204
205 ObjectFeederAction<InstallPlan> feeder_action;
Don Garrett83692e42013-11-08 10:11:30 -0800206 FilesystemCopierAction copier_action(&mock_system_state,
Alex Deymo42432912013-07-12 20:21:15 -0700207 use_kernel_partition,
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700208 verify_hash != 0);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000209 ObjectCollectorAction<InstallPlan> collector_action;
210
211 BondActions(&feeder_action, &copier_action);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700212 BondActions(&copier_action, &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000213
Ben Chan2add7d72012-10-08 19:28:37 -0700214 FilesystemCopierActionTestDelegate delegate(loop, &copier_action);
215 processor.set_delegate(&delegate);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216 processor.EnqueueAction(&feeder_action);
217 processor.EnqueueAction(&copier_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218 processor.EnqueueAction(&collector_action);
219
Darin Petkov3aefa862010-12-07 14:45:00 -0800220 if (!verify_hash) {
221 copier_action.set_copy_source(a_dev);
222 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223 feeder_action.set_obj(install_plan);
224
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700225 StartProcessorCallbackArgs start_callback_args;
226 start_callback_args.processor = &processor;
227 start_callback_args.filesystem_copier_action = &copier_action;
228 start_callback_args.terminate_early = terminate_early;
229
230 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000231 g_main_loop_run(loop);
232 g_main_loop_unref(loop);
233
Gilad Arnoldae236702012-05-17 09:33:20 -0700234 if (!terminate_early) {
235 bool is_delegate_ran = delegate.ran();
236 EXPECT_TRUE(is_delegate_ran);
237 success = success && is_delegate_ran;
238 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700239 if (run_out_of_space || terminate_early) {
David Zeuthena99981f2013-04-29 13:42:47 -0700240 EXPECT_EQ(kErrorCodeError, delegate.code());
241 return (kErrorCodeError == delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000242 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800243 if (verify_hash == 2) {
David Zeuthena99981f2013-04-29 13:42:47 -0700244 ErrorCode expected_exit_code =
Gilad Arnoldae236702012-05-17 09:33:20 -0700245 (use_kernel_partition ?
David Zeuthena99981f2013-04-29 13:42:47 -0700246 kErrorCodeNewKernelVerificationError :
247 kErrorCodeNewRootfsVerificationError);
Gilad Arnoldae236702012-05-17 09:33:20 -0700248 EXPECT_EQ(expected_exit_code, delegate.code());
249 return (expected_exit_code == delegate.code());
Darin Petkov3aefa862010-12-07 14:45:00 -0800250 }
David Zeuthena99981f2013-04-29 13:42:47 -0700251 EXPECT_EQ(kErrorCodeSuccess, delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000252
adlr@google.com3defe6a2009-12-04 20:57:17 +0000253 // Make sure everything in the out_image is there
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700254 vector<char> a_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700255 if (!utils::ReadFile(a_dev, &a_out)) {
256 ADD_FAILURE();
257 return false;
258 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700259 const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out);
260 EXPECT_TRUE(is_a_file_reading_eq);
261 success = success && is_a_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800262 if (!verify_hash) {
263 vector<char> b_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700264 if (!utils::ReadFile(b_dev, &b_out)) {
265 ADD_FAILURE();
266 return false;
267 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700268 const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out);
269 EXPECT_TRUE(is_b_file_reading_eq);
270 success = success && is_b_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800271 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000272
Gilad Arnoldae236702012-05-17 09:33:20 -0700273 bool is_install_plan_eq = (collector_action.object() == install_plan);
274 EXPECT_TRUE(is_install_plan_eq);
275 success = success && is_install_plan_eq;
276
Don Garrett83692e42013-11-08 10:11:30 -0800277 LOG(INFO) << "Verifying bootable flag on: " << a_dev;
278 bool bootable;
279 EXPECT_TRUE(mock_system_state.get_mock_hardware().fake().
280 IsKernelBootable(a_dev, &bootable));
281 // We should always mark a partition as unbootable if it's a kernel
282 // partition, but never if it's anything else.
283 EXPECT_EQ(bootable, !use_kernel_partition);
284
Gilad Arnoldae236702012-05-17 09:33:20 -0700285 return success;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000286}
287
288class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
289 public:
290 void ActionCompleted(ActionProcessor* processor,
291 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700292 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000293 if (action->Type() == FilesystemCopierAction::StaticType()) {
294 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700295 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000296 }
297 }
298 GMainLoop *loop_;
299 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -0700300 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000301};
302
303TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
304 ActionProcessor processor;
Don Garrett83692e42013-11-08 10:11:30 -0800305 MockSystemState mock_system_state;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000306 FilesystemCopierActionTest2Delegate delegate;
307
308 processor.set_delegate(&delegate);
309
Don Garrett83692e42013-11-08 10:11:30 -0800310 FilesystemCopierAction copier_action(&mock_system_state, false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000311 ObjectCollectorAction<InstallPlan> collector_action;
312
313 BondActions(&copier_action, &collector_action);
314
315 processor.EnqueueAction(&copier_action);
316 processor.EnqueueAction(&collector_action);
317 processor.StartProcessing();
318 EXPECT_FALSE(processor.IsRunning());
319 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700320 EXPECT_EQ(kErrorCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000321}
322
Darin Petkov9b230572010-10-08 10:20:09 -0700323TEST_F(FilesystemCopierActionTest, ResumeTest) {
324 ActionProcessor processor;
Don Garrett83692e42013-11-08 10:11:30 -0800325 MockSystemState mock_system_state;
Darin Petkov9b230572010-10-08 10:20:09 -0700326 FilesystemCopierActionTest2Delegate delegate;
327
328 processor.set_delegate(&delegate);
329
330 ObjectFeederAction<InstallPlan> feeder_action;
331 const char* kUrl = "http://some/url";
David Zeuthene7f89172013-10-31 10:21:04 -0700332 InstallPlan install_plan(false, true, kUrl, 0, "", 0, "", "", "", "");
Darin Petkov9b230572010-10-08 10:20:09 -0700333 feeder_action.set_obj(install_plan);
Don Garrett83692e42013-11-08 10:11:30 -0800334 FilesystemCopierAction copier_action(&mock_system_state, false, false);
Darin Petkov9b230572010-10-08 10:20:09 -0700335 ObjectCollectorAction<InstallPlan> collector_action;
336
337 BondActions(&feeder_action, &copier_action);
338 BondActions(&copier_action, &collector_action);
339
340 processor.EnqueueAction(&feeder_action);
341 processor.EnqueueAction(&copier_action);
342 processor.EnqueueAction(&collector_action);
343 processor.StartProcessing();
344 EXPECT_FALSE(processor.IsRunning());
345 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700346 EXPECT_EQ(kErrorCodeSuccess, delegate.code_);
Darin Petkov9b230572010-10-08 10:20:09 -0700347 EXPECT_EQ(kUrl, collector_action.object().download_url);
348}
349
adlr@google.com3defe6a2009-12-04 20:57:17 +0000350TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) {
351 ActionProcessor processor;
Don Garrett83692e42013-11-08 10:11:30 -0800352 MockSystemState mock_system_state;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000353 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);
Don Garrett83692e42013-11-08 10:11:30 -0800369 FilesystemCopierAction copier_action(&mock_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_);
David Zeuthena99981f2013-04-29 13:42:47 -0700380 EXPECT_EQ(kErrorCodeError, 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) {
Don Garrett83692e42013-11-08 10:11:30 -0800406 MockSystemState mock_system_state;
Darin Petkov698d0412010-10-13 10:59:44 -0700407 string img;
408 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL));
409 ScopedPathUnlinker img_unlinker(img);
410 CreateExtImageAtPath(img, NULL);
411 // Extend the "partition" holding the file system from 10MiB to 20MiB.
412 EXPECT_EQ(0, System(StringPrintf(
413 "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;
Don Garrett83692e42013-11-08 10:11:30 -0800419 FilesystemCopierAction action(&mock_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