Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 16 | |
| 17 | #include "update_engine/filesystem_verifier_action.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #include <set> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 25 | #include <base/bind.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 26 | #include <base/posix/eintr_wrapper.h> |
| 27 | #include <base/strings/string_util.h> |
| 28 | #include <base/strings/stringprintf.h> |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 29 | #include <chromeos/message_loops/fake_message_loop.h> |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 30 | #include <chromeos/message_loops/message_loop_utils.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 31 | #include <gmock/gmock.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | |
| 34 | #include "update_engine/fake_system_state.h" |
| 35 | #include "update_engine/mock_hardware.h" |
| 36 | #include "update_engine/omaha_hash_calculator.h" |
| 37 | #include "update_engine/test_utils.h" |
| 38 | #include "update_engine/utils.h" |
| 39 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 40 | using chromeos::MessageLoop; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 41 | using std::set; |
| 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
| 45 | namespace chromeos_update_engine { |
| 46 | |
| 47 | class FilesystemVerifierActionTest : public ::testing::Test { |
| 48 | protected: |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 49 | void SetUp() override { |
| 50 | loop_.SetAsCurrent(); |
| 51 | } |
| 52 | |
| 53 | void TearDown() override { |
| 54 | EXPECT_EQ(0, chromeos::MessageLoopRunMaxIterations(&loop_, 1)); |
| 55 | } |
| 56 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 57 | // Returns true iff test has completed successfully. |
| 58 | bool DoTest(bool terminate_early, |
| 59 | bool hash_fail, |
| 60 | PartitionType partition_type); |
| 61 | |
Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 62 | chromeos::FakeMessageLoop loop_{nullptr}; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 63 | FakeSystemState fake_system_state_; |
| 64 | }; |
| 65 | |
| 66 | class FilesystemVerifierActionTestDelegate : public ActionProcessorDelegate { |
| 67 | public: |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 68 | explicit FilesystemVerifierActionTestDelegate( |
| 69 | FilesystemVerifierAction* action) |
| 70 | : action_(action), ran_(false), code_(ErrorCode::kError) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 71 | void ExitMainLoop() { |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 72 | // We need to wait for the Action to call Cleanup. |
| 73 | if (action_->IsCleanupPending()) { |
| 74 | LOG(INFO) << "Waiting for Cleanup() to be called."; |
| 75 | MessageLoop::current()->PostDelayedTask( |
| 76 | FROM_HERE, |
| 77 | base::Bind(&FilesystemVerifierActionTestDelegate::ExitMainLoop, |
| 78 | base::Unretained(this)), |
| 79 | base::TimeDelta::FromMilliseconds(100)); |
| 80 | } else { |
| 81 | MessageLoop::current()->BreakLoop(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 82 | } |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 83 | } |
| 84 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code) { |
| 85 | ExitMainLoop(); |
| 86 | } |
| 87 | void ProcessingStopped(const ActionProcessor* processor) { |
| 88 | ExitMainLoop(); |
| 89 | } |
| 90 | void ActionCompleted(ActionProcessor* processor, |
| 91 | AbstractAction* action, |
| 92 | ErrorCode code) { |
| 93 | if (action->Type() == FilesystemVerifierAction::StaticType()) { |
| 94 | ran_ = true; |
| 95 | code_ = code; |
| 96 | } |
| 97 | } |
| 98 | bool ran() const { return ran_; } |
| 99 | ErrorCode code() const { return code_; } |
| 100 | |
| 101 | private: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 102 | FilesystemVerifierAction* action_; |
| 103 | bool ran_; |
| 104 | ErrorCode code_; |
| 105 | }; |
| 106 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 107 | void StartProcessorInRunLoop(ActionProcessor* processor, |
| 108 | FilesystemVerifierAction* filesystem_copier_action, |
| 109 | bool terminate_early) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 110 | processor->StartProcessing(); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 111 | if (terminate_early) { |
| 112 | EXPECT_NE(nullptr, filesystem_copier_action); |
| 113 | processor->StopProcessing(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 114 | } |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // TODO(garnold) Temporarily disabling this test, see chromium-os:31082 for |
| 118 | // details; still trying to track down the root cause for these rare write |
| 119 | // failures and whether or not they are due to the test setup or an inherent |
| 120 | // issue with the chroot environment, library versions we use, etc. |
| 121 | TEST_F(FilesystemVerifierActionTest, DISABLED_RunAsRootSimpleTest) { |
| 122 | ASSERT_EQ(0, getuid()); |
| 123 | bool test = DoTest(false, false, PartitionType::kKernel); |
| 124 | EXPECT_TRUE(test); |
| 125 | if (!test) |
| 126 | return; |
| 127 | test = DoTest(false, false, PartitionType::kRootfs); |
| 128 | EXPECT_TRUE(test); |
| 129 | } |
| 130 | |
| 131 | bool FilesystemVerifierActionTest::DoTest(bool terminate_early, |
| 132 | bool hash_fail, |
| 133 | PartitionType partition_type) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 134 | string a_loop_file; |
| 135 | |
| 136 | if (!(utils::MakeTempFile("a_loop_file.XXXXXX", &a_loop_file, nullptr))) { |
| 137 | ADD_FAILURE(); |
| 138 | return false; |
| 139 | } |
| 140 | ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
| 141 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 142 | // Make random data for a. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 143 | const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
| 144 | chromeos::Blob a_loop_data(kLoopFileSize); |
| 145 | test_utils::FillWithData(&a_loop_data); |
| 146 | |
| 147 | |
| 148 | // Write data to disk |
| 149 | if (!(test_utils::WriteFileVector(a_loop_file, a_loop_data))) { |
| 150 | ADD_FAILURE(); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // Attach loop devices to the files |
| 155 | string a_dev; |
| 156 | test_utils::ScopedLoopbackDeviceBinder a_dev_releaser(a_loop_file, &a_dev); |
| 157 | if (!(a_dev_releaser.is_bound())) { |
| 158 | ADD_FAILURE(); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | LOG(INFO) << "verifying: " << a_loop_file << " (" << a_dev << ")"; |
| 163 | |
| 164 | bool success = true; |
| 165 | |
| 166 | // Set up the action objects |
| 167 | InstallPlan install_plan; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 168 | install_plan.source_slot = 0; |
| 169 | install_plan.target_slot = 1; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 170 | switch (partition_type) { |
| 171 | case PartitionType::kRootfs: |
| 172 | install_plan.rootfs_size = kLoopFileSize - (hash_fail ? 1 : 0); |
| 173 | install_plan.install_path = a_dev; |
| 174 | if (!OmahaHashCalculator::RawHashOfData( |
| 175 | a_loop_data, &install_plan.rootfs_hash)) { |
| 176 | ADD_FAILURE(); |
| 177 | success = false; |
| 178 | } |
| 179 | break; |
| 180 | case PartitionType::kKernel: |
| 181 | install_plan.kernel_size = kLoopFileSize - (hash_fail ? 1 : 0); |
| 182 | install_plan.kernel_install_path = a_dev; |
| 183 | if (!OmahaHashCalculator::RawHashOfData( |
| 184 | a_loop_data, &install_plan.kernel_hash)) { |
| 185 | ADD_FAILURE(); |
| 186 | success = false; |
| 187 | } |
| 188 | break; |
| 189 | case PartitionType::kSourceRootfs: |
| 190 | install_plan.source_path = a_dev; |
| 191 | if (!OmahaHashCalculator::RawHashOfData( |
| 192 | a_loop_data, &install_plan.source_rootfs_hash)) { |
| 193 | ADD_FAILURE(); |
| 194 | success = false; |
| 195 | } |
| 196 | break; |
| 197 | case PartitionType::kSourceKernel: |
| 198 | install_plan.kernel_source_path = a_dev; |
| 199 | if (!OmahaHashCalculator::RawHashOfData( |
| 200 | a_loop_data, &install_plan.source_kernel_hash)) { |
| 201 | ADD_FAILURE(); |
| 202 | success = false; |
| 203 | } |
| 204 | break; |
| 205 | } |
| 206 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 207 | ActionProcessor processor; |
| 208 | |
| 209 | ObjectFeederAction<InstallPlan> feeder_action; |
| 210 | FilesystemVerifierAction copier_action(&fake_system_state_, partition_type); |
| 211 | ObjectCollectorAction<InstallPlan> collector_action; |
| 212 | |
| 213 | BondActions(&feeder_action, &copier_action); |
| 214 | BondActions(&copier_action, &collector_action); |
| 215 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 216 | FilesystemVerifierActionTestDelegate delegate(&copier_action); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 217 | processor.set_delegate(&delegate); |
| 218 | processor.EnqueueAction(&feeder_action); |
| 219 | processor.EnqueueAction(&copier_action); |
| 220 | processor.EnqueueAction(&collector_action); |
| 221 | |
| 222 | feeder_action.set_obj(install_plan); |
| 223 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 224 | loop_.PostTask(FROM_HERE, base::Bind(&StartProcessorInRunLoop, |
| 225 | &processor, |
| 226 | &copier_action, |
| 227 | terminate_early)); |
| 228 | loop_.Run(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 229 | |
| 230 | if (!terminate_early) { |
| 231 | bool is_delegate_ran = delegate.ran(); |
| 232 | EXPECT_TRUE(is_delegate_ran); |
| 233 | success = success && is_delegate_ran; |
| 234 | } else { |
| 235 | EXPECT_EQ(ErrorCode::kError, delegate.code()); |
| 236 | return (ErrorCode::kError == delegate.code()); |
| 237 | } |
| 238 | if (hash_fail) { |
| 239 | ErrorCode expected_exit_code = |
| 240 | ((partition_type == PartitionType::kKernel || |
| 241 | partition_type == PartitionType::kSourceKernel) ? |
| 242 | ErrorCode::kNewKernelVerificationError : |
| 243 | ErrorCode::kNewRootfsVerificationError); |
| 244 | EXPECT_EQ(expected_exit_code, delegate.code()); |
| 245 | return (expected_exit_code == delegate.code()); |
| 246 | } |
| 247 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code()); |
| 248 | |
| 249 | // Make sure everything in the out_image is there |
| 250 | chromeos::Blob a_out; |
| 251 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 252 | ADD_FAILURE(); |
| 253 | return false; |
| 254 | } |
| 255 | const bool is_a_file_reading_eq = |
| 256 | test_utils::ExpectVectorsEq(a_loop_data, a_out); |
| 257 | EXPECT_TRUE(is_a_file_reading_eq); |
| 258 | success = success && is_a_file_reading_eq; |
| 259 | |
| 260 | bool is_install_plan_eq = (collector_action.object() == install_plan); |
| 261 | EXPECT_TRUE(is_install_plan_eq); |
| 262 | success = success && is_install_plan_eq; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 263 | return success; |
| 264 | } |
| 265 | |
| 266 | class FilesystemVerifierActionTest2Delegate : public ActionProcessorDelegate { |
| 267 | public: |
| 268 | void ActionCompleted(ActionProcessor* processor, |
| 269 | AbstractAction* action, |
| 270 | ErrorCode code) { |
| 271 | if (action->Type() == FilesystemVerifierAction::StaticType()) { |
| 272 | ran_ = true; |
| 273 | code_ = code; |
| 274 | } |
| 275 | } |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 276 | bool ran_; |
| 277 | ErrorCode code_; |
| 278 | }; |
| 279 | |
| 280 | TEST_F(FilesystemVerifierActionTest, MissingInputObjectTest) { |
| 281 | ActionProcessor processor; |
| 282 | FilesystemVerifierActionTest2Delegate delegate; |
| 283 | |
| 284 | processor.set_delegate(&delegate); |
| 285 | |
| 286 | FilesystemVerifierAction copier_action(&fake_system_state_, |
| 287 | PartitionType::kRootfs); |
| 288 | ObjectCollectorAction<InstallPlan> collector_action; |
| 289 | |
| 290 | BondActions(&copier_action, &collector_action); |
| 291 | |
| 292 | processor.EnqueueAction(&copier_action); |
| 293 | processor.EnqueueAction(&collector_action); |
| 294 | processor.StartProcessing(); |
| 295 | EXPECT_FALSE(processor.IsRunning()); |
| 296 | EXPECT_TRUE(delegate.ran_); |
| 297 | EXPECT_EQ(ErrorCode::kError, delegate.code_); |
| 298 | } |
| 299 | |
| 300 | TEST_F(FilesystemVerifierActionTest, NonExistentDriveTest) { |
| 301 | ActionProcessor processor; |
| 302 | FilesystemVerifierActionTest2Delegate delegate; |
| 303 | |
| 304 | processor.set_delegate(&delegate); |
| 305 | |
| 306 | ObjectFeederAction<InstallPlan> feeder_action; |
| 307 | InstallPlan install_plan(false, |
| 308 | false, |
| 309 | "", |
| 310 | 0, |
| 311 | "", |
| 312 | 0, |
| 313 | "", |
| 314 | "/no/such/file", |
| 315 | "/no/such/file", |
| 316 | "/no/such/file", |
| 317 | "/no/such/file", |
| 318 | ""); |
| 319 | feeder_action.set_obj(install_plan); |
| 320 | FilesystemVerifierAction verifier_action(&fake_system_state_, |
| 321 | PartitionType::kRootfs); |
| 322 | ObjectCollectorAction<InstallPlan> collector_action; |
| 323 | |
| 324 | BondActions(&verifier_action, &collector_action); |
| 325 | |
| 326 | processor.EnqueueAction(&feeder_action); |
| 327 | processor.EnqueueAction(&verifier_action); |
| 328 | processor.EnqueueAction(&collector_action); |
| 329 | processor.StartProcessing(); |
| 330 | EXPECT_FALSE(processor.IsRunning()); |
| 331 | EXPECT_TRUE(delegate.ran_); |
| 332 | EXPECT_EQ(ErrorCode::kError, delegate.code_); |
| 333 | } |
| 334 | |
| 335 | TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashTest) { |
| 336 | ASSERT_EQ(0, getuid()); |
| 337 | EXPECT_TRUE(DoTest(false, false, PartitionType::kRootfs)); |
| 338 | EXPECT_TRUE(DoTest(false, false, PartitionType::kKernel)); |
| 339 | EXPECT_TRUE(DoTest(false, false, PartitionType::kSourceRootfs)); |
| 340 | EXPECT_TRUE(DoTest(false, false, PartitionType::kSourceKernel)); |
| 341 | } |
| 342 | |
| 343 | TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashFailTest) { |
| 344 | ASSERT_EQ(0, getuid()); |
| 345 | EXPECT_TRUE(DoTest(false, true, PartitionType::kRootfs)); |
| 346 | EXPECT_TRUE(DoTest(false, true, PartitionType::kKernel)); |
| 347 | } |
| 348 | |
| 349 | TEST_F(FilesystemVerifierActionTest, RunAsRootTerminateEarlyTest) { |
| 350 | ASSERT_EQ(0, getuid()); |
| 351 | EXPECT_TRUE(DoTest(true, false, PartitionType::kKernel)); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 352 | // TerminateEarlyTest may leak some null callbacks from the Stream class. |
| 353 | while (loop_.RunOnce(false)) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | TEST_F(FilesystemVerifierActionTest, RunAsRootDetermineFilesystemSizeTest) { |
| 357 | string img; |
| 358 | EXPECT_TRUE(utils::MakeTempFile("img.XXXXXX", &img, nullptr)); |
| 359 | ScopedPathUnlinker img_unlinker(img); |
| 360 | test_utils::CreateExtImageAtPath(img, nullptr); |
| 361 | // Extend the "partition" holding the file system from 10MiB to 20MiB. |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 362 | EXPECT_EQ(0, truncate(img.c_str(), 20 * 1024 * 1024)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 363 | |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 364 | { |
| 365 | FilesystemVerifierAction action(&fake_system_state_, |
| 366 | PartitionType::kSourceKernel); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 367 | EXPECT_EQ(kint64max, action.remaining_size_); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 368 | action.DetermineFilesystemSize(img); |
| 369 | EXPECT_EQ(kint64max, action.remaining_size_); |
| 370 | } |
| 371 | { |
| 372 | FilesystemVerifierAction action(&fake_system_state_, |
| 373 | PartitionType::kSourceRootfs); |
| 374 | action.DetermineFilesystemSize(img); |
| 375 | EXPECT_EQ(10 * 1024 * 1024, action.remaining_size_); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | } // namespace chromeos_update_engine |