blob: 8c857680400ac3814151053b20a9f6e05114cf14 [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,
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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700105 brillo::Blob GeneratePayload(const brillo::Blob& blob_data,
106 const vector<AnnotatedOperation>& aops,
107 bool sign_payload,
Sen Jiang76bfa742015-10-12 17:13:26 -0700108 uint64_t major_version,
109 uint32_t minor_version) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700110 string blob_path;
111 EXPECT_TRUE(utils::MakeTempFile("Blob-XXXXXX", &blob_path, nullptr));
112 ScopedPathUnlinker blob_unlinker(blob_path);
113 EXPECT_TRUE(utils::WriteFile(blob_path.c_str(),
114 blob_data.data(),
115 blob_data.size()));
116
117 PayloadGenerationConfig config;
Sen Jiang76bfa742015-10-12 17:13:26 -0700118 config.major_version = major_version;
Sen Jiangd78b3892015-09-25 15:19:31 -0700119 config.minor_version = minor_version;
Sen Jiangd78b3892015-09-25 15:19:31 -0700120
121 PayloadFile payload;
122 EXPECT_TRUE(payload.Init(config));
123
Sen Jiang981eb112015-08-25 17:03:18 -0700124 PartitionConfig old_part(kLegacyPartitionNameRoot);
125 PartitionConfig new_part(kLegacyPartitionNameRoot);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700126 new_part.path = "/dev/zero";
127 new_part.size = 1234;
Sen Jiang981eb112015-08-25 17:03:18 -0700128
129 payload.AddPartition(old_part, new_part, aops);
Sen Jiangd78b3892015-09-25 15:19:31 -0700130
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131 // We include a kernel partition without operations.
132 old_part.name = kLegacyPartitionNameKernel;
133 new_part.name = kLegacyPartitionNameKernel;
134 new_part.size = 0;
135 payload.AddPartition(old_part, new_part, {});
136
Sen Jiangd78b3892015-09-25 15:19:31 -0700137 string payload_path;
138 EXPECT_TRUE(utils::MakeTempFile("Payload-XXXXXX", &payload_path, nullptr));
139 ScopedPathUnlinker payload_unlinker(payload_path);
140 EXPECT_TRUE(payload.WritePayload(payload_path, blob_path,
141 sign_payload ? kUnittestPrivateKeyPath : "",
Sen Jiang2d528b42015-09-25 11:18:12 -0700142 &install_plan_.metadata_size));
Sen Jiangd78b3892015-09-25 15:19:31 -0700143
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700144 brillo::Blob payload_data;
Sen Jiangd78b3892015-09-25 15:19:31 -0700145 EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data));
146 return payload_data;
147 }
Allie Woodfdf00512015-03-02 13:34:55 -0800148
Sen Jiang2d528b42015-09-25 11:18:12 -0700149 // Apply |payload_data| on partition specified in |source_path|.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700150 brillo::Blob ApplyPayload(const brillo::Blob& payload_data,
151 const string& source_path) {
152 return ApplyPayloadToData(payload_data, source_path, brillo::Blob());
Alex Deymo79715ad2015-10-02 14:27:53 -0700153 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800154
Alex Deymo79715ad2015-10-02 14:27:53 -0700155 // Apply the payload provided in |payload_data| reading from the |source_path|
156 // file and writing the contents to a new partition. The existing data in the
157 // new target file are set to |target_data| before applying the payload.
158 // Returns the result of the payload application.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700159 brillo::Blob ApplyPayloadToData(const brillo::Blob& payload_data,
160 const string& source_path,
161 const brillo::Blob& target_data) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700162 string new_part;
163 EXPECT_TRUE(utils::MakeTempFile("Partition-XXXXXX", &new_part, nullptr));
164 ScopedPathUnlinker partition_unlinker(new_part);
Alex Deymo79715ad2015-10-02 14:27:53 -0700165 EXPECT_TRUE(utils::WriteFile(new_part.c_str(), target_data.data(),
166 target_data.size()));
167
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700168 // We installed the operations only in the rootfs partition, but the
169 // delta performer needs to access all the partitions.
170 fake_system_state_.fake_boot_control()->SetPartitionDevice(
171 kLegacyPartitionNameRoot, install_plan_.target_slot, new_part);
172 fake_system_state_.fake_boot_control()->SetPartitionDevice(
173 kLegacyPartitionNameRoot, install_plan_.source_slot, source_path);
174 fake_system_state_.fake_boot_control()->SetPartitionDevice(
175 kLegacyPartitionNameKernel, install_plan_.target_slot, "/dev/null");
176 fake_system_state_.fake_boot_control()->SetPartitionDevice(
177 kLegacyPartitionNameKernel, install_plan_.source_slot, "/dev/null");
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800178
Sen Jiang2d528b42015-09-25 11:18:12 -0700179 EXPECT_TRUE(performer_.Write(payload_data.data(), payload_data.size()));
180 EXPECT_EQ(0, performer_.Close());
181
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700182 brillo::Blob partition_data;
Sen Jiang2d528b42015-09-25 11:18:12 -0700183 EXPECT_TRUE(utils::ReadFile(new_part, &partition_data));
184 return partition_data;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800185 }
186
Sen Jiang2d528b42015-09-25 11:18:12 -0700187 // Calls delta performer's Write method by pretending to pass in bytes from a
188 // delta file whose metadata size is actual_metadata_size and tests if all
189 // checks are correctly performed if the install plan contains
190 // expected_metadata_size and that the result of the parsing are as per
191 // hash_checks_mandatory flag.
192 void DoMetadataSizeTest(uint64_t expected_metadata_size,
193 uint64_t actual_metadata_size,
194 bool hash_checks_mandatory) {
195 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800196
Sen Jiang2d528b42015-09-25 11:18:12 -0700197 // Set a valid magic string and version number 1.
198 EXPECT_TRUE(performer_.Write("CrAU", 4));
199 uint64_t version = htobe64(kChromeOSMajorPayloadVersion);
200 EXPECT_TRUE(performer_.Write(&version, 8));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800201
Sen Jiang2d528b42015-09-25 11:18:12 -0700202 install_plan_.metadata_size = expected_metadata_size;
203 ErrorCode error_code;
204 // When filling in size in manifest, exclude the size of the 20-byte header.
205 uint64_t size_in_manifest = htobe64(actual_metadata_size - 20);
206 bool result = performer_.Write(&size_in_manifest, 8, &error_code);
207 if (expected_metadata_size == actual_metadata_size ||
208 !hash_checks_mandatory) {
209 EXPECT_TRUE(result);
210 } else {
211 EXPECT_FALSE(result);
212 EXPECT_EQ(ErrorCode::kDownloadInvalidMetadataSize, error_code);
213 }
Sen Jiangd78b3892015-09-25 15:19:31 -0700214
Sen Jiang2d528b42015-09-25 11:18:12 -0700215 EXPECT_LT(performer_.Close(), 0);
216 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800217
Sen Jiang2d528b42015-09-25 11:18:12 -0700218 // Generates a valid delta file but tests the delta performer by suppling
219 // different metadata signatures as per metadata_signature_test flag and
220 // sees if the result of the parsing are as per hash_checks_mandatory flag.
221 void DoMetadataSignatureTest(MetadataSignatureTest metadata_signature_test,
222 bool sign_payload,
223 bool hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800224
Sen Jiang2d528b42015-09-25 11:18:12 -0700225 // Loads the payload and parses the manifest.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700226 brillo::Blob payload = GeneratePayload(brillo::Blob(),
Sen Jiang2d528b42015-09-25 11:18:12 -0700227 vector<AnnotatedOperation>(), sign_payload,
Sen Jiang76bfa742015-10-12 17:13:26 -0700228 kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800229
Sen Jiang2d528b42015-09-25 11:18:12 -0700230 LOG(INFO) << "Payload size: " << payload.size();
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800231
Sen Jiang2d528b42015-09-25 11:18:12 -0700232 install_plan_.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800233
Sen Jiang2d528b42015-09-25 11:18:12 -0700234 DeltaPerformer::MetadataParseResult expected_result, actual_result;
235 ErrorCode expected_error, actual_error;
236
237 // Fill up the metadata signature in install plan according to the test.
238 switch (metadata_signature_test) {
239 case kEmptyMetadataSignature:
240 install_plan_.metadata_signature.clear();
241 expected_result = DeltaPerformer::kMetadataParseError;
242 expected_error = ErrorCode::kDownloadMetadataSignatureMissingError;
243 break;
244
245 case kInvalidMetadataSignature:
246 install_plan_.metadata_signature = kBogusMetadataSignature1;
247 expected_result = DeltaPerformer::kMetadataParseError;
248 expected_error = ErrorCode::kDownloadMetadataSignatureMismatch;
249 break;
250
251 case kValidMetadataSignature:
252 default:
253 // Set the install plan's metadata size to be the same as the one
254 // in the manifest so that we pass the metadata size checks. Only
255 // then we can get to manifest signature checks.
256 ASSERT_TRUE(PayloadSigner::GetMetadataSignature(
257 payload.data(),
258 install_plan_.metadata_size,
259 kUnittestPrivateKeyPath,
260 &install_plan_.metadata_signature));
261 EXPECT_FALSE(install_plan_.metadata_signature.empty());
262 expected_result = DeltaPerformer::kMetadataParseSuccess;
263 expected_error = ErrorCode::kSuccess;
264 break;
265 }
266
267 // Ignore the expected result/error if hash checks are not mandatory.
268 if (!hash_checks_mandatory) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800269 expected_result = DeltaPerformer::kMetadataParseSuccess;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700270 expected_error = ErrorCode::kSuccess;
Sen Jiang2d528b42015-09-25 11:18:12 -0700271 }
272
273 // Use the public key corresponding to the private key used above to
274 // sign the metadata.
275 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
276 performer_.set_public_key_path(kUnittestPublicKeyPath);
277
278 // Init actual_error with an invalid value so that we make sure
279 // ParsePayloadMetadata properly populates it in all cases.
280 actual_error = ErrorCode::kUmaReportedMax;
281 actual_result = performer_.ParsePayloadMetadata(payload, &actual_error);
282
283 EXPECT_EQ(expected_result, actual_result);
284 EXPECT_EQ(expected_error, actual_error);
285
286 // Check that the parsed metadata size is what's expected. This test
287 // implicitly confirms that the metadata signature is valid, if required.
288 EXPECT_EQ(install_plan_.metadata_size, performer_.GetMetadataSize());
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800289 }
290
Sen Jiangb8060e42015-09-24 17:30:50 -0700291 void SetSupportedMajorVersion(uint64_t major_version) {
292 performer_.supported_major_version_ = major_version;
293 }
Sen Jiang2d528b42015-09-25 11:18:12 -0700294 FakePrefs prefs_;
295 InstallPlan install_plan_;
296 FakeSystemState fake_system_state_;
297 DeltaPerformer performer_{&prefs_, &fake_system_state_, &install_plan_};
298};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800299
Sen Jiang2d528b42015-09-25 11:18:12 -0700300TEST_F(DeltaPerformerTest, FullPayloadWriteTest) {
301 install_plan_.is_full_update = true;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700302 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
303 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700304 expected_data.resize(4096); // block size
305 vector<AnnotatedOperation> aops;
306 AnnotatedOperation aop;
307 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
308 aop.op.set_data_offset(0);
309 aop.op.set_data_length(expected_data.size());
310 aop.op.set_type(InstallOperation::REPLACE);
311 aops.push_back(aop);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800312
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700313 brillo::Blob payload_data = GeneratePayload(expected_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700314 kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800315
Alex Deymo79715ad2015-10-02 14:27:53 -0700316 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800317}
318
Sen Jiang2d528b42015-09-25 11:18:12 -0700319TEST_F(DeltaPerformerTest, ReplaceOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700320 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
321 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700322 expected_data.resize(4096); // block size
323 vector<AnnotatedOperation> aops;
324 AnnotatedOperation aop;
325 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
326 aop.op.set_data_offset(0);
327 aop.op.set_data_length(expected_data.size());
328 aop.op.set_type(InstallOperation::REPLACE);
329 aops.push_back(aop);
330
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700331 brillo::Blob payload_data = GeneratePayload(expected_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700332 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700333 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700334
335 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
336}
337
338TEST_F(DeltaPerformerTest, ReplaceBzOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700339 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
340 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700341 expected_data.resize(4096); // block size
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700342 brillo::Blob bz_data;
Sen Jiang2d528b42015-09-25 11:18:12 -0700343 EXPECT_TRUE(BzipCompress(expected_data, &bz_data));
344
345 vector<AnnotatedOperation> aops;
346 AnnotatedOperation aop;
347 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
348 aop.op.set_data_offset(0);
349 aop.op.set_data_length(bz_data.size());
350 aop.op.set_type(InstallOperation::REPLACE_BZ);
351 aops.push_back(aop);
352
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700353 brillo::Blob payload_data = GeneratePayload(bz_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700354 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700355 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700356
357 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
358}
359
Alex Deymo2d621a32015-10-01 11:09:01 -0700360TEST_F(DeltaPerformerTest, ReplaceXzOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700361 brillo::Blob xz_data(std::begin(kXzCompressedData),
Alex Deymo2d621a32015-10-01 11:09:01 -0700362 std::end(kXzCompressedData));
363 // The compressed xz data contains only a single "a", but the operation should
364 // pad the rest of the two blocks with zeros.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700365 brillo::Blob expected_data = brillo::Blob(4096, 0);
Alex Deymo2d621a32015-10-01 11:09:01 -0700366 expected_data[0] = 'a';
367
368 AnnotatedOperation aop;
369 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
370 aop.op.set_data_offset(0);
371 aop.op.set_data_length(xz_data.size());
372 aop.op.set_type(InstallOperation::REPLACE_XZ);
373 vector<AnnotatedOperation> aops = {aop};
374
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700375 brillo::Blob payload_data = GeneratePayload(xz_data, aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700376 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700377 kSourceMinorPayloadVersion);
Alex Deymo2d621a32015-10-01 11:09:01 -0700378
379 EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null"));
380}
381
Alex Deymo79715ad2015-10-02 14:27:53 -0700382TEST_F(DeltaPerformerTest, ZeroOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700383 brillo::Blob existing_data = brillo::Blob(4096 * 10, 'a');
384 brillo::Blob expected_data = existing_data;
Alex Deymo79715ad2015-10-02 14:27:53 -0700385 // Blocks 4, 5 and 7 should have zeros instead of 'a' after the operation is
386 // applied.
387 std::fill(expected_data.data() + 4096 * 4, expected_data.data() + 4096 * 6,
388 0);
389 std::fill(expected_data.data() + 4096 * 7, expected_data.data() + 4096 * 8,
390 0);
391
392 AnnotatedOperation aop;
393 *(aop.op.add_dst_extents()) = ExtentForRange(4, 2);
394 *(aop.op.add_dst_extents()) = ExtentForRange(7, 1);
395 aop.op.set_type(InstallOperation::ZERO);
396 vector<AnnotatedOperation> aops = {aop};
397
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700398 brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700399 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700400 kSourceMinorPayloadVersion);
Alex Deymo79715ad2015-10-02 14:27:53 -0700401
402 EXPECT_EQ(expected_data,
403 ApplyPayloadToData(payload_data, "/dev/null", existing_data));
404}
405
Sen Jiang2d528b42015-09-25 11:18:12 -0700406TEST_F(DeltaPerformerTest, SourceCopyOperationTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700407 brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString),
408 std::end(kRandomString));
Sen Jiang2d528b42015-09-25 11:18:12 -0700409 expected_data.resize(4096); // block size
410 vector<AnnotatedOperation> aops;
411 AnnotatedOperation aop;
412 *(aop.op.add_src_extents()) = ExtentForRange(0, 1);
413 *(aop.op.add_dst_extents()) = ExtentForRange(0, 1);
414 aop.op.set_type(InstallOperation::SOURCE_COPY);
415 aops.push_back(aop);
416
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700417 brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false,
Sen Jiang76bfa742015-10-12 17:13:26 -0700418 kChromeOSMajorPayloadVersion,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700419 kSourceMinorPayloadVersion);
Sen Jiang2d528b42015-09-25 11:18:12 -0700420 string source_path;
421 EXPECT_TRUE(utils::MakeTempFile("Source-XXXXXX",
422 &source_path, nullptr));
423 ScopedPathUnlinker path_unlinker(source_path);
424 EXPECT_TRUE(utils::WriteFile(source_path.c_str(),
425 expected_data.data(),
426 expected_data.size()));
427
428 EXPECT_EQ(expected_data, ApplyPayload(payload_data, source_path));
429}
430
431TEST_F(DeltaPerformerTest, ExtentsToByteStringTest) {
Allie Wood56873452015-03-27 17:48:40 -0700432 uint64_t test[] = {1, 1, 4, 2, 0, 1};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800433 COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven);
434 const uint64_t block_size = 4096;
Allie Wood56873452015-03-27 17:48:40 -0700435 const uint64_t file_length = 4 * block_size - 13;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800436
437 google::protobuf::RepeatedPtrField<Extent> extents;
438 for (size_t i = 0; i < arraysize(test); i += 2) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700439 *(extents.Add()) = ExtentForRange(test[i], test[i + 1]);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800440 }
441
Allie Wood56873452015-03-27 17:48:40 -0700442 string expected_output = "4096:4096,16384:8192,0:4083";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800443 string actual_output;
444 EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents,
445 block_size,
446 file_length,
447 &actual_output));
448 EXPECT_EQ(expected_output, actual_output);
449}
Darin Petkov68c10d12010-10-14 09:24:37 -0700450
Sen Jiang2d528b42015-09-25 11:18:12 -0700451TEST_F(DeltaPerformerTest, ValidateManifestFullGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800452 // The Manifest we are validating.
453 DeltaArchiveManifest manifest;
454 manifest.mutable_new_kernel_info();
455 manifest.mutable_new_rootfs_info();
Alex Deymocbf09892015-09-11 16:13:16 -0700456 manifest.set_minor_version(kFullPayloadMinorVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800457
Sen Jiang2d528b42015-09-25 11:18:12 -0700458 RunManifestValidation(manifest, true, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800459}
460
Sen Jiang2d528b42015-09-25 11:18:12 -0700461TEST_F(DeltaPerformerTest, ValidateManifestDeltaGoodTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800462 // The Manifest we are validating.
463 DeltaArchiveManifest manifest;
464 manifest.mutable_old_kernel_info();
465 manifest.mutable_old_rootfs_info();
466 manifest.mutable_new_kernel_info();
467 manifest.mutable_new_rootfs_info();
468 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
469
Sen Jiang2d528b42015-09-25 11:18:12 -0700470 RunManifestValidation(manifest, false, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800471}
472
Sen Jiang2d528b42015-09-25 11:18:12 -0700473TEST_F(DeltaPerformerTest, ValidateManifestFullUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800474 // The Manifest we are validating.
475 DeltaArchiveManifest manifest;
476
Sen Jiang2d528b42015-09-25 11:18:12 -0700477 RunManifestValidation(manifest, true, ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800478}
479
Sen Jiang2d528b42015-09-25 11:18:12 -0700480TEST_F(DeltaPerformerTest, ValidateManifestDeltaUnsetMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800481 // The Manifest we are validating.
482 DeltaArchiveManifest manifest;
483
Sen Jiang2d528b42015-09-25 11:18:12 -0700484 RunManifestValidation(manifest, false,
485 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800486}
487
Sen Jiang2d528b42015-09-25 11:18:12 -0700488TEST_F(DeltaPerformerTest, ValidateManifestFullOldKernelTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800489 // The Manifest we are validating.
490 DeltaArchiveManifest manifest;
491 manifest.mutable_old_kernel_info();
492 manifest.mutable_new_kernel_info();
493 manifest.mutable_new_rootfs_info();
494 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
495
Sen Jiang2d528b42015-09-25 11:18:12 -0700496 RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800497}
498
Sen Jiang2d528b42015-09-25 11:18:12 -0700499TEST_F(DeltaPerformerTest, ValidateManifestFullOldRootfsTest) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800500 // The Manifest we are validating.
501 DeltaArchiveManifest manifest;
502 manifest.mutable_old_rootfs_info();
503 manifest.mutable_new_kernel_info();
504 manifest.mutable_new_rootfs_info();
505 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
506
Sen Jiang2d528b42015-09-25 11:18:12 -0700507 RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800508}
509
Sen Jiangc8f6b7a2015-10-21 11:09:59 -0700510TEST_F(DeltaPerformerTest, ValidateManifestFullPartitionUpdateTest) {
511 // The Manifest we are validating.
512 DeltaArchiveManifest manifest;
513 PartitionUpdate* partition = manifest.add_partitions();
514 partition->mutable_old_partition_info();
515 partition->mutable_new_partition_info();
516 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
517
518 RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType);
519}
520
Sen Jiang2d528b42015-09-25 11:18:12 -0700521TEST_F(DeltaPerformerTest, ValidateManifestBadMinorVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800522 // The Manifest we are validating.
523 DeltaArchiveManifest manifest;
524
525 // Generate a bad version number.
526 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion +
527 10000);
528
Sen Jiang2d528b42015-09-25 11:18:12 -0700529 RunManifestValidation(manifest, false,
530 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800531}
532
Sen Jiangb8060e42015-09-24 17:30:50 -0700533TEST_F(DeltaPerformerTest, BrilloMetadataSignatureSizeTest) {
534 SetSupportedMajorVersion(kBrilloMajorPayloadVersion);
Sen Jiangb8060e42015-09-24 17:30:50 -0700535 EXPECT_TRUE(performer_.Write(kDeltaMagic, sizeof(kDeltaMagic)));
536
537 uint64_t major_version = htobe64(kBrilloMajorPayloadVersion);
538 EXPECT_TRUE(performer_.Write(&major_version, 8));
539
540 uint64_t manifest_size = rand() % 256;
541 uint64_t manifest_size_be = htobe64(manifest_size);
542 EXPECT_TRUE(performer_.Write(&manifest_size_be, 8));
543
544 uint32_t metadata_signature_size = rand() % 256;
545 uint32_t metadata_signature_size_be = htobe32(metadata_signature_size);
546 EXPECT_TRUE(performer_.Write(&metadata_signature_size_be, 4));
547
548 EXPECT_LT(performer_.Close(), 0);
549
550 EXPECT_TRUE(performer_.IsHeaderParsed());
551 EXPECT_EQ(kBrilloMajorPayloadVersion, performer_.GetMajorVersion());
552 uint64_t manifest_offset;
553 EXPECT_TRUE(performer_.GetManifestOffset(&manifest_offset));
554 EXPECT_EQ(24, manifest_offset); // 4 + 8 + 8 + 4
Sen Jiang76bfa742015-10-12 17:13:26 -0700555 EXPECT_EQ(manifest_offset + manifest_size, performer_.GetMetadataSize());
556 EXPECT_EQ(metadata_signature_size, performer_.metadata_signature_size_);
557}
558
559TEST_F(DeltaPerformerTest, BrilloVerifyMetadataSignatureTest) {
560 SetSupportedMajorVersion(kBrilloMajorPayloadVersion);
561 brillo::Blob payload_data = GeneratePayload({}, {}, true,
562 kBrilloMajorPayloadVersion,
563 kSourceMinorPayloadVersion);
564 install_plan_.hash_checks_mandatory = true;
565 // Just set these value so that we can use ValidateMetadataSignature directly.
566 performer_.major_payload_version_ = kBrilloMajorPayloadVersion;
567 performer_.metadata_size_ = install_plan_.metadata_size;
568 uint64_t signature_length;
569 EXPECT_TRUE(PayloadSigner::SignatureBlobLength({kUnittestPrivateKeyPath},
570 &signature_length));
571 performer_.metadata_signature_size_ = signature_length;
572 performer_.set_public_key_path(kUnittestPublicKeyPath);
573 EXPECT_EQ(ErrorCode::kSuccess,
574 performer_.ValidateMetadataSignature(payload_data));
Sen Jiangb8060e42015-09-24 17:30:50 -0700575}
576
Sen Jiang2d528b42015-09-25 11:18:12 -0700577TEST_F(DeltaPerformerTest, BadDeltaMagicTest) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700578 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700579 EXPECT_FALSE(performer_.Write("morejunk", 8));
580 EXPECT_LT(performer_.Close(), 0);
Darin Petkov934bb412010-11-18 11:21:35 -0800581}
582
Sen Jiang2d528b42015-09-25 11:18:12 -0700583TEST_F(DeltaPerformerTest, WriteUpdatesPayloadState) {
Sen Jiang2d528b42015-09-25 11:18:12 -0700584 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800585 DownloadProgress(4)).Times(1);
Sen Jiang2d528b42015-09-25 11:18:12 -0700586 EXPECT_CALL(*(fake_system_state_.mock_payload_state()),
Sen Jiangb8060e42015-09-24 17:30:50 -0700587 DownloadProgress(8)).Times(1);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800588
Sen Jiang2d528b42015-09-25 11:18:12 -0700589 EXPECT_TRUE(performer_.Write("junk", 4));
Sen Jiang2d528b42015-09-25 11:18:12 -0700590 EXPECT_FALSE(performer_.Write("morejunk", 8));
591 EXPECT_LT(performer_.Close(), 0);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800592}
593
Sen Jiang2d528b42015-09-25 11:18:12 -0700594TEST_F(DeltaPerformerTest, MissingMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800595 DoMetadataSizeTest(0, 75456, true);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700596}
597
Sen Jiang2d528b42015-09-25 11:18:12 -0700598TEST_F(DeltaPerformerTest, MissingNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800599 DoMetadataSizeTest(0, 123456, false);
600}
601
Sen Jiang2d528b42015-09-25 11:18:12 -0700602TEST_F(DeltaPerformerTest, InvalidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800603 DoMetadataSizeTest(13000, 140000, true);
604}
605
Sen Jiang2d528b42015-09-25 11:18:12 -0700606TEST_F(DeltaPerformerTest, InvalidNonMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800607 DoMetadataSizeTest(40000, 50000, false);
608}
609
Sen Jiang2d528b42015-09-25 11:18:12 -0700610TEST_F(DeltaPerformerTest, ValidMandatoryMetadataSizeTest) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800611 DoMetadataSizeTest(85376, 85376, true);
612}
613
Sen Jiang2d528b42015-09-25 11:18:12 -0700614TEST_F(DeltaPerformerTest, MandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700615 DoMetadataSignatureTest(kEmptyMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800616}
617
Sen Jiang2d528b42015-09-25 11:18:12 -0700618TEST_F(DeltaPerformerTest, NonMandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700619 DoMetadataSignatureTest(kEmptyMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800620}
621
Sen Jiang2d528b42015-09-25 11:18:12 -0700622TEST_F(DeltaPerformerTest, MandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700623 DoMetadataSignatureTest(kInvalidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800624}
625
Sen Jiang2d528b42015-09-25 11:18:12 -0700626TEST_F(DeltaPerformerTest, NonMandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700627 DoMetadataSignatureTest(kInvalidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800628}
629
Sen Jiang2d528b42015-09-25 11:18:12 -0700630TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature1Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700631 DoMetadataSignatureTest(kValidMetadataSignature, false, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800632}
633
Sen Jiang2d528b42015-09-25 11:18:12 -0700634TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature2Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700635 DoMetadataSignatureTest(kValidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800636}
637
Sen Jiang2d528b42015-09-25 11:18:12 -0700638TEST_F(DeltaPerformerTest, NonMandatoryValidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700639 DoMetadataSignatureTest(kValidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800640}
641
Sen Jiang2d528b42015-09-25 11:18:12 -0700642TEST_F(DeltaPerformerTest, UsePublicKeyFromResponse) {
David Zeuthene7f89172013-10-31 10:21:04 -0700643 base::FilePath key_path;
644
645 // The result of the GetPublicKeyResponse() method is based on three things
646 //
647 // 1. Whether it's an official build; and
648 // 2. Whether the Public RSA key to be used is in the root filesystem; and
Alex Vakulenko072359c2014-07-18 11:41:07 -0700649 // 3. Whether the response has a public key
David Zeuthene7f89172013-10-31 10:21:04 -0700650 //
651 // We test all eight combinations to ensure that we only use the
652 // public key in the response if
653 //
654 // a. it's not an official build; and
655 // b. there is no key in the root filesystem.
656
Sen Jiang2d528b42015-09-25 11:18:12 -0700657 FakeHardware* fake_hardware = fake_system_state_.fake_hardware();
David Zeuthene7f89172013-10-31 10:21:04 -0700658
659 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800660 EXPECT_TRUE(utils::MakeTempDirectory("PublicKeyFromResponseTests.XXXXXX",
David Zeuthene7f89172013-10-31 10:21:04 -0700661 &temp_dir));
662 string non_existing_file = temp_dir + "/non-existing";
663 string existing_file = temp_dir + "/existing";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700664 EXPECT_EQ(0, System(base::StringPrintf("touch %s", existing_file.c_str())));
David Zeuthene7f89172013-10-31 10:21:04 -0700665
666 // Non-official build, non-existing public-key, key in response -> true
Don Garrett6646b442013-11-13 15:29:11 -0800667 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700668 performer_.public_key_path_ = non_existing_file;
669 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
670 EXPECT_TRUE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700671 EXPECT_FALSE(key_path.empty());
672 EXPECT_EQ(unlink(key_path.value().c_str()), 0);
673 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800674 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700675 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700676
677 // Non-official build, existing public-key, key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800678 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700679 performer_.public_key_path_ = existing_file;
680 install_plan_.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
681 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700682 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800683 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700684 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700685
686 // Non-official build, non-existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800687 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700688 performer_.public_key_path_ = non_existing_file;
689 install_plan_.public_key_rsa = "";
690 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700691 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800692 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700693 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700694
695 // Non-official build, existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800696 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700697 performer_.public_key_path_ = existing_file;
698 install_plan_.public_key_rsa = "";
699 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700700 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800701 fake_hardware->SetIsOfficialBuild(true);
Sen Jiang2d528b42015-09-25 11:18:12 -0700702 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700703
704 // Non-official build, non-existing public-key, key in response
705 // but invalid base64 -> false
Don Garrett6646b442013-11-13 15:29:11 -0800706 fake_hardware->SetIsOfficialBuild(false);
Sen Jiang2d528b42015-09-25 11:18:12 -0700707 performer_.public_key_path_ = non_existing_file;
708 install_plan_.public_key_rsa = "not-valid-base64";
709 EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path));
David Zeuthene7f89172013-10-31 10:21:04 -0700710
Alex Deymo110e0302015-10-19 20:35:21 -0700711 EXPECT_TRUE(base::DeleteFile(base::FilePath(temp_dir), true));
David Zeuthene7f89172013-10-31 10:21:04 -0700712}
713
Alex Deymocbf09892015-09-11 16:13:16 -0700714TEST_F(DeltaPerformerTest, ConfVersionsMatch) {
715 // Test that the versions in update_engine.conf that is installed to the
716 // image match the supported delta versions in the update engine.
Allie Wood78750a42015-02-11 15:42:11 -0800717 uint32_t minor_version;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700718 brillo::KeyValueStore store;
Alex Deymob42b98d2015-07-06 17:42:38 -0700719 EXPECT_TRUE(store.Load(base::FilePath("update_engine.conf")));
720 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
721 EXPECT_EQ(DeltaPerformer::kSupportedMinorPayloadVersion, minor_version);
Alex Deymocbf09892015-09-11 16:13:16 -0700722
723 string major_version_str;
724 uint64_t major_version;
725 EXPECT_TRUE(store.GetString("PAYLOAD_MAJOR_VERSION", &major_version_str));
726 EXPECT_TRUE(base::StringToUint64(major_version_str, &major_version));
727 EXPECT_EQ(DeltaPerformer::kSupportedMajorPayloadVersion, major_version);
Allie Wood78750a42015-02-11 15:42:11 -0800728}
729
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700730} // namespace chromeos_update_engine