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> |
| 12 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 13 | #include <base/stringprintf.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 14 | #include <glib.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 16 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | #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 | |
| 23 | using std::set; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 24 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | using std::vector; |
| 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | class FilesystemCopierActionTest : public ::testing::Test { |
| 30 | protected: |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 31 | // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification, |
| 32 | // 2 -- hash verification failure. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 33 | // Returns true iff test has completed successfully. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 34 | bool DoTest(bool run_out_of_space, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 35 | bool terminate_early, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 36 | bool use_kernel_partition, |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 37 | int verify_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 38 | void SetUp() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | } |
| 40 | void TearDown() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | } |
| 42 | }; |
| 43 | |
| 44 | class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 45 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 46 | FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {} |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 47 | void ExitMainLoop() { |
| 48 | while (g_main_context_pending(NULL)) { |
| 49 | g_main_context_iteration(NULL, false); |
| 50 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | g_main_loop_quit(loop_); |
| 52 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 53 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 54 | ExitMainLoop(); |
| 55 | } |
| 56 | void ProcessingStopped(const ActionProcessor* processor) { |
| 57 | ExitMainLoop(); |
| 58 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | void ActionCompleted(ActionProcessor* processor, |
| 60 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 61 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 63 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 64 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | void set_loop(GMainLoop* loop) { |
| 68 | loop_ = loop; |
| 69 | } |
| 70 | bool ran() { return ran_; } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 71 | ActionExitCode code() { return code_; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | private: |
| 73 | GMainLoop* loop_; |
| 74 | bool ran_; |
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 | }; |
| 77 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 78 | struct StartProcessorCallbackArgs { |
| 79 | ActionProcessor* processor; |
| 80 | FilesystemCopierAction* filesystem_copier_action; |
| 81 | bool terminate_early; |
| 82 | }; |
| 83 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 84 | gboolean StartProcessorInRunLoop(gpointer data) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 85 | StartProcessorCallbackArgs* args = |
| 86 | reinterpret_cast<StartProcessorCallbackArgs*>(data); |
| 87 | ActionProcessor* processor = args->processor; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | processor->StartProcessing(); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 89 | if (args->terminate_early) { |
| 90 | EXPECT_TRUE(args->filesystem_copier_action); |
| 91 | args->processor->StopProcessing(); |
| 92 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | return FALSE; |
| 94 | } |
| 95 | |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 96 | // TODO(garnold) Temporarily disabling this test, see chromium-os:31082 for |
| 97 | // details; still trying to track down the root cause for these rare write |
| 98 | // failures and whether or not they are due to the test setup or an inherent |
| 99 | // issue with the chroot environiment, library versions we use, etc. |
| 100 | TEST_F(FilesystemCopierActionTest, DISABLED_RunAsRootSimpleTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 101 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 102 | bool test = DoTest(false, false, false, 0); |
| 103 | EXPECT_TRUE(test); |
| 104 | if (!test) |
| 105 | return; |
| 106 | test = DoTest(false, false, true, 0); |
| 107 | EXPECT_TRUE(test); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 108 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 109 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 110 | bool FilesystemCopierActionTest::DoTest(bool run_out_of_space, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 111 | bool terminate_early, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 112 | bool use_kernel_partition, |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 113 | int verify_hash) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 114 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 115 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 116 | string a_loop_file; |
| 117 | string b_loop_file; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 118 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 119 | if (!(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", &a_loop_file, NULL) && |
| 120 | utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", &b_loop_file, NULL))) { |
| 121 | ADD_FAILURE(); |
| 122 | return false; |
| 123 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 124 | ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 125 | ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 126 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 127 | // Make random data for a, zero filled data for b. |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 128 | const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 129 | vector<char> a_loop_data(kLoopFileSize); |
| 130 | FillWithData(&a_loop_data); |
| 131 | vector<char> b_loop_data(run_out_of_space ? |
| 132 | (kLoopFileSize - 1) : |
| 133 | kLoopFileSize, |
| 134 | '\0'); // Fill with 0s |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 135 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 136 | // Write data to disk |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 137 | if (!(WriteFileVector(a_loop_file, a_loop_data) && |
| 138 | WriteFileVector(b_loop_file, b_loop_data))) { |
| 139 | ADD_FAILURE(); |
| 140 | return false; |
| 141 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 142 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 143 | // Attach loop devices to the files |
| 144 | string a_dev; |
| 145 | string b_dev; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 147 | ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev); |
| 148 | ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev); |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 149 | if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) { |
| 150 | ADD_FAILURE(); |
| 151 | return false; |
| 152 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 154 | LOG(INFO) << "copying: " |
| 155 | << a_loop_file << " (" << a_dev << ") -> " |
| 156 | << b_loop_file << " (" << b_dev << ", " |
| 157 | << kLoopFileSize << " bytes"; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 158 | bool success = true; |
| 159 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 160 | // Set up the action objects |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 161 | InstallPlan install_plan; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 162 | if (verify_hash) { |
| 163 | if (use_kernel_partition) { |
| 164 | install_plan.kernel_install_path = a_dev; |
| 165 | install_plan.kernel_size = |
| 166 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 167 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 168 | &install_plan.kernel_hash)) { |
| 169 | ADD_FAILURE(); |
| 170 | success = false; |
| 171 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 172 | } else { |
| 173 | install_plan.install_path = a_dev; |
| 174 | install_plan.rootfs_size = |
| 175 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 176 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 177 | &install_plan.rootfs_hash)) { |
| 178 | ADD_FAILURE(); |
| 179 | success = false; |
| 180 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 181 | } |
| 182 | } else { |
| 183 | if (use_kernel_partition) { |
| 184 | install_plan.kernel_install_path = b_dev; |
| 185 | } else { |
| 186 | install_plan.install_path = b_dev; |
| 187 | } |
| 188 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | |
| 190 | ActionProcessor processor; |
| 191 | FilesystemCopierActionTestDelegate delegate; |
| 192 | delegate.set_loop(loop); |
| 193 | processor.set_delegate(&delegate); |
| 194 | |
| 195 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 196 | FilesystemCopierAction copier_action(use_kernel_partition, |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 197 | verify_hash != 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 198 | ObjectCollectorAction<InstallPlan> collector_action; |
| 199 | |
| 200 | BondActions(&feeder_action, &copier_action); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 201 | BondActions(&copier_action, &collector_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 202 | |
| 203 | processor.EnqueueAction(&feeder_action); |
| 204 | processor.EnqueueAction(&copier_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 205 | processor.EnqueueAction(&collector_action); |
| 206 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 207 | if (!verify_hash) { |
| 208 | copier_action.set_copy_source(a_dev); |
| 209 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 210 | feeder_action.set_obj(install_plan); |
| 211 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 212 | StartProcessorCallbackArgs start_callback_args; |
| 213 | start_callback_args.processor = &processor; |
| 214 | start_callback_args.filesystem_copier_action = &copier_action; |
| 215 | start_callback_args.terminate_early = terminate_early; |
| 216 | |
| 217 | g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 218 | g_main_loop_run(loop); |
| 219 | g_main_loop_unref(loop); |
| 220 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 221 | if (!terminate_early) { |
| 222 | bool is_delegate_ran = delegate.ran(); |
| 223 | EXPECT_TRUE(is_delegate_ran); |
| 224 | success = success && is_delegate_ran; |
| 225 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 226 | if (run_out_of_space || terminate_early) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 227 | EXPECT_EQ(kActionCodeError, delegate.code()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 228 | return (kActionCodeError == delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 229 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 230 | if (verify_hash == 2) { |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 231 | ActionExitCode expected_exit_code = |
| 232 | (use_kernel_partition ? |
| 233 | kActionCodeNewKernelVerificationError : |
| 234 | kActionCodeNewRootfsVerificationError); |
| 235 | EXPECT_EQ(expected_exit_code, delegate.code()); |
| 236 | return (expected_exit_code == delegate.code()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 237 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 238 | EXPECT_EQ(kActionCodeSuccess, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 239 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 240 | // Make sure everything in the out_image is there |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 241 | vector<char> a_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 242 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 243 | ADD_FAILURE(); |
| 244 | return false; |
| 245 | } |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 246 | const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out); |
| 247 | EXPECT_TRUE(is_a_file_reading_eq); |
| 248 | success = success && is_a_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 249 | if (!verify_hash) { |
| 250 | vector<char> b_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 251 | if (!utils::ReadFile(b_dev, &b_out)) { |
| 252 | ADD_FAILURE(); |
| 253 | return false; |
| 254 | } |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 255 | const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out); |
| 256 | EXPECT_TRUE(is_b_file_reading_eq); |
| 257 | success = success && is_b_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 258 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 259 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 260 | bool is_install_plan_eq = (collector_action.object() == install_plan); |
| 261 | EXPECT_TRUE(is_install_plan_eq); |
| 262 | success = success && is_install_plan_eq; |
| 263 | |
| 264 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 268 | public: |
| 269 | void ActionCompleted(ActionProcessor* processor, |
| 270 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 271 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 272 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 273 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 274 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | GMainLoop *loop_; |
| 278 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 279 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 283 | ActionProcessor processor; |
| 284 | FilesystemCopierActionTest2Delegate delegate; |
| 285 | |
| 286 | processor.set_delegate(&delegate); |
| 287 | |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 288 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 289 | ObjectCollectorAction<InstallPlan> collector_action; |
| 290 | |
| 291 | BondActions(&copier_action, &collector_action); |
| 292 | |
| 293 | processor.EnqueueAction(&copier_action); |
| 294 | processor.EnqueueAction(&collector_action); |
| 295 | processor.StartProcessing(); |
| 296 | EXPECT_FALSE(processor.IsRunning()); |
| 297 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 298 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 301 | TEST_F(FilesystemCopierActionTest, ResumeTest) { |
| 302 | ActionProcessor processor; |
| 303 | FilesystemCopierActionTest2Delegate delegate; |
| 304 | |
| 305 | processor.set_delegate(&delegate); |
| 306 | |
| 307 | ObjectFeederAction<InstallPlan> feeder_action; |
| 308 | const char* kUrl = "http://some/url"; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 309 | InstallPlan install_plan(true, kUrl, 0, "", 0, "", "", ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 310 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 311 | FilesystemCopierAction copier_action(false, false); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 312 | ObjectCollectorAction<InstallPlan> collector_action; |
| 313 | |
| 314 | BondActions(&feeder_action, &copier_action); |
| 315 | BondActions(&copier_action, &collector_action); |
| 316 | |
| 317 | processor.EnqueueAction(&feeder_action); |
| 318 | processor.EnqueueAction(&copier_action); |
| 319 | processor.EnqueueAction(&collector_action); |
| 320 | processor.StartProcessing(); |
| 321 | EXPECT_FALSE(processor.IsRunning()); |
| 322 | EXPECT_TRUE(delegate.ran_); |
| 323 | EXPECT_EQ(kActionCodeSuccess, delegate.code_); |
| 324 | EXPECT_EQ(kUrl, collector_action.object().download_url); |
| 325 | } |
| 326 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 327 | TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 328 | ActionProcessor processor; |
| 329 | FilesystemCopierActionTest2Delegate delegate; |
| 330 | |
| 331 | processor.set_delegate(&delegate); |
| 332 | |
| 333 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 334 | InstallPlan install_plan(false, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 335 | "", |
| 336 | 0, |
| 337 | "", |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 338 | 0, |
| 339 | "", |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 340 | "/no/such/file", |
| 341 | "/no/such/file"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 342 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 343 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 344 | ObjectCollectorAction<InstallPlan> collector_action; |
| 345 | |
| 346 | BondActions(&copier_action, &collector_action); |
| 347 | |
| 348 | processor.EnqueueAction(&feeder_action); |
| 349 | processor.EnqueueAction(&copier_action); |
| 350 | processor.EnqueueAction(&collector_action); |
| 351 | processor.StartProcessing(); |
| 352 | EXPECT_FALSE(processor.IsRunning()); |
| 353 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 354 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 357 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) { |
| 358 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 359 | EXPECT_TRUE(DoTest(false, false, false, 1)); |
| 360 | EXPECT_TRUE(DoTest(false, false, true, 1)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) { |
| 364 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 365 | EXPECT_TRUE(DoTest(false, false, false, 2)); |
| 366 | EXPECT_TRUE(DoTest(false, false, true, 2)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 369 | TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 370 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 371 | EXPECT_TRUE(DoTest(true, false, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 374 | TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 375 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 376 | EXPECT_TRUE(DoTest(false, true, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 379 | TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) { |
| 380 | string img; |
| 381 | EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL)); |
| 382 | ScopedPathUnlinker img_unlinker(img); |
| 383 | CreateExtImageAtPath(img, NULL); |
| 384 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
| 385 | EXPECT_EQ(0, System(StringPrintf( |
| 386 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 387 | img.c_str()))); |
| 388 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 389 | |
| 390 | for (int i = 0; i < 2; ++i) { |
| 391 | bool is_kernel = i == 1; |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 392 | FilesystemCopierAction action(is_kernel, false); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 393 | EXPECT_EQ(kint64max, action.filesystem_size_); |
| 394 | { |
| 395 | int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY)); |
| 396 | EXPECT_TRUE(fd > 0); |
| 397 | ScopedFdCloser fd_closer(&fd); |
| 398 | action.DetermineFilesystemSize(fd); |
| 399 | } |
| 400 | EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024, |
| 401 | action.filesystem_size_); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 406 | } // namespace chromeos_update_engine |