Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2020 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 | // |
| 16 | |
| 17 | #include "common/mock_action_processor.h" |
| 18 | #include <gmock/gmock-actions.h> |
| 19 | #include <gmock/gmock-function-mocker.h> |
| 20 | #include <gmock/gmock-spec-builders.h> |
| 21 | |
| 22 | #include "payload_consumer/install_plan.h" |
| 23 | #include "update_engine/common/action_pipe.h" |
| 24 | #include "update_engine/common/boot_control_stub.h" |
| 25 | #include "update_engine/common/constants.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 26 | #include "update_engine/common/download_action.h" |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 27 | #include "update_engine/common/mock_http_fetcher.h" |
| 28 | #include "update_engine/common/mock_prefs.h" |
| 29 | #include "update_engine/common/test_utils.h" |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 30 | |
| 31 | #include <gmock/gmock.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | #include <memory> |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | using testing::_; |
| 37 | using testing::DoAll; |
| 38 | using testing::Return; |
| 39 | using testing::SetArgPointee; |
| 40 | |
| 41 | class DownloadActionTest : public ::testing::Test { |
| 42 | public: |
| 43 | static constexpr int64_t METADATA_SIZE = 1024; |
| 44 | static constexpr int64_t SIGNATURE_SIZE = 256; |
| 45 | std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{ |
| 46 | new ActionPipe<InstallPlan>()}; |
| 47 | }; |
| 48 | |
| 49 | TEST_F(DownloadActionTest, CacheManifestInvalid) { |
| 50 | std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-'); |
| 51 | MockPrefs prefs; |
| 52 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _)) |
| 53 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 54 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _)) |
| 55 | .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true))); |
| 56 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _)) |
| 57 | .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true))); |
| 58 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _)) |
| 59 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 60 | EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _)) |
| 61 | .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true))); |
| 62 | |
| 63 | BootControlStub boot_control; |
| 64 | MockHttpFetcher* http_fetcher = |
| 65 | new MockHttpFetcher(data.data(), data.size(), nullptr); |
| 66 | http_fetcher->set_delay(false); |
| 67 | InstallPlan install_plan; |
| 68 | auto& payload = install_plan.payloads.emplace_back(); |
| 69 | install_plan.download_url = "http://fake_url.invalid"; |
| 70 | payload.size = data.size(); |
| 71 | payload.payload_urls.emplace_back("http://fake_url.invalid"); |
| 72 | install_plan.is_resume = true; |
| 73 | action_pipe->set_contents(install_plan); |
| 74 | |
| 75 | // takes ownership of passed in HttpFetcher |
| 76 | auto download_action = |
| 77 | std::make_unique<DownloadAction>(&prefs, |
| 78 | &boot_control, |
| 79 | nullptr, |
| 80 | nullptr, |
| 81 | http_fetcher, |
| 82 | false /* interactive */); |
| 83 | download_action->set_in_pipe(action_pipe); |
| 84 | MockActionProcessor mock_processor; |
| 85 | download_action->SetProcessor(&mock_processor); |
| 86 | download_action->PerformAction(); |
| 87 | ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size()); |
| 88 | } |
| 89 | |
| 90 | } // namespace chromeos_update_engine |