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/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/annotated_operation.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 extern const char* kUnittestPrivateKeyPath;
50 extern const char* kUnittestPublicKeyPath;
51
52 class DownloadActionTest : public ::testing::Test {
53 public:
54 static constexpr int64_t METADATA_SIZE = 1024;
55 static constexpr int64_t SIGNATURE_SIZE = 256;
56 std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{
57 new ActionPipe<InstallPlan>()};
58 };
59
TEST_F(DownloadActionTest,CacheManifestInvalid)60 TEST_F(DownloadActionTest, CacheManifestInvalid) {
61 std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-');
62 MockPrefs prefs;
63 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
64 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
65 EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
66 .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true)));
67 EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
68 .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true)));
69 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
70 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
71 EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
72 .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
73
74 BootControlStub boot_control;
75 MockHttpFetcher* http_fetcher =
76 new MockHttpFetcher(data.data(), data.size(), nullptr);
77 http_fetcher->set_delay(false);
78 InstallPlan install_plan;
79 auto& payload = install_plan.payloads.emplace_back();
80 install_plan.download_url = "http://fake_url.invalid";
81 payload.size = data.size();
82 payload.payload_urls.emplace_back("http://fake_url.invalid");
83 install_plan.is_resume = true;
84 action_pipe->set_contents(install_plan);
85
86 // takes ownership of passed in HttpFetcher
87 auto download_action = std::make_unique<DownloadAction>(
88 &prefs, &boot_control, nullptr, http_fetcher, false /* interactive */);
89 download_action->set_in_pipe(action_pipe);
90 MockActionProcessor mock_processor;
91 download_action->SetProcessor(&mock_processor);
92 download_action->PerformAction();
93 ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size());
94 }
95
TEST_F(DownloadActionTest,CacheManifestValid)96 TEST_F(DownloadActionTest, CacheManifestValid) {
97 // Create a valid manifest
98 PayloadGenerationConfig config;
99 config.version.major = kMaxSupportedMajorPayloadVersion;
100 config.version.minor = kMaxSupportedMinorPayloadVersion;
101
102 PayloadFile payload_file;
103 ASSERT_TRUE(payload_file.Init(config));
104 PartitionConfig partition_config{"system"};
105 ScopedTempFile partition_file("part-system-XXXXXX", true);
106 ftruncate(partition_file.fd(), 4096);
107 partition_config.size = 4096;
108 partition_config.path = partition_file.path();
109 ASSERT_TRUE(
110 payload_file.AddPartition(partition_config, partition_config, {}, {}, 0));
111 ScopedTempFile blob_file("Blob-XXXXXX");
112 ScopedTempFile manifest_file("Manifest-XXXXXX");
113 uint64_t metadata_size;
114 std::string private_key =
115 test_utils::GetBuildArtifactsPath(kUnittestPrivateKeyPath);
116 payload_file.WritePayload(
117 manifest_file.path(), blob_file.path(), private_key, &metadata_size);
118 uint64_t signature_blob_length = 0;
119 ASSERT_TRUE(PayloadSigner::SignatureBlobLength({private_key},
120 &signature_blob_length));
121 std::string data;
122 ASSERT_TRUE(utils::ReadFile(manifest_file.path(), &data));
123 data.resize(metadata_size + signature_blob_length);
124
125 // Setup the prefs so that manifest is cached
126 MockPrefs prefs;
127 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
128 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
129 EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
130 .WillRepeatedly(DoAll(SetArgPointee<1>(metadata_size), Return(true)));
131 EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
132 .WillRepeatedly(
133 DoAll(SetArgPointee<1>(signature_blob_length), Return(true)));
134 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
135 .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
136 EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
137 .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
138 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _))
139 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
140 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
141 .WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(true)));
142
143 BootControlStub boot_control;
144 MockHttpFetcher* http_fetcher =
145 new MockHttpFetcher(data.data(), data.size(), nullptr);
146 http_fetcher->set_delay(false);
147 InstallPlan install_plan;
148 auto& payload = install_plan.payloads.emplace_back();
149 install_plan.download_url = "http://fake_url.invalid";
150 payload.size = data.size();
151 payload.payload_urls.emplace_back("http://fake_url.invalid");
152 install_plan.is_resume = true;
153 auto& install_part = install_plan.partitions.emplace_back();
154 install_part.source_path = partition_file.path();
155 install_part.target_path = partition_file.path();
156 action_pipe->set_contents(install_plan);
157
158 FakeHardware hardware;
159 // takes ownership of passed in HttpFetcher
160 auto download_action = std::make_unique<DownloadAction>(
161 &prefs, &boot_control, &hardware, http_fetcher, false /* interactive */);
162
163 auto delta_performer = std::make_unique<DeltaPerformer>(&prefs,
164 &boot_control,
165 &hardware,
166 nullptr,
167 &install_plan,
168 &payload,
169 false);
170 delta_performer->set_public_key_path(kUnittestPublicKeyPath);
171 download_action->SetTestFileWriter(std::move(delta_performer));
172 download_action->set_in_pipe(action_pipe);
173 MockActionProcessor mock_processor;
174 download_action->SetProcessor(&mock_processor);
175 download_action->PerformAction();
176
177 // Manifest is cached, so no data should be downloaded from http fetcher.
178 ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), 0UL);
179 }
180 } // namespace chromeos_update_engine
181