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