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