Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <sys/mount.h> |
| 6 | #include <inttypes.h> |
| 7 | |
| 8 | #include <algorithm> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 12 | #include <base/file_util.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 13 | #include <base/scoped_ptr.h> |
| 14 | #include <base/string_util.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 15 | #include <google/protobuf/repeated_field.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 18 | #include "update_engine/delta_diff_generator.h" |
| 19 | #include "update_engine/delta_performer.h" |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 20 | #include "update_engine/extent_ranges.h" |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 21 | #include "update_engine/full_update_generator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 22 | #include "update_engine/graph_types.h" |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 23 | #include "update_engine/payload_signer.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 24 | #include "update_engine/prefs_mock.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 25 | #include "update_engine/test_utils.h" |
| 26 | #include "update_engine/update_metadata.pb.h" |
| 27 | #include "update_engine/utils.h" |
| 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
| 31 | using std::min; |
| 32 | using std::string; |
| 33 | using std::vector; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 34 | using testing::_; |
| 35 | using testing::Return; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 36 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 37 | extern const char* kUnittestPrivateKeyPath; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 38 | extern const char* kUnittestPublicKeyPath; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 39 | |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 40 | namespace { |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame^] | 41 | const size_t kBlockSize = 4096; |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 42 | } // namespace {} |
| 43 | |
| 44 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 45 | class DeltaPerformerTest : public ::testing::Test { }; |
| 46 | |
| 47 | TEST(DeltaPerformerTest, ExtentsToByteStringTest) { |
| 48 | uint64_t test[] = {1, 1, 4, 2, kSparseHole, 1, 0, 1}; |
| 49 | COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven); |
| 50 | const uint64_t block_size = 4096; |
| 51 | const uint64_t file_length = 5 * block_size - 13; |
| 52 | |
| 53 | google::protobuf::RepeatedPtrField<Extent> extents; |
| 54 | for (size_t i = 0; i < arraysize(test); i += 2) { |
| 55 | Extent* extent = extents.Add(); |
| 56 | extent->set_start_block(test[i]); |
| 57 | extent->set_num_blocks(test[i + 1]); |
| 58 | } |
| 59 | |
| 60 | string expected_output = "4096:4096,16384:8192,-1:4096,0:4083"; |
| 61 | string actual_output; |
| 62 | EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents, |
| 63 | block_size, |
| 64 | file_length, |
| 65 | &actual_output)); |
| 66 | EXPECT_EQ(expected_output, actual_output); |
| 67 | } |
| 68 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 69 | void CompareFilesByBlock(const string& a_file, const string& b_file) { |
| 70 | vector<char> a_data, b_data; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 71 | EXPECT_TRUE(utils::ReadFile(a_file, &a_data)) << "file failed: " << a_file; |
| 72 | EXPECT_TRUE(utils::ReadFile(b_file, &b_data)) << "file failed: " << b_file; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 73 | |
| 74 | EXPECT_EQ(a_data.size(), b_data.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 75 | EXPECT_EQ(0, a_data.size() % kBlockSize); |
| 76 | for (size_t i = 0; i < a_data.size(); i += kBlockSize) { |
| 77 | EXPECT_EQ(0, i % kBlockSize); |
| 78 | vector<char> a_sub(&a_data[i], &a_data[i + kBlockSize]); |
| 79 | vector<char> b_sub(&b_data[i], &b_data[i + kBlockSize]); |
| 80 | EXPECT_TRUE(a_sub == b_sub) << "Block " << (i/kBlockSize) << " differs"; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | namespace { |
| 85 | bool WriteSparseFile(const string& path, off_t size) { |
| 86 | int fd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 87 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 88 | ScopedFdCloser fd_closer(&fd); |
| 89 | off_t rc = lseek(fd, size + 1, SEEK_SET); |
| 90 | TEST_AND_RETURN_FALSE_ERRNO(rc != static_cast<off_t>(-1)); |
| 91 | int return_code = ftruncate(fd, size); |
| 92 | TEST_AND_RETURN_FALSE_ERRNO(return_code == 0); |
| 93 | return true; |
| 94 | } |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 95 | } // namespace {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 96 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 97 | namespace { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 98 | enum SignatureTest { |
| 99 | kSignatureNone, // No payload signing. |
| 100 | kSignatureGenerator, // Sign the payload at generation time. |
| 101 | kSignatureGenerated, // Sign the payload after it's generated. |
| 102 | kSignatureGeneratedShell, // Sign the generated payload through shell cmds. |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 103 | kSignatureGeneratedShellBadKey, // Sign with a bad key through shell cmds. |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 106 | size_t GetSignatureSize(const string& private_key_path) { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 107 | const vector<char> data(1, 'x'); |
| 108 | vector<char> hash; |
| 109 | EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(data, &hash)); |
| 110 | vector<char> signature; |
| 111 | EXPECT_TRUE(PayloadSigner::SignHash(hash, |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 112 | private_key_path, |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 113 | &signature)); |
| 114 | return signature.size(); |
| 115 | } |
| 116 | |
| 117 | void SignGeneratedPayload(const string& payload_path) { |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 118 | int signature_size = GetSignatureSize(kUnittestPrivateKeyPath); |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 119 | vector<char> hash; |
| 120 | ASSERT_TRUE(PayloadSigner::HashPayloadForSigning(payload_path, |
| 121 | signature_size, |
| 122 | &hash)); |
| 123 | vector<char> signature; |
| 124 | ASSERT_TRUE(PayloadSigner::SignHash(hash, |
| 125 | kUnittestPrivateKeyPath, |
| 126 | &signature)); |
| 127 | ASSERT_TRUE(PayloadSigner::AddSignatureToPayload(payload_path, |
| 128 | signature, |
| 129 | payload_path)); |
| 130 | EXPECT_TRUE(PayloadSigner::VerifySignedPayload(payload_path, |
| 131 | kUnittestPublicKeyPath)); |
| 132 | } |
| 133 | |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 134 | void SignGeneratedShellPayload(SignatureTest signature_test, |
| 135 | const string& payload_path) { |
| 136 | string private_key_path = kUnittestPrivateKeyPath; |
| 137 | if (signature_test == kSignatureGeneratedShellBadKey) { |
| 138 | ASSERT_TRUE(utils::MakeTempFile("/tmp/key.XXXXXX", |
| 139 | &private_key_path, |
| 140 | NULL)); |
| 141 | } else { |
| 142 | ASSERT_EQ(kSignatureGeneratedShell, signature_test); |
| 143 | } |
| 144 | ScopedPathUnlinker key_unlinker(private_key_path); |
| 145 | key_unlinker.set_should_remove(signature_test == |
| 146 | kSignatureGeneratedShellBadKey); |
| 147 | // Generates a new private key that will not match the public key. |
| 148 | if (signature_test == kSignatureGeneratedShellBadKey) { |
| 149 | LOG(INFO) << "Generating a mismatched private key."; |
| 150 | ASSERT_EQ(0, |
| 151 | System(StringPrintf( |
| 152 | "/usr/bin/openssl genrsa -out %s 1024", |
| 153 | private_key_path.c_str()))); |
| 154 | } |
| 155 | int signature_size = GetSignatureSize(private_key_path); |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 156 | string hash_file; |
| 157 | ASSERT_TRUE(utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_file, NULL)); |
| 158 | ScopedPathUnlinker hash_unlinker(hash_file); |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 159 | ASSERT_EQ(0, |
| 160 | System(StringPrintf( |
| 161 | "./delta_generator -in_file %s -signature_size %d " |
| 162 | "-out_hash_file %s", |
| 163 | payload_path.c_str(), |
| 164 | signature_size, |
| 165 | hash_file.c_str()))); |
| 166 | |
| 167 | string sig_file; |
| 168 | ASSERT_TRUE(utils::MakeTempFile("/tmp/signature.XXXXXX", &sig_file, NULL)); |
| 169 | ScopedPathUnlinker sig_unlinker(sig_file); |
| 170 | ASSERT_EQ(0, |
| 171 | System(StringPrintf( |
| 172 | "/usr/bin/openssl rsautl -pkcs -sign -inkey %s -in %s -out %s", |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 173 | private_key_path.c_str(), |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 174 | hash_file.c_str(), |
| 175 | sig_file.c_str()))); |
| 176 | ASSERT_EQ(0, |
| 177 | System(StringPrintf( |
| 178 | "./delta_generator -in_file %s -signature_file %s " |
| 179 | "-out_file %s", |
| 180 | payload_path.c_str(), |
| 181 | sig_file.c_str(), |
| 182 | payload_path.c_str()))); |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 183 | int verify_result = |
| 184 | System(StringPrintf("./delta_generator -in_file %s -public_key %s", |
| 185 | payload_path.c_str(), |
| 186 | kUnittestPublicKeyPath)); |
| 187 | if (signature_test == kSignatureGeneratedShellBadKey) { |
| 188 | ASSERT_NE(0, verify_result); |
| 189 | } else { |
| 190 | ASSERT_EQ(0, verify_result); |
| 191 | } |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 194 | void DoSmallImageTest(bool full_kernel, bool full_rootfs, bool noop, |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 195 | SignatureTest signature_test) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 196 | string a_img, b_img; |
| 197 | EXPECT_TRUE(utils::MakeTempFile("/tmp/a_img.XXXXXX", &a_img, NULL)); |
| 198 | ScopedPathUnlinker a_img_unlinker(a_img); |
| 199 | EXPECT_TRUE(utils::MakeTempFile("/tmp/b_img.XXXXXX", &b_img, NULL)); |
| 200 | ScopedPathUnlinker b_img_unlinker(b_img); |
| 201 | |
| 202 | CreateExtImageAtPath(a_img, NULL); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 203 | |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 204 | int image_size = static_cast<int>(utils::FileSize(a_img)); |
| 205 | |
| 206 | // Extend the "partitions" holding the file system a bit. |
| 207 | EXPECT_EQ(0, System(base::StringPrintf( |
| 208 | "dd if=/dev/zero of=%s seek=%d bs=1 count=1", |
| 209 | a_img.c_str(), |
| 210 | image_size + 1024 * 1024 - 1))); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 211 | EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(a_img)); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 212 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 213 | // Make some changes to the A image. |
| 214 | { |
| 215 | string a_mnt; |
| 216 | ScopedLoopMounter b_mounter(a_img, &a_mnt, 0); |
| 217 | |
| 218 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
| 219 | a_mnt.c_str()).c_str(), |
| 220 | reinterpret_cast<const char*>(kRandomString), |
| 221 | sizeof(kRandomString) - 1)); |
| 222 | // Write 1 MiB of 0xff to try to catch the case where writing a bsdiff |
| 223 | // patch fails to zero out the final block. |
| 224 | vector<char> ones(1024 * 1024, 0xff); |
| 225 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/ones", |
| 226 | a_mnt.c_str()).c_str(), |
| 227 | &ones[0], |
| 228 | ones.size())); |
| 229 | } |
| 230 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 231 | if (noop) { |
| 232 | EXPECT_TRUE(file_util::CopyFile(FilePath(a_img), FilePath(b_img))); |
| 233 | } else { |
| 234 | CreateExtImageAtPath(b_img, NULL); |
| 235 | EXPECT_EQ(0, System(base::StringPrintf( |
| 236 | "dd if=/dev/zero of=%s seek=%d bs=1 count=1", |
| 237 | b_img.c_str(), |
| 238 | image_size + 1024 * 1024 - 1))); |
| 239 | EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(b_img)); |
| 240 | |
| 241 | // Make some changes to the B image. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 242 | string b_mnt; |
| 243 | ScopedLoopMounter b_mounter(b_img, &b_mnt, 0); |
| 244 | |
| 245 | EXPECT_EQ(0, system(StringPrintf("cp %s/hello %s/hello2", b_mnt.c_str(), |
| 246 | b_mnt.c_str()).c_str())); |
| 247 | EXPECT_EQ(0, system(StringPrintf("rm %s/hello", b_mnt.c_str()).c_str())); |
| 248 | EXPECT_EQ(0, system(StringPrintf("mv %s/hello2 %s/hello", b_mnt.c_str(), |
| 249 | b_mnt.c_str()).c_str())); |
| 250 | EXPECT_EQ(0, system(StringPrintf("echo foo > %s/foo", |
| 251 | b_mnt.c_str()).c_str())); |
| 252 | EXPECT_EQ(0, system(StringPrintf("touch %s/emptyfile", |
| 253 | b_mnt.c_str()).c_str())); |
| 254 | EXPECT_TRUE(WriteSparseFile(StringPrintf("%s/fullsparse", b_mnt.c_str()), |
| 255 | 1024 * 1024)); |
| 256 | EXPECT_EQ(0, system(StringPrintf("dd if=/dev/zero of=%s/partsparese bs=1 " |
| 257 | "seek=4096 count=1", |
| 258 | b_mnt.c_str()).c_str())); |
Andrew de los Reyes | 29da8aa | 2011-02-15 13:34:57 -0800 | [diff] [blame^] | 259 | EXPECT_EQ(0, system(StringPrintf("cp %s/srchardlink0 %s/tmp && " |
| 260 | "mv %s/tmp %s/srchardlink1", |
| 261 | b_mnt.c_str(), b_mnt.c_str(), |
| 262 | b_mnt.c_str(), b_mnt.c_str()).c_str())); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 263 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
| 264 | b_mnt.c_str()).c_str(), |
| 265 | reinterpret_cast<const char*>(kRandomString), |
| 266 | sizeof(kRandomString))); |
| 267 | } |
| 268 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 269 | string old_kernel; |
| 270 | EXPECT_TRUE(utils::MakeTempFile("/tmp/old_kernel.XXXXXX", &old_kernel, NULL)); |
| 271 | ScopedPathUnlinker old_kernel_unlinker(old_kernel); |
| 272 | |
| 273 | string new_kernel; |
| 274 | EXPECT_TRUE(utils::MakeTempFile("/tmp/new_kernel.XXXXXX", &new_kernel, NULL)); |
| 275 | ScopedPathUnlinker new_kernel_unlinker(new_kernel); |
| 276 | |
| 277 | vector<char> old_kernel_data(4096); // Something small for a test |
| 278 | vector<char> new_kernel_data(old_kernel_data.size()); |
| 279 | FillWithData(&old_kernel_data); |
| 280 | FillWithData(&new_kernel_data); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 281 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 282 | // change the new kernel data |
| 283 | const char* new_data_string = "This is new data."; |
| 284 | strcpy(&new_kernel_data[0], new_data_string); |
| 285 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 286 | if (noop) { |
| 287 | old_kernel_data = new_kernel_data; |
| 288 | } |
| 289 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 290 | // Write kernels to disk |
| 291 | EXPECT_TRUE(utils::WriteFile( |
| 292 | old_kernel.c_str(), &old_kernel_data[0], old_kernel_data.size())); |
| 293 | EXPECT_TRUE(utils::WriteFile( |
| 294 | new_kernel.c_str(), &new_kernel_data[0], new_kernel_data.size())); |
| 295 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 296 | string delta_path; |
| 297 | EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 298 | LOG(INFO) << "delta path: " << delta_path; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 299 | ScopedPathUnlinker delta_path_unlinker(delta_path); |
| 300 | { |
| 301 | string a_mnt, b_mnt; |
| 302 | ScopedLoopMounter a_mounter(a_img, &a_mnt, MS_RDONLY); |
| 303 | ScopedLoopMounter b_mounter(b_img, &b_mnt, MS_RDONLY); |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 304 | const string private_key = |
| 305 | signature_test == kSignatureGenerator ? kUnittestPrivateKeyPath : ""; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 306 | EXPECT_TRUE( |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 307 | DeltaDiffGenerator::GenerateDeltaUpdateFile( |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 308 | full_rootfs ? "" : a_mnt, |
| 309 | full_rootfs ? "" : a_img, |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 310 | b_mnt, |
| 311 | b_img, |
| 312 | full_kernel ? "" : old_kernel, |
| 313 | new_kernel, |
| 314 | delta_path, |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 315 | private_key)); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 318 | if (signature_test == kSignatureGenerated) { |
| 319 | SignGeneratedPayload(delta_path); |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 320 | } else if (signature_test == kSignatureGeneratedShell || |
| 321 | signature_test == kSignatureGeneratedShellBadKey) { |
| 322 | SignGeneratedShellPayload(signature_test, delta_path); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Read delta into memory. |
| 326 | vector<char> delta; |
| 327 | EXPECT_TRUE(utils::ReadFile(delta_path, &delta)); |
| 328 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 329 | uint64_t manifest_metadata_size; |
| 330 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 331 | // Check the metadata. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 332 | { |
| 333 | LOG(INFO) << "delta size: " << delta.size(); |
| 334 | DeltaArchiveManifest manifest; |
| 335 | const int kManifestSizeOffset = 12; |
| 336 | const int kManifestOffset = 20; |
| 337 | uint64_t manifest_size = 0; |
| 338 | memcpy(&manifest_size, &delta[kManifestSizeOffset], sizeof(manifest_size)); |
| 339 | manifest_size = be64toh(manifest_size); |
| 340 | LOG(INFO) << "manifest size: " << manifest_size; |
| 341 | EXPECT_TRUE(manifest.ParseFromArray(&delta[kManifestOffset], |
| 342 | manifest_size)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 343 | manifest_metadata_size = kManifestOffset + manifest_size; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 344 | |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 345 | if (signature_test == kSignatureNone) { |
| 346 | EXPECT_FALSE(manifest.has_signatures_offset()); |
| 347 | EXPECT_FALSE(manifest.has_signatures_size()); |
| 348 | } else { |
| 349 | EXPECT_TRUE(manifest.has_signatures_offset()); |
| 350 | EXPECT_TRUE(manifest.has_signatures_size()); |
| 351 | Signatures sigs_message; |
| 352 | EXPECT_TRUE(sigs_message.ParseFromArray( |
| 353 | &delta[manifest_metadata_size + manifest.signatures_offset()], |
| 354 | manifest.signatures_size())); |
| 355 | EXPECT_EQ(1, sigs_message.signatures_size()); |
| 356 | const Signatures_Signature& signature = sigs_message.signatures(0); |
| 357 | EXPECT_EQ(1, signature.version()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 358 | |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 359 | uint64_t expected_sig_data_length = 0; |
| 360 | EXPECT_TRUE(PayloadSigner::SignatureBlobLength( |
| 361 | kUnittestPrivateKeyPath, &expected_sig_data_length)); |
| 362 | EXPECT_EQ(expected_sig_data_length, manifest.signatures_size()); |
| 363 | EXPECT_FALSE(signature.data().empty()); |
| 364 | } |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 365 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 366 | if (noop) { |
| 367 | EXPECT_EQ(1, manifest.install_operations_size()); |
| 368 | EXPECT_EQ(1, manifest.kernel_install_operations_size()); |
| 369 | } |
| 370 | |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 371 | if (full_kernel) { |
| 372 | EXPECT_FALSE(manifest.has_old_kernel_info()); |
| 373 | } else { |
| 374 | EXPECT_EQ(old_kernel_data.size(), manifest.old_kernel_info().size()); |
| 375 | EXPECT_FALSE(manifest.old_kernel_info().hash().empty()); |
| 376 | } |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 377 | |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 378 | if (full_rootfs) { |
| 379 | EXPECT_FALSE(manifest.has_old_rootfs_info()); |
| 380 | } else { |
| 381 | EXPECT_EQ(image_size, manifest.old_rootfs_info().size()); |
| 382 | EXPECT_FALSE(manifest.old_rootfs_info().hash().empty()); |
| 383 | } |
| 384 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 385 | EXPECT_EQ(new_kernel_data.size(), manifest.new_kernel_info().size()); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 386 | EXPECT_EQ(image_size, manifest.new_rootfs_info().size()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 387 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 388 | EXPECT_FALSE(manifest.new_kernel_info().hash().empty()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 389 | EXPECT_FALSE(manifest.new_rootfs_info().hash().empty()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 392 | PrefsMock prefs; |
| 393 | EXPECT_CALL(prefs, SetInt64(kPrefsManifestMetadataSize, |
| 394 | manifest_metadata_size)).WillOnce(Return(true)); |
| 395 | EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextOperation, _)) |
| 396 | .WillRepeatedly(Return(true)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 397 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _)) |
| 398 | .WillOnce(Return(false)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 399 | EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextDataOffset, _)) |
| 400 | .WillRepeatedly(Return(true)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 401 | EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSHA256Context, _)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 402 | .WillRepeatedly(Return(true)); |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 403 | if (signature_test != kSignatureNone) { |
| 404 | EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSignedSHA256Context, _)) |
| 405 | .WillOnce(Return(true)); |
| 406 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 407 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 408 | // Update the A image in place. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 409 | DeltaPerformer performer(&prefs); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 410 | |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 411 | vector<char> rootfs_hash; |
| 412 | EXPECT_EQ(image_size, |
| 413 | OmahaHashCalculator::RawHashOfFile(a_img, |
| 414 | image_size, |
| 415 | &rootfs_hash)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 416 | performer.set_current_rootfs_hash(rootfs_hash); |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 417 | vector<char> kernel_hash; |
| 418 | EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(old_kernel_data, |
| 419 | &kernel_hash)); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 420 | performer.set_current_kernel_hash(kernel_hash); |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 421 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 422 | EXPECT_EQ(0, performer.Open(a_img.c_str(), 0, 0)); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 423 | EXPECT_TRUE(performer.OpenKernel(old_kernel.c_str())); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 424 | |
| 425 | // Write at some number of bytes per operation. Arbitrarily chose 5. |
| 426 | const size_t kBytesPerWrite = 5; |
| 427 | for (size_t i = 0; i < delta.size(); i += kBytesPerWrite) { |
| 428 | size_t count = min(delta.size() - i, kBytesPerWrite); |
| 429 | EXPECT_EQ(count, performer.Write(&delta[i], count)); |
| 430 | } |
| 431 | |
| 432 | // Wrapper around close. Returns 0 on success or -errno on error. |
| 433 | EXPECT_EQ(0, performer.Close()); |
| 434 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 435 | CompareFilesByBlock(old_kernel, new_kernel); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 436 | CompareFilesByBlock(a_img, b_img); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 437 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 438 | vector<char> updated_kernel_partition; |
| 439 | EXPECT_TRUE(utils::ReadFile(old_kernel, &updated_kernel_partition)); |
| 440 | EXPECT_EQ(0, strncmp(&updated_kernel_partition[0], new_data_string, |
| 441 | strlen(new_data_string))); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 442 | |
| 443 | EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath)); |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 444 | bool expect_verify_success = |
| 445 | signature_test != kSignatureNone && |
| 446 | signature_test != kSignatureGeneratedShellBadKey; |
| 447 | EXPECT_EQ(expect_verify_success, |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 448 | performer.VerifyPayload( |
| 449 | kUnittestPublicKeyPath, |
| 450 | OmahaHashCalculator::OmahaHashOfData(delta), |
| 451 | delta.size())); |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 452 | EXPECT_TRUE(performer.VerifyPayload( |
| 453 | "/public/key/does/not/exists", |
| 454 | OmahaHashCalculator::OmahaHashOfData(delta), |
| 455 | delta.size())); |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 456 | |
| 457 | uint64_t new_kernel_size; |
| 458 | vector<char> new_kernel_hash; |
| 459 | uint64_t new_rootfs_size; |
| 460 | vector<char> new_rootfs_hash; |
| 461 | EXPECT_TRUE(performer.GetNewPartitionInfo(&new_kernel_size, |
| 462 | &new_kernel_hash, |
| 463 | &new_rootfs_size, |
| 464 | &new_rootfs_hash)); |
| 465 | EXPECT_EQ(4096, new_kernel_size); |
| 466 | vector<char> expected_new_kernel_hash; |
| 467 | EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(new_kernel_data, |
| 468 | &expected_new_kernel_hash)); |
| 469 | EXPECT_TRUE(expected_new_kernel_hash == new_kernel_hash); |
| 470 | EXPECT_EQ(image_size, new_rootfs_size); |
| 471 | vector<char> expected_new_rootfs_hash; |
| 472 | EXPECT_EQ(image_size, |
| 473 | OmahaHashCalculator::RawHashOfFile(b_img, |
| 474 | image_size, |
| 475 | &expected_new_rootfs_hash)); |
| 476 | EXPECT_TRUE(expected_new_rootfs_hash == new_rootfs_hash); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 477 | } |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | TEST(DeltaPerformerTest, RunAsRootSmallImageTest) { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 481 | DoSmallImageTest(false, false, false, kSignatureGenerator); |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | TEST(DeltaPerformerTest, RunAsRootFullKernelSmallImageTest) { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 485 | DoSmallImageTest(true, false, false, kSignatureGenerator); |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST(DeltaPerformerTest, RunAsRootFullSmallImageTest) { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 489 | DoSmallImageTest(true, true, false, kSignatureGenerator); |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST(DeltaPerformerTest, RunAsRootNoopSmallImageTest) { |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 493 | DoSmallImageTest(false, false, true, kSignatureGenerator); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Darin Petkov | cbfb017 | 2011-01-14 15:24:45 -0800 | [diff] [blame] | 496 | TEST(DeltaPerformerTest, RunAsRootSmallImageSignNoneTest) { |
| 497 | DoSmallImageTest(false, false, false, kSignatureNone); |
| 498 | } |
| 499 | |
| 500 | TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedTest) { |
| 501 | DoSmallImageTest(false, false, false, kSignatureGenerated); |
| 502 | } |
| 503 | |
| 504 | TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellTest) { |
| 505 | DoSmallImageTest(false, false, false, kSignatureGeneratedShell); |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Darin Petkov | 52dcaeb | 2011-01-14 15:33:06 -0800 | [diff] [blame] | 508 | TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellBadKeyTest) { |
| 509 | DoSmallImageTest(false, false, false, kSignatureGeneratedShellBadKey); |
| 510 | } |
| 511 | |
Darin Petkov | 934bb41 | 2010-11-18 11:21:35 -0800 | [diff] [blame] | 512 | TEST(DeltaPerformerTest, BadDeltaMagicTest) { |
| 513 | PrefsMock prefs; |
| 514 | DeltaPerformer performer(&prefs); |
| 515 | EXPECT_EQ(0, performer.Open("/dev/null", 0, 0)); |
| 516 | EXPECT_TRUE(performer.OpenKernel("/dev/null")); |
| 517 | EXPECT_EQ(4, performer.Write("junk", 4)); |
| 518 | EXPECT_EQ(8, performer.Write("morejunk", 8)); |
| 519 | EXPECT_LT(performer.Write("morejunk", 8), 0); |
| 520 | EXPECT_LT(performer.Close(), 0); |
| 521 | } |
| 522 | |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 523 | TEST(DeltaPerformerTest, IsIdempotentOperationTest) { |
| 524 | DeltaArchiveManifest_InstallOperation op; |
| 525 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 526 | *(op.add_dst_extents()) = ExtentForRange(0, 5); |
| 527 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 528 | *(op.add_src_extents()) = ExtentForRange(4, 1); |
| 529 | EXPECT_FALSE(DeltaPerformer::IsIdempotentOperation(op)); |
| 530 | op.clear_src_extents(); |
| 531 | *(op.add_src_extents()) = ExtentForRange(5, 3); |
| 532 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 533 | *(op.add_dst_extents()) = ExtentForRange(20, 6); |
| 534 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 535 | *(op.add_src_extents()) = ExtentForRange(19, 2); |
| 536 | EXPECT_FALSE(DeltaPerformer::IsIdempotentOperation(op)); |
| 537 | } |
| 538 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 539 | } // namespace chromeos_update_engine |