blob: b820ab846eb5987609b5f3f61f0467db1b1e468c [file] [log] [blame]
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07002// 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>
Chris Masoned903c3b2011-05-12 15:35:46 -070013#include <base/memory/scoped_ptr.h>
Darin Petkov73058b42010-10-06 16:32:19 -070014#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 Reyesc24e3f32011-08-30 15:45:20 -070039extern const char* kUnittestPrivateKey2Path;
40extern const char* kUnittestPublicKey2Path;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070041
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070042namespace {
Andrew de los Reyes29da8aa2011-02-15 13:34:57 -080043const size_t kBlockSize = 4096;
Andrew de los Reyes27f7d372010-10-07 11:26:07 -070044} // namespace {}
45
46
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070047class DeltaPerformerTest : public ::testing::Test { };
48
49TEST(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 Reyes09e56d62010-04-23 13:45:53 -070071void CompareFilesByBlock(const string& a_file, const string& b_file) {
72 vector<char> a_data, b_data;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070073 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 Reyes09e56d62010-04-23 13:45:53 -070075
76 EXPECT_EQ(a_data.size(), b_data.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070077 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
86namespace {
87bool 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 Petkov9574f7e2011-01-13 10:48:12 -080097} // namespace {}
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070098
Darin Petkov9574f7e2011-01-13 10:48:12 -080099namespace {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800100enum 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 Petkov52dcaeb2011-01-14 15:33:06 -0800105 kSignatureGeneratedShellBadKey, // Sign with a bad key through shell cmds.
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700106 kSignatureGeneratedShellRotateCl1, // Rotate key, test client v1
107 kSignatureGeneratedShellRotateCl2, // Rotate key, test client v2
Darin Petkovcbfb0172011-01-14 15:24:45 -0800108};
109
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800110size_t GetSignatureSize(const string& private_key_path) {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800111 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 Petkov52dcaeb2011-01-14 15:33:06 -0800116 private_key_path,
Darin Petkovcbfb0172011-01-14 15:24:45 -0800117 &signature));
118 return signature.size();
119}
120
121void SignGeneratedPayload(const string& payload_path) {
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800122 int signature_size = GetSignatureSize(kUnittestPrivateKeyPath);
Darin Petkovcbfb0172011-01-14 15:24:45 -0800123 vector<char> hash;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700124 ASSERT_TRUE(PayloadSigner::HashPayloadForSigning(
125 payload_path,
126 vector<int>(1, signature_size),
127 &hash));
Darin Petkovcbfb0172011-01-14 15:24:45 -0800128 vector<char> signature;
129 ASSERT_TRUE(PayloadSigner::SignHash(hash,
130 kUnittestPrivateKeyPath,
131 &signature));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700132 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 Petkovcbfb0172011-01-14 15:24:45 -0800140}
141
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800142void 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 Reyesc24e3f32011-08-30 15:45:20 -0700150 ASSERT_TRUE(signature_test == kSignatureGeneratedShell ||
151 signature_test == kSignatureGeneratedShellRotateCl1 ||
152 signature_test == kSignatureGeneratedShellRotateCl2);
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800153 }
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 Reyesbdfaaf02011-03-30 10:35:12 -0700162 "/usr/bin/openssl genrsa -out %s 2048",
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800163 private_key_path.c_str())));
164 }
165 int signature_size = GetSignatureSize(private_key_path);
Darin Petkovcbfb0172011-01-14 15:24:45 -0800166 string hash_file;
167 ASSERT_TRUE(utils::MakeTempFile("/tmp/hash.XXXXXX", &hash_file, NULL));
168 ScopedPathUnlinker hash_unlinker(hash_file);
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700169 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 Petkovcbfb0172011-01-14 15:24:45 -0800176 ASSERT_EQ(0,
177 System(StringPrintf(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700178 "./delta_generator -in_file %s -signature_size %s "
Darin Petkovcbfb0172011-01-14 15:24:45 -0800179 "-out_hash_file %s",
180 payload_path.c_str(),
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700181 signature_size_string.c_str(),
Darin Petkovcbfb0172011-01-14 15:24:45 -0800182 hash_file.c_str())));
183
Andrew de los Reyesbdfaaf02011-03-30 10:35:12 -0700184 // 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 Petkovcbfb0172011-01-14 15:24:45 -0800190 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 Reyesbdfaaf02011-03-30 10:35:12 -0700195 "/usr/bin/openssl rsautl -raw -sign -inkey %s -in %s -out %s",
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800196 private_key_path.c_str(),
Darin Petkovcbfb0172011-01-14 15:24:45 -0800197 hash_file.c_str(),
198 sig_file.c_str())));
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700199 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 Petkovcbfb0172011-01-14 15:24:45 -0800214 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 Petkov52dcaeb2011-01-14 15:33:06 -0800221 int verify_result =
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700222 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 Petkov52dcaeb2011-01-14 15:33:06 -0800228 if (signature_test == kSignatureGeneratedShellBadKey) {
229 ASSERT_NE(0, verify_result);
230 } else {
231 ASSERT_EQ(0, verify_result);
232 }
Darin Petkovcbfb0172011-01-14 15:24:45 -0800233}
234
Darin Petkov9574f7e2011-01-13 10:48:12 -0800235void DoSmallImageTest(bool full_kernel, bool full_rootfs, bool noop,
Darin Petkovcbfb0172011-01-14 15:24:45 -0800236 SignatureTest signature_test) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700237 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 Reyes09e56d62010-04-23 13:45:53 -0700244
Darin Petkov7ea32332010-10-13 10:46:11 -0700245 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 Petkov7ea32332010-10-13 10:46:11 -0700252 EXPECT_EQ(image_size + 1024 * 1024, utils::FileSize(a_img));
Darin Petkov7ea32332010-10-13 10:46:11 -0700253
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700254 // 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 Petkov9fa7ec52010-10-18 11:45:23 -0700272 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 Reyes09e56d62010-04-23 13:45:53 -0700283 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 Reyes29da8aa2011-02-15 13:34:57 -0800300 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 Reyes48a0a482011-02-22 15:32:11 -0800304 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 Reyes09e56d62010-04-23 13:45:53 -0700307 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 Reyesf4c7ef12010-04-30 10:37:00 -0700313 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 Reyes932bc4c2010-08-23 18:14:09 -0700325
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700326 // 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 Petkov9fa7ec52010-10-18 11:45:23 -0700330 if (noop) {
331 old_kernel_data = new_kernel_data;
332 }
333
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700334 // 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 Reyes09e56d62010-04-23 13:45:53 -0700340 string delta_path;
341 EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700342 LOG(INFO) << "delta path: " << delta_path;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700343 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 Petkovcbfb0172011-01-14 15:24:45 -0800348 const string private_key =
349 signature_test == kSignatureGenerator ? kUnittestPrivateKeyPath : "";
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700350 EXPECT_TRUE(
Darin Petkov68c10d12010-10-14 09:24:37 -0700351 DeltaDiffGenerator::GenerateDeltaUpdateFile(
Darin Petkov7a22d792010-11-08 14:10:00 -0800352 full_rootfs ? "" : a_mnt,
353 full_rootfs ? "" : a_img,
Darin Petkov68c10d12010-10-14 09:24:37 -0700354 b_mnt,
355 b_img,
356 full_kernel ? "" : old_kernel,
357 new_kernel,
358 delta_path,
Darin Petkovcbfb0172011-01-14 15:24:45 -0800359 private_key));
Darin Petkov9574f7e2011-01-13 10:48:12 -0800360 }
361
Darin Petkovcbfb0172011-01-14 15:24:45 -0800362 if (signature_test == kSignatureGenerated) {
363 SignGeneratedPayload(delta_path);
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800364 } else if (signature_test == kSignatureGeneratedShell ||
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700365 signature_test == kSignatureGeneratedShellBadKey ||
366 signature_test == kSignatureGeneratedShellRotateCl1 ||
367 signature_test == kSignatureGeneratedShellRotateCl2) {
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800368 SignGeneratedShellPayload(signature_test, delta_path);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700369 }
370
371 // Read delta into memory.
372 vector<char> delta;
373 EXPECT_TRUE(utils::ReadFile(delta_path, &delta));
374
Darin Petkov73058b42010-10-06 16:32:19 -0700375 uint64_t manifest_metadata_size;
376
Darin Petkov36a58222010-10-07 22:00:09 -0700377 // Check the metadata.
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700378 {
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 Petkov73058b42010-10-06 16:32:19 -0700389 manifest_metadata_size = kManifestOffset + manifest_size;
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700390
Darin Petkovcbfb0172011-01-14 15:24:45 -0800391 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 Reyesc24e3f32011-08-30 15:45:20 -0700401 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 Petkovcbfb0172011-01-14 15:24:45 -0800406 const Signatures_Signature& signature = sigs_message.signatures(0);
407 EXPECT_EQ(1, signature.version());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700408
Darin Petkovcbfb0172011-01-14 15:24:45 -0800409 uint64_t expected_sig_data_length = 0;
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700410 vector<string> key_paths (1, kUnittestPrivateKeyPath);
411 if (signature_test == kSignatureGeneratedShellRotateCl1 ||
412 signature_test == kSignatureGeneratedShellRotateCl2) {
413 key_paths.push_back(kUnittestPrivateKey2Path);
414 }
Darin Petkovcbfb0172011-01-14 15:24:45 -0800415 EXPECT_TRUE(PayloadSigner::SignatureBlobLength(
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700416 key_paths,
417 &expected_sig_data_length));
Darin Petkovcbfb0172011-01-14 15:24:45 -0800418 EXPECT_EQ(expected_sig_data_length, manifest.signatures_size());
419 EXPECT_FALSE(signature.data().empty());
420 }
Darin Petkov36a58222010-10-07 22:00:09 -0700421
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700422 if (noop) {
423 EXPECT_EQ(1, manifest.install_operations_size());
424 EXPECT_EQ(1, manifest.kernel_install_operations_size());
425 }
426
Darin Petkovd43d6902010-10-14 11:17:50 -0700427 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 Petkov698d0412010-10-13 10:59:44 -0700433
Darin Petkov7a22d792010-11-08 14:10:00 -0800434 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 Petkov36a58222010-10-07 22:00:09 -0700441 EXPECT_EQ(new_kernel_data.size(), manifest.new_kernel_info().size());
Darin Petkov7ea32332010-10-13 10:46:11 -0700442 EXPECT_EQ(image_size, manifest.new_rootfs_info().size());
Darin Petkov36a58222010-10-07 22:00:09 -0700443
Darin Petkov36a58222010-10-07 22:00:09 -0700444 EXPECT_FALSE(manifest.new_kernel_info().hash().empty());
Darin Petkov36a58222010-10-07 22:00:09 -0700445 EXPECT_FALSE(manifest.new_rootfs_info().hash().empty());
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700446 }
447
Darin Petkov73058b42010-10-06 16:32:19 -0700448 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 Petkov9b230572010-10-08 10:20:09 -0700453 EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextOperation, _))
454 .WillOnce(Return(false));
Darin Petkov73058b42010-10-06 16:32:19 -0700455 EXPECT_CALL(prefs, SetInt64(kPrefsUpdateStateNextDataOffset, _))
456 .WillRepeatedly(Return(true));
Darin Petkov437adc42010-10-07 13:12:24 -0700457 EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSHA256Context, _))
Darin Petkov73058b42010-10-06 16:32:19 -0700458 .WillRepeatedly(Return(true));
Darin Petkovcbfb0172011-01-14 15:24:45 -0800459 if (signature_test != kSignatureNone) {
460 EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSignedSHA256Context, _))
461 .WillOnce(Return(true));
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700462 EXPECT_CALL(prefs, SetString(kPrefsUpdateStateSignatureBlob, _))
463 .WillOnce(Return(true));
Darin Petkovcbfb0172011-01-14 15:24:45 -0800464 }
Darin Petkov73058b42010-10-06 16:32:19 -0700465
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700466 // Update the A image in place.
Darin Petkov73058b42010-10-06 16:32:19 -0700467 DeltaPerformer performer(&prefs);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700468
Darin Petkovd43d6902010-10-14 11:17:50 -0700469 vector<char> rootfs_hash;
470 EXPECT_EQ(image_size,
471 OmahaHashCalculator::RawHashOfFile(a_img,
472 image_size,
473 &rootfs_hash));
Darin Petkov3aefa862010-12-07 14:45:00 -0800474 performer.set_current_rootfs_hash(rootfs_hash);
Darin Petkovd43d6902010-10-14 11:17:50 -0700475 vector<char> kernel_hash;
476 EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(old_kernel_data,
477 &kernel_hash));
Darin Petkov3aefa862010-12-07 14:45:00 -0800478 performer.set_current_kernel_hash(kernel_hash);
Darin Petkovd43d6902010-10-14 11:17:50 -0700479
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700480 EXPECT_EQ(0, performer.Open(a_img.c_str(), 0, 0));
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700481 EXPECT_TRUE(performer.OpenKernel(old_kernel.c_str()));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700482
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 Garrette410e0f2011-11-10 15:39:01 -0800487 EXPECT_TRUE(performer.Write(&delta[i], count));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700488 }
489
490 // Wrapper around close. Returns 0 on success or -errno on error.
491 EXPECT_EQ(0, performer.Close());
492
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700493 CompareFilesByBlock(old_kernel, new_kernel);
Darin Petkov2dd01092010-10-08 15:43:05 -0700494 CompareFilesByBlock(a_img, b_img);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700495
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700496 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 Petkovd7061ab2010-10-06 14:37:09 -0700500
501 EXPECT_TRUE(utils::FileExists(kUnittestPublicKeyPath));
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700502 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 Reyesfb830ba2011-04-04 11:42:43 -0700513 kUnittestPublicKeyPath,
514 OmahaHashCalculator::OmahaHashOfData(delta),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700515 delta.size()));
516 EXPECT_EQ(kActionCodeSuccess, performer.VerifyPayload(
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800517 "/public/key/does/not/exists",
518 OmahaHashCalculator::OmahaHashOfData(delta),
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -0700519 delta.size()));
Darin Petkov3aefa862010-12-07 14:45:00 -0800520
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 Reyes09e56d62010-04-23 13:45:53 -0700541}
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700542} // namespace {}
Darin Petkov68c10d12010-10-14 09:24:37 -0700543
544TEST(DeltaPerformerTest, RunAsRootSmallImageTest) {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800545 DoSmallImageTest(false, false, false, kSignatureGenerator);
Darin Petkov68c10d12010-10-14 09:24:37 -0700546}
547
548TEST(DeltaPerformerTest, RunAsRootFullKernelSmallImageTest) {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800549 DoSmallImageTest(true, false, false, kSignatureGenerator);
Darin Petkov7a22d792010-11-08 14:10:00 -0800550}
551
552TEST(DeltaPerformerTest, RunAsRootFullSmallImageTest) {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800553 DoSmallImageTest(true, true, false, kSignatureGenerator);
Darin Petkov9fa7ec52010-10-18 11:45:23 -0700554}
555
556TEST(DeltaPerformerTest, RunAsRootNoopSmallImageTest) {
Darin Petkovcbfb0172011-01-14 15:24:45 -0800557 DoSmallImageTest(false, false, true, kSignatureGenerator);
Darin Petkov9574f7e2011-01-13 10:48:12 -0800558}
559
Darin Petkovcbfb0172011-01-14 15:24:45 -0800560TEST(DeltaPerformerTest, RunAsRootSmallImageSignNoneTest) {
561 DoSmallImageTest(false, false, false, kSignatureNone);
562}
563
564TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedTest) {
565 DoSmallImageTest(false, false, false, kSignatureGenerated);
566}
567
568TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellTest) {
569 DoSmallImageTest(false, false, false, kSignatureGeneratedShell);
Andrew de los Reyes27f7d372010-10-07 11:26:07 -0700570}
571
Darin Petkov52dcaeb2011-01-14 15:33:06 -0800572TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellBadKeyTest) {
573 DoSmallImageTest(false, false, false, kSignatureGeneratedShellBadKey);
574}
575
Andrew de los Reyesc24e3f32011-08-30 15:45:20 -0700576TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellRotateCl1Test) {
577 DoSmallImageTest(false, false, false, kSignatureGeneratedShellRotateCl1);
578}
579
580TEST(DeltaPerformerTest, RunAsRootSmallImageSignGeneratedShellRotateCl2Test) {
581 DoSmallImageTest(false, false, false, kSignatureGeneratedShellRotateCl2);
582}
583
Darin Petkov934bb412010-11-18 11:21:35 -0800584TEST(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 Garrette410e0f2011-11-10 15:39:01 -0800589 EXPECT_TRUE(performer.Write("junk", 4));
590 EXPECT_TRUE(performer.Write("morejunk", 8));
591 EXPECT_FALSE(performer.Write("morejunk", 8));
Darin Petkov934bb412010-11-18 11:21:35 -0800592 EXPECT_LT(performer.Close(), 0);
593}
594
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700595TEST(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 Reyes09e56d62010-04-23 13:45:53 -0700611} // namespace chromeos_update_engine