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 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "update_engine/filesystem_copier_action.h" |
| 6 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <set> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 10 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <vector> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 12 | |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 13 | #include <base/posix/eintr_wrapper.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 14 | #include <base/strings/string_util.h> |
| 15 | #include <base/strings/stringprintf.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 16 | #include <glib.h> |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 19 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 20 | #include "update_engine/fake_system_state.h" |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 21 | #include "update_engine/mock_hardware.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | #include "update_engine/omaha_hash_calculator.h" |
| 23 | #include "update_engine/test_utils.h" |
| 24 | #include "update_engine/utils.h" |
| 25 | |
| 26 | using std::set; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 27 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | using std::vector; |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
| 32 | class FilesystemCopierActionTest : public ::testing::Test { |
| 33 | protected: |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 34 | // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification, |
| 35 | // 2 -- hash verification failure. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 36 | // Returns true iff test has completed successfully. |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 37 | bool DoTest(bool run_out_of_space, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 38 | bool terminate_early, |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 39 | bool use_kernel_partition, |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 40 | int verify_hash); |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 41 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 42 | FakeSystemState fake_system_state_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 46 | public: |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 47 | FilesystemCopierActionTestDelegate(GMainLoop* loop, |
| 48 | FilesystemCopierAction* action) |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 49 | : loop_(loop), action_(action), ran_(false), code_(ErrorCode::kError) {} |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 50 | void ExitMainLoop() { |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 51 | GMainContext* context = g_main_loop_get_context(loop_); |
| 52 | // We cannot use g_main_context_pending() alone to determine if it is safe |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 53 | // to quit the main loop here because g_main_context_pending() may return |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 54 | // FALSE when g_input_stream_read_async() in FilesystemCopierAction has |
| 55 | // been cancelled but the callback has not yet been invoked. |
| 56 | while (g_main_context_pending(context) || action_->IsCleanupPending()) { |
| 57 | g_main_context_iteration(context, false); |
| 58 | g_usleep(100); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 59 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 60 | g_main_loop_quit(loop_); |
| 61 | } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 62 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 63 | ExitMainLoop(); |
| 64 | } |
| 65 | void ProcessingStopped(const ActionProcessor* processor) { |
| 66 | ExitMainLoop(); |
| 67 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | void ActionCompleted(ActionProcessor* processor, |
| 69 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 70 | ErrorCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 72 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 73 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 76 | bool ran() const { return ran_; } |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 77 | ErrorCode code() const { return code_; } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 78 | |
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 |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 107 | // issue with the chroot environment, library versions we use, etc. |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 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 | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 122 | // We need MockHardware to verify MarkUnbootable calls, but don't want |
| 123 | // warnings about other usages. |
| 124 | testing::NiceMock<MockHardware> mock_hardware; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 125 | fake_system_state_.set_hardware(&mock_hardware); |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 126 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 127 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 128 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 129 | string a_loop_file; |
| 130 | string b_loop_file; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 131 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 132 | if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, nullptr) && |
| 133 | utils::MakeTempFile("b_loop_file.XXXXXX", &b_loop_file, nullptr))) { |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 134 | ADD_FAILURE(); |
| 135 | return false; |
| 136 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 137 | ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 138 | ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 139 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 140 | // Make random data for a, zero filled data for b. |
Gilad Arnold | 6dbbd39 | 2012-07-10 16:19:11 -0700 | [diff] [blame] | 141 | const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 142 | vector<char> a_loop_data(kLoopFileSize); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 143 | test_utils::FillWithData(&a_loop_data); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 144 | vector<char> b_loop_data(run_out_of_space ? |
| 145 | (kLoopFileSize - 1) : |
| 146 | kLoopFileSize, |
| 147 | '\0'); // Fill with 0s |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 148 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 149 | // Write data to disk |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 150 | if (!(test_utils::WriteFileVector(a_loop_file, a_loop_data) && |
| 151 | test_utils::WriteFileVector(b_loop_file, b_loop_data))) { |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 152 | ADD_FAILURE(); |
| 153 | return false; |
| 154 | } |
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 | // Attach loop devices to the files |
| 157 | string a_dev; |
| 158 | string b_dev; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 160 | test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev); |
| 161 | test_utils::ScopedLoopbackDeviceBinder b_dev_releaser(b_loop_file, &b_dev); |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 162 | if (!(a_dev_releaser.is_bound() && b_dev_releaser.is_bound())) { |
| 163 | ADD_FAILURE(); |
| 164 | return false; |
| 165 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | |
Gilad Arnold | c33faeb | 2012-07-24 15:11:11 -0700 | [diff] [blame] | 167 | LOG(INFO) << "copying: " |
| 168 | << a_loop_file << " (" << a_dev << ") -> " |
| 169 | << b_loop_file << " (" << b_dev << ", " |
| 170 | << kLoopFileSize << " bytes"; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 171 | bool success = true; |
| 172 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 173 | // Set up the action objects |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | InstallPlan install_plan; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 175 | if (verify_hash) { |
| 176 | if (use_kernel_partition) { |
| 177 | install_plan.kernel_install_path = a_dev; |
| 178 | install_plan.kernel_size = |
| 179 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 180 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 181 | &install_plan.kernel_hash)) { |
| 182 | ADD_FAILURE(); |
| 183 | success = false; |
| 184 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 185 | } else { |
| 186 | install_plan.install_path = a_dev; |
| 187 | install_plan.rootfs_size = |
| 188 | kLoopFileSize - ((verify_hash == 2) ? 1 : 0); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 189 | if (!OmahaHashCalculator::RawHashOfData(a_loop_data, |
| 190 | &install_plan.rootfs_hash)) { |
| 191 | ADD_FAILURE(); |
| 192 | success = false; |
| 193 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 194 | } |
| 195 | } else { |
| 196 | if (use_kernel_partition) { |
| 197 | install_plan.kernel_install_path = b_dev; |
| 198 | } else { |
| 199 | install_plan.install_path = b_dev; |
| 200 | } |
| 201 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 202 | |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 203 | EXPECT_CALL(mock_hardware, |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 204 | MarkKernelUnbootable(a_dev)).Times(use_kernel_partition ? 1 : 0); |
| 205 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 206 | ActionProcessor processor; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 207 | |
| 208 | ObjectFeederAction<InstallPlan> feeder_action; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 209 | FilesystemCopierAction copier_action(&fake_system_state_, |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 210 | use_kernel_partition, |
Gilad Arnold | 581c2ea | 2012-07-19 12:33:49 -0700 | [diff] [blame] | 211 | verify_hash != 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 212 | ObjectCollectorAction<InstallPlan> collector_action; |
| 213 | |
| 214 | BondActions(&feeder_action, &copier_action); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 215 | BondActions(&copier_action, &collector_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 216 | |
Ben Chan | 2add7d7 | 2012-10-08 19:28:37 -0700 | [diff] [blame] | 217 | FilesystemCopierActionTestDelegate delegate(loop, &copier_action); |
| 218 | processor.set_delegate(&delegate); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 219 | processor.EnqueueAction(&feeder_action); |
| 220 | processor.EnqueueAction(&copier_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 221 | processor.EnqueueAction(&collector_action); |
| 222 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 223 | if (!verify_hash) { |
| 224 | copier_action.set_copy_source(a_dev); |
| 225 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 226 | feeder_action.set_obj(install_plan); |
| 227 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 228 | StartProcessorCallbackArgs start_callback_args; |
| 229 | start_callback_args.processor = &processor; |
| 230 | start_callback_args.filesystem_copier_action = &copier_action; |
| 231 | start_callback_args.terminate_early = terminate_early; |
| 232 | |
| 233 | g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 234 | g_main_loop_run(loop); |
| 235 | g_main_loop_unref(loop); |
| 236 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 237 | if (!terminate_early) { |
| 238 | bool is_delegate_ran = delegate.ran(); |
| 239 | EXPECT_TRUE(is_delegate_ran); |
| 240 | success = success && is_delegate_ran; |
| 241 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 242 | if (run_out_of_space || terminate_early) { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 243 | EXPECT_EQ(ErrorCode::kError, delegate.code()); |
| 244 | return (ErrorCode::kError == delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 245 | } |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 246 | if (verify_hash == 2) { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 247 | ErrorCode expected_exit_code = |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 248 | (use_kernel_partition ? |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 249 | ErrorCode::kNewKernelVerificationError : |
| 250 | ErrorCode::kNewRootfsVerificationError); |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 251 | EXPECT_EQ(expected_exit_code, delegate.code()); |
| 252 | return (expected_exit_code == delegate.code()); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 253 | } |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 254 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 255 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 256 | // Make sure everything in the out_image is there |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 257 | vector<char> a_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 258 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 259 | ADD_FAILURE(); |
| 260 | return false; |
| 261 | } |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 262 | const bool is_a_file_reading_eq = |
| 263 | test_utils::ExpectVectorsEq(a_loop_data, a_out); |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 264 | EXPECT_TRUE(is_a_file_reading_eq); |
| 265 | success = success && is_a_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 266 | if (!verify_hash) { |
| 267 | vector<char> b_out; |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 268 | if (!utils::ReadFile(b_dev, &b_out)) { |
| 269 | ADD_FAILURE(); |
| 270 | return false; |
| 271 | } |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 272 | const bool is_b_file_reading_eq = test_utils::ExpectVectorsEq(a_out, b_out); |
Gilad Arnold | 308b85e | 2012-06-07 15:50:16 -0700 | [diff] [blame] | 273 | EXPECT_TRUE(is_b_file_reading_eq); |
| 274 | success = success && is_b_file_reading_eq; |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 275 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 276 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 277 | bool is_install_plan_eq = (collector_action.object() == install_plan); |
| 278 | EXPECT_TRUE(is_install_plan_eq); |
| 279 | success = success && is_install_plan_eq; |
| 280 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 281 | LOG(INFO) << "Verifying bootable flag on: " << a_dev; |
| 282 | bool bootable; |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 283 | EXPECT_TRUE(mock_hardware.fake().IsKernelBootable(a_dev, &bootable)); |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 284 | // We should always mark a partition as unbootable if it's a kernel |
| 285 | // partition, but never if it's anything else. |
| 286 | EXPECT_EQ(bootable, !use_kernel_partition); |
| 287 | |
Gilad Arnold | ae23670 | 2012-05-17 09:33:20 -0700 | [diff] [blame] | 288 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 292 | public: |
| 293 | void ActionCompleted(ActionProcessor* processor, |
| 294 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 295 | ErrorCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 296 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 297 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 298 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | GMainLoop *loop_; |
| 302 | bool ran_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 303 | ErrorCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 307 | ActionProcessor processor; |
| 308 | FilesystemCopierActionTest2Delegate delegate; |
| 309 | |
| 310 | processor.set_delegate(&delegate); |
| 311 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 312 | FilesystemCopierAction copier_action(&fake_system_state_, false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 313 | ObjectCollectorAction<InstallPlan> collector_action; |
| 314 | |
| 315 | BondActions(&copier_action, &collector_action); |
| 316 | |
| 317 | processor.EnqueueAction(&copier_action); |
| 318 | processor.EnqueueAction(&collector_action); |
| 319 | processor.StartProcessing(); |
| 320 | EXPECT_FALSE(processor.IsRunning()); |
| 321 | EXPECT_TRUE(delegate.ran_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 322 | EXPECT_EQ(ErrorCode::kError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 325 | TEST_F(FilesystemCopierActionTest, ResumeTest) { |
| 326 | ActionProcessor processor; |
| 327 | FilesystemCopierActionTest2Delegate delegate; |
| 328 | |
| 329 | processor.set_delegate(&delegate); |
| 330 | |
| 331 | ObjectFeederAction<InstallPlan> feeder_action; |
| 332 | const char* kUrl = "http://some/url"; |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 333 | InstallPlan install_plan(false, true, kUrl, 0, "", 0, "", "", "", ""); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 334 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 335 | FilesystemCopierAction copier_action(&fake_system_state_, false, false); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 336 | ObjectCollectorAction<InstallPlan> collector_action; |
| 337 | |
| 338 | BondActions(&feeder_action, &copier_action); |
| 339 | BondActions(&copier_action, &collector_action); |
| 340 | |
| 341 | processor.EnqueueAction(&feeder_action); |
| 342 | processor.EnqueueAction(&copier_action); |
| 343 | processor.EnqueueAction(&collector_action); |
| 344 | processor.StartProcessing(); |
| 345 | EXPECT_FALSE(processor.IsRunning()); |
| 346 | EXPECT_TRUE(delegate.ran_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 347 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code_); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 348 | EXPECT_EQ(kUrl, collector_action.object().download_url); |
| 349 | } |
| 350 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 351 | TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 352 | ActionProcessor processor; |
| 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); |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 369 | FilesystemCopierAction copier_action(&fake_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_); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 380 | EXPECT_EQ(ErrorCode::kError, 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) { |
| 406 | string img; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 407 | EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr)); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 408 | ScopedPathUnlinker img_unlinker(img); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 409 | test_utils::CreateExtImageAtPath(img, nullptr); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 410 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 411 | EXPECT_EQ(0, test_utils::System(base::StringPrintf( |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 412 | "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", |
| 413 | img.c_str()))); |
| 414 | EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); |
| 415 | |
| 416 | for (int i = 0; i < 2; ++i) { |
| 417 | bool is_kernel = i == 1; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 418 | FilesystemCopierAction action(&fake_system_state_, is_kernel, false); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 419 | EXPECT_EQ(kint64max, action.filesystem_size_); |
| 420 | { |
| 421 | int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY)); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 422 | EXPECT_GT(fd, 0); |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 423 | ScopedFdCloser fd_closer(&fd); |
| 424 | action.DetermineFilesystemSize(fd); |
| 425 | } |
| 426 | EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024, |
| 427 | action.filesystem_size_); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 432 | } // namespace chromeos_update_engine |