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