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 <unistd.h>
18 #include <cstdint>
19 #include <memory>
20 
21 #include <gmock/gmock.h>
22 #include <gmock/gmock-actions.h>
23 #include <gmock/gmock-function-mocker.h>
24 #include <gmock/gmock-spec-builders.h>
25 #include <gtest/gtest.h>
26 
27 #include "update_engine/common/action_pipe.h"
28 #include "update_engine/common/boot_control_stub.h"
29 #include "update_engine/common/constants.h"
30 #include "update_engine/common/download_action.h"
31 #include "update_engine/common/fake_hardware.h"
32 #include "update_engine/common/mock_action_processor.h"
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"
36 #include "update_engine/common/testing_constants.h"
37 #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"
42 
43 namespace chromeos_update_engine {
44 using testing::_;
45 using testing::DoAll;
46 using testing::Return;
47 using testing::SetArgPointee;
48 
49 class 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 
TEST_F(DownloadActionTest,CacheManifestInvalid)57 TEST_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;
72   MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
73   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
83   auto download_action = std::make_unique<DownloadAction>(
84       &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */);
85   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 
TEST_F(DownloadActionTest,CacheManifestValid)92 TEST_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(payload_file.AddPartition(
106       partition_config, partition_config, {}, {}, {}));
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;
140   MockHttpFetcher* http_fetcher = new MockHttpFetcher(data.data(), data.size());
141   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;
148   auto& install_part = install_plan.partitions.emplace_back();
149   install_part.source_path = partition_file.path();
150   install_part.target_path = partition_file.path();
151   action_pipe->set_contents(install_plan);
152 
153   FakeHardware hardware;
154   // takes ownership of passed in HttpFetcher
155   auto download_action = std::make_unique<DownloadAction>(
156       &prefs, &boot_control, &hardware, http_fetcher, false /* interactive */);
157 
158   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));
167   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 }
175 }  // namespace chromeos_update_engine
176