blob: 09a4c7d3e1807237c56af260a00df84eab22989a [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
12#include <google/protobuf/repeated_field.h>
13#include <gtest/gtest.h>
14
15#include "base/scoped_ptr.h"
16#include "base/string_util.h"
17#include "update_engine/delta_diff_generator.h"
18#include "update_engine/delta_performer.h"
19#include "update_engine/graph_types.h"
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070020#include "update_engine/payload_signer.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070021#include "update_engine/test_utils.h"
22#include "update_engine/update_metadata.pb.h"
23#include "update_engine/utils.h"
24
25namespace chromeos_update_engine {
26
27using std::min;
28using std::string;
29using std::vector;
30
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -070031extern const char* kUnittestPrivateKeyPath;
32
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070033class DeltaPerformerTest : public ::testing::Test { };
34
35TEST(DeltaPerformerTest, ExtentsToByteStringTest) {
36 uint64_t test[] = {1, 1, 4, 2, kSparseHole, 1, 0, 1};
37 COMPILE_ASSERT(arraysize(test) % 2 == 0, array_size_uneven);
38 const uint64_t block_size = 4096;
39 const uint64_t file_length = 5 * block_size - 13;
40
41 google::protobuf::RepeatedPtrField<Extent> extents;
42 for (size_t i = 0; i < arraysize(test); i += 2) {
43 Extent* extent = extents.Add();
44 extent->set_start_block(test[i]);
45 extent->set_num_blocks(test[i + 1]);
46 }
47
48 string expected_output = "4096:4096,16384:8192,-1:4096,0:4083";
49 string actual_output;
50 EXPECT_TRUE(DeltaPerformer::ExtentsToBsdiffPositionsString(extents,
51 block_size,
52 file_length,
53 &actual_output));
54 EXPECT_EQ(expected_output, actual_output);
55}
56
57class ScopedLoopMounter {
58 public:
59 explicit ScopedLoopMounter(const string& file_path, string* mnt_path,
60 unsigned long flags) {
61 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/mnt.XXXXXX", mnt_path));
62 dir_remover_.reset(new ScopedDirRemover(*mnt_path));
63
64 string loop_dev = GetUnusedLoopDevice();
65 EXPECT_EQ(0, system(StringPrintf("losetup %s %s", loop_dev.c_str(),
66 file_path.c_str()).c_str()));
67 loop_releaser_.reset(new ScopedLoopbackDeviceReleaser(loop_dev));
68
69 EXPECT_TRUE(utils::MountFilesystem(loop_dev, *mnt_path, flags));
70 unmounter_.reset(new ScopedFilesystemUnmounter(*mnt_path));
71 }
72 private:
73 scoped_ptr<ScopedDirRemover> dir_remover_;
74 scoped_ptr<ScopedLoopbackDeviceReleaser> loop_releaser_;
75 scoped_ptr<ScopedFilesystemUnmounter> unmounter_;
76};
77
78void CompareFilesByBlock(const string& a_file, const string& b_file) {
79 vector<char> a_data, b_data;
Andrew de los Reyes3270f742010-07-15 22:28:14 -070080 EXPECT_TRUE(utils::ReadFile(a_file, &a_data)) << "file failed: " << a_file;
81 EXPECT_TRUE(utils::ReadFile(b_file, &b_data)) << "file failed: " << b_file;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070082
83 EXPECT_EQ(a_data.size(), b_data.size());
84 size_t kBlockSize = 4096;
85 EXPECT_EQ(0, a_data.size() % kBlockSize);
86 for (size_t i = 0; i < a_data.size(); i += kBlockSize) {
87 EXPECT_EQ(0, i % kBlockSize);
88 vector<char> a_sub(&a_data[i], &a_data[i + kBlockSize]);
89 vector<char> b_sub(&b_data[i], &b_data[i + kBlockSize]);
90 EXPECT_TRUE(a_sub == b_sub) << "Block " << (i/kBlockSize) << " differs";
91 }
92}
93
94namespace {
95bool WriteSparseFile(const string& path, off_t size) {
96 int fd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
97 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
98 ScopedFdCloser fd_closer(&fd);
99 off_t rc = lseek(fd, size + 1, SEEK_SET);
100 TEST_AND_RETURN_FALSE_ERRNO(rc != static_cast<off_t>(-1));
101 int return_code = ftruncate(fd, size);
102 TEST_AND_RETURN_FALSE_ERRNO(return_code == 0);
103 return true;
104}
105}
106
107TEST(DeltaPerformerTest, RunAsRootSmallImageTest) {
108 string a_img, b_img;
109 EXPECT_TRUE(utils::MakeTempFile("/tmp/a_img.XXXXXX", &a_img, NULL));
110 ScopedPathUnlinker a_img_unlinker(a_img);
111 EXPECT_TRUE(utils::MakeTempFile("/tmp/b_img.XXXXXX", &b_img, NULL));
112 ScopedPathUnlinker b_img_unlinker(b_img);
113
114 CreateExtImageAtPath(a_img, NULL);
115 CreateExtImageAtPath(b_img, NULL);
116
117 // Make some changes to the A image.
118 {
119 string a_mnt;
120 ScopedLoopMounter b_mounter(a_img, &a_mnt, 0);
121
122 EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress",
123 a_mnt.c_str()).c_str(),
124 reinterpret_cast<const char*>(kRandomString),
125 sizeof(kRandomString) - 1));
126 // Write 1 MiB of 0xff to try to catch the case where writing a bsdiff
127 // patch fails to zero out the final block.
128 vector<char> ones(1024 * 1024, 0xff);
129 EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/ones",
130 a_mnt.c_str()).c_str(),
131 &ones[0],
132 ones.size()));
133 }
134
135 // Make some changes to the B image.
136 {
137 string b_mnt;
138 ScopedLoopMounter b_mounter(b_img, &b_mnt, 0);
139
140 EXPECT_EQ(0, system(StringPrintf("cp %s/hello %s/hello2", b_mnt.c_str(),
141 b_mnt.c_str()).c_str()));
142 EXPECT_EQ(0, system(StringPrintf("rm %s/hello", b_mnt.c_str()).c_str()));
143 EXPECT_EQ(0, system(StringPrintf("mv %s/hello2 %s/hello", b_mnt.c_str(),
144 b_mnt.c_str()).c_str()));
145 EXPECT_EQ(0, system(StringPrintf("echo foo > %s/foo",
146 b_mnt.c_str()).c_str()));
147 EXPECT_EQ(0, system(StringPrintf("touch %s/emptyfile",
148 b_mnt.c_str()).c_str()));
149 EXPECT_TRUE(WriteSparseFile(StringPrintf("%s/fullsparse", b_mnt.c_str()),
150 1024 * 1024));
151 EXPECT_EQ(0, system(StringPrintf("dd if=/dev/zero of=%s/partsparese bs=1 "
152 "seek=4096 count=1",
153 b_mnt.c_str()).c_str()));
154 EXPECT_TRUE(utils::WriteFile(StringPrintf("%s/hardtocompress",
155 b_mnt.c_str()).c_str(),
156 reinterpret_cast<const char*>(kRandomString),
157 sizeof(kRandomString)));
158 }
159
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700160 string old_kernel;
161 EXPECT_TRUE(utils::MakeTempFile("/tmp/old_kernel.XXXXXX", &old_kernel, NULL));
162 ScopedPathUnlinker old_kernel_unlinker(old_kernel);
163
164 string new_kernel;
165 EXPECT_TRUE(utils::MakeTempFile("/tmp/new_kernel.XXXXXX", &new_kernel, NULL));
166 ScopedPathUnlinker new_kernel_unlinker(new_kernel);
167
168 vector<char> old_kernel_data(4096); // Something small for a test
169 vector<char> new_kernel_data(old_kernel_data.size());
170 FillWithData(&old_kernel_data);
171 FillWithData(&new_kernel_data);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700172
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700173 // change the new kernel data
174 const char* new_data_string = "This is new data.";
175 strcpy(&new_kernel_data[0], new_data_string);
176
177 // Write kernels to disk
178 EXPECT_TRUE(utils::WriteFile(
179 old_kernel.c_str(), &old_kernel_data[0], old_kernel_data.size()));
180 EXPECT_TRUE(utils::WriteFile(
181 new_kernel.c_str(), &new_kernel_data[0], new_kernel_data.size()));
182
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700183 string delta_path;
184 EXPECT_TRUE(utils::MakeTempFile("/tmp/delta.XXXXXX", &delta_path, NULL));
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700185 LOG(INFO) << "delta path: " << delta_path;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700186 ScopedPathUnlinker delta_path_unlinker(delta_path);
187 {
188 string a_mnt, b_mnt;
189 ScopedLoopMounter a_mounter(a_img, &a_mnt, MS_RDONLY);
190 ScopedLoopMounter b_mounter(b_img, &b_mnt, MS_RDONLY);
191
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700192 EXPECT_TRUE(
193 DeltaDiffGenerator::GenerateDeltaUpdateFile(a_mnt,
194 a_img,
195 b_mnt,
196 b_img,
197 old_kernel,
198 new_kernel,
199 delta_path,
200 kUnittestPrivateKeyPath));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700201 }
202
203 // Read delta into memory.
204 vector<char> delta;
205 EXPECT_TRUE(utils::ReadFile(delta_path, &delta));
206
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700207 // Check that the null signature blob exists
208 {
209 LOG(INFO) << "delta size: " << delta.size();
210 DeltaArchiveManifest manifest;
211 const int kManifestSizeOffset = 12;
212 const int kManifestOffset = 20;
213 uint64_t manifest_size = 0;
214 memcpy(&manifest_size, &delta[kManifestSizeOffset], sizeof(manifest_size));
215 manifest_size = be64toh(manifest_size);
216 LOG(INFO) << "manifest size: " << manifest_size;
217 EXPECT_TRUE(manifest.ParseFromArray(&delta[kManifestOffset],
218 manifest_size));
219 EXPECT_TRUE(manifest.has_signatures_offset());
220
221 Signatures sigs_message;
222 EXPECT_TRUE(sigs_message.ParseFromArray(
223 &delta[kManifestOffset + manifest_size + manifest.signatures_offset()],
224 manifest.signatures_size()));
225 EXPECT_EQ(1, sigs_message.signatures_size());
226 const Signatures_Signature& signature = sigs_message.signatures(0);
227 EXPECT_EQ(1, signature.version());
228
229 uint64_t expected_sig_data_length = 0;
230 EXPECT_TRUE(PayloadSigner::SignatureBlobLength(kUnittestPrivateKeyPath,
231 &expected_sig_data_length));
232 EXPECT_EQ(expected_sig_data_length, manifest.signatures_size());
233 EXPECT_FALSE(signature.data().empty());
234 }
235
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700236 // Update the A image in place.
237 DeltaPerformer performer;
238
239 EXPECT_EQ(0, performer.Open(a_img.c_str(), 0, 0));
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700240 EXPECT_TRUE(performer.OpenKernel(old_kernel.c_str()));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700241
242 // Write at some number of bytes per operation. Arbitrarily chose 5.
243 const size_t kBytesPerWrite = 5;
244 for (size_t i = 0; i < delta.size(); i += kBytesPerWrite) {
245 size_t count = min(delta.size() - i, kBytesPerWrite);
246 EXPECT_EQ(count, performer.Write(&delta[i], count));
247 }
248
249 // Wrapper around close. Returns 0 on success or -errno on error.
250 EXPECT_EQ(0, performer.Close());
251
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700252 CompareFilesByBlock(old_kernel, new_kernel);
Andrew de los Reyes932bc4c2010-08-23 18:14:09 -0700253
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700254 vector<char> updated_kernel_partition;
255 EXPECT_TRUE(utils::ReadFile(old_kernel, &updated_kernel_partition));
256 EXPECT_EQ(0, strncmp(&updated_kernel_partition[0], new_data_string,
257 strlen(new_data_string)));
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700258}
259
260} // namespace chromeos_update_engine