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 { |
| 41 | const size_t kBlockSize = 4096; |
| 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 | |
| 69 | class ScopedLoopMounter { |
| 70 | public: |
| 71 | explicit ScopedLoopMounter(const string& file_path, string* mnt_path, |
| 72 | unsigned long flags) { |
| 73 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/mnt.XXXXXX", mnt_path)); |
| 74 | dir_remover_.reset(new ScopedDirRemover(*mnt_path)); |
| 75 | |
| 76 | string loop_dev = GetUnusedLoopDevice(); |
| 77 | EXPECT_EQ(0, system(StringPrintf("losetup %s %s", loop_dev.c_str(), |
| 78 | file_path.c_str()).c_str())); |
| 79 | loop_releaser_.reset(new ScopedLoopbackDeviceReleaser(loop_dev)); |
| 80 | |
| 81 | EXPECT_TRUE(utils::MountFilesystem(loop_dev, *mnt_path, flags)); |
| 82 | unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path)); |
| 83 | } |
| 84 | private: |
| 85 | scoped_ptr<ScopedDirRemover> dir_remover_; |
| 86 | scoped_ptr<ScopedLoopbackDeviceReleaser> loop_releaser_; |
| 87 | scoped_ptr<ScopedFilesystemUnmounter> unmounter_; |
| 88 | }; |
| 89 | |
| 90 | void CompareFilesByBlock(const string& a_file, const string& b_file) { |
| 91 | vector<char> a_data, b_data; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 92 | EXPECT_TRUE(utils::ReadFile(a_file, &a_data)) << "file failed: " << a_file; |
| 93 | 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] | 94 | |
| 95 | EXPECT_EQ(a_data.size(), b_data.size()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 96 | EXPECT_EQ(0, a_data.size() % kBlockSize); |
| 97 | for (size_t i = 0; i < a_data.size(); i += kBlockSize) { |
| 98 | EXPECT_EQ(0, i % kBlockSize); |
| 99 | vector<char> a_sub(&a_data[i], &a_data[i + kBlockSize]); |
| 100 | vector<char> b_sub(&b_data[i], &b_data[i + kBlockSize]); |
| 101 | EXPECT_TRUE(a_sub == b_sub) << "Block " << (i/kBlockSize) << " differs"; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | namespace { |
| 106 | bool WriteSparseFile(const string& path, off_t size) { |
| 107 | int fd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644); |
| 108 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 109 | ScopedFdCloser fd_closer(&fd); |
| 110 | off_t rc = lseek(fd, size + 1, SEEK_SET); |
| 111 | TEST_AND_RETURN_FALSE_ERRNO(rc != static_cast<off_t>(-1)); |
| 112 | int return_code = ftruncate(fd, size); |
| 113 | TEST_AND_RETURN_FALSE_ERRNO(return_code == 0); |
| 114 | return true; |
| 115 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 116 | |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 117 | void DoSmallImageTest(bool full_kernel, bool full_rootfs, bool noop) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 118 | string a_img, b_img; |
| 119 | EXPECT_TRUE(utils::MakeTempFile("/tmp/a_img.XXXXXX", &a_img, NULL)); |
| 120 | ScopedPathUnlinker a_img_unlinker(a_img); |
| 121 | EXPECT_TRUE(utils::MakeTempFile("/tmp/b_img.XXXXXX", &b_img, NULL)); |
| 122 | ScopedPathUnlinker b_img_unlinker(b_img); |
| 123 | |
| 124 | CreateExtImageAtPath(a_img, NULL); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 125 | |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 126 | int image_size = static_cast<int>(utils::FileSize(a_img)); |
| 127 | |
| 128 | // Extend the "partitions" holding the file system a bit. |
| 129 | EXPECT_EQ(0, System(base::StringPrintf( |
| 130 | "dd if=/dev/zero of=%s seek=%d bs=1 count=1", |
| 131 | a_img.c_str(), |
| 132 | image_size + 1024 * 1024 - 1))); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 133 | EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(a_img)); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 134 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 135 | // Make some changes to the A image. |
| 136 | { |
| 137 | string a_mnt; |
| 138 | ScopedLoopMounter b_mounter(a_img, &a_mnt, 0); |
| 139 | |
| 140 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
| 141 | a_mnt.c_str()).c_str(), |
| 142 | reinterpret_cast<const char*>(kRandomString), |
| 143 | sizeof(kRandomString) - 1)); |
| 144 | // Write 1 MiB of 0xff to try to catch the case where writing a bsdiff |
| 145 | // patch fails to zero out the final block. |
| 146 | vector<char> ones(1024 * 1024, 0xff); |
| 147 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/ones", |
| 148 | a_mnt.c_str()).c_str(), |
| 149 | &ones[0], |
| 150 | ones.size())); |
| 151 | } |
| 152 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 153 | if (noop) { |
| 154 | EXPECT_TRUE(file_util::CopyFile(FilePath(a_img), FilePath(b_img))); |
| 155 | } else { |
| 156 | CreateExtImageAtPath(b_img, NULL); |
| 157 | EXPECT_EQ(0, System(base::StringPrintf( |
| 158 | "dd if=/dev/zero of=%s seek=%d bs=1 count=1", |
| 159 | b_img.c_str(), |
| 160 | image_size + 1024 * 1024 - 1))); |
| 161 | EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(b_img)); |
| 162 | |
| 163 | // Make some changes to the B image. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 164 | string b_mnt; |
| 165 | ScopedLoopMounter b_mounter(b_img, &b_mnt, 0); |
| 166 | |
| 167 | EXPECT_EQ(0, system(StringPrintf("cp %s/hello %s/hello2", b_mnt.c_str(), |
| 168 | b_mnt.c_str()).c_str())); |
| 169 | EXPECT_EQ(0, system(StringPrintf("rm %s/hello", b_mnt.c_str()).c_str())); |
| 170 | EXPECT_EQ(0, system(StringPrintf("mv %s/hello2 %s/hello", b_mnt.c_str(), |
| 171 | b_mnt.c_str()).c_str())); |
| 172 | EXPECT_EQ(0, system(StringPrintf("echo foo > %s/foo", |
| 173 | b_mnt.c_str()).c_str())); |
| 174 | EXPECT_EQ(0, system(StringPrintf("touch %s/emptyfile", |
| 175 | b_mnt.c_str()).c_str())); |
| 176 | EXPECT_TRUE(WriteSparseFile(StringPrintf("%s/fullsparse", b_mnt.c_str()), |
| 177 | 1024 * 1024)); |
| 178 | EXPECT_EQ(0, system(StringPrintf("dd if=/dev/zero of=%s/partsparese bs=1 " |
| 179 | "seek=4096 count=1", |
| 180 | b_mnt.c_str()).c_str())); |
| 181 | EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress", |
| 182 | b_mnt.c_str()).c_str(), |
| 183 | reinterpret_cast<const char*>(kRandomString), |
| 184 | sizeof(kRandomString))); |
| 185 | } |
| 186 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 187 | string old_kernel; |
| 188 | EXPECT_TRUE(utils::MakeTempFile("/tmp/old_kernel.XXXXXX", &old_kernel, NULL)); |
| 189 | ScopedPathUnlinker old_kernel_unlinker(old_kernel); |
| 190 | |
| 191 | string new_kernel; |
| 192 | EXPECT_TRUE(utils::MakeTempFile("/tmp/new_kernel.XXXXXX", &new_kernel, NULL)); |
| 193 | ScopedPathUnlinker new_kernel_unlinker(new_kernel); |
| 194 | |
| 195 | vector<char> old_kernel_data(4096); // Something small for a test |
| 196 | vector<char> new_kernel_data(old_kernel_data.size()); |
| 197 | FillWithData(&old_kernel_data); |
| 198 | FillWithData(&new_kernel_data); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 199 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 200 | // change the new kernel data |
| 201 | const char* new_data_string = "This is new data."; |
| 202 | strcpy(&new_kernel_data[0], new_data_string); |
| 203 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 204 | if (noop) { |
| 205 | old_kernel_data = new_kernel_data; |
| 206 | } |
| 207 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 208 | // Write kernels to disk |
| 209 | EXPECT_TRUE(utils::WriteFile( |
| 210 | old_kernel.c_str(), &old_kernel_data[0], old_kernel_data.size())); |
| 211 | EXPECT_TRUE(utils::WriteFile( |
| 212 | new_kernel.c_str(), &new_kernel_data[0], new_kernel_data.size())); |
| 213 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 214 | string delta_path; |
| 215 | EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL)); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 216 | LOG(INFO) << "delta path: " << delta_path; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 217 | ScopedPathUnlinker delta_path_unlinker(delta_path); |
| 218 | { |
| 219 | string a_mnt, b_mnt; |
| 220 | ScopedLoopMounter a_mounter(a_img, &a_mnt, MS_RDONLY); |
| 221 | ScopedLoopMounter b_mounter(b_img, &b_mnt, MS_RDONLY); |
| 222 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 223 | EXPECT_TRUE( |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 224 | DeltaDiffGenerator::GenerateDeltaUpdateFile( |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 225 | full_rootfs ? "" : a_mnt, |
| 226 | full_rootfs ? "" : a_img, |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 227 | b_mnt, |
| 228 | b_img, |
| 229 | full_kernel ? "" : old_kernel, |
| 230 | new_kernel, |
| 231 | delta_path, |
| 232 | kUnittestPrivateKeyPath)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Read delta into memory. |
| 236 | vector<char> delta; |
| 237 | EXPECT_TRUE(utils::ReadFile(delta_path, &delta)); |
| 238 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 239 | uint64_t manifest_metadata_size; |
| 240 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 241 | // Check the metadata. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 242 | { |
| 243 | LOG(INFO) << "delta size: " << delta.size(); |
| 244 | DeltaArchiveManifest manifest; |
| 245 | const int kManifestSizeOffset = 12; |
| 246 | const int kManifestOffset = 20; |
| 247 | uint64_t manifest_size = 0; |
| 248 | memcpy(&manifest_size, &delta[kManifestSizeOffset], sizeof(manifest_size)); |
| 249 | manifest_size = be64toh(manifest_size); |
| 250 | LOG(INFO) << "manifest size: " << manifest_size; |
| 251 | EXPECT_TRUE(manifest.ParseFromArray(&delta[kManifestOffset], |
| 252 | manifest_size)); |
| 253 | EXPECT_TRUE(manifest.has_signatures_offset()); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 254 | manifest_metadata_size = kManifestOffset + manifest_size; |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 255 | |
| 256 | Signatures sigs_message; |
| 257 | EXPECT_TRUE(sigs_message.ParseFromArray( |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 258 | &delta[manifest_metadata_size + manifest.signatures_offset()], |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 259 | manifest.signatures_size())); |
| 260 | EXPECT_EQ(1, sigs_message.signatures_size()); |
| 261 | const Signatures_Signature& signature = sigs_message.signatures(0); |
| 262 | EXPECT_EQ(1, signature.version()); |
| 263 | |
| 264 | uint64_t expected_sig_data_length = 0; |
| 265 | EXPECT_TRUE(PayloadSigner::SignatureBlobLength(kUnittestPrivateKeyPath, |
| 266 | &expected_sig_data_length)); |
| 267 | EXPECT_EQ(expected_sig_data_length, manifest.signatures_size()); |
| 268 | EXPECT_FALSE(signature.data().empty()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 269 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 270 | if (noop) { |
| 271 | EXPECT_EQ(1, manifest.install_operations_size()); |
| 272 | EXPECT_EQ(1, manifest.kernel_install_operations_size()); |
| 273 | } |
| 274 | |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 275 | if (full_kernel) { |
| 276 | EXPECT_FALSE(manifest.has_old_kernel_info()); |
| 277 | } else { |
| 278 | EXPECT_EQ(old_kernel_data.size(), manifest.old_kernel_info().size()); |
| 279 | EXPECT_FALSE(manifest.old_kernel_info().hash().empty()); |
| 280 | } |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 281 | |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 282 | if (full_rootfs) { |
| 283 | EXPECT_FALSE(manifest.has_old_rootfs_info()); |
| 284 | } else { |
| 285 | EXPECT_EQ(image_size, manifest.old_rootfs_info().size()); |
| 286 | EXPECT_FALSE(manifest.old_rootfs_info().hash().empty()); |
| 287 | } |
| 288 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 289 | EXPECT_EQ(new_kernel_data.size(), manifest.new_kernel_info().size()); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 290 | EXPECT_EQ(image_size, manifest.new_rootfs_info().size()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 291 | |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 292 | EXPECT_FALSE(manifest.new_kernel_info().hash().empty()); |
Darin Petkov | 36a5822 | 2010-10-07 22:00:09 -0700 | [diff] [blame] | 293 | EXPECT_FALSE(manifest.new_rootfs_info().hash().empty()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 296 | PrefsMock prefs; |
| 297 | EXPECT_CALL(prefs, SetInt64(kPrefsManifestMetadataSize, |
| 298 | manifest_metadata_size)).WillOnce(Return(true)); |
| 299 | EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextOperation, _)) |
| 300 | .WillRepeatedly(Return(true)); |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 301 | EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _)) |
| 302 | .WillOnce(Return(false)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 303 | EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextDataOffset, _)) |
| 304 | .WillRepeatedly(Return(true)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 305 | EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSHA256Context, _)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 306 | .WillRepeatedly(Return(true)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 307 | EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSignedSHA256Context, _)) |
| 308 | .WillOnce(Return(true)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 309 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 310 | // Update the A image in place. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 311 | DeltaPerformer performer(&prefs); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 312 | |
Darin Petkov | d43d690 | 2010-10-14 11:17:50 -0700 | [diff] [blame] | 313 | vector<char> rootfs_hash; |
| 314 | EXPECT_EQ(image_size, |
| 315 | OmahaHashCalculator::RawHashOfFile(a_img, |
| 316 | image_size, |
| 317 | &rootfs_hash)); |
| 318 | performer.set_current_rootfs_hash(&rootfs_hash); |
| 319 | vector<char> kernel_hash; |
| 320 | EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(old_kernel_data, |
| 321 | &kernel_hash)); |
| 322 | performer.set_current_kernel_hash(&kernel_hash); |
| 323 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 324 | 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] | 325 | EXPECT_TRUE(performer.OpenKernel(old_kernel.c_str())); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 326 | |
| 327 | // Write at some number of bytes per operation. Arbitrarily chose 5. |
| 328 | const size_t kBytesPerWrite = 5; |
| 329 | for (size_t i = 0; i < delta.size(); i += kBytesPerWrite) { |
| 330 | size_t count = min(delta.size() - i, kBytesPerWrite); |
| 331 | EXPECT_EQ(count, performer.Write(&delta[i], count)); |
| 332 | } |
| 333 | |
| 334 | // Wrapper around close. Returns 0 on success or -errno on error. |
| 335 | EXPECT_EQ(0, performer.Close()); |
| 336 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 337 | CompareFilesByBlock(old_kernel, new_kernel); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 338 | CompareFilesByBlock(a_img, b_img); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 339 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 340 | vector<char> updated_kernel_partition; |
| 341 | EXPECT_TRUE(utils::ReadFile(old_kernel, &updated_kernel_partition)); |
| 342 | EXPECT_EQ(0, strncmp(&updated_kernel_partition[0], new_data_string, |
| 343 | strlen(new_data_string))); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 344 | |
| 345 | EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 346 | EXPECT_TRUE(performer.VerifyPayload( |
| 347 | kUnittestPublicKeyPath, |
| 348 | OmahaHashCalculator::OmahaHashOfData(delta), |
| 349 | delta.size())); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 350 | EXPECT_TRUE(performer.VerifyAppliedUpdate(a_img, old_kernel)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 351 | } |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | TEST(DeltaPerformerTest, RunAsRootSmallImageTest) { |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 355 | DoSmallImageTest(false, false, false); |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | TEST(DeltaPerformerTest, RunAsRootFullKernelSmallImageTest) { |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 359 | DoSmallImageTest(true, false, false); |
| 360 | } |
| 361 | |
| 362 | TEST(DeltaPerformerTest, RunAsRootFullSmallImageTest) { |
| 363 | DoSmallImageTest(true, true, false); |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | TEST(DeltaPerformerTest, RunAsRootNoopSmallImageTest) { |
Darin Petkov | 7a22d79 | 2010-11-08 14:10:00 -0800 | [diff] [blame^] | 367 | DoSmallImageTest(false, false, true); |
Andrew de los Reyes | 27f7d37 | 2010-10-07 11:26:07 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 370 | TEST(DeltaPerformerTest, IsIdempotentOperationTest) { |
| 371 | DeltaArchiveManifest_InstallOperation op; |
| 372 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 373 | *(op.add_dst_extents()) = ExtentForRange(0, 5); |
| 374 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 375 | *(op.add_src_extents()) = ExtentForRange(4, 1); |
| 376 | EXPECT_FALSE(DeltaPerformer::IsIdempotentOperation(op)); |
| 377 | op.clear_src_extents(); |
| 378 | *(op.add_src_extents()) = ExtentForRange(5, 3); |
| 379 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 380 | *(op.add_dst_extents()) = ExtentForRange(20, 6); |
| 381 | EXPECT_TRUE(DeltaPerformer::IsIdempotentOperation(op)); |
| 382 | *(op.add_src_extents()) = ExtentForRange(19, 2); |
| 383 | EXPECT_FALSE(DeltaPerformer::IsIdempotentOperation(op)); |
| 384 | } |
| 385 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 386 | } // namespace chromeos_update_engine |