blob: fa0692f0a2098829d306065091a6a64140317d50 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/delta_performer.h"
18
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include <inttypes.h>
20
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070021#include <string>
22#include <vector>
23
Allie Wood78750a42015-02-11 15:42:11 -080024#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070025#include <base/files/file_util.h>
Alex Deymocbf09892015-09-11 16:13:16 -070026#include <base/strings/string_number_conversions.h>
Alex Deymof1cbe172015-03-05 15:58:37 -080027#include <base/strings/string_util.h>
Alex Deymo2d621a32015-10-01 11:09:01 -070028#include <base/strings/stringprintf.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070029#include <google/protobuf/repeated_field.h>
30#include <gtest/gtest.h>
31
Sen Jiang2d528b42015-09-25 11:18:12 -070032#include "update_engine/bzip.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070033#include "update_engine/constants.h"
David Zeuthene7f89172013-10-31 10:21:04 -070034#include "update_engine/fake_hardware.h"
Sen Jiang2d528b42015-09-25 11:18:12 -070035#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070036#include "update_engine/fake_system_state.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070037#include "update_engine/payload_constants.h"
Sen Jiang2d528b42015-09-25 11:18:12 -070038#include "update_engine/payload_generator/extent_ranges.h"
Sen Jiangd78b3892015-09-25 15:19:31 -070039#include "update_engine/payload_generator/payload_file.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070040#include "update_engine/payload_generator/payload_signer.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070041#include "update_engine/test_utils.h"
42#include "update_engine/update_metadata.pb.h"
43#include "update_engine/utils.h"
44
45namespace chromeos_update_engine {
46
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070047using std::string;
48using std::vector;
Alex Deymo10875d92014-11-10 21:52:57 -080049using test_utils::System;
Alex Deymo2d621a32015-10-01 11:09:01 -070050using test_utils::kRandomString;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070051
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070052extern const char* kUnittestPrivateKeyPath;
Darin Petkovd7061ab2010-10-06 14:37:09 -070053extern const char* kUnittestPublicKeyPath;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070054
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070055static const char* kBogusMetadataSignature1 =
56 "awSFIUdUZz2VWFiR+ku0Pj00V7bPQPQFYQSXjEXr3vaw3TE4xHV5CraY3/YrZpBv"
57 "J5z4dSBskoeuaO1TNC/S6E05t+yt36tE4Fh79tMnJ/z9fogBDXWgXLEUyG78IEQr"
58 "YH6/eBsQGT2RJtBgXIXbZ9W+5G9KmGDoPOoiaeNsDuqHiBc/58OFsrxskH8E6vMS"
59 "BmMGGk82mvgzic7ApcoURbCGey1b3Mwne/hPZ/bb9CIyky8Og9IfFMdL2uAweOIR"
60 "fjoTeLYZpt+WN65Vu7jJ0cQN8e1y+2yka5112wpRf/LLtPgiAjEZnsoYpLUd7CoV"
61 "pLRtClp97kN2+tXGNBQqkA==";
Jay Srinivasan738fdf32012-12-07 17:40:54 -080062
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070063namespace {
Jay Srinivasan738fdf32012-12-07 17:40:54 -080064// Different options that determine what we should fill into the
65// install_plan.metadata_signature to simulate the contents received in the
66// Omaha response.
67enum MetadataSignatureTest {
68 kEmptyMetadataSignature,
69 kInvalidMetadataSignature,
70 kValidMetadataSignature,
71};
72
Alex Deymo2d621a32015-10-01 11:09:01 -070073// Compressed data without checksum, generated with:
74// echo -n a | xz -9 --check=none | hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
75const uint8_t kXzCompressedData[] = {
76 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x00, 0xff, 0x12, 0xd9, 0x41,
77 0x02, 0x00, 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x10, 0xcf, 0x58, 0xcc,
78 0x01, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x11, 0x01,
79 0xad, 0xa6, 0x58, 0x04, 0x06, 0x72, 0x9e, 0x7a, 0x01, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x59, 0x5a,
81};
82
Alex Vakulenkod2779df2014-06-16 13:19:00 -070083} // namespace
Jay Srinivasan738fdf32012-12-07 17:40:54 -080084
Allie Woodfdf00512015-03-02 13:34:55 -080085class DeltaPerformerTest : public ::testing::Test {
Sen Jiang2d528b42015-09-25 11:18:12 -070086 protected:
Alex Deymoe5e5fe92015-10-05 09:28:19 -070087 void SetUp() override {
88 install_plan_.source_slot = 0;
89 install_plan_.target_slot = 1;
90 }
Allie Woodfdf00512015-03-02 13:34:55 -080091
Sen Jiang2d528b42015-09-25 11:18:12 -070092 // Test helper placed where it can easily be friended from DeltaPerformer.
93 void RunManifestValidation(const DeltaArchiveManifest& manifest,
94 bool full_payload,
95 ErrorCode expected) {
Allie Woodfdf00512015-03-02 13:34:55 -080096 // The install plan is for Full or Delta.
Sen Jiang2d528b42015-09-25 11:18:12 -070097 install_plan_.is_full_update = full_payload;
Allie Woodfdf00512015-03-02 13:34:55 -080098
99 // The Manifest we are validating.
Sen Jiang2d528b42015-09-25 11:18:12 -0700100 performer_.manifest_.CopyFrom(manifest);
Allie Woodfdf00512015-03-02 13:34:55 -0800101
Sen Jiang2d528b42015-09-25 11:18:12 -0700102 EXPECT_EQ(expected, performer_.ValidateManifest());
Allie Woodfdf00512015-03-02 13:34:55 -0800103 }
104
Sen Jiang2d528b42015-09-25 11:18:12 -0700105 chromeos::Blob GeneratePayload(const chromeos::Blob& blob_data,
106 const vector<AnnotatedOperation>& aops,
107 bool sign_payload,
108 int32_t minor_version) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700109 string blob_path;
110 EXPECT_TRUE(utils::MakeTempFile("Blob-XXXXXX", &blob_path, nullptr));
111 ScopedPathUnlinker blob_unlinker(blob_path);
112 EXPECT_TRUE(utils::WriteFile(blob_path.c_str(),
113 blob_data.data(),
114 blob_data.size()));
115
116 PayloadGenerationConfig config;
117 config.major_version = kChromeOSMajorPayloadVersion;
118 config.minor_version = minor_version;
Sen Jiangd78b3892015-09-25 15:19:31 -0700119
120 PayloadFile payload;
121 EXPECT_TRUE(payload.Init(config));
122
Sen Jiang981eb112015-08-25 17:03:18 -0700123 PartitionConfig old_part(kLegacyPartitionNameRoot);
124 PartitionConfig new_part(kLegacyPartitionNameRoot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700125 new_part.path = "/dev/zero";
126 new_part.size = 1234;
Sen Jiang981eb112015-08-25 17:03:18 -0700127
128 payload.AddPartition(old_part, new_part, aops);
Sen Jiangd78b3892015-09-25 15:19:31 -0700129
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700130 // We include a kernel partition without operations.
131 old_part.name = kLegacyPartitionNameKernel;
132 new_part.name = kLegacyPartitionNameKernel;
133 new_part.size = 0;
134 payload.AddPartition(old_part, new_part, {});
135
Sen Jiangd78b3892015-09-25 15:19:31 -0700136 string payload_path;
137 EXPECT_TRUE(utils::MakeTempFile("Payload-XXXXXX", &payload_path, nullptr));
138 ScopedPathUnlinker payload_unlinker(payload_path);
139 EXPECT_TRUE(payload.WritePayload(payload_path, blob_path,
140 sign_payload ? kUnittestPrivateKeyPath : "",
Sen Jiang2d528b42015-09-25 11:18:12 -0700141 &install_plan_.metadata_size));
Sen Jiangd78b3892015-09-25 15:19:31 -0700142
143 chromeos::Blob payload_data;
144 EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data));
145 return payload_data;
146 }
Allie Woodfdf00512015-03-02 13:34:55 -0800147
Sen Jiang2d528b42015-09-25 11:18:12 -0700148 // Apply |payload_data| on partition specified in |source_path|.
149 chromeos::Blob ApplyPayload(const chromeos::Blob& payload_data,
150 const string& source_path) {
Alex Deymo79715ad2015-10-02 14:27:53 -0700151 return ApplyPayloadToData(payload_data, source_path, chromeos::Blob());
152 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800153
Alex Deymo79715ad2015-10-02 14:27:53 -0700154 // Apply the payload provided in |payload_data| reading from the |source_path|
155 // file and writing the contents to a new partition. The existing data in the
156 // new target file are set to |target_data| before applying the payload.
157 // Returns the result of the payload application.
158 chromeos::Blob ApplyPayloadToData(const chromeos::Blob& payload_data,
159 const string& source_path,
160 const chromeos::Blob& target_data) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700161 string new_part;
162 EXPECT_TRUE(utils::MakeTempFile("Partition-XXXXXX", &new_part, nullptr));
163 ScopedPathUnlinker partition_unlinker(new_part);
Alex Deymo79715ad2015-10-02 14:27:53 -0700164 EXPECT_TRUE(utils::WriteFile(new_part.c_str(), target_data.data(),
165 target_data.size()));
166
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700167 // We installed the operations only in the rootfs partition, but the
168 // delta performer needs to access all the partitions.
169 fake_system_state_.fake_boot_control()->SetPartitionDevice(
170 kLegacyPartitionNameRoot, install_plan_.target_slot, new_part);
171 fake_system_state_.fake_boot_control()->SetPartitionDevice(
172 kLegacyPartitionNameRoot, install_plan_.source_slot, source_path);
173 fake_system_state_.fake_boot_control()->SetPartitionDevice(
174 kLegacyPartitionNameKernel, install_plan_.target_slot, "/dev/null");
175 fake_system_state_.fake_boot_control()->SetPartitionDevice(
176 kLegacyPartitionNameKernel, install_plan_.source_slot, "/dev/null");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800177
Sen Jiang2d528b42015-09-25 11:18:12 -0700178 EXPECT_TRUE(performer_.Write(payload_data.data(), payload_data.size()));
179 EXPECT_EQ(0, performer_.Close());
180
181 chromeos::Blob partition_data;
182 EXPECT_TRUE(utils::ReadFile(new_part, &partition_data));
183 return partition_data;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800184 }
185
Sen Jiang2d528b42015-09-25 11:18:12 -0700186 // Calls delta performer's Write method by pretending to pass in bytes from a
187 // delta file whose metadata size is actual_metadata_size and tests if all
188 // checks are correctly performed if the install plan contains
189 // expected_metadata_size and that the result of the parsing are as per
190 // hash_checks_mandatory flag.
191 void DoMetadataSizeTest(uint64_t expected_metadata_size,
192 uint64_t actual_metadata_size,
193 bool hash_checks_mandatory) {
194 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800195
Sen Jiang2d528b42015-09-25 11:18:12 -0700196 // Set a valid magic string and version number 1.
197 EXPECT_TRUE(performer_.Write("CrAU", 4));
198 uint64_t version = htobe64(kChromeOSMajorPayloadVersion);
199 EXPECT_TRUE(performer_.Write(&version, 8));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800200
Sen Jiang2d528b42015-09-25 11:18:12 -0700201 install_plan_.metadata_size = expected_metadata_size;
202 ErrorCode error_code;
203 // When filling in size in manifest, exclude the size of the 20-byte header.
204 uint64_t size_in_manifest = htobe64(actual_metadata_size - 20);
205 bool result = performer_.Write(&size_in_manifest, 8, &error_code);
206 if (expected_metadata_size == actual_metadata_size ||
207 !hash_checks_mandatory) {
208 EXPECT_TRUE(result);
209 } else {
210 EXPECT_FALSE(result);
211 EXPECT_EQ(ErrorCode::kDownloadInvalidMetadataSize, error_code);
212 }
Sen Jiangd78b3892015-09-25 15:19:31 -0700213
Sen Jiang2d528b42015-09-25 11:18:12 -0700214 EXPECT_LT(performer_.Close(), 0);
215 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800216
Sen Jiang2d528b42015-09-25 11:18:12 -0700217 // Generates a valid delta file but tests the delta performer by suppling
218 // different metadata signatures as per metadata_signature_test flag and
219 // sees if the result of the parsing are as per hash_checks_mandatory flag.
220 void DoMetadataSignatureTest(MetadataSignatureTest metadata_signature_test,
221 bool sign_payload,
222 bool hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800223
Sen Jiang2d528b42015-09-25 11:18:12 -0700224 // Loads the payload and parses the manifest.
225 chromeos::Blob payload = GeneratePayload(chromeos::Blob(),
226 vector<AnnotatedOperation>(), sign_payload,
Alex Deymocbf09892015-09-11 16:13:16 -0700227 kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800228
Sen Jiang2d528b42015-09-25 11:18:12 -0700229 LOG(INFO) << "Payload size: " << payload.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800230
Sen Jiang2d528b42015-09-25 11:18:12 -0700231 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800232
Sen Jiang2d528b42015-09-25 11:18:12 -0700233 DeltaPerformer::MetadataParseResult expected_result, actual_result;
234 ErrorCode expected_error, actual_error;
235
236 // Fill up the metadata signature in install plan according to the test.
237 switch (metadata_signature_test) {
238 case kEmptyMetadataSignature:
239 install_plan_.metadata_signature.clear();
240 expected_result = DeltaPerformer::kMetadataParseError;
241 expected_error = ErrorCode::kDownloadMetadataSignatureMissingError;
242 break;
243
244 case kInvalidMetadataSignature:
245 install_plan_.metadata_signature = kBogusMetadataSignature1;
246 expected_result = DeltaPerformer::kMetadataParseError;
247 expected_error = ErrorCode::kDownloadMetadataSignatureMismatch;
248 break;
249
250 case kValidMetadataSignature:
251 default:
252 // Set the install plan's metadata size to be the same as the one
253 // in the manifest so that we pass the metadata size checks. Only
254 // then we can get to manifest signature checks.
255 ASSERT_TRUE(PayloadSigner::GetMetadataSignature(
256 payload.data(),
257 install_plan_.metadata_size,
258 kUnittestPrivateKeyPath,
259 &install_plan_.metadata_signature));
260 EXPECT_FALSE(install_plan_.metadata_signature.empty());
261 expected_result = DeltaPerformer::kMetadataParseSuccess;
262 expected_error = ErrorCode::kSuccess;
263 break;
264 }
265
266 // Ignore the expected result/error if hash checks are not mandatory.
267 if (!hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800268 expected_result = DeltaPerformer::kMetadataParseSuccess;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700269 expected_error = ErrorCode::kSuccess;
Sen Jiang2d528b42015-09-25 11:18:12 -0700270 }
271
272 // Use the public key corresponding to the private key used above to
273 // sign the metadata.
274 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
275 performer_.set_public_key_path(kUnittestPublicKeyPath);
276
277 // Init actual_error with an invalid value so that we make sure
278 // ParsePayloadMetadata properly populates it in all cases.
279 actual_error = ErrorCode::kUmaReportedMax;
280 actual_result = performer_.ParsePayloadMetadata(payload, &actual_error);
281
282 EXPECT_EQ(expected_result, actual_result);
283 EXPECT_EQ(expected_error, actual_error);
284
285 // Check that the parsed metadata size is what's expected. This test
286 // implicitly confirms that the metadata signature is valid, if required.
287 EXPECT_EQ(install_plan_.metadata_size, performer_.GetMetadataSize());
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800288 }
289
Sen Jiangb8060e42015-09-24 17:30:50 -0700290 void SetSupportedMajorVersion(uint64_t major_version) {
291 performer_.supported_major_version_ = major_version;
292 }
Sen Jiang2d528b42015-09-25 11:18:12 -0700293 FakePrefs prefs_;
294 InstallPlan install_plan_;
295 FakeSystemState fake_system_state_;
296 DeltaPerformer performer_{&prefs_, &fake_system_state_, &install_plan_};
297};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800298
Sen Jiang2d528b42015-09-25 11:18:12 -0700299TEST_F(DeltaPerformerTest, FullPayloadWriteTest) {
300 install_plan_.is_full_update = true;
301 chromeos::Blob expected_data = chromeos::Blob(std::begin(kRandomString),
302 std::end(kRandomString));
303 expected_data.resize(4096); // block size
304 vector<AnnotatedOperation> aops;
305 AnnotatedOperation aop;
306 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
307 aop.op.set_data_offset(0);
308 aop.op.set_data_length(expected_data.size());
309 aop.op.set_type(InstallOperation::REPLACE);
310 aops.push_back(aop);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800311
Sen Jiang2d528b42015-09-25 11:18:12 -0700312 chromeos::Blob payload_data = GeneratePayload(expected_data, aops, false,
Alex Deymocbf09892015-09-11 16:13:16 -0700313 kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800314
Alex Deymo79715ad2015-10-02 14:27:53 -0700315 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800316}
317
Sen Jiang2d528b42015-09-25 11:18:12 -0700318TEST_F(DeltaPerformerTest, ReplaceOperationTest) {
319 chromeos::Blob expected_data = chromeos::Blob(std::begin(kRandomString),
320 std::end(kRandomString));
321 expected_data.resize(4096); // block size
322 vector<AnnotatedOperation> aops;
323 AnnotatedOperation aop;
324 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
325 aop.op.set_data_offset(0);
326 aop.op.set_data_length(expected_data.size());
327 aop.op.set_type(InstallOperation::REPLACE);
328 aops.push_back(aop);
329
330 chromeos::Blob payload_data = GeneratePayload(expected_data, aops, false,
331 kSourceMinorPayloadVersion);
332
333 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
334}
335
336TEST_F(DeltaPerformerTest, ReplaceBzOperationTest) {
337 chromeos::Blob expected_data = chromeos::Blob(std::begin(kRandomString),
338 std::end(kRandomString));
339 expected_data.resize(4096); // block size
340 chromeos::Blob bz_data;
341 EXPECT_TRUE(BzipCompress(expected_data, &bz_data));
342
343 vector<AnnotatedOperation> aops;
344 AnnotatedOperation aop;
345 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
346 aop.op.set_data_offset(0);
347 aop.op.set_data_length(bz_data.size());
348 aop.op.set_type(InstallOperation::REPLACE_BZ);
349 aops.push_back(aop);
350
351 chromeos::Blob payload_data = GeneratePayload(bz_data, aops, false,
352 kSourceMinorPayloadVersion);
353
354 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
355}
356
Alex Deymo2d621a32015-10-01 11:09:01 -0700357TEST_F(DeltaPerformerTest, ReplaceXzOperationTest) {
358 chromeos::Blob xz_data(std::begin(kXzCompressedData),
359 std::end(kXzCompressedData));
360 // The compressed xz data contains only a single "a", but the operation should
361 // pad the rest of the two blocks with zeros.
362 chromeos::Blob expected_data = chromeos::Blob(4096, 0);
363 expected_data[0] = 'a';
364
365 AnnotatedOperation aop;
366 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
367 aop.op.set_data_offset(0);
368 aop.op.set_data_length(xz_data.size());
369 aop.op.set_type(InstallOperation::REPLACE_XZ);
370 vector<AnnotatedOperation> aops = {aop};
371
372 chromeos::Blob payload_data = GeneratePayload(xz_data, aops, false,
373 kSourceMinorPayloadVersion);
374
375 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
376}
377
Alex Deymo79715ad2015-10-02 14:27:53 -0700378TEST_F(DeltaPerformerTest, ZeroOperationTest) {
379 chromeos::Blob existing_data = chromeos::Blob(4096 * 10, 'a');
380 chromeos::Blob expected_data = existing_data;
381 // Blocks 4, 5 and 7 should have zeros instead of 'a' after the operation is
382 // applied.
383 std::fill(expected_data.data() + 4096 * 4, expected_data.data() + 4096 * 6,
384 0);
385 std::fill(expected_data.data() + 4096 * 7, expected_data.data() + 4096 * 8,
386 0);
387
388 AnnotatedOperation aop;
389 *(aop.op.add_dst_extents()) = ExtentForRange(4, 2);
390 *(aop.op.add_dst_extents()) = ExtentForRange(7, 1);
391 aop.op.set_type(InstallOperation::ZERO);
392 vector<AnnotatedOperation> aops = {aop};
393
394 chromeos::Blob payload_data = GeneratePayload(chromeos::Blob(), aops, false,
395 kSourceMinorPayloadVersion);
396
397 EXPECT_EQ(expected_data,
398 ApplyPayloadToData(payload_data, "/dev/null", existing_data));
399}
400
Sen Jiang2d528b42015-09-25 11:18:12 -0700401TEST_F(DeltaPerformerTest, SourceCopyOperationTest) {
402 chromeos::Blob expected_data = chromeos::Blob(std::begin(kRandomString),
403 std::end(kRandomString));
404 expected_data.resize(4096); // block size
405 vector<AnnotatedOperation> aops;
406 AnnotatedOperation aop;
407 *(aop.op.add_src_extents()) = ExtentForRange(0, 1);
408 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
409 aop.op.set_type(InstallOperation::SOURCE_COPY);
410 aops.push_back(aop);
411
412 chromeos::Blob payload_data = GeneratePayload(chromeos::Blob(), aops, false,
413 kSourceMinorPayloadVersion);
414 string source_path;
415 EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX",
416 &source_path, nullptr));
417 ScopedPathUnlinker path_unlinker(source_path);
418 EXPECT_TRUE(utils::WriteFile(source_path.c_str(),
419 expected_data.data(),
420 expected_data.size()));
421
422 EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path));
423}
424
425TEST_F(DeltaPerformerTest, ExtentsToByteStringTest) {
Allie Wood56873452015-03-27 17:48:40 -0700426 uint64_t test[] = {1, 1, 4, 2, 0, 1};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800427 COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven);
428 const uint64_t block_size = 4096;
Allie Wood56873452015-03-27 17:48:40 -0700429 const uint64_t file_length = 4 * block_size - 13;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800430
431 google::protobuf::RepeatedPtrField<Extent> extents;
432 for (size_t i = 0; i < arraysize(test); i += 2) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700433 *(extents.Add()) = ExtentForRange(test[i], test[i + 1]);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800434 }
435
Allie Wood56873452015-03-27 17:48:40 -0700436 string expected_output = "4096:4096,16384:8192,0:4083";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800437 string actual_output;
438 EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents,
439 block_size,
440 file_length,
441 &actual_output));
442 EXPECT_EQ(expected_output, actual_output);
443}
Darin Petkov68c10d12010-10-14 09:24:37 -0700444
Sen Jiang2d528b42015-09-25 11:18:12 -0700445TEST_F(DeltaPerformerTest, ValidateManifestFullGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800446 // The Manifest we are validating.
447 DeltaArchiveManifest manifest;
448 manifest.mutable_new_kernel_info();
449 manifest.mutable_new_rootfs_info();
Alex Deymocbf09892015-09-11 16:13:16 -0700450 manifest.set_minor_version(kFullPayloadMinorVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800451
Sen Jiang2d528b42015-09-25 11:18:12 -0700452 RunManifestValidation(manifest, true, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800453}
454
Sen Jiang2d528b42015-09-25 11:18:12 -0700455TEST_F(DeltaPerformerTest, ValidateManifestDeltaGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800456 // The Manifest we are validating.
457 DeltaArchiveManifest manifest;
458 manifest.mutable_old_kernel_info();
459 manifest.mutable_old_rootfs_info();
460 manifest.mutable_new_kernel_info();
461 manifest.mutable_new_rootfs_info();
462 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
463
Sen Jiang2d528b42015-09-25 11:18:12 -0700464 RunManifestValidation(manifest, false, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800465}
466
Sen Jiang2d528b42015-09-25 11:18:12 -0700467TEST_F(DeltaPerformerTest, ValidateManifestFullUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800468 // The Manifest we are validating.
469 DeltaArchiveManifest manifest;
470
Sen Jiang2d528b42015-09-25 11:18:12 -0700471 RunManifestValidation(manifest, true, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800472}
473
Sen Jiang2d528b42015-09-25 11:18:12 -0700474TEST_F(DeltaPerformerTest, ValidateManifestDeltaUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800475 // The Manifest we are validating.
476 DeltaArchiveManifest manifest;
477
Sen Jiang2d528b42015-09-25 11:18:12 -0700478 RunManifestValidation(manifest, false,
479 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800480}
481
Sen Jiang2d528b42015-09-25 11:18:12 -0700482TEST_F(DeltaPerformerTest, ValidateManifestFullOldKernelTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800483 // The Manifest we are validating.
484 DeltaArchiveManifest manifest;
485 manifest.mutable_old_kernel_info();
486 manifest.mutable_new_kernel_info();
487 manifest.mutable_new_rootfs_info();
488 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
489
Sen Jiang2d528b42015-09-25 11:18:12 -0700490 RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800491}
492
Sen Jiang2d528b42015-09-25 11:18:12 -0700493TEST_F(DeltaPerformerTest, ValidateManifestFullOldRootfsTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800494 // The Manifest we are validating.
495 DeltaArchiveManifest manifest;
496 manifest.mutable_old_rootfs_info();
497 manifest.mutable_new_kernel_info();
498 manifest.mutable_new_rootfs_info();
499 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
500
Sen Jiang2d528b42015-09-25 11:18:12 -0700501 RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800502}
503
Sen Jiang2d528b42015-09-25 11:18:12 -0700504TEST_F(DeltaPerformerTest, ValidateManifestBadMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800505 // The Manifest we are validating.
506 DeltaArchiveManifest manifest;
507
508 // Generate a bad version number.
509 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion +
510 10000);
511
Sen Jiang2d528b42015-09-25 11:18:12 -0700512 RunManifestValidation(manifest, false,
513 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800514}
515
Sen Jiangb8060e42015-09-24 17:30:50 -0700516TEST_F(DeltaPerformerTest, BrilloMetadataSignatureSizeTest) {
517 SetSupportedMajorVersion(kBrilloMajorPayloadVersion);
Sen Jiangb8060e42015-09-24 17:30:50 -0700518 EXPECT_TRUE(performer_.Write(kDeltaMagic, sizeof(kDeltaMagic)));
519
520 uint64_t major_version = htobe64(kBrilloMajorPayloadVersion);
521 EXPECT_TRUE(performer_.Write(&major_version, 8));
522
523 uint64_t manifest_size = rand() % 256;
524 uint64_t manifest_size_be = htobe64(manifest_size);
525 EXPECT_TRUE(performer_.Write(&manifest_size_be, 8));
526
527 uint32_t metadata_signature_size = rand() % 256;
528 uint32_t metadata_signature_size_be = htobe32(metadata_signature_size);
529 EXPECT_TRUE(performer_.Write(&metadata_signature_size_be, 4));
530
531 EXPECT_LT(performer_.Close(), 0);
532
533 EXPECT_TRUE(performer_.IsHeaderParsed());
534 EXPECT_EQ(kBrilloMajorPayloadVersion, performer_.GetMajorVersion());
535 uint64_t manifest_offset;
536 EXPECT_TRUE(performer_.GetManifestOffset(&manifest_offset));
537 EXPECT_EQ(24, manifest_offset); // 4 + 8 + 8 + 4
538 EXPECT_EQ(24 + manifest_size + metadata_signature_size,
539 performer_.GetMetadataSize());
540}
541
Sen Jiang2d528b42015-09-25 11:18:12 -0700542TEST_F(DeltaPerformerTest, BadDeltaMagicTest) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700543 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700544 EXPECT_FALSE(performer_.Write("morejunk", 8));
545 EXPECT_LT(performer_.Close(), 0);
Darin Petkov934bb412010-11-18 11:21:35 -0800546}
547
Sen Jiang2d528b42015-09-25 11:18:12 -0700548TEST_F(DeltaPerformerTest, WriteUpdatesPayloadState) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700549 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800550 DownloadProgress(4)).Times(1);
Sen Jiang2d528b42015-09-25 11:18:12 -0700551 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Sen Jiangb8060e42015-09-24 17:30:50 -0700552 DownloadProgress(8)).Times(1);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800553
Sen Jiang2d528b42015-09-25 11:18:12 -0700554 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700555 EXPECT_FALSE(performer_.Write("morejunk", 8));
556 EXPECT_LT(performer_.Close(), 0);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800557}
558
Sen Jiang2d528b42015-09-25 11:18:12 -0700559TEST_F(DeltaPerformerTest, MissingMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800560 DoMetadataSizeTest(0, 75456, true);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700561}
562
Sen Jiang2d528b42015-09-25 11:18:12 -0700563TEST_F(DeltaPerformerTest, MissingNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800564 DoMetadataSizeTest(0, 123456, false);
565}
566
Sen Jiang2d528b42015-09-25 11:18:12 -0700567TEST_F(DeltaPerformerTest, InvalidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800568 DoMetadataSizeTest(13000, 140000, true);
569}
570
Sen Jiang2d528b42015-09-25 11:18:12 -0700571TEST_F(DeltaPerformerTest, InvalidNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800572 DoMetadataSizeTest(40000, 50000, false);
573}
574
Sen Jiang2d528b42015-09-25 11:18:12 -0700575TEST_F(DeltaPerformerTest, ValidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800576 DoMetadataSizeTest(85376, 85376, true);
577}
578
Sen Jiang2d528b42015-09-25 11:18:12 -0700579TEST_F(DeltaPerformerTest, MandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700580 DoMetadataSignatureTest(kEmptyMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800581}
582
Sen Jiang2d528b42015-09-25 11:18:12 -0700583TEST_F(DeltaPerformerTest, NonMandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700584 DoMetadataSignatureTest(kEmptyMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800585}
586
Sen Jiang2d528b42015-09-25 11:18:12 -0700587TEST_F(DeltaPerformerTest, MandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700588 DoMetadataSignatureTest(kInvalidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800589}
590
Sen Jiang2d528b42015-09-25 11:18:12 -0700591TEST_F(DeltaPerformerTest, NonMandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700592 DoMetadataSignatureTest(kInvalidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800593}
594
Sen Jiang2d528b42015-09-25 11:18:12 -0700595TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature1Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700596 DoMetadataSignatureTest(kValidMetadataSignature, false, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800597}
598
Sen Jiang2d528b42015-09-25 11:18:12 -0700599TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature2Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700600 DoMetadataSignatureTest(kValidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800601}
602
Sen Jiang2d528b42015-09-25 11:18:12 -0700603TEST_F(DeltaPerformerTest, NonMandatoryValidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700604 DoMetadataSignatureTest(kValidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800605}
606
Sen Jiang2d528b42015-09-25 11:18:12 -0700607TEST_F(DeltaPerformerTest, UsePublicKeyFromResponse) {
David Zeuthene7f89172013-10-31 10:21:04 -0700608 base::FilePath key_path;
609
610 // The result of the GetPublicKeyResponse() method is based on three things
611 //
612 // 1. Whether it's an official build; and
613 // 2. Whether the Public RSA key to be used is in the root filesystem; and
Alex Vakulenko072359c2014-07-18 11:41:07 -0700614 // 3. Whether the response has a public key
David Zeuthene7f89172013-10-31 10:21:04 -0700615 //
616 // We test all eight combinations to ensure that we only use the
617 // public key in the response if
618 //
619 // a. it's not an official build; and
620 // b. there is no key in the root filesystem.
621
Sen Jiang2d528b42015-09-25 11:18:12 -0700622 FakeHardware* fake_hardware = fake_system_state_.fake_hardware();
David Zeuthene7f89172013-10-31 10:21:04 -0700623
624 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800625 EXPECT_TRUE(utils::MakeTempDirectory("PublicKeyFromResponseTests.XXXXXX",
David Zeuthene7f89172013-10-31 10:21:04 -0700626 &temp_dir));
627 string non_existing_file = temp_dir + "/non-existing";
628 string existing_file = temp_dir + "/existing";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700629 EXPECT_EQ(0, System(base::StringPrintf("touch %s", existing_file.c_str())));
David Zeuthene7f89172013-10-31 10:21:04 -0700630
631 // Non-official build, non-existing public-key, key in response -> true
Don Garrett6646b442013-11-13 15:29:11 -0800632 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700633 performer_.public_key_path_ = non_existing_file;
634 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
635 EXPECT_TRUE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700636 EXPECT_FALSE(key_path.empty());
637 EXPECT_EQ(unlink(key_path.value().c_str()), 0);
638 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800639 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700640 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700641
642 // Non-official build, existing public-key, key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800643 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700644 performer_.public_key_path_ = existing_file;
645 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
646 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700647 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800648 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700649 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700650
651 // Non-official build, non-existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800652 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700653 performer_.public_key_path_ = non_existing_file;
654 install_plan_.public_key_rsa = "";
655 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700656 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800657 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700658 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700659
660 // Non-official build, existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800661 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700662 performer_.public_key_path_ = existing_file;
663 install_plan_.public_key_rsa = "";
664 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700665 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800666 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700667 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700668
669 // Non-official build, non-existing public-key, key in response
670 // but invalid base64 -> false
Don Garrett6646b442013-11-13 15:29:11 -0800671 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700672 performer_.public_key_path_ = non_existing_file;
673 install_plan_.public_key_rsa = "not-valid-base64";
674 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700675
Alex Deymo10875d92014-11-10 21:52:57 -0800676 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
David Zeuthene7f89172013-10-31 10:21:04 -0700677}
678
Alex Deymocbf09892015-09-11 16:13:16 -0700679TEST_F(DeltaPerformerTest, ConfVersionsMatch) {
680 // Test that the versions in update_engine.conf that is installed to the
681 // image match the supported delta versions in the update engine.
Allie Wood78750a42015-02-11 15:42:11 -0800682 uint32_t minor_version;
Alex Deymob42b98d2015-07-06 17:42:38 -0700683 chromeos::KeyValueStore store;
684 EXPECT_TRUE(store.Load(base::FilePath("update_engine.conf")));
685 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
686 EXPECT_EQ(DeltaPerformer::kSupportedMinorPayloadVersion, minor_version);
Alex Deymocbf09892015-09-11 16:13:16 -0700687
688 string major_version_str;
689 uint64_t major_version;
690 EXPECT_TRUE(store.GetString("PAYLOAD_MAJOR_VERSION", &major_version_str));
691 EXPECT_TRUE(base::StringToUint64(major_version_str, &major_version));
692 EXPECT_EQ(DeltaPerformer::kSupportedMajorPayloadVersion, major_version);
Allie Wood78750a42015-02-11 15:42:11 -0800693}
694
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700695} // namespace chromeos_update_engine