blob: 0db1640531e92d677531ddfeaa9d8e58be520990 [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>
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include <gtest/gtest.h>
Darin Petkov698d0412010-10-13 10:59:44 -070016
adlr@google.com3defe6a2009-12-04 20:57:17 +000017#include "update_engine/filesystem_copier_action.h"
18#include "update_engine/filesystem_iterator.h"
19#include "update_engine/omaha_hash_calculator.h"
20#include "update_engine/test_utils.h"
21#include "update_engine/utils.h"
22
23using std::set;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080024using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000025using std::vector;
26
27namespace chromeos_update_engine {
28
29class FilesystemCopierActionTest : public ::testing::Test {
30 protected:
Darin Petkov3aefa862010-12-07 14:45:00 -080031 // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification,
32 // 2 -- hash verification failure.
Gilad Arnoldae236702012-05-17 09:33:20 -070033 // Returns true iff test has completed successfully.
Gilad Arnoldae236702012-05-17 09:33:20 -070034 bool DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -070035 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -080036 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -070037 int verify_hash);
adlr@google.com3defe6a2009-12-04 20:57:17 +000038 void SetUp() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000039 }
40 void TearDown() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000041 }
42};
43
44class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
45 public:
Ben Chan2add7d72012-10-08 19:28:37 -070046 FilesystemCopierActionTestDelegate(GMainLoop* loop,
47 FilesystemCopierAction* action)
David Zeuthena99981f2013-04-29 13:42:47 -070048 : loop_(loop), action_(action), ran_(false), code_(kErrorCodeError) {}
Andrew de los Reyesc7020782010-04-28 10:46:04 -070049 void ExitMainLoop() {
Ben Chan2add7d72012-10-08 19:28:37 -070050 GMainContext* context = g_main_loop_get_context(loop_);
51 // We cannot use g_main_context_pending() alone to determine if it is safe
52 // to quit the main loop here becasuse g_main_context_pending() may return
53 // FALSE when g_input_stream_read_async() in FilesystemCopierAction has
54 // been cancelled but the callback has not yet been invoked.
55 while (g_main_context_pending(context) || action_->IsCleanupPending()) {
56 g_main_context_iteration(context, false);
57 g_usleep(100);
Andrew de los Reyesc7020782010-04-28 10:46:04 -070058 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 g_main_loop_quit(loop_);
60 }
David Zeuthena99981f2013-04-29 13:42:47 -070061 void ProcessingDone(const ActionProcessor* processor, ErrorCode code) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070062 ExitMainLoop();
63 }
64 void ProcessingStopped(const ActionProcessor* processor) {
65 ExitMainLoop();
66 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000067 void ActionCompleted(ActionProcessor* processor,
68 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070069 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000070 if (action->Type() == FilesystemCopierAction::StaticType()) {
71 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070072 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +000073 }
74 }
Ben Chan2add7d72012-10-08 19:28:37 -070075 bool ran() const { return ran_; }
David Zeuthena99981f2013-04-29 13:42:47 -070076 ErrorCode code() const { return code_; }
adlr@google.com3defe6a2009-12-04 20:57:17 +000077 private:
78 GMainLoop* loop_;
Ben Chan2add7d72012-10-08 19:28:37 -070079 FilesystemCopierAction* action_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000080 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -070081 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000082};
83
Andrew de los Reyesc7020782010-04-28 10:46:04 -070084struct StartProcessorCallbackArgs {
85 ActionProcessor* processor;
86 FilesystemCopierAction* filesystem_copier_action;
87 bool terminate_early;
88};
89
adlr@google.com3defe6a2009-12-04 20:57:17 +000090gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070091 StartProcessorCallbackArgs* args =
92 reinterpret_cast<StartProcessorCallbackArgs*>(data);
93 ActionProcessor* processor = args->processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +000094 processor->StartProcessing();
Andrew de los Reyesc7020782010-04-28 10:46:04 -070095 if (args->terminate_early) {
96 EXPECT_TRUE(args->filesystem_copier_action);
97 args->processor->StopProcessing();
98 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000099 return FALSE;
100}
101
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700102// TODO(garnold) Temporarily disabling this test, see chromium-os:31082 for
103// details; still trying to track down the root cause for these rare write
104// failures and whether or not they are due to the test setup or an inherent
105// issue with the chroot environiment, library versions we use, etc.
106TEST_F(FilesystemCopierActionTest, DISABLED_RunAsRootSimpleTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107 ASSERT_EQ(0, getuid());
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700108 bool test = DoTest(false, false, false, 0);
109 EXPECT_TRUE(test);
110 if (!test)
111 return;
112 test = DoTest(false, false, true, 0);
113 EXPECT_TRUE(test);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000114}
Darin Petkov3aefa862010-12-07 14:45:00 -0800115
Gilad Arnoldae236702012-05-17 09:33:20 -0700116bool FilesystemCopierActionTest::DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700117 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -0800118 bool use_kernel_partition,
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700119 int verify_hash) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000120 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
121
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700122 string a_loop_file;
123 string b_loop_file;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700124
Gilad Arnoldae236702012-05-17 09:33:20 -0700125 if (!(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", &a_loop_file, NULL) &&
126 utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", &b_loop_file, NULL))) {
127 ADD_FAILURE();
128 return false;
129 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700130 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700131 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700132
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700133 // Make random data for a, zero filled data for b.
Gilad Arnold6dbbd392012-07-10 16:19:11 -0700134 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512;
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700135 vector<char> a_loop_data(kLoopFileSize);
136 FillWithData(&a_loop_data);
137 vector<char> b_loop_data(run_out_of_space ?
138 (kLoopFileSize - 1) :
139 kLoopFileSize,
140 '\0'); // Fill with 0s
adlr@google.com3defe6a2009-12-04 20:57:17 +0000141
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700142 // Write data to disk
Gilad Arnoldae236702012-05-17 09:33:20 -0700143 if (!(WriteFileVector(a_loop_file, a_loop_data) &&
144 WriteFileVector(b_loop_file, b_loop_data))) {
145 ADD_FAILURE();
146 return false;
147 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148
Don Garrett58e8b1f2012-01-31 16:38:16 -0800149 // Attach loop devices to the files
150 string a_dev;
151 string b_dev;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000152
Don Garrett58e8b1f2012-01-31 16:38:16 -0800153 ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev);
154 ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev);
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700155 if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) {
156 ADD_FAILURE();
157 return false;
158 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159
Gilad Arnoldc33faeb2012-07-24 15:11:11 -0700160 LOG(INFO) << "copying: "
161 << a_loop_file << " (" << a_dev << ") -> "
162 << b_loop_file << " (" << b_dev << ", "
163 << kLoopFileSize << " bytes";
Gilad Arnoldae236702012-05-17 09:33:20 -0700164 bool success = true;
165
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700166 // Set up the action objects
adlr@google.com3defe6a2009-12-04 20:57:17 +0000167 InstallPlan install_plan;
Darin Petkov3aefa862010-12-07 14:45:00 -0800168 if (verify_hash) {
169 if (use_kernel_partition) {
170 install_plan.kernel_install_path = a_dev;
171 install_plan.kernel_size =
172 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700173 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
174 &install_plan.kernel_hash)) {
175 ADD_FAILURE();
176 success = false;
177 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800178 } else {
179 install_plan.install_path = a_dev;
180 install_plan.rootfs_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.rootfs_hash)) {
184 ADD_FAILURE();
185 success = false;
186 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800187 }
188 } else {
189 if (use_kernel_partition) {
190 install_plan.kernel_install_path = b_dev;
191 } else {
192 install_plan.install_path = b_dev;
193 }
194 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195
196 ActionProcessor processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000197
198 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov3aefa862010-12-07 14:45:00 -0800199 FilesystemCopierAction copier_action(use_kernel_partition,
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700200 verify_hash != 0);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 ObjectCollectorAction<InstallPlan> collector_action;
202
203 BondActions(&feeder_action, &copier_action);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700204 BondActions(&copier_action, &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000205
Ben Chan2add7d72012-10-08 19:28:37 -0700206 FilesystemCopierActionTestDelegate delegate(loop, &copier_action);
207 processor.set_delegate(&delegate);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208 processor.EnqueueAction(&feeder_action);
209 processor.EnqueueAction(&copier_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000210 processor.EnqueueAction(&collector_action);
211
Darin Petkov3aefa862010-12-07 14:45:00 -0800212 if (!verify_hash) {
213 copier_action.set_copy_source(a_dev);
214 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000215 feeder_action.set_obj(install_plan);
216
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700217 StartProcessorCallbackArgs start_callback_args;
218 start_callback_args.processor = &processor;
219 start_callback_args.filesystem_copier_action = &copier_action;
220 start_callback_args.terminate_early = terminate_early;
221
222 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223 g_main_loop_run(loop);
224 g_main_loop_unref(loop);
225
Gilad Arnoldae236702012-05-17 09:33:20 -0700226 if (!terminate_early) {
227 bool is_delegate_ran = delegate.ran();
228 EXPECT_TRUE(is_delegate_ran);
229 success = success && is_delegate_ran;
230 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700231 if (run_out_of_space || terminate_early) {
David Zeuthena99981f2013-04-29 13:42:47 -0700232 EXPECT_EQ(kErrorCodeError, delegate.code());
233 return (kErrorCodeError == delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000234 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800235 if (verify_hash == 2) {
David Zeuthena99981f2013-04-29 13:42:47 -0700236 ErrorCode expected_exit_code =
Gilad Arnoldae236702012-05-17 09:33:20 -0700237 (use_kernel_partition ?
David Zeuthena99981f2013-04-29 13:42:47 -0700238 kErrorCodeNewKernelVerificationError :
239 kErrorCodeNewRootfsVerificationError);
Gilad Arnoldae236702012-05-17 09:33:20 -0700240 EXPECT_EQ(expected_exit_code, delegate.code());
241 return (expected_exit_code == delegate.code());
Darin Petkov3aefa862010-12-07 14:45:00 -0800242 }
David Zeuthena99981f2013-04-29 13:42:47 -0700243 EXPECT_EQ(kErrorCodeSuccess, delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000244
adlr@google.com3defe6a2009-12-04 20:57:17 +0000245 // Make sure everything in the out_image is there
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700246 vector<char> a_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700247 if (!utils::ReadFile(a_dev, &a_out)) {
248 ADD_FAILURE();
249 return false;
250 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700251 const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out);
252 EXPECT_TRUE(is_a_file_reading_eq);
253 success = success && is_a_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800254 if (!verify_hash) {
255 vector<char> b_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700256 if (!utils::ReadFile(b_dev, &b_out)) {
257 ADD_FAILURE();
258 return false;
259 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700260 const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out);
261 EXPECT_TRUE(is_b_file_reading_eq);
262 success = success && is_b_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800263 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000264
Gilad Arnoldae236702012-05-17 09:33:20 -0700265 bool is_install_plan_eq = (collector_action.object() == install_plan);
266 EXPECT_TRUE(is_install_plan_eq);
267 success = success && is_install_plan_eq;
268
269 return success;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000270}
271
272class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
273 public:
274 void ActionCompleted(ActionProcessor* processor,
275 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700276 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000277 if (action->Type() == FilesystemCopierAction::StaticType()) {
278 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700279 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000280 }
281 }
282 GMainLoop *loop_;
283 bool ran_;
David Zeuthena99981f2013-04-29 13:42:47 -0700284 ErrorCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000285};
286
287TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
288 ActionProcessor processor;
289 FilesystemCopierActionTest2Delegate delegate;
290
291 processor.set_delegate(&delegate);
292
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700293 FilesystemCopierAction copier_action(false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000294 ObjectCollectorAction<InstallPlan> collector_action;
295
296 BondActions(&copier_action, &collector_action);
297
298 processor.EnqueueAction(&copier_action);
299 processor.EnqueueAction(&collector_action);
300 processor.StartProcessing();
301 EXPECT_FALSE(processor.IsRunning());
302 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700303 EXPECT_EQ(kErrorCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000304}
305
Darin Petkov9b230572010-10-08 10:20:09 -0700306TEST_F(FilesystemCopierActionTest, ResumeTest) {
307 ActionProcessor processor;
308 FilesystemCopierActionTest2Delegate delegate;
309
310 processor.set_delegate(&delegate);
311
312 ObjectFeederAction<InstallPlan> feeder_action;
313 const char* kUrl = "http://some/url";
Gilad Arnold21504f02013-05-24 08:51:22 -0700314 InstallPlan install_plan(false, true, kUrl, 0, "", 0, "", "", "");
Darin Petkov9b230572010-10-08 10:20:09 -0700315 feeder_action.set_obj(install_plan);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700316 FilesystemCopierAction copier_action(false, false);
Darin Petkov9b230572010-10-08 10:20:09 -0700317 ObjectCollectorAction<InstallPlan> collector_action;
318
319 BondActions(&feeder_action, &copier_action);
320 BondActions(&copier_action, &collector_action);
321
322 processor.EnqueueAction(&feeder_action);
323 processor.EnqueueAction(&copier_action);
324 processor.EnqueueAction(&collector_action);
325 processor.StartProcessing();
326 EXPECT_FALSE(processor.IsRunning());
327 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700328 EXPECT_EQ(kErrorCodeSuccess, delegate.code_);
Darin Petkov9b230572010-10-08 10:20:09 -0700329 EXPECT_EQ(kUrl, collector_action.object().download_url);
330}
331
adlr@google.com3defe6a2009-12-04 20:57:17 +0000332TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) {
333 ActionProcessor processor;
334 FilesystemCopierActionTest2Delegate delegate;
335
336 processor.set_delegate(&delegate);
337
338 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov0406e402010-10-06 21:33:11 -0700339 InstallPlan install_plan(false,
Gilad Arnold21504f02013-05-24 08:51:22 -0700340 false,
Darin Petkov0406e402010-10-06 21:33:11 -0700341 "",
342 0,
343 "",
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700344 0,
345 "",
Darin Petkov0406e402010-10-06 21:33:11 -0700346 "/no/such/file",
347 "/no/such/file");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000348 feeder_action.set_obj(install_plan);
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700349 FilesystemCopierAction copier_action(false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000350 ObjectCollectorAction<InstallPlan> collector_action;
351
352 BondActions(&copier_action, &collector_action);
353
354 processor.EnqueueAction(&feeder_action);
355 processor.EnqueueAction(&copier_action);
356 processor.EnqueueAction(&collector_action);
357 processor.StartProcessing();
358 EXPECT_FALSE(processor.IsRunning());
359 EXPECT_TRUE(delegate.ran_);
David Zeuthena99981f2013-04-29 13:42:47 -0700360 EXPECT_EQ(kErrorCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000361}
362
Darin Petkov3aefa862010-12-07 14:45:00 -0800363TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) {
364 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700365 EXPECT_TRUE(DoTest(false, false, false, 1));
366 EXPECT_TRUE(DoTest(false, false, true, 1));
Darin Petkov3aefa862010-12-07 14:45:00 -0800367}
368
369TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) {
370 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700371 EXPECT_TRUE(DoTest(false, false, false, 2));
372 EXPECT_TRUE(DoTest(false, false, true, 2));
Darin Petkov3aefa862010-12-07 14:45:00 -0800373}
374
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700375TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000376 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700377 EXPECT_TRUE(DoTest(true, false, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000378}
379
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700380TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000381 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700382 EXPECT_TRUE(DoTest(false, true, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000383}
384
Darin Petkov698d0412010-10-13 10:59:44 -0700385TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) {
386 string img;
387 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL));
388 ScopedPathUnlinker img_unlinker(img);
389 CreateExtImageAtPath(img, NULL);
390 // Extend the "partition" holding the file system from 10MiB to 20MiB.
391 EXPECT_EQ(0, System(StringPrintf(
392 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
393 img.c_str())));
394 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
395
396 for (int i = 0; i < 2; ++i) {
397 bool is_kernel = i == 1;
Gilad Arnold581c2ea2012-07-19 12:33:49 -0700398 FilesystemCopierAction action(is_kernel, false);
Darin Petkov698d0412010-10-13 10:59:44 -0700399 EXPECT_EQ(kint64max, action.filesystem_size_);
400 {
401 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY));
402 EXPECT_TRUE(fd > 0);
403 ScopedFdCloser fd_closer(&fd);
404 action.DetermineFilesystemSize(fd);
405 }
406 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024,
407 action.filesystem_size_);
408 }
409}
410
411
adlr@google.com3defe6a2009-12-04 20:57:17 +0000412} // namespace chromeos_update_engine