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