Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 17 | #include "update_engine/delta_performer.h" |
| 18 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 24 | #include <base/files/file_path.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 25 | #include <base/files/file_util.h> |
Alex Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 26 | #include <base/strings/string_number_conversions.h> |
Alex Deymo | f1cbe17 | 2015-03-05 15:58:37 -0800 | [diff] [blame] | 27 | #include <base/strings/string_util.h> |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 28 | #include <base/strings/stringprintf.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 29 | #include <google/protobuf/repeated_field.h> |
| 30 | #include <gtest/gtest.h> |
| 31 | |
Jay Srinivasan | d29695d | 2013-04-08 15:08:05 -0700 | [diff] [blame] | 32 | #include "update_engine/constants.h" |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 33 | #include "update_engine/fake_hardware.h" |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 34 | #include "update_engine/fake_prefs.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 35 | #include "update_engine/fake_system_state.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 36 | #include "update_engine/payload_constants.h" |
Alex Deymo | 0bc2611 | 2015-10-19 20:54:57 -0700 | [diff] [blame] | 37 | #include "update_engine/payload_generator/bzip.h" |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 38 | #include "update_engine/payload_generator/extent_ranges.h" |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 39 | #include "update_engine/payload_generator/payload_file.h" |
Alex Deymo | 923d8fa | 2014-07-15 17:58:51 -0700 | [diff] [blame] | 40 | #include "update_engine/payload_generator/payload_signer.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 41 | #include "update_engine/test_utils.h" |
| 42 | #include "update_engine/update_metadata.pb.h" |
| 43 | #include "update_engine/utils.h" |
| 44 | |
| 45 | namespace chromeos_update_engine { |
| 46 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 47 | using std::string; |
| 48 | using std::vector; |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 49 | using test_utils::System; |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 50 | using test_utils::kRandomString; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 51 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 52 | extern const char* kUnittestPrivateKeyPath; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 53 | extern const char* kUnittestPublicKeyPath; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 54 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 55 | static 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 62 | |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 63 | namespace { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 64 | // 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. |
| 67 | enum MetadataSignatureTest { |
| 68 | kEmptyMetadataSignature, |
| 69 | kInvalidMetadataSignature, |
| 70 | kValidMetadataSignature, |
| 71 | }; |
| 72 | |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 73 | // Compressed data without checksum, generated with: |
| 74 | // echo -n a | xz -9 --check=none | hexdump -v -e '" " 12/1 "0x%02x, " "\n"' |
| 75 | const 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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 83 | } // namespace |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 84 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 85 | class DeltaPerformerTest : public ::testing::Test { |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 86 | protected: |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 87 | void SetUp() override { |
| 88 | install_plan_.source_slot = 0; |
| 89 | install_plan_.target_slot = 1; |
| 90 | } |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 91 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 92 | // 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 Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 96 | // The install plan is for Full or Delta. |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 97 | install_plan_.is_full_update = full_payload; |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 98 | |
| 99 | // The Manifest we are validating. |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 100 | performer_.manifest_.CopyFrom(manifest); |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 101 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 102 | EXPECT_EQ(expected, performer_.ValidateManifest()); |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 105 | brillo::Blob GeneratePayload(const brillo::Blob& blob_data, |
| 106 | const vector<AnnotatedOperation>& aops, |
| 107 | bool sign_payload, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 108 | uint64_t major_version, |
| 109 | uint32_t minor_version) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 110 | 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 Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 118 | config.major_version = major_version; |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 119 | config.minor_version = minor_version; |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 120 | |
| 121 | PayloadFile payload; |
| 122 | EXPECT_TRUE(payload.Init(config)); |
| 123 | |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 124 | PartitionConfig old_part(kLegacyPartitionNameRoot); |
| 125 | PartitionConfig new_part(kLegacyPartitionNameRoot); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 126 | new_part.path = "/dev/zero"; |
| 127 | new_part.size = 1234; |
Sen Jiang | 981eb11 | 2015-08-25 17:03:18 -0700 | [diff] [blame] | 128 | |
| 129 | payload.AddPartition(old_part, new_part, aops); |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 130 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 131 | // 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 Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 137 | 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 142 | &install_plan_.metadata_size)); |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 143 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 144 | brillo::Blob payload_data; |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 145 | EXPECT_TRUE(utils::ReadFile(payload_path, &payload_data)); |
| 146 | return payload_data; |
| 147 | } |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 148 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 149 | // Apply |payload_data| on partition specified in |source_path|. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 150 | brillo::Blob ApplyPayload(const brillo::Blob& payload_data, |
| 151 | const string& source_path) { |
| 152 | return ApplyPayloadToData(payload_data, source_path, brillo::Blob()); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 153 | } |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 154 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 155 | // 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 159 | brillo::Blob ApplyPayloadToData(const brillo::Blob& payload_data, |
| 160 | const string& source_path, |
| 161 | const brillo::Blob& target_data) { |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 162 | string new_part; |
| 163 | EXPECT_TRUE(utils::MakeTempFile("Partition-XXXXXX", &new_part, nullptr)); |
| 164 | ScopedPathUnlinker partition_unlinker(new_part); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(utils::WriteFile(new_part.c_str(), target_data.data(), |
| 166 | target_data.size())); |
| 167 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 168 | // 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 178 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 179 | EXPECT_TRUE(performer_.Write(payload_data.data(), payload_data.size())); |
| 180 | EXPECT_EQ(0, performer_.Close()); |
| 181 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 182 | brillo::Blob partition_data; |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 183 | EXPECT_TRUE(utils::ReadFile(new_part, &partition_data)); |
| 184 | return partition_data; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 187 | // 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 196 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 197 | // 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 201 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 202 | 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 Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 214 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 215 | EXPECT_LT(performer_.Close(), 0); |
| 216 | } |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 217 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 218 | // 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 224 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 225 | // Loads the payload and parses the manifest. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 226 | brillo::Blob payload = GeneratePayload(brillo::Blob(), |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 227 | vector<AnnotatedOperation>(), sign_payload, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 228 | kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 229 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 230 | LOG(INFO) << "Payload size: " << payload.size(); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 231 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 232 | install_plan_.hash_checks_mandatory = hash_checks_mandatory; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 233 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 234 | 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 269 | expected_result = DeltaPerformer::kMetadataParseSuccess; |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 270 | expected_error = ErrorCode::kSuccess; |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 271 | } |
| 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 291 | void SetSupportedMajorVersion(uint64_t major_version) { |
| 292 | performer_.supported_major_version_ = major_version; |
| 293 | } |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 294 | FakePrefs prefs_; |
| 295 | InstallPlan install_plan_; |
| 296 | FakeSystemState fake_system_state_; |
| 297 | DeltaPerformer performer_{&prefs_, &fake_system_state_, &install_plan_}; |
| 298 | }; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 299 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 300 | TEST_F(DeltaPerformerTest, FullPayloadWriteTest) { |
| 301 | install_plan_.is_full_update = true; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 302 | brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString), |
| 303 | std::end(kRandomString)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 304 | 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 Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 312 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 313 | brillo::Blob payload_data = GeneratePayload(expected_data, aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 314 | kChromeOSMajorPayloadVersion, kFullPayloadMinorVersion); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 315 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 316 | EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null")); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 319 | TEST_F(DeltaPerformerTest, ReplaceOperationTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 320 | brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString), |
| 321 | std::end(kRandomString)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 322 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 331 | brillo::Blob payload_data = GeneratePayload(expected_data, aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 332 | kChromeOSMajorPayloadVersion, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 333 | kSourceMinorPayloadVersion); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 334 | |
| 335 | EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null")); |
| 336 | } |
| 337 | |
| 338 | TEST_F(DeltaPerformerTest, ReplaceBzOperationTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 339 | brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString), |
| 340 | std::end(kRandomString)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 341 | expected_data.resize(4096); // block size |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 342 | brillo::Blob bz_data; |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 343 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 353 | brillo::Blob payload_data = GeneratePayload(bz_data, aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 354 | kChromeOSMajorPayloadVersion, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 355 | kSourceMinorPayloadVersion); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 356 | |
| 357 | EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null")); |
| 358 | } |
| 359 | |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 360 | TEST_F(DeltaPerformerTest, ReplaceXzOperationTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 361 | brillo::Blob xz_data(std::begin(kXzCompressedData), |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 362 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 365 | brillo::Blob expected_data = brillo::Blob(4096, 0); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 366 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 375 | brillo::Blob payload_data = GeneratePayload(xz_data, aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 376 | kChromeOSMajorPayloadVersion, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 377 | kSourceMinorPayloadVersion); |
Alex Deymo | 2d621a3 | 2015-10-01 11:09:01 -0700 | [diff] [blame] | 378 | |
| 379 | EXPECT_EQ(expected_data, ApplyPayload(payload_data, "/dev/null")); |
| 380 | } |
| 381 | |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 382 | TEST_F(DeltaPerformerTest, ZeroOperationTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 383 | brillo::Blob existing_data = brillo::Blob(4096 * 10, 'a'); |
| 384 | brillo::Blob expected_data = existing_data; |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 385 | // 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 398 | brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 399 | kChromeOSMajorPayloadVersion, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 400 | kSourceMinorPayloadVersion); |
Alex Deymo | 79715ad | 2015-10-02 14:27:53 -0700 | [diff] [blame] | 401 | |
| 402 | EXPECT_EQ(expected_data, |
| 403 | ApplyPayloadToData(payload_data, "/dev/null", existing_data)); |
| 404 | } |
| 405 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 406 | TEST_F(DeltaPerformerTest, SourceCopyOperationTest) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 407 | brillo::Blob expected_data = brillo::Blob(std::begin(kRandomString), |
| 408 | std::end(kRandomString)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 409 | 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 417 | brillo::Blob payload_data = GeneratePayload(brillo::Blob(), aops, false, |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 418 | kChromeOSMajorPayloadVersion, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 419 | kSourceMinorPayloadVersion); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 420 | 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 | |
| 431 | TEST_F(DeltaPerformerTest, ExtentsToByteStringTest) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 432 | uint64_t test[] = {1, 1, 4, 2, 0, 1}; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 433 | COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven); |
| 434 | const uint64_t block_size = 4096; |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 435 | const uint64_t file_length = 4 * block_size - 13; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 436 | |
| 437 | google::protobuf::RepeatedPtrField<Extent> extents; |
| 438 | for (size_t i = 0; i < arraysize(test); i += 2) { |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 439 | *(extents.Add()) = ExtentForRange(test[i], test[i + 1]); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 442 | string expected_output = "4096:4096,16384:8192,0:4083"; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 443 | 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 Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 450 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 451 | TEST_F(DeltaPerformerTest, ValidateManifestFullGoodTest) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 452 | // The Manifest we are validating. |
| 453 | DeltaArchiveManifest manifest; |
| 454 | manifest.mutable_new_kernel_info(); |
| 455 | manifest.mutable_new_rootfs_info(); |
Alex Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 456 | manifest.set_minor_version(kFullPayloadMinorVersion); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 457 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 458 | RunManifestValidation(manifest, true, ErrorCode::kSuccess); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 461 | TEST_F(DeltaPerformerTest, ValidateManifestDeltaGoodTest) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 462 | // 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 470 | RunManifestValidation(manifest, false, ErrorCode::kSuccess); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 473 | TEST_F(DeltaPerformerTest, ValidateManifestFullUnsetMinorVersion) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 474 | // The Manifest we are validating. |
| 475 | DeltaArchiveManifest manifest; |
| 476 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 477 | RunManifestValidation(manifest, true, ErrorCode::kSuccess); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 480 | TEST_F(DeltaPerformerTest, ValidateManifestDeltaUnsetMinorVersion) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 481 | // The Manifest we are validating. |
| 482 | DeltaArchiveManifest manifest; |
| 483 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 484 | RunManifestValidation(manifest, false, |
| 485 | ErrorCode::kUnsupportedMinorPayloadVersion); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 488 | TEST_F(DeltaPerformerTest, ValidateManifestFullOldKernelTest) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 489 | // 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 496 | RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 499 | TEST_F(DeltaPerformerTest, ValidateManifestFullOldRootfsTest) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 500 | // 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 507 | RunManifestValidation(manifest, true, ErrorCode::kPayloadMismatchedType); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Sen Jiang | c8f6b7a | 2015-10-21 11:09:59 -0700 | [diff] [blame] | 510 | TEST_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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 521 | TEST_F(DeltaPerformerTest, ValidateManifestBadMinorVersion) { |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 522 | // 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 529 | RunManifestValidation(manifest, false, |
| 530 | ErrorCode::kUnsupportedMinorPayloadVersion); |
Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 533 | TEST_F(DeltaPerformerTest, BrilloMetadataSignatureSizeTest) { |
| 534 | SetSupportedMajorVersion(kBrilloMajorPayloadVersion); |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 535 | 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 Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame^] | 555 | EXPECT_EQ(manifest_offset + manifest_size, performer_.GetMetadataSize()); |
| 556 | EXPECT_EQ(metadata_signature_size, performer_.metadata_signature_size_); |
| 557 | } |
| 558 | |
| 559 | TEST_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 Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 577 | TEST_F(DeltaPerformerTest, BadDeltaMagicTest) { |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 578 | EXPECT_TRUE(performer_.Write("junk", 4)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 579 | EXPECT_FALSE(performer_.Write("morejunk", 8)); |
| 580 | EXPECT_LT(performer_.Close(), 0); |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 583 | TEST_F(DeltaPerformerTest, WriteUpdatesPayloadState) { |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 584 | EXPECT_CALL(*(fake_system_state_.mock_payload_state()), |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 585 | DownloadProgress(4)).Times(1); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 586 | EXPECT_CALL(*(fake_system_state_.mock_payload_state()), |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 587 | DownloadProgress(8)).Times(1); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 588 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 589 | EXPECT_TRUE(performer_.Write("junk", 4)); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 590 | EXPECT_FALSE(performer_.Write("morejunk", 8)); |
| 591 | EXPECT_LT(performer_.Close(), 0); |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 592 | } |
| 593 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 594 | TEST_F(DeltaPerformerTest, MissingMandatoryMetadataSizeTest) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 595 | DoMetadataSizeTest(0, 75456, true); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 598 | TEST_F(DeltaPerformerTest, MissingNonMandatoryMetadataSizeTest) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 599 | DoMetadataSizeTest(0, 123456, false); |
| 600 | } |
| 601 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 602 | TEST_F(DeltaPerformerTest, InvalidMandatoryMetadataSizeTest) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 603 | DoMetadataSizeTest(13000, 140000, true); |
| 604 | } |
| 605 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 606 | TEST_F(DeltaPerformerTest, InvalidNonMandatoryMetadataSizeTest) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 607 | DoMetadataSizeTest(40000, 50000, false); |
| 608 | } |
| 609 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 610 | TEST_F(DeltaPerformerTest, ValidMandatoryMetadataSizeTest) { |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 611 | DoMetadataSizeTest(85376, 85376, true); |
| 612 | } |
| 613 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 614 | TEST_F(DeltaPerformerTest, MandatoryEmptyMetadataSignatureTest) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 615 | DoMetadataSignatureTest(kEmptyMetadataSignature, true, true); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 618 | TEST_F(DeltaPerformerTest, NonMandatoryEmptyMetadataSignatureTest) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 619 | DoMetadataSignatureTest(kEmptyMetadataSignature, true, false); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 622 | TEST_F(DeltaPerformerTest, MandatoryInvalidMetadataSignatureTest) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 623 | DoMetadataSignatureTest(kInvalidMetadataSignature, true, true); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 626 | TEST_F(DeltaPerformerTest, NonMandatoryInvalidMetadataSignatureTest) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 627 | DoMetadataSignatureTest(kInvalidMetadataSignature, true, false); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 630 | TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature1Test) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 631 | DoMetadataSignatureTest(kValidMetadataSignature, false, true); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 634 | TEST_F(DeltaPerformerTest, MandatoryValidMetadataSignature2Test) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 635 | DoMetadataSignatureTest(kValidMetadataSignature, true, true); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 638 | TEST_F(DeltaPerformerTest, NonMandatoryValidMetadataSignatureTest) { |
Sen Jiang | d78b389 | 2015-09-25 15:19:31 -0700 | [diff] [blame] | 639 | DoMetadataSignatureTest(kValidMetadataSignature, true, false); |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 642 | TEST_F(DeltaPerformerTest, UsePublicKeyFromResponse) { |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 643 | 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 Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 649 | // 3. Whether the response has a public key |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 650 | // |
| 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 Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 657 | FakeHardware* fake_hardware = fake_system_state_.fake_hardware(); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 658 | |
| 659 | string temp_dir; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 660 | EXPECT_TRUE(utils::MakeTempDirectory("PublicKeyFromResponseTests.XXXXXX", |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 661 | &temp_dir)); |
| 662 | string non_existing_file = temp_dir + "/non-existing"; |
| 663 | string existing_file = temp_dir + "/existing"; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 664 | EXPECT_EQ(0, System(base::StringPrintf("touch %s", existing_file.c_str()))); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 665 | |
| 666 | // Non-official build, non-existing public-key, key in response -> true |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 667 | fake_hardware->SetIsOfficialBuild(false); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 668 | 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 Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 671 | EXPECT_FALSE(key_path.empty()); |
| 672 | EXPECT_EQ(unlink(key_path.value().c_str()), 0); |
| 673 | // Same with official build -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 674 | fake_hardware->SetIsOfficialBuild(true); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 675 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 676 | |
| 677 | // Non-official build, existing public-key, key in response -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 678 | fake_hardware->SetIsOfficialBuild(false); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 679 | 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 Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 682 | // Same with official build -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 683 | fake_hardware->SetIsOfficialBuild(true); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 684 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 685 | |
| 686 | // Non-official build, non-existing public-key, no key in response -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 687 | fake_hardware->SetIsOfficialBuild(false); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 688 | performer_.public_key_path_ = non_existing_file; |
| 689 | install_plan_.public_key_rsa = ""; |
| 690 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 691 | // Same with official build -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 692 | fake_hardware->SetIsOfficialBuild(true); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 693 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 694 | |
| 695 | // Non-official build, existing public-key, no key in response -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 696 | fake_hardware->SetIsOfficialBuild(false); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 697 | performer_.public_key_path_ = existing_file; |
| 698 | install_plan_.public_key_rsa = ""; |
| 699 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 700 | // Same with official build -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 701 | fake_hardware->SetIsOfficialBuild(true); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 702 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 703 | |
| 704 | // Non-official build, non-existing public-key, key in response |
| 705 | // but invalid base64 -> false |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 706 | fake_hardware->SetIsOfficialBuild(false); |
Sen Jiang | 2d528b4 | 2015-09-25 11:18:12 -0700 | [diff] [blame] | 707 | performer_.public_key_path_ = non_existing_file; |
| 708 | install_plan_.public_key_rsa = "not-valid-base64"; |
| 709 | EXPECT_FALSE(performer_.GetPublicKeyFromResponse(&key_path)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 710 | |
Alex Deymo | 110e030 | 2015-10-19 20:35:21 -0700 | [diff] [blame] | 711 | EXPECT_TRUE(base::DeleteFile(base::FilePath(temp_dir), true)); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Alex Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 714 | TEST_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 Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 717 | uint32_t minor_version; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 718 | brillo::KeyValueStore store; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 719 | 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 Deymo | cbf0989 | 2015-09-11 16:13:16 -0700 | [diff] [blame] | 722 | |
| 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 Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 730 | } // namespace chromeos_update_engine |