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