blob: d0246910aa9986b974d1c6de3c23d1afaac48199 [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 Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/stringprintf.h>
Alex Deymof1cbe172015-03-05 15:58:37 -080027#include <base/strings/string_util.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070028#include <google/protobuf/repeated_field.h>
29#include <gtest/gtest.h>
30
Jay Srinivasand29695d2013-04-08 15:08:05 -070031#include "update_engine/constants.h"
David Zeuthene7f89172013-10-31 10:21:04 -070032#include "update_engine/fake_hardware.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070033#include "update_engine/fake_system_state.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080034#include "update_engine/mock_prefs.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070035#include "update_engine/payload_constants.h"
Sen Jiangd78b3892015-09-25 15:19:31 -070036#include "update_engine/payload_generator/payload_file.h"
Alex Deymo923d8fa2014-07-15 17:58:51 -070037#include "update_engine/payload_generator/payload_signer.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070038#include "update_engine/test_utils.h"
39#include "update_engine/update_metadata.pb.h"
40#include "update_engine/utils.h"
41
42namespace chromeos_update_engine {
43
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070044using std::string;
45using std::vector;
Darin Petkov73058b42010-10-06 16:32:19 -070046using testing::Return;
Alex Deymo161c4a12014-05-16 15:56:21 -070047using testing::_;
Alex Deymo10875d92014-11-10 21:52:57 -080048using test_utils::kRandomString;
Alex Deymo10875d92014-11-10 21:52:57 -080049using test_utils::System;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070050
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070051extern const char* kUnittestPrivateKeyPath;
Darin Petkovd7061ab2010-10-06 14:37:09 -070052extern const char* kUnittestPublicKeyPath;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070053
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070054static const char* kBogusMetadataSignature1 =
55 "awSFIUdUZz2VWFiR+ku0Pj00V7bPQPQFYQSXjEXr3vaw3TE4xHV5CraY3/YrZpBv"
56 "J5z4dSBskoeuaO1TNC/S6E05t+yt36tE4Fh79tMnJ/z9fogBDXWgXLEUyG78IEQr"
57 "YH6/eBsQGT2RJtBgXIXbZ9W+5G9KmGDoPOoiaeNsDuqHiBc/58OFsrxskH8E6vMS"
58 "BmMGGk82mvgzic7ApcoURbCGey1b3Mwne/hPZ/bb9CIyky8Og9IfFMdL2uAweOIR"
59 "fjoTeLYZpt+WN65Vu7jJ0cQN8e1y+2yka5112wpRf/LLtPgiAjEZnsoYpLUd7CoV"
60 "pLRtClp97kN2+tXGNBQqkA==";
Jay Srinivasan738fdf32012-12-07 17:40:54 -080061
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070062namespace {
Jay Srinivasan738fdf32012-12-07 17:40:54 -080063// Different options that determine what we should fill into the
64// install_plan.metadata_signature to simulate the contents received in the
65// Omaha response.
66enum MetadataSignatureTest {
67 kEmptyMetadataSignature,
68 kInvalidMetadataSignature,
69 kValidMetadataSignature,
70};
71
Alex Vakulenkod2779df2014-06-16 13:19:00 -070072} // namespace
Jay Srinivasan738fdf32012-12-07 17:40:54 -080073
Allie Woodfdf00512015-03-02 13:34:55 -080074class DeltaPerformerTest : public ::testing::Test {
75 public:
76 // Test helper placed where it can easily be friended from DeltaPerformer.
77 static void RunManifestValidation(const DeltaArchiveManifest& manifest,
78 bool full_payload,
79 ErrorCode expected) {
80 MockPrefs prefs;
81 InstallPlan install_plan;
82 FakeSystemState fake_system_state;
83 DeltaPerformer performer(&prefs, &fake_system_state, &install_plan);
84
85 // The install plan is for Full or Delta.
86 install_plan.is_full_update = full_payload;
87
88 // The Manifest we are validating.
89 performer.manifest_.CopyFrom(manifest);
90
91 EXPECT_EQ(expected, performer.ValidateManifest());
92 }
93
Sen Jiangd78b3892015-09-25 15:19:31 -070094 static chromeos::Blob GeneratePayload(const chromeos::Blob& blob_data,
95 const vector<AnnotatedOperation>& aops,
96 bool sign_payload,
97 int32_t minor_version,
98 uint64_t* out_metadata_size) {
99 string blob_path;
100 EXPECT_TRUE(utils::MakeTempFile("Blob-XXXXXX", &blob_path, nullptr));
101 ScopedPathUnlinker blob_unlinker(blob_path);
102 EXPECT_TRUE(utils::WriteFile(blob_path.c_str(),
103 blob_data.data(),
104 blob_data.size()));
105
106 PayloadGenerationConfig config;
107 config.major_version = kChromeOSMajorPayloadVersion;
108 config.minor_version = minor_version;
109 config.target.rootfs.path = blob_path;
110 config.target.rootfs.size = blob_data.size();
111 config.target.kernel.path = blob_path;
112 config.target.kernel.size = blob_data.size();
113
114 PayloadFile payload;
115 EXPECT_TRUE(payload.Init(config));
116
117 payload.AddPartition(config.source.rootfs, config.target.rootfs, aops);
118
119 string payload_path;
120 EXPECT_TRUE(utils::MakeTempFile("Payload-XXXXXX", &payload_path, nullptr));
121 ScopedPathUnlinker payload_unlinker(payload_path);
122 EXPECT_TRUE(payload.WritePayload(payload_path, blob_path,
123 sign_payload ? kUnittestPrivateKeyPath : "",
124 out_metadata_size));
125
126 chromeos::Blob payload_data;
127 EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data));
128 return payload_data;
129 }
Allie Woodfdf00512015-03-02 13:34:55 -0800130};
131
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800132// Calls delta performer's Write method by pretending to pass in bytes from a
133// delta file whose metadata size is actual_metadata_size and tests if all
134// checks are correctly performed if the install plan contains
135// expected_metadata_size and that the result of the parsing are as per
136// hash_checks_mandatory flag.
137void DoMetadataSizeTest(uint64_t expected_metadata_size,
138 uint64_t actual_metadata_size,
139 bool hash_checks_mandatory) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800140 MockPrefs prefs;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800141 InstallPlan install_plan;
142 install_plan.hash_checks_mandatory = hash_checks_mandatory;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700143 FakeSystemState fake_system_state;
144 DeltaPerformer performer(&prefs, &fake_system_state, &install_plan);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800145 EXPECT_EQ(0, performer.Open("/dev/null", 0, 0));
146 EXPECT_TRUE(performer.OpenKernel("/dev/null"));
147
148 // Set a valid magic string and version number 1.
149 EXPECT_TRUE(performer.Write("CrAU", 4));
150 uint64_t version = htobe64(1);
151 EXPECT_TRUE(performer.Write(&version, 8));
152
153 install_plan.metadata_size = expected_metadata_size;
David Zeuthena99981f2013-04-29 13:42:47 -0700154 ErrorCode error_code;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800155 // When filling in size in manifest, exclude the size of the 20-byte header.
156 uint64_t size_in_manifest = htobe64(actual_metadata_size - 20);
157 bool result = performer.Write(&size_in_manifest, 8, &error_code);
158 if (expected_metadata_size == actual_metadata_size ||
159 !hash_checks_mandatory) {
160 EXPECT_TRUE(result);
161 } else {
162 EXPECT_FALSE(result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700163 EXPECT_EQ(ErrorCode::kDownloadInvalidMetadataSize, error_code);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800164 }
165
166 EXPECT_LT(performer.Close(), 0);
167}
168
169// Generates a valid delta file but tests the delta performer by suppling
170// different metadata signatures as per omaha_metadata_signature flag and
171// sees if the result of the parsing are as per hash_checks_mandatory flag.
172void DoMetadataSignatureTest(MetadataSignatureTest metadata_signature_test,
Sen Jiangd78b3892015-09-25 15:19:31 -0700173 bool sign_payload,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800174 bool hash_checks_mandatory) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700175 InstallPlan install_plan;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800176
177 // Loads the payload and parses the manifest.
Sen Jiangd78b3892015-09-25 15:19:31 -0700178 chromeos::Blob payload = DeltaPerformerTest::GeneratePayload(chromeos::Blob(),
179 vector<AnnotatedOperation>(), sign_payload,
180 DeltaPerformer::kFullPayloadMinorVersion, &install_plan.metadata_size);
181
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800182 LOG(INFO) << "Payload size: " << payload.size();
183
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800184 install_plan.hash_checks_mandatory = hash_checks_mandatory;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800185
186 DeltaPerformer::MetadataParseResult expected_result, actual_result;
David Zeuthena99981f2013-04-29 13:42:47 -0700187 ErrorCode expected_error, actual_error;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800188
189 // Fill up the metadata signature in install plan according to the test.
190 switch (metadata_signature_test) {
191 case kEmptyMetadataSignature:
192 install_plan.metadata_signature.clear();
193 expected_result = DeltaPerformer::kMetadataParseError;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700194 expected_error = ErrorCode::kDownloadMetadataSignatureMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800195 break;
196
197 case kInvalidMetadataSignature:
198 install_plan.metadata_signature = kBogusMetadataSignature1;
199 expected_result = DeltaPerformer::kMetadataParseError;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700200 expected_error = ErrorCode::kDownloadMetadataSignatureMismatch;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800201 break;
202
203 case kValidMetadataSignature:
204 default:
205 // Set the install plan's metadata size to be the same as the one
206 // in the manifest so that we pass the metadata size checks. Only
207 // then we can get to manifest signature checks.
208 ASSERT_TRUE(PayloadSigner::GetMetadataSignature(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700209 payload.data(),
Sen Jiangd78b3892015-09-25 15:19:31 -0700210 install_plan.metadata_size,
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800211 kUnittestPrivateKeyPath,
212 &install_plan.metadata_signature));
213 EXPECT_FALSE(install_plan.metadata_signature.empty());
214 expected_result = DeltaPerformer::kMetadataParseSuccess;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700215 expected_error = ErrorCode::kSuccess;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800216 break;
217 }
218
219 // Ignore the expected result/error if hash checks are not mandatory.
220 if (!hash_checks_mandatory) {
221 expected_result = DeltaPerformer::kMetadataParseSuccess;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700222 expected_error = ErrorCode::kSuccess;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800223 }
224
225 // Create the delta performer object.
Alex Deymo8427b4a2014-11-05 14:00:32 -0800226 MockPrefs prefs;
Sen Jiangd78b3892015-09-25 15:19:31 -0700227 FakeSystemState fake_system_state;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800228 DeltaPerformer delta_performer(&prefs,
Sen Jiangd78b3892015-09-25 15:19:31 -0700229 &fake_system_state,
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800230 &install_plan);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800231
232 // Use the public key corresponding to the private key used above to
233 // sign the metadata.
234 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
235 delta_performer.set_public_key_path(kUnittestPublicKeyPath);
236
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800237 // Init actual_error with an invalid value so that we make sure
238 // ParsePayloadMetadata properly populates it in all cases.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700239 actual_error = ErrorCode::kUmaReportedMax;
Gilad Arnolddaa27402014-01-23 11:56:17 -0800240 actual_result = delta_performer.ParsePayloadMetadata(payload, &actual_error);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800241
242 EXPECT_EQ(expected_result, actual_result);
243 EXPECT_EQ(expected_error, actual_error);
244
245 // Check that the parsed metadata size is what's expected. This test
246 // implicitly confirms that the metadata signature is valid, if required.
Sen Jiangd78b3892015-09-25 15:19:31 -0700247 EXPECT_EQ(install_plan.metadata_size, delta_performer.GetMetadataSize());
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800248}
249
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800250TEST(DeltaPerformerTest, ExtentsToByteStringTest) {
Allie Wood56873452015-03-27 17:48:40 -0700251 uint64_t test[] = {1, 1, 4, 2, 0, 1};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800252 COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven);
253 const uint64_t block_size = 4096;
Allie Wood56873452015-03-27 17:48:40 -0700254 const uint64_t file_length = 4 * block_size - 13;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800255
256 google::protobuf::RepeatedPtrField<Extent> extents;
257 for (size_t i = 0; i < arraysize(test); i += 2) {
258 Extent* extent = extents.Add();
259 extent->set_start_block(test[i]);
260 extent->set_num_blocks(test[i + 1]);
261 }
262
Allie Wood56873452015-03-27 17:48:40 -0700263 string expected_output = "4096:4096,16384:8192,0:4083";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800264 string actual_output;
265 EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents,
266 block_size,
267 file_length,
268 &actual_output));
269 EXPECT_EQ(expected_output, actual_output);
270}
Darin Petkov68c10d12010-10-14 09:24:37 -0700271
Don Garrettb8dd1d92013-11-22 17:40:02 -0800272TEST(DeltaPerformerTest, ValidateManifestFullGoodTest) {
273 // The Manifest we are validating.
274 DeltaArchiveManifest manifest;
275 manifest.mutable_new_kernel_info();
276 manifest.mutable_new_rootfs_info();
277 manifest.set_minor_version(DeltaPerformer::kFullPayloadMinorVersion);
278
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700279 DeltaPerformerTest::RunManifestValidation(manifest, true,
280 ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800281}
282
283TEST(DeltaPerformerTest, ValidateManifestDeltaGoodTest) {
284 // The Manifest we are validating.
285 DeltaArchiveManifest manifest;
286 manifest.mutable_old_kernel_info();
287 manifest.mutable_old_rootfs_info();
288 manifest.mutable_new_kernel_info();
289 manifest.mutable_new_rootfs_info();
290 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
291
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700292 DeltaPerformerTest::RunManifestValidation(manifest, false,
293 ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800294}
295
296TEST(DeltaPerformerTest, ValidateManifestFullUnsetMinorVersion) {
297 // The Manifest we are validating.
298 DeltaArchiveManifest manifest;
299
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700300 DeltaPerformerTest::RunManifestValidation(manifest, true,
301 ErrorCode::kSuccess);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800302}
303
304TEST(DeltaPerformerTest, ValidateManifestDeltaUnsetMinorVersion) {
305 // The Manifest we are validating.
306 DeltaArchiveManifest manifest;
307
308 DeltaPerformerTest::RunManifestValidation(
309 manifest, false,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700310 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800311}
312
313TEST(DeltaPerformerTest, ValidateManifestFullOldKernelTest) {
314 // The Manifest we are validating.
315 DeltaArchiveManifest manifest;
316 manifest.mutable_old_kernel_info();
317 manifest.mutable_new_kernel_info();
318 manifest.mutable_new_rootfs_info();
319 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
320
321 DeltaPerformerTest::RunManifestValidation(
322 manifest, true,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700323 ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800324}
325
326TEST(DeltaPerformerTest, ValidateManifestFullOldRootfsTest) {
327 // The Manifest we are validating.
328 DeltaArchiveManifest manifest;
329 manifest.mutable_old_rootfs_info();
330 manifest.mutable_new_kernel_info();
331 manifest.mutable_new_rootfs_info();
332 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion);
333
334 DeltaPerformerTest::RunManifestValidation(
335 manifest, true,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700336 ErrorCode::kPayloadMismatchedType);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800337}
338
339TEST(DeltaPerformerTest, ValidateManifestBadMinorVersion) {
340 // The Manifest we are validating.
341 DeltaArchiveManifest manifest;
342
343 // Generate a bad version number.
344 manifest.set_minor_version(DeltaPerformer::kSupportedMinorPayloadVersion +
345 10000);
346
347 DeltaPerformerTest::RunManifestValidation(
348 manifest, false,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700349 ErrorCode::kUnsupportedMinorPayloadVersion);
Don Garrettb8dd1d92013-11-22 17:40:02 -0800350}
351
Darin Petkov934bb412010-11-18 11:21:35 -0800352TEST(DeltaPerformerTest, BadDeltaMagicTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800353 MockPrefs prefs;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700354 InstallPlan install_plan;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700355 FakeSystemState fake_system_state;
356 DeltaPerformer performer(&prefs, &fake_system_state, &install_plan);
Darin Petkov934bb412010-11-18 11:21:35 -0800357 EXPECT_EQ(0, performer.Open("/dev/null", 0, 0));
358 EXPECT_TRUE(performer.OpenKernel("/dev/null"));
Don Garrette410e0f2011-11-10 15:39:01 -0800359 EXPECT_TRUE(performer.Write("junk", 4));
360 EXPECT_TRUE(performer.Write("morejunk", 8));
361 EXPECT_FALSE(performer.Write("morejunk", 8));
Darin Petkov934bb412010-11-18 11:21:35 -0800362 EXPECT_LT(performer.Close(), 0);
363}
364
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800365TEST(DeltaPerformerTest, WriteUpdatesPayloadState) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800366 MockPrefs prefs;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800367 InstallPlan install_plan;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700368 FakeSystemState fake_system_state;
369 DeltaPerformer performer(&prefs, &fake_system_state, &install_plan);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800370 EXPECT_EQ(0, performer.Open("/dev/null", 0, 0));
371 EXPECT_TRUE(performer.OpenKernel("/dev/null"));
372
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700373 EXPECT_CALL(*(fake_system_state.mock_payload_state()),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800374 DownloadProgress(4)).Times(1);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700375 EXPECT_CALL(*(fake_system_state.mock_payload_state()),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800376 DownloadProgress(8)).Times(2);
377
378 EXPECT_TRUE(performer.Write("junk", 4));
379 EXPECT_TRUE(performer.Write("morejunk", 8));
380 EXPECT_FALSE(performer.Write("morejunk", 8));
381 EXPECT_LT(performer.Close(), 0);
382}
383
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800384TEST(DeltaPerformerTest, MissingMandatoryMetadataSizeTest) {
385 DoMetadataSizeTest(0, 75456, true);
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700386}
387
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800388TEST(DeltaPerformerTest, MissingNonMandatoryMetadataSizeTest) {
389 DoMetadataSizeTest(0, 123456, false);
390}
391
392TEST(DeltaPerformerTest, InvalidMandatoryMetadataSizeTest) {
393 DoMetadataSizeTest(13000, 140000, true);
394}
395
396TEST(DeltaPerformerTest, InvalidNonMandatoryMetadataSizeTest) {
397 DoMetadataSizeTest(40000, 50000, false);
398}
399
400TEST(DeltaPerformerTest, ValidMandatoryMetadataSizeTest) {
401 DoMetadataSizeTest(85376, 85376, true);
402}
403
404TEST(DeltaPerformerTest, RunAsRootMandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700405 DoMetadataSignatureTest(kEmptyMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800406}
407
408TEST(DeltaPerformerTest, RunAsRootNonMandatoryEmptyMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700409 DoMetadataSignatureTest(kEmptyMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800410}
411
412TEST(DeltaPerformerTest, RunAsRootMandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700413 DoMetadataSignatureTest(kInvalidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800414}
415
416TEST(DeltaPerformerTest, RunAsRootNonMandatoryInvalidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700417 DoMetadataSignatureTest(kInvalidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800418}
419
420TEST(DeltaPerformerTest, RunAsRootMandatoryValidMetadataSignature1Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700421 DoMetadataSignatureTest(kValidMetadataSignature, false, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800422}
423
424TEST(DeltaPerformerTest, RunAsRootMandatoryValidMetadataSignature2Test) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700425 DoMetadataSignatureTest(kValidMetadataSignature, true, true);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800426}
427
428TEST(DeltaPerformerTest, RunAsRootNonMandatoryValidMetadataSignatureTest) {
Sen Jiangd78b3892015-09-25 15:19:31 -0700429 DoMetadataSignatureTest(kValidMetadataSignature, true, false);
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800430}
431
David Zeuthene7f89172013-10-31 10:21:04 -0700432TEST(DeltaPerformerTest, UsePublicKeyFromResponse) {
Alex Deymo8427b4a2014-11-05 14:00:32 -0800433 MockPrefs prefs;
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700434 FakeSystemState fake_system_state;
David Zeuthene7f89172013-10-31 10:21:04 -0700435 InstallPlan install_plan;
436 base::FilePath key_path;
437
438 // The result of the GetPublicKeyResponse() method is based on three things
439 //
440 // 1. Whether it's an official build; and
441 // 2. Whether the Public RSA key to be used is in the root filesystem; and
Alex Vakulenko072359c2014-07-18 11:41:07 -0700442 // 3. Whether the response has a public key
David Zeuthene7f89172013-10-31 10:21:04 -0700443 //
444 // We test all eight combinations to ensure that we only use the
445 // public key in the response if
446 //
447 // a. it's not an official build; and
448 // b. there is no key in the root filesystem.
449
450 DeltaPerformer *performer = new DeltaPerformer(&prefs,
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700451 &fake_system_state,
David Zeuthene7f89172013-10-31 10:21:04 -0700452 &install_plan);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700453 FakeHardware* fake_hardware = fake_system_state.fake_hardware();
David Zeuthene7f89172013-10-31 10:21:04 -0700454
455 string temp_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800456 EXPECT_TRUE(utils::MakeTempDirectory("PublicKeyFromResponseTests.XXXXXX",
David Zeuthene7f89172013-10-31 10:21:04 -0700457 &temp_dir));
458 string non_existing_file = temp_dir + "/non-existing";
459 string existing_file = temp_dir + "/existing";
Alex Vakulenko75039d72014-03-25 12:36:28 -0700460 EXPECT_EQ(0, System(base::StringPrintf("touch %s", existing_file.c_str())));
David Zeuthene7f89172013-10-31 10:21:04 -0700461
462 // Non-official build, non-existing public-key, key in response -> true
Don Garrett6646b442013-11-13 15:29:11 -0800463 fake_hardware->SetIsOfficialBuild(false);
David Zeuthene7f89172013-10-31 10:21:04 -0700464 performer->public_key_path_ = non_existing_file;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700465 install_plan.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
David Zeuthene7f89172013-10-31 10:21:04 -0700466 EXPECT_TRUE(performer->GetPublicKeyFromResponse(&key_path));
467 EXPECT_FALSE(key_path.empty());
468 EXPECT_EQ(unlink(key_path.value().c_str()), 0);
469 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800470 fake_hardware->SetIsOfficialBuild(true);
David Zeuthene7f89172013-10-31 10:21:04 -0700471 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
472
473 // Non-official build, existing public-key, key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800474 fake_hardware->SetIsOfficialBuild(false);
David Zeuthene7f89172013-10-31 10:21:04 -0700475 performer->public_key_path_ = existing_file;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700476 install_plan.public_key_rsa = "VGVzdAo="; // result of 'echo "Test" | base64'
David Zeuthene7f89172013-10-31 10:21:04 -0700477 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
478 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800479 fake_hardware->SetIsOfficialBuild(true);
David Zeuthene7f89172013-10-31 10:21:04 -0700480 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
481
482 // Non-official build, non-existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800483 fake_hardware->SetIsOfficialBuild(false);
David Zeuthene7f89172013-10-31 10:21:04 -0700484 performer->public_key_path_ = non_existing_file;
485 install_plan.public_key_rsa = "";
486 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
487 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800488 fake_hardware->SetIsOfficialBuild(true);
David Zeuthene7f89172013-10-31 10:21:04 -0700489 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
490
491 // Non-official build, existing public-key, no key in response -> false
Don Garrett6646b442013-11-13 15:29:11 -0800492 fake_hardware->SetIsOfficialBuild(false);
David Zeuthene7f89172013-10-31 10:21:04 -0700493 performer->public_key_path_ = existing_file;
494 install_plan.public_key_rsa = "";
495 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
496 // Same with official build -> false
Don Garrett6646b442013-11-13 15:29:11 -0800497 fake_hardware->SetIsOfficialBuild(true);
David Zeuthene7f89172013-10-31 10:21:04 -0700498 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
499
500 // Non-official build, non-existing public-key, key in response
501 // but invalid base64 -> false
Don Garrett6646b442013-11-13 15:29:11 -0800502 fake_hardware->SetIsOfficialBuild(false);
David Zeuthene7f89172013-10-31 10:21:04 -0700503 performer->public_key_path_ = non_existing_file;
504 install_plan.public_key_rsa = "not-valid-base64";
505 EXPECT_FALSE(performer->GetPublicKeyFromResponse(&key_path));
506
507 delete performer;
Alex Deymo10875d92014-11-10 21:52:57 -0800508 EXPECT_TRUE(test_utils::RecursiveUnlinkDir(temp_dir));
David Zeuthene7f89172013-10-31 10:21:04 -0700509}
510
Allie Wood78750a42015-02-11 15:42:11 -0800511TEST(DeltaPerformerTest, MinorVersionsMatch) {
512 // Test that the minor version in update_engine.conf that is installed to
513 // the image matches the supported delta minor version in the update engine.
514 uint32_t minor_version;
Alex Deymob42b98d2015-07-06 17:42:38 -0700515 chromeos::KeyValueStore store;
516 EXPECT_TRUE(store.Load(base::FilePath("update_engine.conf")));
517 EXPECT_TRUE(utils::GetMinorVersion(store, &minor_version));
518 EXPECT_EQ(DeltaPerformer::kSupportedMinorPayloadVersion, minor_version);
Allie Wood78750a42015-02-11 15:42:11 -0800519}
520
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700521} // namespace chromeos_update_engine