blob: 7a8cdcfbb52f1a614b05a1ad443a1fc5ee502ce9 [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
Jay Srinivasand29695d2013-04-08 15:08:05 -070032#include "update_engine/constants.h"
David Zeuthene7f89172013-10-31 10:21:04 -070033#include "update_engine/fake_hardware.h"
Sen Jiang2d528b42015-09-25 11:18:12 -070034#include "update_engine/fake_prefs.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070035#include "update_engine/fake_system_state.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070036#include "update_engine/payload_constants.h"
Alex Deymo0bc26112015-10-19 20:54:57 -070037#include "update_engine/payload_generator/bzip.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,
Sen Jiang3e728fe2015-11-05 11:37:23 -080094 uint64_t major_version,
Sen Jiang2d528b42015-09-25 11:18:12 -070095 bool full_payload,
96 ErrorCode expected) {
Allie Woodfdf00512015-03-02 13:34:55 -080097 // The install plan is for Full or Delta.
Sen Jiang2d528b42015-09-25 11:18:12 -070098 install_plan_.is_full_update = full_payload;
Allie Woodfdf00512015-03-02 13:34:55 -080099
100 // The Manifest we are validating.
Sen Jiang2d528b42015-09-25 11:18:12 -0700101 performer_.manifest_.CopyFrom(manifest);
Sen Jiang3e728fe2015-11-05 11:37:23 -0800102 performer_.major_payload_version_ = major_version;
Allie Woodfdf00512015-03-02 13:34:55 -0800103
Sen Jiang2d528b42015-09-25 11:18:12 -0700104 EXPECT_EQ(expected, performer_.ValidateManifest());
Allie Woodfdf00512015-03-02 13:34:55 -0800105 }
106
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700107 brillo::Blob GeneratePayload(const brillo::Blob& blob_data,
108 const vector<AnnotatedOperation>& aops,
109 bool sign_payload,
Sen Jiang76bfa742015-10-12 17:13:26 -0700110 uint64_t major_version,
111 uint32_t minor_version) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700112 string blob_path;
113 EXPECT_TRUE(utils::MakeTempFile("Blob-XXXXXX", &blob_path, nullptr));
114 ScopedPathUnlinker blob_unlinker(blob_path);
115 EXPECT_TRUE(utils::WriteFile(blob_path.c_str(),
116 blob_data.data(),
117 blob_data.size()));
118
119 PayloadGenerationConfig config;
Sen Jiang76bfa742015-10-12 17:13:26 -0700120 config.major_version = major_version;
Sen Jiangd78b3892015-09-25 15:19:31 -0700121 config.minor_version = minor_version;
Sen Jiangd78b3892015-09-25 15:19:31 -0700122
123 PayloadFile payload;
124 EXPECT_TRUE(payload.Init(config));
125
Sen Jiang981eb112015-08-25 17:03:18 -0700126 PartitionConfig old_part(kLegacyPartitionNameRoot);
127 PartitionConfig new_part(kLegacyPartitionNameRoot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700128 new_part.path = "/dev/zero";
129 new_part.size = 1234;
Sen Jiang981eb112015-08-25 17:03:18 -0700130
131 payload.AddPartition(old_part, new_part, aops);
Sen Jiangd78b3892015-09-25 15:19:31 -0700132
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700133 // We include a kernel partition without operations.
134 old_part.name = kLegacyPartitionNameKernel;
135 new_part.name = kLegacyPartitionNameKernel;
136 new_part.size = 0;
137 payload.AddPartition(old_part, new_part, {});
138
Sen Jiangd78b3892015-09-25 15:19:31 -0700139 string payload_path;
140 EXPECT_TRUE(utils::MakeTempFile("Payload-XXXXXX", &payload_path, nullptr));
141 ScopedPathUnlinker payload_unlinker(payload_path);
142 EXPECT_TRUE(payload.WritePayload(payload_path, blob_path,
143 sign_payload ? kUnittestPrivateKeyPath : "",
Sen Jiang2d528b42015-09-25 11:18:12 -0700144 &install_plan_.metadata_size));
Sen Jiangd78b3892015-09-25 15:19:31 -0700145
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700146 brillo::Blob payload_data;
Sen Jiangd78b3892015-09-25 15:19:31 -0700147 EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data));
148 return payload_data;
149 }
Allie Woodfdf00512015-03-02 13:34:55 -0800150
Sen Jiang2d528b42015-09-25 11:18:12 -0700151 // Apply |payload_data| on partition specified in |source_path|.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700152 brillo::Blob ApplyPayload(const brillo::Blob& payload_data,
153 const string& source_path) {
154 return ApplyPayloadToData(payload_data, source_path, brillo::Blob());
Alex Deymo79715ad2015-10-02 14:27:53 -0700155 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800156
Alex Deymo79715ad2015-10-02 14:27:53 -0700157 // Apply the payload provided in |payload_data| reading from the |source_path|
158 // file and writing the contents to a new partition. The existing data in the
159 // new target file are set to |target_data| before applying the payload.
160 // Returns the result of the payload application.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700161 brillo::Blob ApplyPayloadToData(const brillo::Blob& payload_data,
162 const string& source_path,
163 const brillo::Blob& target_data) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700164 string new_part;
165 EXPECT_TRUE(utils::MakeTempFile("Partition-XXXXXX", &new_part, nullptr));
166 ScopedPathUnlinker partition_unlinker(new_part);
Alex Deymo79715ad2015-10-02 14:27:53 -0700167 EXPECT_TRUE(utils::WriteFile(new_part.c_str(), target_data.data(),
168 target_data.size()));
169
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700170 // We installed the operations only in the rootfs partition, but the
171 // delta performer needs to access all the partitions.
172 fake_system_state_.fake_boot_control()->SetPartitionDevice(
173 kLegacyPartitionNameRoot, install_plan_.target_slot, new_part);
174 fake_system_state_.fake_boot_control()->SetPartitionDevice(
175 kLegacyPartitionNameRoot, install_plan_.source_slot, source_path);
176 fake_system_state_.fake_boot_control()->SetPartitionDevice(
177 kLegacyPartitionNameKernel, install_plan_.target_slot, "/dev/null");
178 fake_system_state_.fake_boot_control()->SetPartitionDevice(
179 kLegacyPartitionNameKernel, install_plan_.source_slot, "/dev/null");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800180
Sen Jiang2d528b42015-09-25 11:18:12 -0700181 EXPECT_TRUE(performer_.Write(payload_data.data(), payload_data.size()));
182 EXPECT_EQ(0, performer_.Close());
183
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700184 brillo::Blob partition_data;
Sen Jiang2d528b42015-09-25 11:18:12 -0700185 EXPECT_TRUE(utils::ReadFile(new_part, &partition_data));
186 return partition_data;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800187 }
188
Sen Jiang2d528b42015-09-25 11:18:12 -0700189 // Calls delta performer's Write method by pretending to pass in bytes from a
190 // delta file whose metadata size is actual_metadata_size and tests if all
191 // checks are correctly performed if the install plan contains
192 // expected_metadata_size and that the result of the parsing are as per
193 // hash_checks_mandatory flag.
194 void DoMetadataSizeTest(uint64_t expected_metadata_size,
195 uint64_t actual_metadata_size,
196 bool hash_checks_mandatory) {
197 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800198
Sen Jiang2d528b42015-09-25 11:18:12 -0700199 // Set a valid magic string and version number 1.
200 EXPECT_TRUE(performer_.Write("CrAU", 4));
201 uint64_t version = htobe64(kChromeOSMajorPayloadVersion);
202 EXPECT_TRUE(performer_.Write(&version, 8));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800203
Sen Jiang2d528b42015-09-25 11:18:12 -0700204 install_plan_.metadata_size = expected_metadata_size;
205 ErrorCode error_code;
206 // When filling in size in manifest, exclude the size of the 20-byte header.
207 uint64_t size_in_manifest = htobe64(actual_metadata_size - 20);
208 bool result = performer_.Write(&size_in_manifest, 8, &error_code);
209 if (expected_metadata_size == actual_metadata_size ||
210 !hash_checks_mandatory) {
211 EXPECT_TRUE(result);
212 } else {
213 EXPECT_FALSE(result);
214 EXPECT_EQ(ErrorCode::kDownloadInvalidMetadataSize, error_code);
215 }
Sen Jiangd78b3892015-09-25 15:19:31 -0700216
Sen Jiang2d528b42015-09-25 11:18:12 -0700217 EXPECT_LT(performer_.Close(), 0);
218 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800219
Sen Jiang2d528b42015-09-25 11:18:12 -0700220 // Generates a valid delta file but tests the delta performer by suppling
221 // different metadata signatures as per metadata_signature_test flag and
222 // sees if the result of the parsing are as per hash_checks_mandatory flag.
223 void DoMetadataSignatureTest(MetadataSignatureTest metadata_signature_test,
224 bool sign_payload,
225 bool hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800226
Sen Jiang2d528b42015-09-25 11:18:12 -0700227 // Loads the payload and parses the manifest.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700228 brillo::Blob payload = GeneratePayload(brillo::Blob(),
Sen Jiang2d528b42015-09-25 11:18:12 -0700229 vector<AnnotatedOperation>(), sign_payload,
Sen Jiang76bfa742015-10-12 17:13:26 -0700230 kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800231
Sen Jiang2d528b42015-09-25 11:18:12 -0700232 LOG(INFO) << "Payload size: " << payload.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800233
Sen Jiang2d528b42015-09-25 11:18:12 -0700234 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800235
Sen Jiang2d528b42015-09-25 11:18:12 -0700236 DeltaPerformer::MetadataParseResult expected_result, actual_result;
237 ErrorCode expected_error, actual_error;
238
239 // Fill up the metadata signature in install plan according to the test.
240 switch (metadata_signature_test) {
241 case kEmptyMetadataSignature:
242 install_plan_.metadata_signature.clear();
243 expected_result = DeltaPerformer::kMetadataParseError;
244 expected_error = ErrorCode::kDownloadMetadataSignatureMissingError;
245 break;
246
247 case kInvalidMetadataSignature:
248 install_plan_.metadata_signature = kBogusMetadataSignature1;
249 expected_result = DeltaPerformer::kMetadataParseError;
250 expected_error = ErrorCode::kDownloadMetadataSignatureMismatch;
251 break;
252
253 case kValidMetadataSignature:
254 default:
255 // Set the install plan's metadata size to be the same as the one
256 // in the manifest so that we pass the metadata size checks. Only
257 // then we can get to manifest signature checks.
258 ASSERT_TRUE(PayloadSigner::GetMetadataSignature(
259 payload.data(),
260 install_plan_.metadata_size,
261 kUnittestPrivateKeyPath,
262 &install_plan_.metadata_signature));
263 EXPECT_FALSE(install_plan_.metadata_signature.empty());
264 expected_result = DeltaPerformer::kMetadataParseSuccess;
265 expected_error = ErrorCode::kSuccess;
266 break;
267 }
268
269 // Ignore the expected result/error if hash checks are not mandatory.
270 if (!hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800271 expected_result = DeltaPerformer::kMetadataParseSuccess;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700272 expected_error = ErrorCode::kSuccess;
Sen Jiang2d528b42015-09-25 11:18:12 -0700273 }
274
275 // Use the public key corresponding to the private key used above to
276 // sign the metadata.
277 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
278 performer_.set_public_key_path(kUnittestPublicKeyPath);
279
280 // Init actual_error with an invalid value so that we make sure
281 // ParsePayloadMetadata properly populates it in all cases.
282 actual_error = ErrorCode::kUmaReportedMax;
283 actual_result = performer_.ParsePayloadMetadata(payload, &actual_error);
284
285 EXPECT_EQ(expected_result, actual_result);
286 EXPECT_EQ(expected_error, actual_error);
287
288 // Check that the parsed metadata size is what's expected. This test
289 // implicitly confirms that the metadata signature is valid, if required.
290 EXPECT_EQ(install_plan_.metadata_size, performer_.GetMetadataSize());
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800291 }
292
Sen Jiangb8060e42015-09-24 17:30:50 -0700293 void SetSupportedMajorVersion(uint64_t major_version) {
294 performer_.supported_major_version_ = major_version;
295 }
Sen Jiang2d528b42015-09-25 11:18:12 -0700296 FakePrefs prefs_;
297 InstallPlan install_plan_;
298 FakeSystemState fake_system_state_;
299 DeltaPerformer performer_{&prefs_, &fake_system_state_, &install_plan_};
300};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800301
Sen Jiang2d528b42015-09-25 11:18:12 -0700302TEST_F(DeltaPerformerTest, FullPayloadWriteTest) {
303 install_plan_.is_full_update = true;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700304 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
305 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700306 expected_data.resize(4096); // block size
307 vector<AnnotatedOperation> aops;
308 AnnotatedOperation aop;
309 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
310 aop.op.set_data_offset(0);
311 aop.op.set_data_length(expected_data.size());
312 aop.op.set_type(InstallOperation::REPLACE);
313 aops.push_back(aop);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800314
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700315 brillo::Blob payload_data = GeneratePayload(expected_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700316 kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800317
Alex Deymo79715ad2015-10-02 14:27:53 -0700318 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800319}
320
Sen Jiang2d528b42015-09-25 11:18:12 -0700321TEST_F(DeltaPerformerTest, ReplaceOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700322 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
323 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700324 expected_data.resize(4096); // block size
325 vector<AnnotatedOperation> aops;
326 AnnotatedOperation aop;
327 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
328 aop.op.set_data_offset(0);
329 aop.op.set_data_length(expected_data.size());
330 aop.op.set_type(InstallOperation::REPLACE);
331 aops.push_back(aop);
332
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700333 brillo::Blob payload_data = GeneratePayload(expected_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700334 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700335 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700336
337 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
338}
339
340TEST_F(DeltaPerformerTest, ReplaceBzOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700341 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
342 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700343 expected_data.resize(4096); // block size
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700344 brillo::Blob bz_data;
Sen Jiang2d528b42015-09-25 11:18:12 -0700345 EXPECT_TRUE(BzipCompress(expected_data, &bz_data));
346
347 vector<AnnotatedOperation> aops;
348 AnnotatedOperation aop;
349 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
350 aop.op.set_data_offset(0);
351 aop.op.set_data_length(bz_data.size());
352 aop.op.set_type(InstallOperation::REPLACE_BZ);
353 aops.push_back(aop);
354
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700355 brillo::Blob payload_data = GeneratePayload(bz_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700356 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700357 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700358
359 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
360}
361
Alex Deymo2d621a32015-10-01 11:09:01 -0700362TEST_F(DeltaPerformerTest, ReplaceXzOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700363 brillo::Blob xz_data(std::begin(kXzCompressedData),
Alex Deymo2d621a32015-10-01 11:09:01 -0700364 std::end(kXzCompressedData));
365 // The compressed xz data contains only a single "a", but the operation should
366 // pad the rest of the two blocks with zeros.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700367 brillo::Blob expected_data = brillo::Blob(4096, 0);
Alex Deymo2d621a32015-10-01 11:09:01 -0700368 expected_data[0] = 'a';
369
370 AnnotatedOperation aop;
371 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
372 aop.op.set_data_offset(0);
373 aop.op.set_data_length(xz_data.size());
374 aop.op.set_type(InstallOperation::REPLACE_XZ);
375 vector<AnnotatedOperation> aops = {aop};
376
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700377 brillo::Blob payload_data = GeneratePayload(xz_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700378 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700379 kSourceMinorPayloadVersion);
Alex Deymo2d621a32015-10-01 11:09:01 -0700380
381 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
382}
383
Alex Deymo79715ad2015-10-02 14:27:53 -0700384TEST_F(DeltaPerformerTest, ZeroOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700385 brillo::Blob existing_data = brillo::Blob(4096 * 10, 'a');
386 brillo::Blob expected_data = existing_data;
Alex Deymo79715ad2015-10-02 14:27:53 -0700387 // Blocks 4, 5 and 7 should have zeros instead of 'a' after the operation is
388 // applied.
389 std::fill(expected_data.data() + 4096 * 4, expected_data.data() + 4096 * 6,
390 0);
391 std::fill(expected_data.data() + 4096 * 7, expected_data.data() + 4096 * 8,
392 0);
393
394 AnnotatedOperation aop;
395 *(aop.op.add_dst_extents()) = ExtentForRange(4, 2);
396 *(aop.op.add_dst_extents()) = ExtentForRange(7, 1);
397 aop.op.set_type(InstallOperation::ZERO);
398 vector<AnnotatedOperation> aops = {aop};
399
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700400 brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700401 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700402 kSourceMinorPayloadVersion);
Alex Deymo79715ad2015-10-02 14:27:53 -0700403
404 EXPECT_EQ(expected_data,
405 ApplyPayloadToData(payload_data, "/dev/null", existing_data));
406}
407
Sen Jiang2d528b42015-09-25 11:18:12 -0700408TEST_F(DeltaPerformerTest, SourceCopyOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700409 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
410 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700411 expected_data.resize(4096); // block size
412 vector<AnnotatedOperation> aops;
413 AnnotatedOperation aop;
414 *(aop.op.add_src_extents()) = ExtentForRange(0, 1);
415 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
416 aop.op.set_type(InstallOperation::SOURCE_COPY);
417 aops.push_back(aop);
418
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700419 brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700420 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700421 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700422 string source_path;
423 EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX",
424 &source_path, nullptr));
425 ScopedPathUnlinker path_unlinker(source_path);
426 EXPECT_TRUE(utils::WriteFile(source_path.c_str(),
427 expected_data.data(),
428 expected_data.size()));
429
430 EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path));
431}
432
433TEST_F(DeltaPerformerTest, ExtentsToByteStringTest) {
Allie Wood56873452015-03-27 17:48:40 -0700434 uint64_t test[] = {1, 1, 4, 2, 0, 1};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800435 COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven);
436 const uint64_t block_size = 4096;
Allie Wood56873452015-03-27 17:48:40 -0700437 const uint64_t file_length = 4 * block_size - 13;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800438
439 google::protobuf::RepeatedPtrField<Extent> extents;
440 for (size_t i = 0; i < arraysize(test); i += 2) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700441 *(extents.Add()) = ExtentForRange(test[i], test[i + 1]);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800442 }
443
Allie Wood56873452015-03-27 17:48:40 -0700444 string expected_output = "4096:4096,16384:8192,0:4083";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800445 string actual_output;
446 EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents,
447 block_size,
448 file_length,
449 &actual_output));
450 EXPECT_EQ(expected_output, actual_output);
451}
Darin Petkov68c10d12010-10-14 09:24:37 -0700452
Sen Jiang2d528b42015-09-25 11:18:12 -0700453TEST_F(DeltaPerformerTest, ValidateManifestFullGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800454 // The Manifest we are validating.
455 DeltaArchiveManifest manifest;
456 manifest.mutable_new_kernel_info();
457 manifest.mutable_new_rootfs_info();
Alex Deymocbf09892015-09-11 16:13:16 -0700458 manifest.set_minor_version(kFullPayloadMinorVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800459
Sen Jiang3e728fe2015-11-05 11:37:23 -0800460 RunManifestValidation(manifest, kChromeOSMajorPayloadVersion, true,
461 ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800462}
463
Sen Jiang2d528b42015-09-25 11:18:12 -0700464TEST_F(DeltaPerformerTest, ValidateManifestDeltaGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800465 // The Manifest we are validating.
466 DeltaArchiveManifest manifest;
467 manifest.mutable_old_kernel_info();
468 manifest.mutable_old_rootfs_info();
469 manifest.mutable_new_kernel_info();
470 manifest.mutable_new_rootfs_info();
471 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
472
Sen Jiang3e728fe2015-11-05 11:37:23 -0800473 RunManifestValidation(manifest, kChromeOSMajorPayloadVersion, false,
474 ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800475}
476
Sen Jiang2d528b42015-09-25 11:18:12 -0700477TEST_F(DeltaPerformerTest, ValidateManifestFullUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800478 // The Manifest we are validating.
479 DeltaArchiveManifest manifest;
480
Sen Jiang3e728fe2015-11-05 11:37:23 -0800481 RunManifestValidation(manifest, DeltaPerformer::kSupportedMajorPayloadVersion,
482 true, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800483}
484
Sen Jiang2d528b42015-09-25 11:18:12 -0700485TEST_F(DeltaPerformerTest, ValidateManifestDeltaUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800486 // The Manifest we are validating.
487 DeltaArchiveManifest manifest;
488
Sen Jiang3e728fe2015-11-05 11:37:23 -0800489 RunManifestValidation(manifest, DeltaPerformer::kSupportedMajorPayloadVersion,
490 false, ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800491}
492
Sen Jiang2d528b42015-09-25 11:18:12 -0700493TEST_F(DeltaPerformerTest, ValidateManifestFullOldKernelTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800494 // The Manifest we are validating.
495 DeltaArchiveManifest manifest;
496 manifest.mutable_old_kernel_info();
497 manifest.mutable_new_kernel_info();
498 manifest.mutable_new_rootfs_info();
499 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
500
Sen Jiang3e728fe2015-11-05 11:37:23 -0800501 RunManifestValidation(manifest, kChromeOSMajorPayloadVersion, true,
502 ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800503}
504
Sen Jiang2d528b42015-09-25 11:18:12 -0700505TEST_F(DeltaPerformerTest, ValidateManifestFullOldRootfsTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800506 // The Manifest we are validating.
507 DeltaArchiveManifest manifest;
508 manifest.mutable_old_rootfs_info();
509 manifest.mutable_new_kernel_info();
510 manifest.mutable_new_rootfs_info();
511 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
512
Sen Jiang3e728fe2015-11-05 11:37:23 -0800513 RunManifestValidation(manifest, kChromeOSMajorPayloadVersion, true,
514 ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800515}
516
Sen Jiangc8f6b7a2015-10-21 11:09:59 -0700517TEST_F(DeltaPerformerTest, ValidateManifestFullPartitionUpdateTest) {
518 // The Manifest we are validating.
519 DeltaArchiveManifest manifest;
520 PartitionUpdate* partition = manifest.add_partitions();
521 partition->mutable_old_partition_info();
522 partition->mutable_new_partition_info();
523 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
524
Sen Jiang3e728fe2015-11-05 11:37:23 -0800525 RunManifestValidation(manifest, kBrilloMajorPayloadVersion, true,
526 ErrorCode::kPayloadMismatchedType);
Sen Jiangc8f6b7a2015-10-21 11:09:59 -0700527}
528
Sen Jiang2d528b42015-09-25 11:18:12 -0700529TEST_F(DeltaPerformerTest, ValidateManifestBadMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800530 // The Manifest we are validating.
531 DeltaArchiveManifest manifest;
532
533 // Generate a bad version number.
534 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion +
535 10000);
536
Sen Jiang3e728fe2015-11-05 11:37:23 -0800537 RunManifestValidation(manifest, DeltaPerformer::kSupportedMajorPayloadVersion,
538 false, ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800539}
540
Sen Jiangb8060e42015-09-24 17:30:50 -0700541TEST_F(DeltaPerformerTest, BrilloMetadataSignatureSizeTest) {
Sen Jiangb8060e42015-09-24 17:30:50 -0700542 EXPECT_TRUE(performer_.Write(kDeltaMagic, sizeof(kDeltaMagic)));
543
544 uint64_t major_version = htobe64(kBrilloMajorPayloadVersion);
545 EXPECT_TRUE(performer_.Write(&major_version, 8));
546
547 uint64_t manifest_size = rand() % 256;
548 uint64_t manifest_size_be = htobe64(manifest_size);
549 EXPECT_TRUE(performer_.Write(&manifest_size_be, 8));
550
551 uint32_t metadata_signature_size = rand() % 256;
552 uint32_t metadata_signature_size_be = htobe32(metadata_signature_size);
553 EXPECT_TRUE(performer_.Write(&metadata_signature_size_be, 4));
554
555 EXPECT_LT(performer_.Close(), 0);
556
557 EXPECT_TRUE(performer_.IsHeaderParsed());
558 EXPECT_EQ(kBrilloMajorPayloadVersion, performer_.GetMajorVersion());
559 uint64_t manifest_offset;
560 EXPECT_TRUE(performer_.GetManifestOffset(&manifest_offset));
561 EXPECT_EQ(24, manifest_offset); // 4 + 8 + 8 + 4
Sen Jiang76bfa742015-10-12 17:13:26 -0700562 EXPECT_EQ(manifest_offset + manifest_size, performer_.GetMetadataSize());
563 EXPECT_EQ(metadata_signature_size, performer_.metadata_signature_size_);
564}
565
566TEST_F(DeltaPerformerTest, BrilloVerifyMetadataSignatureTest) {
Sen Jiang76bfa742015-10-12 17:13:26 -0700567 brillo::Blob payload_data = GeneratePayload({}, {}, true,
568 kBrilloMajorPayloadVersion,
569 kSourceMinorPayloadVersion);
570 install_plan_.hash_checks_mandatory = true;
571 // Just set these value so that we can use ValidateMetadataSignature directly.
572 performer_.major_payload_version_ = kBrilloMajorPayloadVersion;
573 performer_.metadata_size_ = install_plan_.metadata_size;
574 uint64_t signature_length;
575 EXPECT_TRUE(PayloadSigner::SignatureBlobLength({kUnittestPrivateKeyPath},
576 &signature_length));
577 performer_.metadata_signature_size_ = signature_length;
578 performer_.set_public_key_path(kUnittestPublicKeyPath);
579 EXPECT_EQ(ErrorCode::kSuccess,
580 performer_.ValidateMetadataSignature(payload_data));
Sen Jiangb8060e42015-09-24 17:30:50 -0700581}
582
Sen Jiang2d528b42015-09-25 11:18:12 -0700583TEST_F(DeltaPerformerTest, BadDeltaMagicTest) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700584 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700585 EXPECT_FALSE(performer_.Write("morejunk", 8));
586 EXPECT_LT(performer_.Close(), 0);
Darin Petkov934bb412010-11-18 11:21:35 -0800587}
588
Sen Jiang2d528b42015-09-25 11:18:12 -0700589TEST_F(DeltaPerformerTest, WriteUpdatesPayloadState) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700590 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800591 DownloadProgress(4)).Times(1);
Sen Jiang2d528b42015-09-25 11:18:12 -0700592 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Sen Jiangb8060e42015-09-24 17:30:50 -0700593 DownloadProgress(8)).Times(1);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800594
Sen Jiang2d528b42015-09-25 11:18:12 -0700595 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700596 EXPECT_FALSE(performer_.Write("morejunk", 8));
597 EXPECT_LT(performer_.Close(), 0);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800598}
599
Sen Jiang2d528b42015-09-25 11:18:12 -0700600TEST_F(DeltaPerformerTest, MissingMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800601 DoMetadataSizeTest(0, 75456, true);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700602}
603
Sen Jiang2d528b42015-09-25 11:18:12 -0700604TEST_F(DeltaPerformerTest, MissingNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800605 DoMetadataSizeTest(0, 123456, false);
606}
607
Sen Jiang2d528b42015-09-25 11:18:12 -0700608TEST_F(DeltaPerformerTest, InvalidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800609 DoMetadataSizeTest(13000, 140000, true);
610}
611
Sen Jiang2d528b42015-09-25 11:18:12 -0700612TEST_F(DeltaPerformerTest, InvalidNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800613 DoMetadataSizeTest(40000, 50000, false);
614}
615
Sen Jiang2d528b42015-09-25 11:18:12 -0700616TEST_F(DeltaPerformerTest, ValidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800617 DoMetadataSizeTest(85376, 85376, true);
618}
619
Sen Jiang2d528b42015-09-25 11:18:12 -0700620TEST_F(DeltaPerformerTest, MandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700621 DoMetadataSignatureTest(kEmptyMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800622}
623
Sen Jiang2d528b42015-09-25 11:18:12 -0700624TEST_F(DeltaPerformerTest, NonMandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700625 DoMetadataSignatureTest(kEmptyMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800626}
627
Sen Jiang2d528b42015-09-25 11:18:12 -0700628TEST_F(DeltaPerformerTest, MandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700629 DoMetadataSignatureTest(kInvalidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800630}
631
Sen Jiang2d528b42015-09-25 11:18:12 -0700632TEST_F(DeltaPerformerTest, NonMandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700633 DoMetadataSignatureTest(kInvalidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800634}
635
Sen Jiang2d528b42015-09-25 11:18:12 -0700636TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature1Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700637 DoMetadataSignatureTest(kValidMetadataSignature, false, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800638}
639
Sen Jiang2d528b42015-09-25 11:18:12 -0700640TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature2Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700641 DoMetadataSignatureTest(kValidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800642}
643
Sen Jiang2d528b42015-09-25 11:18:12 -0700644TEST_F(DeltaPerformerTest, NonMandatoryValidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700645 DoMetadataSignatureTest(kValidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800646}
647
Sen Jiang2d528b42015-09-25 11:18:12 -0700648TEST_F(DeltaPerformerTest, UsePublicKeyFromResponse) {
David Zeuthene7f89172013-10-31 10:21:04 -0700649 base::FilePath key_path;
650
651 // The result of the GetPublicKeyResponse() method is based on three things
652 //
653 // 1. Whether it's an official build; and
654 // 2. Whether the Public RSA key to be used is in the root filesystem; and
Alex Vakulenko072359c2014-07-18 11:41:07 -0700655 // 3. Whether the response has a public key
David Zeuthene7f89172013-10-31 10:21:04 -0700656 //
657 // We test all eight combinations to ensure that we only use the
658 // public key in the response if
659 //
660 // a. it's not an official build; and
661 // b. there is no key in the root filesystem.
662
Sen Jiang2d528b42015-09-25 11:18:12 -0700663 FakeHardware* fake_hardware = fake_system_state_.fake_hardware();
David Zeuthene7f89172013-10-31 10:21:04 -0700664
665 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800666 EXPECT_TRUE(utils::MakeTempDirectory("PublicKeyFromResponseTests.XXXXXX",
David Zeuthene7f89172013-10-31 10:21:04 -0700667 &temp_dir));
668 string non_existing_file = temp_dir + "/non-existing";
669 string existing_file = temp_dir + "/existing";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700670 EXPECT_EQ(0, System(base::StringPrintf("touch %s", existing_file.c_str())));
David Zeuthene7f89172013-10-31 10:21:04 -0700671
672 // Non-official build, non-existing public-key, key in response -> true
Don Garrett6646b442013-11-13 15:29:11 -0800673 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700674 performer_.public_key_path_ = non_existing_file;
675 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
676 EXPECT_TRUE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700677 EXPECT_FALSE(key_path.empty());
678 EXPECT_EQ(unlink(key_path.value().c_str()), 0);
679 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800680 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700681 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700682
683 // Non-official build, existing public-key, key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800684 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700685 performer_.public_key_path_ = existing_file;
686 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
687 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700688 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800689 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700690 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700691
692 // Non-official build, non-existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800693 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700694 performer_.public_key_path_ = non_existing_file;
695 install_plan_.public_key_rsa = "";
696 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700697 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800698 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700699 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700700
701 // Non-official build, existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800702 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700703 performer_.public_key_path_ = existing_file;
704 install_plan_.public_key_rsa = "";
705 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700706 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800707 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700708 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700709
710 // Non-official build, non-existing public-key, key in response
711 // but invalid base64 -> false
Don Garrett6646b442013-11-13 15:29:11 -0800712 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700713 performer_.public_key_path_ = non_existing_file;
714 install_plan_.public_key_rsa = "not-valid-base64";
715 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700716
Alex Deymo110e0302015-10-19 20:35:21 -0700717 EXPECT_TRUE(base::DeleteFile(base::FilePath(temp_dir), true));
David Zeuthene7f89172013-10-31 10:21:04 -0700718}
719
Alex Deymocbf09892015-09-11 16:13:16 -0700720TEST_F(DeltaPerformerTest, ConfVersionsMatch) {
721 // Test that the versions in update_engine.conf that is installed to the
722 // image match the supported delta versions in the update engine.
Allie Wood78750a42015-02-11 15:42:11 -0800723 uint32_t minor_version;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700724 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700725 EXPECT_TRUE(store.Load(base::FilePath("update_engine.conf")));
726 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
727 EXPECT_EQ(DeltaPerformer::kSupportedMinorPayloadVersion, minor_version);
Alex Deymocbf09892015-09-11 16:13:16 -0700728
729 string major_version_str;
730 uint64_t major_version;
731 EXPECT_TRUE(store.GetString("PAYLOAD_MAJOR_VERSION", &major_version_str));
732 EXPECT_TRUE(base::StringToUint64(major_version_str, &major_version));
733 EXPECT_EQ(DeltaPerformer::kSupportedMajorPayloadVersion, major_version);
Allie Wood78750a42015-02-11 15:42:11 -0800734}
735
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700736} // namespace chromeos_update_engine