blob: a626363f71ebe41021566aee852acd9a0ddfed11 [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
11#include <base/eintr_wrapper.h>
Gilad Arnoldae236702012-05-17 09:33:20 -070012// TODO(garnold) remove this include, here for debugging purposes
13#include <base/rand_util.h>
Darin Petkov698d0412010-10-13 10:59:44 -070014#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040015#include <base/stringprintf.h>
Darin Petkov698d0412010-10-13 10:59:44 -070016#include <glib.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000017#include <gtest/gtest.h>
Darin Petkov698d0412010-10-13 10:59:44 -070018
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include "update_engine/filesystem_copier_action.h"
20#include "update_engine/filesystem_iterator.h"
21#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.
36 // TODO(garnold) we temporarily add an |is_debug_random_loop_file_size| to try
37 // copying with different file sizes, in attempt to expose circumstances that
38 // lead to a certain unit test failure. The second variant preserves the
39 // original behavior. This code needs to be reverted.
40 bool DoTest(bool run_out_of_space,
Andrew de los Reyesf9185172010-05-03 11:07:05 -070041 bool terminate_early,
Darin Petkov3aefa862010-12-07 14:45:00 -080042 bool use_kernel_partition,
Gilad Arnoldae236702012-05-17 09:33:20 -070043 int verify_hash,
44 bool is_debug_random_loop_file_size);
45 inline bool DoTest(bool run_out_of_space,
46 bool terminate_early,
47 bool use_kernel_partition,
48 int verify_hash) {
49 return DoTest(run_out_of_space, terminate_early, use_kernel_partition,
50 verify_hash, false);
51 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000052 void SetUp() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000053 }
54 void TearDown() {
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 }
56};
57
58class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
59 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070060 FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {}
Andrew de los Reyesc7020782010-04-28 10:46:04 -070061 void ExitMainLoop() {
62 while (g_main_context_pending(NULL)) {
63 g_main_context_iteration(NULL, false);
64 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000065 g_main_loop_quit(loop_);
66 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070067 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070068 ExitMainLoop();
69 }
70 void ProcessingStopped(const ActionProcessor* processor) {
71 ExitMainLoop();
72 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000073 void ActionCompleted(ActionProcessor* processor,
74 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070075 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000076 if (action->Type() == FilesystemCopierAction::StaticType()) {
77 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070078 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +000079 }
80 }
81 void set_loop(GMainLoop* loop) {
82 loop_ = loop;
83 }
84 bool ran() { return ran_; }
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 ActionExitCode code() { return code_; }
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 private:
87 GMainLoop* loop_;
88 bool ran_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070089 ActionExitCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000090};
91
Andrew de los Reyesc7020782010-04-28 10:46:04 -070092struct StartProcessorCallbackArgs {
93 ActionProcessor* processor;
94 FilesystemCopierAction* filesystem_copier_action;
95 bool terminate_early;
96};
97
adlr@google.com3defe6a2009-12-04 20:57:17 +000098gboolean StartProcessorInRunLoop(gpointer data) {
Andrew de los Reyesc7020782010-04-28 10:46:04 -070099 StartProcessorCallbackArgs* args =
100 reinterpret_cast<StartProcessorCallbackArgs*>(data);
101 ActionProcessor* processor = args->processor;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000102 processor->StartProcessing();
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700103 if (args->terminate_early) {
104 EXPECT_TRUE(args->filesystem_copier_action);
105 args->processor->StopProcessing();
106 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107 return FALSE;
108}
109
110TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) {
111 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700112 EXPECT_TRUE(DoTest(false, false, false, 0, true));
113 EXPECT_TRUE(DoTest(false, false, true, 0, true));
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 Arnoldae236702012-05-17 09:33:20 -0700119 int verify_hash,
120 bool is_debug_random_loop_file_size) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000121 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
122
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700123 string a_loop_file;
124 string b_loop_file;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700125
Gilad Arnoldae236702012-05-17 09:33:20 -0700126 if (!(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", &a_loop_file, NULL) &&
127 utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", &b_loop_file, NULL))) {
128 ADD_FAILURE();
129 return false;
130 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700131 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700132 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700134 // Make random data for a, zero filled data for b.
Gilad Arnoldae236702012-05-17 09:33:20 -0700135 // TODO(garnold) eliminate the random choice, here for debugging purposes of
136 // unittest failures. We're testing the hypothesis that the particular file
137 // size causes this nondeterministic problem.
138 size_t randomLoopFileSize = 10 * 1024 * 1024 + 512; // original value
139 if (is_debug_random_loop_file_size) {
140 int debugRandomChoice = base::RandInt(0, 4);
141 switch (debugRandomChoice) {
142 case 0:
143 break; // keep default value
144 case 1:
145 randomLoopFileSize = 10 * 1024 * 1024; // exactly 10 MB
146 break;
147 case 2:
148 randomLoopFileSize = (10 * 1024 + 128) * 1024; // 10 MB + 128 KB
149 break;
150 case 3:
151 randomLoopFileSize = 30 * 1024 * 1024; // exactly 30 MB
152 break;
153 case 4:
154 randomLoopFileSize = 30 * 1024 * 1024 + 512; // 10 MB + 512 bytes
155 break;
156 default:
157 ADD_FAILURE();
158 return false;
159 }
160 LOG(INFO) << "(debug) randomLoopFileSize=" << randomLoopFileSize
161 << " (option " << debugRandomChoice << ")";
162 }
163 const size_t kLoopFileSize = randomLoopFileSize;
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700164 vector<char> a_loop_data(kLoopFileSize);
165 FillWithData(&a_loop_data);
166 vector<char> b_loop_data(run_out_of_space ?
167 (kLoopFileSize - 1) :
168 kLoopFileSize,
169 '\0'); // Fill with 0s
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700171 // Write data to disk
Gilad Arnoldae236702012-05-17 09:33:20 -0700172 if (!(WriteFileVector(a_loop_file, a_loop_data) &&
173 WriteFileVector(b_loop_file, b_loop_data))) {
174 ADD_FAILURE();
175 return false;
176 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000177
Don Garrett58e8b1f2012-01-31 16:38:16 -0800178 // Attach loop devices to the files
179 string a_dev;
180 string b_dev;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000181
Don Garrett58e8b1f2012-01-31 16:38:16 -0800182 ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev);
183 ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000184
Gilad Arnoldae236702012-05-17 09:33:20 -0700185 bool success = true;
186
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700187 // Set up the action objects
adlr@google.com3defe6a2009-12-04 20:57:17 +0000188 InstallPlan install_plan;
Darin Petkov3aefa862010-12-07 14:45:00 -0800189 if (verify_hash) {
190 if (use_kernel_partition) {
191 install_plan.kernel_install_path = a_dev;
192 install_plan.kernel_size =
193 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700194 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
195 &install_plan.kernel_hash)) {
196 ADD_FAILURE();
197 success = false;
198 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800199 } else {
200 install_plan.install_path = a_dev;
201 install_plan.rootfs_size =
202 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
Gilad Arnoldae236702012-05-17 09:33:20 -0700203 if (!OmahaHashCalculator::RawHashOfData(a_loop_data,
204 &install_plan.rootfs_hash)) {
205 ADD_FAILURE();
206 success = false;
207 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800208 }
209 } else {
210 if (use_kernel_partition) {
211 install_plan.kernel_install_path = b_dev;
212 } else {
213 install_plan.install_path = b_dev;
214 }
215 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216
217 ActionProcessor processor;
218 FilesystemCopierActionTestDelegate delegate;
219 delegate.set_loop(loop);
220 processor.set_delegate(&delegate);
221
222 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov3aefa862010-12-07 14:45:00 -0800223 FilesystemCopierAction copier_action(use_kernel_partition,
224 verify_hash != 0);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000225 ObjectCollectorAction<InstallPlan> collector_action;
226
227 BondActions(&feeder_action, &copier_action);
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700228 BondActions(&copier_action, &collector_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000229
230 processor.EnqueueAction(&feeder_action);
231 processor.EnqueueAction(&copier_action);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000232 processor.EnqueueAction(&collector_action);
233
Darin Petkov3aefa862010-12-07 14:45:00 -0800234 if (!verify_hash) {
235 copier_action.set_copy_source(a_dev);
236 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000237 feeder_action.set_obj(install_plan);
238
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700239 StartProcessorCallbackArgs start_callback_args;
240 start_callback_args.processor = &processor;
241 start_callback_args.filesystem_copier_action = &copier_action;
242 start_callback_args.terminate_early = terminate_early;
243
244 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000245 g_main_loop_run(loop);
246 g_main_loop_unref(loop);
247
Gilad Arnoldae236702012-05-17 09:33:20 -0700248 if (!terminate_early) {
249 bool is_delegate_ran = delegate.ran();
250 EXPECT_TRUE(is_delegate_ran);
251 success = success && is_delegate_ran;
252 }
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700253 if (run_out_of_space || terminate_early) {
Darin Petkovc1a8b422010-07-19 11:34:49 -0700254 EXPECT_EQ(kActionCodeError, delegate.code());
Gilad Arnoldae236702012-05-17 09:33:20 -0700255 return (kActionCodeError == delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000256 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800257 if (verify_hash == 2) {
Gilad Arnoldae236702012-05-17 09:33:20 -0700258 ActionExitCode expected_exit_code =
259 (use_kernel_partition ?
260 kActionCodeNewKernelVerificationError :
261 kActionCodeNewRootfsVerificationError);
262 EXPECT_EQ(expected_exit_code, delegate.code());
263 return (expected_exit_code == delegate.code());
Darin Petkov3aefa862010-12-07 14:45:00 -0800264 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700265 EXPECT_EQ(kActionCodeSuccess, delegate.code());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000266
adlr@google.com3defe6a2009-12-04 20:57:17 +0000267 // Make sure everything in the out_image is there
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700268 vector<char> a_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700269 if (!utils::ReadFile(a_dev, &a_out)) {
270 ADD_FAILURE();
271 return false;
272 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700273 const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out);
274 EXPECT_TRUE(is_a_file_reading_eq);
275 success = success && is_a_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800276 if (!verify_hash) {
277 vector<char> b_out;
Gilad Arnoldae236702012-05-17 09:33:20 -0700278 if (!utils::ReadFile(b_dev, &b_out)) {
279 ADD_FAILURE();
280 return false;
281 }
Gilad Arnold308b85e2012-06-07 15:50:16 -0700282 const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out);
283 EXPECT_TRUE(is_b_file_reading_eq);
284 success = success && is_b_file_reading_eq;
Darin Petkov3aefa862010-12-07 14:45:00 -0800285 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000286
Gilad Arnoldae236702012-05-17 09:33:20 -0700287 bool is_install_plan_eq = (collector_action.object() == install_plan);
288 EXPECT_TRUE(is_install_plan_eq);
289 success = success && is_install_plan_eq;
290
291 return success;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000292}
293
294class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
295 public:
296 void ActionCompleted(ActionProcessor* processor,
297 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700298 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000299 if (action->Type() == FilesystemCopierAction::StaticType()) {
300 ran_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700301 code_ = code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000302 }
303 }
304 GMainLoop *loop_;
305 bool ran_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700306 ActionExitCode code_;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000307};
308
309TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
310 ActionProcessor processor;
311 FilesystemCopierActionTest2Delegate delegate;
312
313 processor.set_delegate(&delegate);
314
Darin Petkov3aefa862010-12-07 14:45:00 -0800315 FilesystemCopierAction copier_action(false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000316 ObjectCollectorAction<InstallPlan> collector_action;
317
318 BondActions(&copier_action, &collector_action);
319
320 processor.EnqueueAction(&copier_action);
321 processor.EnqueueAction(&collector_action);
322 processor.StartProcessing();
323 EXPECT_FALSE(processor.IsRunning());
324 EXPECT_TRUE(delegate.ran_);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700325 EXPECT_EQ(kActionCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000326}
327
Darin Petkov9b230572010-10-08 10:20:09 -0700328TEST_F(FilesystemCopierActionTest, ResumeTest) {
329 ActionProcessor processor;
330 FilesystemCopierActionTest2Delegate delegate;
331
332 processor.set_delegate(&delegate);
333
334 ObjectFeederAction<InstallPlan> feeder_action;
335 const char* kUrl = "http://some/url";
Darin Petkov7ed561b2011-10-04 02:59:03 -0700336 InstallPlan install_plan(true, kUrl, 0, "", "", "");
Darin Petkov9b230572010-10-08 10:20:09 -0700337 feeder_action.set_obj(install_plan);
Darin Petkov3aefa862010-12-07 14:45:00 -0800338 FilesystemCopierAction copier_action(false, false);
Darin Petkov9b230572010-10-08 10:20:09 -0700339 ObjectCollectorAction<InstallPlan> collector_action;
340
341 BondActions(&feeder_action, &copier_action);
342 BondActions(&copier_action, &collector_action);
343
344 processor.EnqueueAction(&feeder_action);
345 processor.EnqueueAction(&copier_action);
346 processor.EnqueueAction(&collector_action);
347 processor.StartProcessing();
348 EXPECT_FALSE(processor.IsRunning());
349 EXPECT_TRUE(delegate.ran_);
350 EXPECT_EQ(kActionCodeSuccess, delegate.code_);
351 EXPECT_EQ(kUrl, collector_action.object().download_url);
352}
353
adlr@google.com3defe6a2009-12-04 20:57:17 +0000354TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) {
355 ActionProcessor processor;
356 FilesystemCopierActionTest2Delegate delegate;
357
358 processor.set_delegate(&delegate);
359
360 ObjectFeederAction<InstallPlan> feeder_action;
Darin Petkov0406e402010-10-06 21:33:11 -0700361 InstallPlan install_plan(false,
Darin Petkov0406e402010-10-06 21:33:11 -0700362 "",
363 0,
364 "",
365 "/no/such/file",
366 "/no/such/file");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000367 feeder_action.set_obj(install_plan);
Darin Petkov3aefa862010-12-07 14:45:00 -0800368 FilesystemCopierAction copier_action(false, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000369 ObjectCollectorAction<InstallPlan> collector_action;
370
371 BondActions(&copier_action, &collector_action);
372
373 processor.EnqueueAction(&feeder_action);
374 processor.EnqueueAction(&copier_action);
375 processor.EnqueueAction(&collector_action);
376 processor.StartProcessing();
377 EXPECT_FALSE(processor.IsRunning());
378 EXPECT_TRUE(delegate.ran_);
Darin Petkovc1a8b422010-07-19 11:34:49 -0700379 EXPECT_EQ(kActionCodeError, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000380}
381
Darin Petkov3aefa862010-12-07 14:45:00 -0800382TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) {
383 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700384 EXPECT_TRUE(DoTest(false, false, false, 1));
385 EXPECT_TRUE(DoTest(false, false, true, 1));
Darin Petkov3aefa862010-12-07 14:45:00 -0800386}
387
388TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) {
389 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700390 EXPECT_TRUE(DoTest(false, false, false, 2));
391 EXPECT_TRUE(DoTest(false, false, true, 2));
Darin Petkov3aefa862010-12-07 14:45:00 -0800392}
393
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700394TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000395 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700396 EXPECT_TRUE(DoTest(true, false, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000397}
398
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700399TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000400 ASSERT_EQ(0, getuid());
Gilad Arnoldae236702012-05-17 09:33:20 -0700401 EXPECT_TRUE(DoTest(false, true, false, 0));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000402}
403
Darin Petkov698d0412010-10-13 10:59:44 -0700404TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) {
405 string img;
406 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL));
407 ScopedPathUnlinker img_unlinker(img);
408 CreateExtImageAtPath(img, NULL);
409 // Extend the "partition" holding the file system from 10MiB to 20MiB.
410 EXPECT_EQ(0, System(StringPrintf(
411 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
412 img.c_str())));
413 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
414
415 for (int i = 0; i < 2; ++i) {
416 bool is_kernel = i == 1;
Darin Petkov3aefa862010-12-07 14:45:00 -0800417 FilesystemCopierAction action(is_kernel, false);
Darin Petkov698d0412010-10-13 10:59:44 -0700418 EXPECT_EQ(kint64max, action.filesystem_size_);
419 {
420 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY));
421 EXPECT_TRUE(fd > 0);
422 ScopedFdCloser fd_closer(&fd);
423 action.DetermineFilesystemSize(fd);
424 }
425 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024,
426 action.filesystem_size_);
427 }
428}
429
430
adlr@google.com3defe6a2009-12-04 20:57:17 +0000431} // namespace chromeos_update_engine