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 | |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <cstdint> |
| 19 | #include <memory> |
| 20 | |
| 21 | #include <gmock/gmock.h> |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 22 | #include <gmock/gmock-actions.h> |
| 23 | #include <gmock/gmock-function-mocker.h> |
| 24 | #include <gmock/gmock-spec-builders.h> |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 26 | |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 27 | #include "update_engine/common/action_pipe.h" |
| 28 | #include "update_engine/common/boot_control_stub.h" |
| 29 | #include "update_engine/common/constants.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 30 | #include "update_engine/common/download_action.h" |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 31 | #include "update_engine/common/fake_hardware.h" |
| 32 | #include "update_engine/common/mock_action_processor.h" |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 33 | #include "update_engine/common/mock_http_fetcher.h" |
| 34 | #include "update_engine/common/mock_prefs.h" |
| 35 | #include "update_engine/common/test_utils.h" |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 36 | #include "update_engine/common/utils.h" |
| 37 | #include "update_engine/payload_consumer/install_plan.h" |
| 38 | #include "update_engine/payload_consumer/payload_constants.h" |
| 39 | #include "update_engine/payload_generator/payload_file.h" |
| 40 | #include "update_engine/payload_generator/payload_signer.h" |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 41 | |
| 42 | namespace chromeos_update_engine { |
| 43 | using testing::_; |
| 44 | using testing::DoAll; |
| 45 | using testing::Return; |
| 46 | using testing::SetArgPointee; |
| 47 | |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 48 | extern const char* kUnittestPrivateKeyPath; |
| 49 | extern const char* kUnittestPublicKeyPath; |
| 50 | |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 51 | class DownloadActionTest : public ::testing::Test { |
| 52 | public: |
| 53 | static constexpr int64_t METADATA_SIZE = 1024; |
| 54 | static constexpr int64_t SIGNATURE_SIZE = 256; |
| 55 | std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{ |
| 56 | new ActionPipe<InstallPlan>()}; |
| 57 | }; |
| 58 | |
| 59 | TEST_F(DownloadActionTest, CacheManifestInvalid) { |
| 60 | std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-'); |
| 61 | MockPrefs prefs; |
| 62 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _)) |
| 63 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 64 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _)) |
| 65 | .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true))); |
| 66 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _)) |
| 67 | .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true))); |
| 68 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _)) |
| 69 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 70 | EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _)) |
| 71 | .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true))); |
| 72 | |
| 73 | BootControlStub boot_control; |
| 74 | MockHttpFetcher* http_fetcher = |
| 75 | new MockHttpFetcher(data.data(), data.size(), nullptr); |
| 76 | http_fetcher->set_delay(false); |
| 77 | InstallPlan install_plan; |
| 78 | auto& payload = install_plan.payloads.emplace_back(); |
| 79 | install_plan.download_url = "http://fake_url.invalid"; |
| 80 | payload.size = data.size(); |
| 81 | payload.payload_urls.emplace_back("http://fake_url.invalid"); |
| 82 | install_plan.is_resume = true; |
| 83 | action_pipe->set_contents(install_plan); |
| 84 | |
| 85 | // takes ownership of passed in HttpFetcher |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 86 | auto download_action = std::make_unique<DownloadAction>( |
| 87 | &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */); |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 88 | download_action->set_in_pipe(action_pipe); |
| 89 | MockActionProcessor mock_processor; |
| 90 | download_action->SetProcessor(&mock_processor); |
| 91 | download_action->PerformAction(); |
| 92 | ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size()); |
| 93 | } |
| 94 | |
Kelvin Zhang | ead9fd7 | 2020-12-03 12:57:35 -0500 | [diff] [blame] | 95 | TEST_F(DownloadActionTest, CacheManifestValid) { |
| 96 | // Create a valid manifest |
| 97 | PayloadGenerationConfig config; |
| 98 | config.version.major = kMaxSupportedMajorPayloadVersion; |
| 99 | config.version.minor = kMaxSupportedMinorPayloadVersion; |
| 100 | |
| 101 | PayloadFile payload_file; |
| 102 | ASSERT_TRUE(payload_file.Init(config)); |
| 103 | PartitionConfig partition_config{"system"}; |
| 104 | ScopedTempFile partition_file("part-system-XXXXXX", true); |
| 105 | ftruncate(partition_file.fd(), 4096); |
| 106 | partition_config.size = 4096; |
| 107 | partition_config.path = partition_file.path(); |
| 108 | ASSERT_TRUE( |
| 109 | payload_file.AddPartition(partition_config, partition_config, {}, {}, 0)); |
| 110 | ScopedTempFile blob_file("Blob-XXXXXX"); |
| 111 | ScopedTempFile manifest_file("Manifest-XXXXXX"); |
| 112 | uint64_t metadata_size; |
| 113 | std::string private_key = |
| 114 | test_utils::GetBuildArtifactsPath(kUnittestPrivateKeyPath); |
| 115 | payload_file.WritePayload( |
| 116 | manifest_file.path(), blob_file.path(), private_key, &metadata_size); |
| 117 | uint64_t signature_blob_length = 0; |
| 118 | ASSERT_TRUE(PayloadSigner::SignatureBlobLength({private_key}, |
| 119 | &signature_blob_length)); |
| 120 | std::string data; |
| 121 | ASSERT_TRUE(utils::ReadFile(manifest_file.path(), &data)); |
| 122 | data.resize(metadata_size + signature_blob_length); |
| 123 | |
| 124 | // Setup the prefs so that manifest is cached |
| 125 | MockPrefs prefs; |
| 126 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _)) |
| 127 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 128 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _)) |
| 129 | .WillRepeatedly(DoAll(SetArgPointee<1>(metadata_size), Return(true))); |
| 130 | EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _)) |
| 131 | .WillRepeatedly( |
| 132 | DoAll(SetArgPointee<1>(signature_blob_length), Return(true))); |
| 133 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _)) |
| 134 | .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true))); |
| 135 | EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _)) |
| 136 | .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true))); |
| 137 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _)) |
| 138 | .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true))); |
| 139 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _)) |
| 140 | .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true))); |
| 141 | |
| 142 | BootControlStub boot_control; |
| 143 | MockHttpFetcher* http_fetcher = |
| 144 | new MockHttpFetcher(data.data(), data.size(), nullptr); |
| 145 | http_fetcher->set_delay(false); |
| 146 | InstallPlan install_plan; |
| 147 | auto& payload = install_plan.payloads.emplace_back(); |
| 148 | install_plan.download_url = "http://fake_url.invalid"; |
| 149 | payload.size = data.size(); |
| 150 | payload.payload_urls.emplace_back("http://fake_url.invalid"); |
| 151 | install_plan.is_resume = true; |
| 152 | action_pipe->set_contents(install_plan); |
| 153 | |
| 154 | // takes ownership of passed in HttpFetcher |
| 155 | auto download_action = std::make_unique<DownloadAction>( |
| 156 | &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */); |
| 157 | |
| 158 | FakeHardware hardware; |
| 159 | DeltaPerformer delta_performer(&prefs, |
| 160 | &boot_control, |
| 161 | &hardware, |
| 162 | nullptr, |
| 163 | &install_plan, |
| 164 | &payload, |
| 165 | false); |
| 166 | delta_performer.set_public_key_path(kUnittestPublicKeyPath); |
| 167 | download_action->SetTestFileWriter(&delta_performer); |
| 168 | download_action->set_in_pipe(action_pipe); |
| 169 | MockActionProcessor mock_processor; |
| 170 | download_action->SetProcessor(&mock_processor); |
| 171 | download_action->PerformAction(); |
| 172 | |
| 173 | // Manifest is cached, so no data should be downloaded from http fetcher. |
| 174 | ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), 0UL); |
| 175 | } |
Kelvin Zhang | 9dd9305 | 2020-07-21 17:31:19 -0400 | [diff] [blame] | 176 | } // namespace chromeos_update_engine |