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); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 149 | |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 150 | LOG(INFO) << "copying: " << a_loop_file << " -> " << b_loop_file |
| 151 | << ", " << kLoopFileSize << " bytes"; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 152 | bool success = true; |
| 153 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 154 | // Set up the action objects |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | InstallPlan install_plan; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 156 | if (verify_hash) { |
| 157 | if (use_kernel_partition) { |
| 158 | install_plan.kernel_install_path = a_dev; |
| 159 | install_plan.kernel_size = |
| 160 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 161 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 162 | &install_plan.kernel_hash)) { |
| 163 | ADD_FAILURE(); |
| 164 | success = false; |
| 165 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 166 | } else { |
| 167 | install_plan.install_path = a_dev; |
| 168 | install_plan.rootfs_size = |
| 169 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 170 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 171 | &install_plan.rootfs_hash)) { |
| 172 | ADD_FAILURE(); |
| 173 | success = false; |
| 174 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 175 | } |
| 176 | } else { |
| 177 | if (use_kernel_partition) { |
| 178 | install_plan.kernel_install_path = b_dev; |
| 179 | } else { |
| 180 | install_plan.install_path = b_dev; |
| 181 | } |
| 182 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 183 | |
| 184 | ActionProcessor processor; |
| 185 | FilesystemCopierActionTestDelegate delegate; |
| 186 | delegate.set_loop(loop); |
| 187 | processor.set_delegate(&delegate); |
| 188 | |
| 189 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 190 | FilesystemCopierAction copier_action(use_kernel_partition, |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 191 | verify_hash != 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 192 | ObjectCollectorAction<InstallPlan> collector_action; |
| 193 | |
| 194 | BondActions(&feeder_action, &copier_action); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 195 | BondActions(&copier_action, &collector_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 196 | |
| 197 | processor.EnqueueAction(&feeder_action); |
| 198 | processor.EnqueueAction(&copier_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | processor.EnqueueAction(&collector_action); |
| 200 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 201 | if (!verify_hash) { |
| 202 | copier_action.set_copy_source(a_dev); |
| 203 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 204 | feeder_action.set_obj(install_plan); |
| 205 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 206 | StartProcessorCallbackArgs start_callback_args; |
| 207 | start_callback_args.processor = &processor; |
| 208 | start_callback_args.filesystem_copier_action = &copier_action; |
| 209 | start_callback_args.terminate_early = terminate_early; |
| 210 | |
| 211 | g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 212 | g_main_loop_run(loop); |
| 213 | g_main_loop_unref(loop); |
| 214 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 215 | if (!terminate_early) { |
| 216 | bool is_delegate_ran = delegate.ran(); |
| 217 | EXPECT_TRUE(is_delegate_ran); |
| 218 | success = success && is_delegate_ran; |
| 219 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 220 | if (run_out_of_space || terminate_early) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 221 | EXPECT_EQ(kActionCodeError, delegate.code()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 222 | return (kActionCodeError == delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 223 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 224 | if (verify_hash == 2) { |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 225 | ActionExitCode expected_exit_code = |
| 226 | (use_kernel_partition ? |
| 227 | kActionCodeNewKernelVerificationError : |
| 228 | kActionCodeNewRootfsVerificationError); |
| 229 | EXPECT_EQ(expected_exit_code, delegate.code()); |
| 230 | return (expected_exit_code == delegate.code()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 231 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 232 | EXPECT_EQ(kActionCodeSuccess, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 233 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 234 | // Make sure everything in the out_image is there |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 235 | vector<char> a_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 236 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 237 | ADD_FAILURE(); |
| 238 | return false; |
| 239 | } |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 240 | const bool is_a_file_reading_eq = ExpectVectorsEq(a_loop_data, a_out); |
| 241 | EXPECT_TRUE(is_a_file_reading_eq); |
| 242 | success = success && is_a_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 243 | if (!verify_hash) { |
| 244 | vector<char> b_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 245 | if (!utils::ReadFile(b_dev, &b_out)) { |
| 246 | ADD_FAILURE(); |
| 247 | return false; |
| 248 | } |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 249 | const bool is_b_file_reading_eq = ExpectVectorsEq(a_out, b_out); |
| 250 | EXPECT_TRUE(is_b_file_reading_eq); |
| 251 | success = success && is_b_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 252 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 253 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 254 | bool is_install_plan_eq = (collector_action.object() == install_plan); |
| 255 | EXPECT_TRUE(is_install_plan_eq); |
| 256 | success = success && is_install_plan_eq; |
| 257 | |
| 258 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 262 | public: |
| 263 | void ActionCompleted(ActionProcessor* processor, |
| 264 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 265 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 266 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 267 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 268 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | GMainLoop *loop_; |
| 272 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 273 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 277 | ActionProcessor processor; |
| 278 | FilesystemCopierActionTest2Delegate delegate; |
| 279 | |
| 280 | processor.set_delegate(&delegate); |
| 281 | |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 282 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 283 | ObjectCollectorAction<InstallPlan> collector_action; |
| 284 | |
| 285 | BondActions(&copier_action, &collector_action); |
| 286 | |
| 287 | processor.EnqueueAction(&copier_action); |
| 288 | processor.EnqueueAction(&collector_action); |
| 289 | processor.StartProcessing(); |
| 290 | EXPECT_FALSE(processor.IsRunning()); |
| 291 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 292 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 295 | TEST_F(FilesystemCopierActionTest, ResumeTest) { |
| 296 | ActionProcessor processor; |
| 297 | FilesystemCopierActionTest2Delegate delegate; |
| 298 | |
| 299 | processor.set_delegate(&delegate); |
| 300 | |
| 301 | ObjectFeederAction<InstallPlan> feeder_action; |
| 302 | const char* kUrl = "http://some/url"; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 303 | InstallPlan install_plan(true, kUrl, 0, "", "", ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 304 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 305 | FilesystemCopierAction copier_action(false, false); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 306 | ObjectCollectorAction<InstallPlan> collector_action; |
| 307 | |
| 308 | BondActions(&feeder_action, &copier_action); |
| 309 | BondActions(&copier_action, &collector_action); |
| 310 | |
| 311 | processor.EnqueueAction(&feeder_action); |
| 312 | processor.EnqueueAction(&copier_action); |
| 313 | processor.EnqueueAction(&collector_action); |
| 314 | processor.StartProcessing(); |
| 315 | EXPECT_FALSE(processor.IsRunning()); |
| 316 | EXPECT_TRUE(delegate.ran_); |
| 317 | EXPECT_EQ(kActionCodeSuccess, delegate.code_); |
| 318 | EXPECT_EQ(kUrl, collector_action.object().download_url); |
| 319 | } |
| 320 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 321 | TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 322 | ActionProcessor processor; |
| 323 | FilesystemCopierActionTest2Delegate delegate; |
| 324 | |
| 325 | processor.set_delegate(&delegate); |
| 326 | |
| 327 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 328 | InstallPlan install_plan(false, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 329 | "", |
| 330 | 0, |
| 331 | "", |
| 332 | "/no/such/file", |
| 333 | "/no/such/file"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 334 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 335 | FilesystemCopierAction copier_action(false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 336 | ObjectCollectorAction<InstallPlan> collector_action; |
| 337 | |
| 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_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 346 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 349 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) { |
| 350 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 351 | EXPECT_TRUE(DoTest(false, false, false, 1)); |
| 352 | EXPECT_TRUE(DoTest(false, false, true, 1)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) { |
| 356 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 357 | EXPECT_TRUE(DoTest(false, false, false, 2)); |
| 358 | EXPECT_TRUE(DoTest(false, false, true, 2)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 361 | TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 362 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 363 | EXPECT_TRUE(DoTest(true, false, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 366 | TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 367 | ASSERT_EQ(0, getuid()); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 368 | EXPECT_TRUE(DoTest(false, true, false, 0)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 371 | TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) { |
| 372 | string img; |
| 373 | EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL)); |
| 374 | ScopedPathUnlinker img_unlinker(img); |
| 375 | CreateExtImageAtPath(img, NULL); |
| 376 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
| 377 | EXPECT_EQ(0, System(StringPrintf( |
| 378 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 379 | img.c_str()))); |
| 380 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 381 | |
| 382 | for (int i = 0; i < 2; ++i) { |
| 383 | bool is_kernel = i == 1; |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame^] | 384 | FilesystemCopierAction action(is_kernel, false); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 385 | EXPECT_EQ(kint64max, action.filesystem_size_); |
| 386 | { |
| 387 | int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY)); |
| 388 | EXPECT_TRUE(fd > 0); |
| 389 | ScopedFdCloser fd_closer(&fd); |
| 390 | action.DetermineFilesystemSize(fd); |
| 391 | } |
| 392 | EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024, |
| 393 | action.filesystem_size_); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 398 | } // namespace chromeos_update_engine |