blob: 968f875803d025e4853107880e25ae49408feffa [file] [log] [blame]
Kelvin Zhang9dd93052020-07-21 17:31:19 -04001//
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 Zhangead9fd72020-12-03 12:57:35 -050017#include <unistd.h>
18#include <cstdint>
19#include <memory>
20
21#include <gmock/gmock.h>
Kelvin Zhang9dd93052020-07-21 17:31:19 -040022#include <gmock/gmock-actions.h>
23#include <gmock/gmock-function-mocker.h>
24#include <gmock/gmock-spec-builders.h>
Kelvin Zhangead9fd72020-12-03 12:57:35 -050025#include <gtest/gtest.h>
Kelvin Zhang9dd93052020-07-21 17:31:19 -040026
Kelvin Zhang9dd93052020-07-21 17:31:19 -040027#include "update_engine/common/action_pipe.h"
28#include "update_engine/common/boot_control_stub.h"
29#include "update_engine/common/constants.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/common/download_action.h"
Kelvin Zhangead9fd72020-12-03 12:57:35 -050031#include "update_engine/common/fake_hardware.h"
32#include "update_engine/common/mock_action_processor.h"
Kelvin Zhang9dd93052020-07-21 17:31:19 -040033#include "update_engine/common/mock_http_fetcher.h"
34#include "update_engine/common/mock_prefs.h"
35#include "update_engine/common/test_utils.h"
Colin Cross26b82b12021-12-22 10:09:19 -080036#include "update_engine/common/testing_constants.h"
Kelvin Zhangead9fd72020-12-03 12:57:35 -050037#include "update_engine/common/utils.h"
38#include "update_engine/payload_consumer/install_plan.h"
39#include "update_engine/payload_consumer/payload_constants.h"
40#include "update_engine/payload_generator/payload_file.h"
41#include "update_engine/payload_generator/payload_signer.h"
Kelvin Zhang9dd93052020-07-21 17:31:19 -040042
43namespace chromeos_update_engine {
44using testing::_;
45using testing::DoAll;
46using testing::Return;
47using testing::SetArgPointee;
48
49class DownloadActionTest : public ::testing::Test {
50 public:
51 static constexpr int64_t METADATA_SIZE = 1024;
52 static constexpr int64_t SIGNATURE_SIZE = 256;
53 std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{
54 new ActionPipe<InstallPlan>()};
55};
56
57TEST_F(DownloadActionTest, CacheManifestInvalid) {
58 std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-');
59 MockPrefs prefs;
60 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
61 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
62 EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
63 .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true)));
64 EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
65 .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true)));
66 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
67 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
68 EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
69 .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
70
71 BootControlStub boot_control;
Kelvin Zhangc7a1d1f2022-07-29 13:36:29 -070072 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
Kelvin Zhang9dd93052020-07-21 17:31:19 -040073 http_fetcher->set_delay(false);
74 InstallPlan install_plan;
75 auto& payload = install_plan.payloads.emplace_back();
76 install_plan.download_url = "http://fake_url.invalid";
77 payload.size = data.size();
78 payload.payload_urls.emplace_back("http://fake_url.invalid");
79 install_plan.is_resume = true;
80 action_pipe->set_contents(install_plan);
81
82 // takes ownership of passed in HttpFetcher
Kelvin Zhangead9fd72020-12-03 12:57:35 -050083 auto download_action = std::make_unique<DownloadAction>(
84 &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */);
Kelvin Zhang9dd93052020-07-21 17:31:19 -040085 download_action->set_in_pipe(action_pipe);
86 MockActionProcessor mock_processor;
87 download_action->SetProcessor(&mock_processor);
88 download_action->PerformAction();
89 ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size());
90}
91
Kelvin Zhangead9fd72020-12-03 12:57:35 -050092TEST_F(DownloadActionTest, CacheManifestValid) {
93 // Create a valid manifest
94 PayloadGenerationConfig config;
95 config.version.major = kMaxSupportedMajorPayloadVersion;
96 config.version.minor = kMaxSupportedMinorPayloadVersion;
97
98 PayloadFile payload_file;
99 ASSERT_TRUE(payload_file.Init(config));
100 PartitionConfig partition_config{"system"};
101 ScopedTempFile partition_file("part-system-XXXXXX", true);
102 ftruncate(partition_file.fd(), 4096);
103 partition_config.size = 4096;
104 partition_config.path = partition_file.path();
105 ASSERT_TRUE(
106 payload_file.AddPartition(partition_config, partition_config, {}, {}, 0));
107 ScopedTempFile blob_file("Blob-XXXXXX");
108 ScopedTempFile manifest_file("Manifest-XXXXXX");
109 uint64_t metadata_size;
110 std::string private_key =
111 test_utils::GetBuildArtifactsPath(kUnittestPrivateKeyPath);
112 payload_file.WritePayload(
113 manifest_file.path(), blob_file.path(), private_key, &metadata_size);
114 uint64_t signature_blob_length = 0;
115 ASSERT_TRUE(PayloadSigner::SignatureBlobLength({private_key},
116 &signature_blob_length));
117 std::string data;
118 ASSERT_TRUE(utils::ReadFile(manifest_file.path(), &data));
119 data.resize(metadata_size + signature_blob_length);
120
121 // Setup the prefs so that manifest is cached
122 MockPrefs prefs;
123 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
124 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
125 EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
126 .WillRepeatedly(DoAll(SetArgPointee<1>(metadata_size), Return(true)));
127 EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
128 .WillRepeatedly(
129 DoAll(SetArgPointee<1>(signature_blob_length), Return(true)));
130 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
131 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
132 EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
133 .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
134 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _))
135 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
136 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
137 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
138
139 BootControlStub boot_control;
Kelvin Zhangc7a1d1f2022-07-29 13:36:29 -0700140 MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500141 http_fetcher->set_delay(false);
142 InstallPlan install_plan;
143 auto& payload = install_plan.payloads.emplace_back();
144 install_plan.download_url = "http://fake_url.invalid";
145 payload.size = data.size();
146 payload.payload_urls.emplace_back("http://fake_url.invalid");
147 install_plan.is_resume = true;
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500148 auto& install_part = install_plan.partitions.emplace_back();
149 install_part.source_path = partition_file.path();
150 install_part.target_path = partition_file.path();
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500151 action_pipe->set_contents(install_plan);
152
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500153 FakeHardware hardware;
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500154 // takes ownership of passed in HttpFetcher
155 auto download_action = std::make_unique<DownloadAction>(
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500156 &prefs, &boot_control, &hardware, http_fetcher, false /* interactive */);
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500157
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500158 auto delta_performer = std::make_unique<DeltaPerformer>(&prefs,
159 &boot_control,
160 &hardware,
161 nullptr,
162 &install_plan,
163 &payload,
164 false);
165 delta_performer->set_public_key_path(kUnittestPublicKeyPath);
166 download_action->SetTestFileWriter(std::move(delta_performer));
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500167 download_action->set_in_pipe(action_pipe);
168 MockActionProcessor mock_processor;
169 download_action->SetProcessor(&mock_processor);
170 download_action->PerformAction();
171
172 // Manifest is cached, so no data should be downloaded from http fetcher.
173 ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), 0UL);
174}
Kelvin Zhang9dd93052020-07-21 17:31:19 -0400175} // namespace chromeos_update_engine