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