1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2020 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #include <unistd.h>
18*5a923131SAndroid Build Coastguard Worker #include <cstdint>
19*5a923131SAndroid Build Coastguard Worker #include <memory>
20*5a923131SAndroid Build Coastguard Worker
21*5a923131SAndroid Build Coastguard Worker #include <gmock/gmock.h>
22*5a923131SAndroid Build Coastguard Worker #include <gmock/gmock-actions.h>
23*5a923131SAndroid Build Coastguard Worker #include <gmock/gmock-function-mocker.h>
24*5a923131SAndroid Build Coastguard Worker #include <gmock/gmock-spec-builders.h>
25*5a923131SAndroid Build Coastguard Worker #include <gtest/gtest.h>
26*5a923131SAndroid Build Coastguard Worker
27*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/action_pipe.h"
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/boot_control_stub.h"
29*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/constants.h"
30*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/download_action.h"
31*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/fake_hardware.h"
32*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/mock_action_processor.h"
33*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/mock_http_fetcher.h"
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/mock_prefs.h"
35*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/test_utils.h"
36*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/testing_constants.h"
37*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
38*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/install_plan.h"
39*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_consumer/payload_constants.h"
40*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_generator/payload_file.h"
41*5a923131SAndroid Build Coastguard Worker #include "update_engine/payload_generator/payload_signer.h"
42*5a923131SAndroid Build Coastguard Worker
43*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
44*5a923131SAndroid Build Coastguard Worker using testing::_;
45*5a923131SAndroid Build Coastguard Worker using testing::DoAll;
46*5a923131SAndroid Build Coastguard Worker using testing::Return;
47*5a923131SAndroid Build Coastguard Worker using testing::SetArgPointee;
48*5a923131SAndroid Build Coastguard Worker
49*5a923131SAndroid Build Coastguard Worker class DownloadActionTest : public ::testing::Test {
50*5a923131SAndroid Build Coastguard Worker public:
51*5a923131SAndroid Build Coastguard Worker static constexpr int64_t METADATA_SIZE = 1024;
52*5a923131SAndroid Build Coastguard Worker static constexpr int64_t SIGNATURE_SIZE = 256;
53*5a923131SAndroid Build Coastguard Worker std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{
54*5a923131SAndroid Build Coastguard Worker new ActionPipe<InstallPlan>()};
55*5a923131SAndroid Build Coastguard Worker };
56*5a923131SAndroid Build Coastguard Worker
TEST_F(DownloadActionTest,CacheManifestInvalid)57*5a923131SAndroid Build Coastguard Worker TEST_F(DownloadActionTest, CacheManifestInvalid) {
58*5a923131SAndroid Build Coastguard Worker std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-');
59*5a923131SAndroid Build Coastguard Worker MockPrefs prefs;
60*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
61*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
62*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
63*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true)));
64*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
65*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true)));
66*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
67*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
68*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
69*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
70*5a923131SAndroid Build Coastguard Worker
71*5a923131SAndroid Build Coastguard Worker BootControlStub boot_control;
72*5a923131SAndroid Build Coastguard Worker MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
73*5a923131SAndroid Build Coastguard Worker http_fetcher->set_delay(false);
74*5a923131SAndroid Build Coastguard Worker InstallPlan install_plan;
75*5a923131SAndroid Build Coastguard Worker auto& payload = install_plan.payloads.emplace_back();
76*5a923131SAndroid Build Coastguard Worker install_plan.download_url = "http://fake_url.invalid";
77*5a923131SAndroid Build Coastguard Worker payload.size = data.size();
78*5a923131SAndroid Build Coastguard Worker payload.payload_urls.emplace_back("http://fake_url.invalid");
79*5a923131SAndroid Build Coastguard Worker install_plan.is_resume = true;
80*5a923131SAndroid Build Coastguard Worker action_pipe->set_contents(install_plan);
81*5a923131SAndroid Build Coastguard Worker
82*5a923131SAndroid Build Coastguard Worker // takes ownership of passed in HttpFetcher
83*5a923131SAndroid Build Coastguard Worker auto download_action = std::make_unique<DownloadAction>(
84*5a923131SAndroid Build Coastguard Worker &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */);
85*5a923131SAndroid Build Coastguard Worker download_action->set_in_pipe(action_pipe);
86*5a923131SAndroid Build Coastguard Worker MockActionProcessor mock_processor;
87*5a923131SAndroid Build Coastguard Worker download_action->SetProcessor(&mock_processor);
88*5a923131SAndroid Build Coastguard Worker download_action->PerformAction();
89*5a923131SAndroid Build Coastguard Worker ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size());
90*5a923131SAndroid Build Coastguard Worker }
91*5a923131SAndroid Build Coastguard Worker
TEST_F(DownloadActionTest,CacheManifestValid)92*5a923131SAndroid Build Coastguard Worker TEST_F(DownloadActionTest, CacheManifestValid) {
93*5a923131SAndroid Build Coastguard Worker // Create a valid manifest
94*5a923131SAndroid Build Coastguard Worker PayloadGenerationConfig config;
95*5a923131SAndroid Build Coastguard Worker config.version.major = kMaxSupportedMajorPayloadVersion;
96*5a923131SAndroid Build Coastguard Worker config.version.minor = kMaxSupportedMinorPayloadVersion;
97*5a923131SAndroid Build Coastguard Worker
98*5a923131SAndroid Build Coastguard Worker PayloadFile payload_file;
99*5a923131SAndroid Build Coastguard Worker ASSERT_TRUE(payload_file.Init(config));
100*5a923131SAndroid Build Coastguard Worker PartitionConfig partition_config{"system"};
101*5a923131SAndroid Build Coastguard Worker ScopedTempFile partition_file("part-system-XXXXXX", true);
102*5a923131SAndroid Build Coastguard Worker ftruncate(partition_file.fd(), 4096);
103*5a923131SAndroid Build Coastguard Worker partition_config.size = 4096;
104*5a923131SAndroid Build Coastguard Worker partition_config.path = partition_file.path();
105*5a923131SAndroid Build Coastguard Worker ASSERT_TRUE(payload_file.AddPartition(
106*5a923131SAndroid Build Coastguard Worker partition_config, partition_config, {}, {}, {}));
107*5a923131SAndroid Build Coastguard Worker ScopedTempFile blob_file("Blob-XXXXXX");
108*5a923131SAndroid Build Coastguard Worker ScopedTempFile manifest_file("Manifest-XXXXXX");
109*5a923131SAndroid Build Coastguard Worker uint64_t metadata_size;
110*5a923131SAndroid Build Coastguard Worker std::string private_key =
111*5a923131SAndroid Build Coastguard Worker test_utils::GetBuildArtifactsPath(kUnittestPrivateKeyPath);
112*5a923131SAndroid Build Coastguard Worker payload_file.WritePayload(
113*5a923131SAndroid Build Coastguard Worker manifest_file.path(), blob_file.path(), private_key, &metadata_size);
114*5a923131SAndroid Build Coastguard Worker uint64_t signature_blob_length = 0;
115*5a923131SAndroid Build Coastguard Worker ASSERT_TRUE(PayloadSigner::SignatureBlobLength({private_key},
116*5a923131SAndroid Build Coastguard Worker &signature_blob_length));
117*5a923131SAndroid Build Coastguard Worker std::string data;
118*5a923131SAndroid Build Coastguard Worker ASSERT_TRUE(utils::ReadFile(manifest_file.path(), &data));
119*5a923131SAndroid Build Coastguard Worker data.resize(metadata_size + signature_blob_length);
120*5a923131SAndroid Build Coastguard Worker
121*5a923131SAndroid Build Coastguard Worker // Setup the prefs so that manifest is cached
122*5a923131SAndroid Build Coastguard Worker MockPrefs prefs;
123*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
124*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
125*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
126*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(metadata_size), Return(true)));
127*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
128*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(
129*5a923131SAndroid Build Coastguard Worker DoAll(SetArgPointee<1>(signature_blob_length), Return(true)));
130*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
131*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
132*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
133*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
134*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _))
135*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
136*5a923131SAndroid Build Coastguard Worker EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
137*5a923131SAndroid Build Coastguard Worker .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
138*5a923131SAndroid Build Coastguard Worker
139*5a923131SAndroid Build Coastguard Worker BootControlStub boot_control;
140*5a923131SAndroid Build Coastguard Worker MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
141*5a923131SAndroid Build Coastguard Worker http_fetcher->set_delay(false);
142*5a923131SAndroid Build Coastguard Worker InstallPlan install_plan;
143*5a923131SAndroid Build Coastguard Worker auto& payload = install_plan.payloads.emplace_back();
144*5a923131SAndroid Build Coastguard Worker install_plan.download_url = "http://fake_url.invalid";
145*5a923131SAndroid Build Coastguard Worker payload.size = data.size();
146*5a923131SAndroid Build Coastguard Worker payload.payload_urls.emplace_back("http://fake_url.invalid");
147*5a923131SAndroid Build Coastguard Worker install_plan.is_resume = true;
148*5a923131SAndroid Build Coastguard Worker auto& install_part = install_plan.partitions.emplace_back();
149*5a923131SAndroid Build Coastguard Worker install_part.source_path = partition_file.path();
150*5a923131SAndroid Build Coastguard Worker install_part.target_path = partition_file.path();
151*5a923131SAndroid Build Coastguard Worker action_pipe->set_contents(install_plan);
152*5a923131SAndroid Build Coastguard Worker
153*5a923131SAndroid Build Coastguard Worker FakeHardware hardware;
154*5a923131SAndroid Build Coastguard Worker // takes ownership of passed in HttpFetcher
155*5a923131SAndroid Build Coastguard Worker auto download_action = std::make_unique<DownloadAction>(
156*5a923131SAndroid Build Coastguard Worker &prefs, &boot_control, &hardware, http_fetcher, false /* interactive */);
157*5a923131SAndroid Build Coastguard Worker
158*5a923131SAndroid Build Coastguard Worker auto delta_performer = std::make_unique<DeltaPerformer>(&prefs,
159*5a923131SAndroid Build Coastguard Worker &boot_control,
160*5a923131SAndroid Build Coastguard Worker &hardware,
161*5a923131SAndroid Build Coastguard Worker nullptr,
162*5a923131SAndroid Build Coastguard Worker &install_plan,
163*5a923131SAndroid Build Coastguard Worker &payload,
164*5a923131SAndroid Build Coastguard Worker false);
165*5a923131SAndroid Build Coastguard Worker delta_performer->set_public_key_path(kUnittestPublicKeyPath);
166*5a923131SAndroid Build Coastguard Worker download_action->SetTestFileWriter(std::move(delta_performer));
167*5a923131SAndroid Build Coastguard Worker download_action->set_in_pipe(action_pipe);
168*5a923131SAndroid Build Coastguard Worker MockActionProcessor mock_processor;
169*5a923131SAndroid Build Coastguard Worker download_action->SetProcessor(&mock_processor);
170*5a923131SAndroid Build Coastguard Worker download_action->PerformAction();
171*5a923131SAndroid Build Coastguard Worker
172*5a923131SAndroid Build Coastguard Worker // Manifest is cached, so no data should be downloaded from http fetcher.
173*5a923131SAndroid Build Coastguard Worker ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), 0UL);
174*5a923131SAndroid Build Coastguard Worker }
175*5a923131SAndroid Build Coastguard Worker } // namespace chromeos_update_engine
176