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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 18 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 19 | #include <memory> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 20 | #include <string> |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 21 | #include <utility> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 22 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 23 | #include <base/bind.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 24 | #include <base/posix/eintr_wrapper.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 25 | #include <brillo/message_loops/fake_message_loop.h> |
| 26 | #include <brillo/message_loops/message_loop_utils.h> |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 27 | #include <brillo/secure_blob.h> |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 28 | #include <gtest/gtest.h> |
| 29 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/hash_calculator.h" |
| 31 | #include "update_engine/common/test_utils.h" |
| 32 | #include "update_engine/common/utils.h" |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 33 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 34 | using brillo::MessageLoop; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 35 | using std::string; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | class FilesystemVerifierActionTest : public ::testing::Test { |
| 40 | protected: |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 41 | void SetUp() override { |
| 42 | loop_.SetAsCurrent(); |
| 43 | } |
| 44 | |
| 45 | void TearDown() override { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 46 | EXPECT_EQ(0, brillo::MessageLoopRunMaxIterations(&loop_, 1)); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 49 | // Returns true iff test has completed successfully. |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 50 | bool DoTest(bool terminate_early, bool hash_fail); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 51 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 52 | void BuildActions(const InstallPlan& install_plan); |
| 53 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 54 | brillo::FakeMessageLoop loop_{nullptr}; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 55 | ActionProcessor processor_; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | class FilesystemVerifierActionTestDelegate : public ActionProcessorDelegate { |
| 59 | public: |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 60 | FilesystemVerifierActionTestDelegate() |
| 61 | : ran_(false), code_(ErrorCode::kError) {} |
| 62 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 63 | void ProcessingDone(const ActionProcessor* processor, ErrorCode code) { |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 64 | MessageLoop::current()->BreakLoop(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 65 | } |
| 66 | void ProcessingStopped(const ActionProcessor* processor) { |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 67 | MessageLoop::current()->BreakLoop(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 68 | } |
| 69 | void ActionCompleted(ActionProcessor* processor, |
| 70 | AbstractAction* action, |
| 71 | ErrorCode code) { |
| 72 | if (action->Type() == FilesystemVerifierAction::StaticType()) { |
| 73 | ran_ = true; |
| 74 | code_ = code; |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 75 | EXPECT_FALSE(static_cast<FilesystemVerifierAction*>(action)->src_stream_); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 76 | } else if (action->Type() == |
| 77 | ObjectCollectorAction<InstallPlan>::StaticType()) { |
| 78 | auto collector_action = |
| 79 | static_cast<ObjectCollectorAction<InstallPlan>*>(action); |
| 80 | install_plan_.reset(new InstallPlan(collector_action->object())); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | bool ran() const { return ran_; } |
| 84 | ErrorCode code() const { return code_; } |
| 85 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 86 | std::unique_ptr<InstallPlan> install_plan_; |
| 87 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 88 | private: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 89 | bool ran_; |
| 90 | ErrorCode code_; |
| 91 | }; |
| 92 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 93 | bool FilesystemVerifierActionTest::DoTest(bool terminate_early, |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 94 | bool hash_fail) { |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 95 | test_utils::ScopedTempFile a_loop_file("a_loop_file.XXXXXX"); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 96 | |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 97 | // Make random data for a. |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 98 | const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 99 | brillo::Blob a_loop_data(kLoopFileSize); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 100 | test_utils::FillWithData(&a_loop_data); |
| 101 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 102 | // Write data to disk |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 103 | if (!(test_utils::WriteFileVector(a_loop_file.path(), a_loop_data))) { |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 104 | ADD_FAILURE(); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // Attach loop devices to the files |
| 109 | string a_dev; |
Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 110 | test_utils::ScopedLoopbackDeviceBinder a_dev_releaser( |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 111 | a_loop_file.path(), false, &a_dev); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 112 | if (!(a_dev_releaser.is_bound())) { |
| 113 | ADD_FAILURE(); |
| 114 | return false; |
| 115 | } |
| 116 | |
Sen Jiang | 0779a15 | 2018-07-02 17:34:56 -0700 | [diff] [blame] | 117 | LOG(INFO) << "verifying: " << a_loop_file.path() << " (" << a_dev << ")"; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 118 | |
| 119 | bool success = true; |
| 120 | |
| 121 | // Set up the action objects |
| 122 | InstallPlan install_plan; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 123 | install_plan.source_slot = 0; |
| 124 | install_plan.target_slot = 1; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 125 | InstallPlan::Partition part; |
| 126 | part.name = "part"; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 127 | part.target_size = kLoopFileSize - (hash_fail ? 1 : 0); |
| 128 | part.target_path = a_dev; |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 129 | if (!HashCalculator::RawHashOfData(a_loop_data, &part.target_hash)) { |
| 130 | ADD_FAILURE(); |
| 131 | success = false; |
Sen Jiang | f987481 | 2015-12-08 16:11:48 -0800 | [diff] [blame] | 132 | } |
| 133 | part.source_size = kLoopFileSize; |
| 134 | part.source_path = a_dev; |
Sen Jiang | f987481 | 2015-12-08 16:11:48 -0800 | [diff] [blame] | 135 | if (!HashCalculator::RawHashOfData(a_loop_data, &part.source_hash)) { |
| 136 | ADD_FAILURE(); |
| 137 | success = false; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 138 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 139 | install_plan.partitions = {part}; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 140 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 141 | BuildActions(install_plan); |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 142 | |
Amin Hassani | abe4a77 | 2018-07-26 11:19:10 -0700 | [diff] [blame] | 143 | FilesystemVerifierActionTestDelegate delegate; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 144 | processor_.set_delegate(&delegate); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 145 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 146 | loop_.PostTask(FROM_HERE, |
| 147 | base::Bind( |
| 148 | [](ActionProcessor* processor, bool terminate_early) { |
| 149 | processor->StartProcessing(); |
| 150 | if (terminate_early) { |
| 151 | processor->StopProcessing(); |
| 152 | } |
| 153 | }, |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 154 | base::Unretained(&processor_), |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 155 | terminate_early)); |
Alex Deymo | 20c9920 | 2015-07-09 16:14:16 -0700 | [diff] [blame] | 156 | loop_.Run(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 157 | |
| 158 | if (!terminate_early) { |
| 159 | bool is_delegate_ran = delegate.ran(); |
| 160 | EXPECT_TRUE(is_delegate_ran); |
| 161 | success = success && is_delegate_ran; |
| 162 | } else { |
| 163 | EXPECT_EQ(ErrorCode::kError, delegate.code()); |
| 164 | return (ErrorCode::kError == delegate.code()); |
| 165 | } |
| 166 | if (hash_fail) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 167 | ErrorCode expected_exit_code = ErrorCode::kNewRootfsVerificationError; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 168 | EXPECT_EQ(expected_exit_code, delegate.code()); |
| 169 | return (expected_exit_code == delegate.code()); |
| 170 | } |
| 171 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code()); |
| 172 | |
| 173 | // Make sure everything in the out_image is there |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 174 | brillo::Blob a_out; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 175 | if (!utils::ReadFile(a_dev, &a_out)) { |
| 176 | ADD_FAILURE(); |
| 177 | return false; |
| 178 | } |
| 179 | const bool is_a_file_reading_eq = |
| 180 | test_utils::ExpectVectorsEq(a_loop_data, a_out); |
| 181 | EXPECT_TRUE(is_a_file_reading_eq); |
| 182 | success = success && is_a_file_reading_eq; |
| 183 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 184 | bool is_install_plan_eq = (*delegate.install_plan_ == install_plan); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 185 | EXPECT_TRUE(is_install_plan_eq); |
| 186 | success = success && is_install_plan_eq; |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 187 | return success; |
| 188 | } |
| 189 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 190 | void FilesystemVerifierActionTest::BuildActions( |
| 191 | const InstallPlan& install_plan) { |
| 192 | auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>(); |
| 193 | auto verifier_action = std::make_unique<FilesystemVerifierAction>(); |
| 194 | auto collector_action = |
| 195 | std::make_unique<ObjectCollectorAction<InstallPlan>>(); |
| 196 | |
| 197 | feeder_action->set_obj(install_plan); |
| 198 | |
| 199 | BondActions(feeder_action.get(), verifier_action.get()); |
| 200 | BondActions(verifier_action.get(), collector_action.get()); |
| 201 | |
| 202 | processor_.EnqueueAction(std::move(feeder_action)); |
| 203 | processor_.EnqueueAction(std::move(verifier_action)); |
| 204 | processor_.EnqueueAction(std::move(collector_action)); |
| 205 | } |
| 206 | |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 207 | class FilesystemVerifierActionTest2Delegate : public ActionProcessorDelegate { |
| 208 | public: |
| 209 | void ActionCompleted(ActionProcessor* processor, |
| 210 | AbstractAction* action, |
| 211 | ErrorCode code) { |
| 212 | if (action->Type() == FilesystemVerifierAction::StaticType()) { |
| 213 | ran_ = true; |
| 214 | code_ = code; |
| 215 | } |
| 216 | } |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 217 | bool ran_; |
| 218 | ErrorCode code_; |
| 219 | }; |
| 220 | |
| 221 | TEST_F(FilesystemVerifierActionTest, MissingInputObjectTest) { |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 222 | auto copier_action = std::make_unique<FilesystemVerifierAction>(); |
| 223 | auto collector_action = |
| 224 | std::make_unique<ObjectCollectorAction<InstallPlan>>(); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 225 | |
Amin Hassani | d3f4bea | 2018-04-30 14:52:40 -0700 | [diff] [blame] | 226 | BondActions(copier_action.get(), collector_action.get()); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 227 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 228 | processor_.EnqueueAction(std::move(copier_action)); |
| 229 | processor_.EnqueueAction(std::move(collector_action)); |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 230 | |
| 231 | FilesystemVerifierActionTest2Delegate delegate; |
| 232 | processor_.set_delegate(&delegate); |
| 233 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 234 | processor_.StartProcessing(); |
| 235 | EXPECT_FALSE(processor_.IsRunning()); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(delegate.ran_); |
| 237 | EXPECT_EQ(ErrorCode::kError, delegate.code_); |
| 238 | } |
| 239 | |
| 240 | TEST_F(FilesystemVerifierActionTest, NonExistentDriveTest) { |
Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 241 | InstallPlan install_plan; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 242 | InstallPlan::Partition part; |
| 243 | part.name = "nope"; |
| 244 | part.source_path = "/no/such/file"; |
| 245 | part.target_path = "/no/such/file"; |
| 246 | install_plan.partitions = {part}; |
| 247 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 248 | BuildActions(install_plan); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 249 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 250 | FilesystemVerifierActionTest2Delegate delegate; |
| 251 | processor_.set_delegate(&delegate); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 252 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 253 | processor_.StartProcessing(); |
| 254 | EXPECT_FALSE(processor_.IsRunning()); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 255 | EXPECT_TRUE(delegate.ran_); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 256 | EXPECT_EQ(ErrorCode::kFilesystemVerifierError, delegate.code_); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashTest) { |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 260 | ASSERT_EQ(0U, getuid()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 261 | EXPECT_TRUE(DoTest(false, false)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | TEST_F(FilesystemVerifierActionTest, RunAsRootVerifyHashFailTest) { |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 265 | ASSERT_EQ(0U, getuid()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 266 | EXPECT_TRUE(DoTest(false, true)); |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | TEST_F(FilesystemVerifierActionTest, RunAsRootTerminateEarlyTest) { |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 270 | ASSERT_EQ(0U, getuid()); |
Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 271 | EXPECT_TRUE(DoTest(true, false)); |
Alex Deymo | b9e8e26 | 2015-08-03 20:23:03 -0700 | [diff] [blame] | 272 | // TerminateEarlyTest may leak some null callbacks from the Stream class. |
| 273 | while (loop_.RunOnce(false)) {} |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 276 | #ifdef __ANDROID__ |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 277 | TEST_F(FilesystemVerifierActionTest, RunAsRootWriteVerityTest) { |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 278 | test_utils::ScopedTempFile part_file("part_file.XXXXXX"); |
| 279 | constexpr size_t filesystem_size = 200 * 4096; |
| 280 | constexpr size_t part_size = 256 * 4096; |
| 281 | brillo::Blob part_data(filesystem_size, 0x1); |
| 282 | part_data.resize(part_size); |
| 283 | ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data)); |
| 284 | string target_path; |
| 285 | test_utils::ScopedLoopbackDeviceBinder target_device( |
| 286 | part_file.path(), true, &target_path); |
| 287 | |
| 288 | InstallPlan install_plan; |
| 289 | InstallPlan::Partition part; |
| 290 | part.name = "part"; |
| 291 | part.target_path = target_path; |
| 292 | part.target_size = part_size; |
| 293 | part.block_size = 4096; |
| 294 | part.hash_tree_algorithm = "sha1"; |
| 295 | part.hash_tree_data_offset = 0; |
| 296 | part.hash_tree_data_size = filesystem_size; |
| 297 | part.hash_tree_offset = filesystem_size; |
| 298 | part.hash_tree_size = 3 * 4096; |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 299 | part.fec_data_offset = 0; |
| 300 | part.fec_data_size = filesystem_size + part.hash_tree_size; |
| 301 | part.fec_offset = part.fec_data_size; |
| 302 | part.fec_size = 2 * 4096; |
| 303 | part.fec_roots = 2; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 304 | // for i in {1..$((200 * 4096))}; do echo -n -e '\x1' >> part; done |
| 305 | // avbtool add_hashtree_footer --image part --partition_size $((256 * 4096)) |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 306 | // --partition_name part --do_not_append_vbmeta_image |
| 307 | // --output_vbmeta_image vbmeta |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 308 | // truncate -s $((256 * 4096)) part |
| 309 | // sha256sum part | xxd -r -p | hexdump -v -e '/1 "0x%02x, "' |
Sen Jiang | a778e5b | 2018-09-13 11:42:56 -0700 | [diff] [blame] | 310 | part.target_hash = {0x28, 0xd4, 0x96, 0x75, 0x4c, 0xf5, 0x8a, 0x3e, |
| 311 | 0x31, 0x85, 0x08, 0x92, 0x85, 0x62, 0xf0, 0x37, |
| 312 | 0xbc, 0x8d, 0x7e, 0xa4, 0xcb, 0x24, 0x18, 0x7b, |
| 313 | 0xf3, 0xeb, 0xb5, 0x8d, 0x6f, 0xc8, 0xd8, 0x1a}; |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 314 | // avbtool info_image --image vbmeta | grep Salt | cut -d':' -f 2 | |
| 315 | // xxd -r -p | hexdump -v -e '/1 "0x%02x, "' |
| 316 | part.hash_tree_salt = {0x9e, 0xcb, 0xf8, 0xd5, 0x0b, 0xb4, 0x43, |
| 317 | 0x0a, 0x7a, 0x10, 0xad, 0x96, 0xd7, 0x15, |
| 318 | 0x70, 0xba, 0xed, 0x27, 0xe2, 0xae}; |
| 319 | install_plan.partitions = {part}; |
| 320 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 321 | BuildActions(install_plan); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 322 | |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 323 | FilesystemVerifierActionTestDelegate delegate; |
| 324 | processor_.set_delegate(&delegate); |
Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 325 | |
| 326 | loop_.PostTask( |
| 327 | FROM_HERE, |
| 328 | base::Bind( |
| 329 | [](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 330 | base::Unretained(&processor_))); |
| 331 | loop_.Run(); |
| 332 | |
| 333 | EXPECT_FALSE(processor_.IsRunning()); |
| 334 | EXPECT_TRUE(delegate.ran()); |
| 335 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code()); |
| 336 | } |
| 337 | #endif // __ANDROID__ |
| 338 | |
Amin Hassani | 2e4eda5 | 2019-01-07 14:01:17 -0800 | [diff] [blame] | 339 | TEST_F(FilesystemVerifierActionTest, RunAsRootSkipWriteVerityTest) { |
Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 340 | test_utils::ScopedTempFile part_file("part_file.XXXXXX"); |
| 341 | constexpr size_t filesystem_size = 200 * 4096; |
| 342 | constexpr size_t part_size = 256 * 4096; |
| 343 | brillo::Blob part_data(part_size); |
| 344 | test_utils::FillWithData(&part_data); |
| 345 | ASSERT_TRUE(test_utils::WriteFileVector(part_file.path(), part_data)); |
| 346 | string target_path; |
| 347 | test_utils::ScopedLoopbackDeviceBinder target_device( |
| 348 | part_file.path(), true, &target_path); |
| 349 | |
| 350 | InstallPlan install_plan; |
| 351 | install_plan.write_verity = false; |
| 352 | InstallPlan::Partition part; |
| 353 | part.name = "part"; |
| 354 | part.target_path = target_path; |
| 355 | part.target_size = part_size; |
| 356 | part.block_size = 4096; |
| 357 | part.hash_tree_data_offset = 0; |
| 358 | part.hash_tree_data_size = filesystem_size; |
| 359 | part.hash_tree_offset = filesystem_size; |
| 360 | part.hash_tree_size = 3 * 4096; |
| 361 | part.fec_data_offset = 0; |
| 362 | part.fec_data_size = filesystem_size + part.hash_tree_size; |
| 363 | part.fec_offset = part.fec_data_size; |
| 364 | part.fec_size = 2 * 4096; |
| 365 | EXPECT_TRUE(HashCalculator::RawHashOfData(part_data, &part.target_hash)); |
| 366 | install_plan.partitions = {part}; |
| 367 | |
| 368 | BuildActions(install_plan); |
| 369 | |
| 370 | FilesystemVerifierActionTestDelegate delegate; |
| 371 | processor_.set_delegate(&delegate); |
| 372 | |
| 373 | loop_.PostTask( |
| 374 | FROM_HERE, |
| 375 | base::Bind( |
| 376 | [](ActionProcessor* processor) { processor->StartProcessing(); }, |
| 377 | base::Unretained(&processor_))); |
| 378 | loop_.Run(); |
| 379 | |
| 380 | EXPECT_FALSE(processor_.IsRunning()); |
| 381 | EXPECT_TRUE(delegate.ran()); |
| 382 | EXPECT_EQ(ErrorCode::kSuccess, delegate.code()); |
| 383 | } |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 384 | } // namespace chromeos_update_engine |