Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 5 | #include <fcntl.h> |
| 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <set> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 8 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <vector> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/eintr_wrapper.h> |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 12 | // TODO(garnold) remove this include, here for debugging purposes |
| 13 | #include <base/rand_util.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 14 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 16 | #include <glib.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 18 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | #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 | |
| 25 | using std::set; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 26 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | using std::vector; |
| 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | class FilesystemCopierActionTest : public ::testing::Test { |
| 32 | protected: |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 33 | // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification, |
| 34 | // 2 -- hash verification failure. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 35 | // 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 Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 41 | bool terminate_early, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 42 | bool use_kernel_partition, |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 43 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 52 | void SetUp() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 53 | } |
| 54 | void TearDown() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | } |
| 56 | }; |
| 57 | |
| 58 | class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 59 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 60 | FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {} |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 61 | void ExitMainLoop() { |
| 62 | while (g_main_context_pending(NULL)) { |
| 63 | g_main_context_iteration(NULL, false); |
| 64 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | g_main_loop_quit(loop_); |
| 66 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 67 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 68 | ExitMainLoop(); |
| 69 | } |
| 70 | void ProcessingStopped(const ActionProcessor* processor) { |
| 71 | ExitMainLoop(); |
| 72 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | void ActionCompleted(ActionProcessor* processor, |
| 74 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 75 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 76 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 77 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 78 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | void set_loop(GMainLoop* loop) { |
| 82 | loop_ = loop; |
| 83 | } |
| 84 | bool ran() { return ran_; } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 85 | ActionExitCode code() { return code_; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 86 | private: |
| 87 | GMainLoop* loop_; |
| 88 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 89 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 92 | struct StartProcessorCallbackArgs { |
| 93 | ActionProcessor* processor; |
| 94 | FilesystemCopierAction* filesystem_copier_action; |
| 95 | bool terminate_early; |
| 96 | }; |
| 97 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 98 | gboolean StartProcessorInRunLoop(gpointer data) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 99 | StartProcessorCallbackArgs* args = |
| 100 | reinterpret_cast<StartProcessorCallbackArgs*>(data); |
| 101 | ActionProcessor* processor = args->processor; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 102 | processor->StartProcessing(); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 103 | if (args->terminate_early) { |
| 104 | EXPECT_TRUE(args->filesystem_copier_action); |
| 105 | args->processor->StopProcessing(); |
| 106 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | return FALSE; |
| 108 | } |
| 109 | |
| 110 | TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) { |
| 111 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 112 | EXPECT_TRUE(DoTest(false, false, false, 0, true)); |
| 113 | EXPECT_TRUE(DoTest(false, false, true, 0, true)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 114 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 115 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 116 | bool FilesystemCopierActionTest::DoTest(bool run_out_of_space, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 117 | bool terminate_early, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 118 | bool use_kernel_partition, |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 119 | int verify_hash, |
| 120 | bool is_debug_random_loop_file_size) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 122 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 123 | string a_loop_file; |
| 124 | string b_loop_file; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 125 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 126 | 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 Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 131 | ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 132 | ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 133 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 134 | // Make random data for a, zero filled data for b. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 135 | // 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 Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 164 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 170 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 171 | // Write data to disk |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 172 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 177 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 178 | // Attach loop devices to the files |
| 179 | string a_dev; |
| 180 | string b_dev; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 181 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 182 | ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev); |
| 183 | ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 184 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 185 | bool success = true; |
| 186 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 187 | // Set up the action objects |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | InstallPlan install_plan; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 189 | 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 Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 194 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 195 | &install_plan.kernel_hash)) { |
| 196 | ADD_FAILURE(); |
| 197 | success = false; |
| 198 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 199 | } else { |
| 200 | install_plan.install_path = a_dev; |
| 201 | install_plan.rootfs_size = |
| 202 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 203 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 204 | &install_plan.rootfs_hash)) { |
| 205 | ADD_FAILURE(); |
| 206 | success = false; |
| 207 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 208 | } |
| 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 216 | |
| 217 | ActionProcessor processor; |
| 218 | FilesystemCopierActionTestDelegate delegate; |
| 219 | delegate.set_loop(loop); |
| 220 | processor.set_delegate(&delegate); |
| 221 | |
| 222 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 223 | FilesystemCopierAction copier_action(use_kernel_partition, |
| 224 | verify_hash != 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 225 | ObjectCollectorAction<InstallPlan> collector_action; |
| 226 | |
| 227 | BondActions(&feeder_action, &copier_action); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 228 | BondActions(&copier_action, &collector_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 229 | |
| 230 | processor.EnqueueAction(&feeder_action); |
| 231 | processor.EnqueueAction(&copier_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 232 | processor.EnqueueAction(&collector_action); |
| 233 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 234 | if (!verify_hash) { |
| 235 | copier_action.set_copy_source(a_dev); |
| 236 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 237 | feeder_action.set_obj(install_plan); |
| 238 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 239 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 245 | g_main_loop_run(loop); |
| 246 | g_main_loop_unref(loop); |
| 247 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 248 | 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 Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 253 | if (run_out_of_space || terminate_early) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 254 | EXPECT_EQ(kActionCodeError, delegate.code()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 255 | return (kActionCodeError == delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 256 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 257 | if (verify_hash == 2) { |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 258 | 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 Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 264 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 265 | EXPECT_EQ(kActionCodeSuccess, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 266 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 267 | // Make sure everything in the out_image is there |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 268 | vector<char> a_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 269 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 270 | ADD_FAILURE(); |
| 271 | return false; |
| 272 | } |
| 273 | success = success && ExpectVectorsEq(a_loop_data, a_out); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 274 | if (!verify_hash) { |
| 275 | vector<char> b_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 276 | if (!utils::ReadFile(b_dev, &b_out)) { |
| 277 | ADD_FAILURE(); |
| 278 | return false; |
| 279 | } |
| 280 | success = success && ExpectVectorsEq(a_out, b_out); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 281 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 282 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 283 | bool is_install_plan_eq = (collector_action.object() == install_plan); |
| 284 | EXPECT_TRUE(is_install_plan_eq); |
| 285 | success = success && is_install_plan_eq; |
| 286 | |
| 287 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 291 | public: |
| 292 | void ActionCompleted(ActionProcessor* processor, |
| 293 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 294 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 295 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 296 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 297 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | GMainLoop *loop_; |
| 301 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 302 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 306 | ActionProcessor processor; |
| 307 | FilesystemCopierActionTest2Delegate delegate; |
| 308 | |
| 309 | processor.set_delegate(&delegate); |
| 310 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 311 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 312 | ObjectCollectorAction<InstallPlan> collector_action; |
| 313 | |
| 314 | BondActions(&copier_action, &collector_action); |
| 315 | |
| 316 | processor.EnqueueAction(&copier_action); |
| 317 | processor.EnqueueAction(&collector_action); |
| 318 | processor.StartProcessing(); |
| 319 | EXPECT_FALSE(processor.IsRunning()); |
| 320 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 321 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 324 | TEST_F(FilesystemCopierActionTest, ResumeTest) { |
| 325 | ActionProcessor processor; |
| 326 | FilesystemCopierActionTest2Delegate delegate; |
| 327 | |
| 328 | processor.set_delegate(&delegate); |
| 329 | |
| 330 | ObjectFeederAction<InstallPlan> feeder_action; |
| 331 | const char* kUrl = "http://some/url"; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 332 | InstallPlan install_plan(true, kUrl, 0, "", "", ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 333 | feeder_action.set_obj(install_plan); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 334 | FilesystemCopierAction copier_action(false, false); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 335 | 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_); |
| 346 | EXPECT_EQ(kActionCodeSuccess, delegate.code_); |
| 347 | EXPECT_EQ(kUrl, collector_action.object().download_url); |
| 348 | } |
| 349 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 350 | TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 351 | ActionProcessor processor; |
| 352 | FilesystemCopierActionTest2Delegate delegate; |
| 353 | |
| 354 | processor.set_delegate(&delegate); |
| 355 | |
| 356 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 357 | InstallPlan install_plan(false, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 358 | "", |
| 359 | 0, |
| 360 | "", |
| 361 | "/no/such/file", |
| 362 | "/no/such/file"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 363 | feeder_action.set_obj(install_plan); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 364 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 365 | ObjectCollectorAction<InstallPlan> collector_action; |
| 366 | |
| 367 | BondActions(&copier_action, &collector_action); |
| 368 | |
| 369 | processor.EnqueueAction(&feeder_action); |
| 370 | processor.EnqueueAction(&copier_action); |
| 371 | processor.EnqueueAction(&collector_action); |
| 372 | processor.StartProcessing(); |
| 373 | EXPECT_FALSE(processor.IsRunning()); |
| 374 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 375 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 378 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) { |
| 379 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 380 | EXPECT_TRUE(DoTest(false, false, false, 1)); |
| 381 | EXPECT_TRUE(DoTest(false, false, true, 1)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) { |
| 385 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 386 | EXPECT_TRUE(DoTest(false, false, false, 2)); |
| 387 | EXPECT_TRUE(DoTest(false, false, true, 2)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 390 | TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 391 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 392 | EXPECT_TRUE(DoTest(true, false, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 395 | TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 396 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 397 | EXPECT_TRUE(DoTest(false, true, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 400 | TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) { |
| 401 | string img; |
| 402 | EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL)); |
| 403 | ScopedPathUnlinker img_unlinker(img); |
| 404 | CreateExtImageAtPath(img, NULL); |
| 405 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
| 406 | EXPECT_EQ(0, System(StringPrintf( |
| 407 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 408 | img.c_str()))); |
| 409 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 410 | |
| 411 | for (int i = 0; i < 2; ++i) { |
| 412 | bool is_kernel = i == 1; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 413 | FilesystemCopierAction action(is_kernel, false); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 414 | EXPECT_EQ(kint64max, action.filesystem_size_); |
| 415 | { |
| 416 | int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY)); |
| 417 | EXPECT_TRUE(fd > 0); |
| 418 | ScopedFdCloser fd_closer(&fd); |
| 419 | action.DetermineFilesystemSize(fd); |
| 420 | } |
| 421 | EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024, |
| 422 | action.filesystem_size_); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 427 | } // namespace chromeos_update_engine |