blob: 37a0c683da72dc9439cae17437f40393a772e74f [file] [log] [blame]
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001// 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 Petkov9fa7ec52010-10-18 11:45:23 -070012#include <base/file_util.h>
Darin Petkov73058b42010-10-06 16:32:19 -070013#include <base/scoped_ptr.h>
14#include <base/string_util.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070015#include <google/protobuf/repeated_field.h>
16#include <gtest/gtest.h>
17
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070018#include "update_engine/delta_diff_generator.h"
19#include "update_engine/delta_performer.h"
Andrew de los Reyes353777c2010-10-08 10:34:30 -070020#include "update_engine/extent_ranges.h"
Darin Petkov7a22d792010-11-08 14:10:00 -080021#include "update_engine/full_update_generator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include "update_engine/graph_types.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070023#include "update_engine/payload_signer.h"
Darin Petkov73058b42010-10-06 16:32:19 -070024#include "update_engine/prefs_mock.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070025#include "update_engine/test_utils.h"
26#include "update_engine/update_metadata.pb.h"
27#include "update_engine/utils.h"
28
29namespace chromeos_update_engine {
30
31using std::min;
32using std::string;
33using std::vector;
Darin Petkov73058b42010-10-06 16:32:19 -070034using testing::_;
35using testing::Return;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070036
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070037extern const char* kUnittestPrivateKeyPath;
Darin Petkovd7061ab2010-10-06 14:37:09 -070038extern const char* kUnittestPublicKeyPath;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070039
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070040namespace {
41 const size_t kBlockSize = 4096;
42} // namespace {}
43
44
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070045class DeltaPerformerTest : public ::testing::Test { };
46
47TEST(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
69class 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
90void CompareFilesByBlock(const string& a_file, const string& b_file) {
91 vector<char> a_data, b_data;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070092 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 Reyes09e56d62010-04-23 13:45:53 -070094
95 EXPECT_EQ(a_data.size(), b_data.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070096 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
105namespace {
106bool 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 Reyes09e56d62010-04-23 13:45:53 -0700116
Darin Petkov7a22d792010-11-08 14:10:00 -0800117void DoSmallImageTest(bool full_kernel, bool full_rootfs, bool noop) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700118 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 Reyes09e56d62010-04-23 13:45:53 -0700125
Darin Petkov7ea32332010-10-13 10:46:11 -0700126 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 Petkov7ea32332010-10-13 10:46:11 -0700133 EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(a_img));
Darin Petkov7ea32332010-10-13 10:46:11 -0700134
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700135 // 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 Petkov9fa7ec52010-10-18 11:45:23 -0700153 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 Reyes09e56d62010-04-23 13:45:53 -0700164 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 Reyesf4c7ef12010-04-30 10:37:00 -0700187 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 Reyes932bc4c2010-08-23 18:14:09 -0700199
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700200 // 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 Petkov9fa7ec52010-10-18 11:45:23 -0700204 if (noop) {
205 old_kernel_data = new_kernel_data;
206 }
207
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700208 // 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 Reyes09e56d62010-04-23 13:45:53 -0700214 string delta_path;
215 EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700216 LOG(INFO) << "delta path: " << delta_path;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700217 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 Reyes932bc4c2010-08-23 18:14:09 -0700223 EXPECT_TRUE(
Darin Petkov68c10d12010-10-14 09:24:37 -0700224 DeltaDiffGenerator::GenerateDeltaUpdateFile(
Darin Petkov7a22d792010-11-08 14:10:00 -0800225 full_rootfs ? "" : a_mnt,
226 full_rootfs ? "" : a_img,
Darin Petkov68c10d12010-10-14 09:24:37 -0700227 b_mnt,
228 b_img,
229 full_kernel ? "" : old_kernel,
230 new_kernel,
231 delta_path,
232 kUnittestPrivateKeyPath));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700233 }
234
235 // Read delta into memory.
236 vector<char> delta;
237 EXPECT_TRUE(utils::ReadFile(delta_path, &delta));
238
Darin Petkov73058b42010-10-06 16:32:19 -0700239 uint64_t manifest_metadata_size;
240
Darin Petkov36a58222010-10-07 22:00:09 -0700241 // Check the metadata.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700242 {
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 Petkov73058b42010-10-06 16:32:19 -0700254 manifest_metadata_size = kManifestOffset + manifest_size;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700255
256 Signatures sigs_message;
257 EXPECT_TRUE(sigs_message.ParseFromArray(
Darin Petkov73058b42010-10-06 16:32:19 -0700258 &delta[manifest_metadata_size + manifest.signatures_offset()],
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700259 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 Petkov36a58222010-10-07 22:00:09 -0700269
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700270 if (noop) {
271 EXPECT_EQ(1, manifest.install_operations_size());
272 EXPECT_EQ(1, manifest.kernel_install_operations_size());
273 }
274
Darin Petkovd43d6902010-10-14 11:17:50 -0700275 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 Petkov698d0412010-10-13 10:59:44 -0700281
Darin Petkov7a22d792010-11-08 14:10:00 -0800282 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 Petkov36a58222010-10-07 22:00:09 -0700289 EXPECT_EQ(new_kernel_data.size(), manifest.new_kernel_info().size());
Darin Petkov7ea32332010-10-13 10:46:11 -0700290 EXPECT_EQ(image_size, manifest.new_rootfs_info().size());
Darin Petkov36a58222010-10-07 22:00:09 -0700291
Darin Petkov36a58222010-10-07 22:00:09 -0700292 EXPECT_FALSE(manifest.new_kernel_info().hash().empty());
Darin Petkov36a58222010-10-07 22:00:09 -0700293 EXPECT_FALSE(manifest.new_rootfs_info().hash().empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700294 }
295
Darin Petkov73058b42010-10-06 16:32:19 -0700296 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 Petkov9b230572010-10-08 10:20:09 -0700301 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _))
302 .WillOnce(Return(false));
Darin Petkov73058b42010-10-06 16:32:19 -0700303 EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextDataOffset, _))
304 .WillRepeatedly(Return(true));
Darin Petkov437adc42010-10-07 13:12:24 -0700305 EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSHA256Context, _))
Darin Petkov73058b42010-10-06 16:32:19 -0700306 .WillRepeatedly(Return(true));
Darin Petkov437adc42010-10-07 13:12:24 -0700307 EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSignedSHA256Context, _))
308 .WillOnce(Return(true));
Darin Petkov73058b42010-10-06 16:32:19 -0700309
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700310 // Update the A image in place.
Darin Petkov73058b42010-10-06 16:32:19 -0700311 DeltaPerformer performer(&prefs);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700312
Darin Petkovd43d6902010-10-14 11:17:50 -0700313 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 Reyes09e56d62010-04-23 13:45:53 -0700324 EXPECT_EQ(0, performer.Open(a_img.c_str(), 0, 0));
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700325 EXPECT_TRUE(performer.OpenKernel(old_kernel.c_str()));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700326
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 Reyesf4c7ef12010-04-30 10:37:00 -0700337 CompareFilesByBlock(old_kernel, new_kernel);
Darin Petkov2dd01092010-10-08 15:43:05 -0700338 CompareFilesByBlock(a_img, b_img);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700339
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700340 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 Petkovd7061ab2010-10-06 14:37:09 -0700344
345 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
Darin Petkov437adc42010-10-07 13:12:24 -0700346 EXPECT_TRUE(performer.VerifyPayload(
347 kUnittestPublicKeyPath,
348 OmahaHashCalculator::OmahaHashOfData(delta),
349 delta.size()));
Darin Petkov2dd01092010-10-08 15:43:05 -0700350 EXPECT_TRUE(performer.VerifyAppliedUpdate(a_img, old_kernel));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700351}
Darin Petkov68c10d12010-10-14 09:24:37 -0700352}
353
354TEST(DeltaPerformerTest, RunAsRootSmallImageTest) {
Darin Petkov7a22d792010-11-08 14:10:00 -0800355 DoSmallImageTest(false, false, false);
Darin Petkov68c10d12010-10-14 09:24:37 -0700356}
357
358TEST(DeltaPerformerTest, RunAsRootFullKernelSmallImageTest) {
Darin Petkov7a22d792010-11-08 14:10:00 -0800359 DoSmallImageTest(true, false, false);
360}
361
362TEST(DeltaPerformerTest, RunAsRootFullSmallImageTest) {
363 DoSmallImageTest(true, true, false);
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700364}
365
366TEST(DeltaPerformerTest, RunAsRootNoopSmallImageTest) {
Darin Petkov7a22d792010-11-08 14:10:00 -0800367 DoSmallImageTest(false, false, true);
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700368}
369
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700370TEST(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 Reyes09e56d62010-04-23 13:45:53 -0700386} // namespace chromeos_update_engine