blob: 6f2cf2d61eaaf962c5d6def6ae5cf2d90649f206 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium 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/stat.h>
6#include <sys/types.h>
7#include <errno.h>
Andrew de los Reyese5733992009-12-08 13:34:00 -08008#include <utime.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <set>
10#include <string>
11#include <vector>
12#include "base/string_util.h"
13#include <gtest/gtest.h>
14#include "update_engine/delta_diff_generator.h"
15#include "update_engine/filesystem_iterator.h"
16#include "update_engine/install_action.h"
17#include "update_engine/test_utils.h"
18#include "update_engine/utils.h"
19
Andrew de los Reyese5733992009-12-08 13:34:00 -080020using chromeos_update_engine::DeltaDiffGenerator;
21using chromeos_update_engine::kRandomString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000022using chromeos_update_engine::System;
Andrew de los Reyese5733992009-12-08 13:34:00 -080023using chromeos_update_engine::WriteFile;
adlr@google.com3defe6a2009-12-04 20:57:17 +000024using std::set;
25using std::string;
26using std::vector;
27
28namespace {
29void GenerateFilesAtPath(const string& base) {
30 EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", base.c_str())));
Andrew de los Reyese5733992009-12-08 13:34:00 -080031 EXPECT_EQ(0, System(StringPrintf("ln %s/hi %s/hard_link", base.c_str(),
32 base.c_str())));
adlr@google.com3defe6a2009-12-04 20:57:17 +000033 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir", base.c_str())));
34 EXPECT_EQ(0, System(StringPrintf("echo hello > %s/dir/hello", base.c_str())));
35 EXPECT_EQ(0, System(StringPrintf("echo -n > %s/dir/newempty", base.c_str())));
36 EXPECT_EQ(0, System(StringPrintf("rm -f %s/dir/bdev", base.c_str())));
37 EXPECT_EQ(0, System(StringPrintf("mknod %s/dir/bdev b 3 1", base.c_str())));
38 EXPECT_EQ(0, System(StringPrintf("rm -f %s/cdev", base.c_str())));
39 EXPECT_EQ(0, System(StringPrintf("mknod %s/cdev c 2 1", base.c_str())));
40 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir/subdir", base.c_str())));
41 EXPECT_EQ(0, System(StringPrintf("mkdir -p %s/dir/emptydir", base.c_str())));
42 EXPECT_EQ(0, System(StringPrintf("echo -n foo > %s/dir/bigfile",
43 base.c_str())));
44 EXPECT_EQ(0, System(StringPrintf("chown 501:503 %s/dir/emptydir",
45 base.c_str())));
46 EXPECT_EQ(0, System(StringPrintf("rm -f %s/dir/subdir/fifo", base.c_str())));
47 EXPECT_EQ(0, System(StringPrintf("mkfifo %s/dir/subdir/fifo", base.c_str())));
48 EXPECT_EQ(0, System(StringPrintf("ln -f -s /target %s/dir/subdir/link",
49 base.c_str())));
Andrew de los Reyese5733992009-12-08 13:34:00 -080050 EXPECT_TRUE(WriteFile((base + "/big_file").c_str(),
51 reinterpret_cast<const char*>(kRandomString),
52 sizeof(kRandomString)));
adlr@google.com3defe6a2009-12-04 20:57:17 +000053}
54
55// Returns true if files at paths a, b are equal and there are no errors.
56bool FilesEqual(const string& a, const string& b) {
57 struct stat a_stbuf;
58 struct stat b_stbuf;
59
60 int r = lstat(a.c_str(), &a_stbuf);
61 TEST_AND_RETURN_FALSE_ERRNO(r == 0);
62 r = lstat(b.c_str(), &b_stbuf);
63 TEST_AND_RETURN_FALSE_ERRNO(r == 0);
64
65 TEST_AND_RETURN_FALSE(a_stbuf.st_mode == b_stbuf.st_mode);
66 if (S_ISBLK(a_stbuf.st_mode) || S_ISCHR(a_stbuf.st_mode))
67 TEST_AND_RETURN_FALSE(a_stbuf.st_rdev == b_stbuf.st_rdev);
Andrew de los Reyese5733992009-12-08 13:34:00 -080068 if (!S_ISDIR(a_stbuf.st_mode))
69 TEST_AND_RETURN_FALSE(a_stbuf.st_nlink == b_stbuf.st_nlink);
adlr@google.com3defe6a2009-12-04 20:57:17 +000070 if (!S_ISREG(a_stbuf.st_mode)) {
71 return true;
72 }
73 // Compare files
74 TEST_AND_RETURN_FALSE(a_stbuf.st_size == b_stbuf.st_size);
75 vector<char> a_data;
76 TEST_AND_RETURN_FALSE(chromeos_update_engine::utils::ReadFile(a, &a_data));
77 vector<char> b_data;
78 TEST_AND_RETURN_FALSE(chromeos_update_engine::utils::ReadFile(b, &b_data));
79 TEST_AND_RETURN_FALSE(a_data == b_data);
80 return true;
81}
82
83class ScopedLoopDevUnmapper {
84 public:
85 explicit ScopedLoopDevUnmapper(const string& dev) : dev_(dev) {}
86 ~ScopedLoopDevUnmapper() {
87 EXPECT_EQ(0, System(string("losetup -d ") + dev_));
88 }
89 private:
90 string dev_;
91};
92
93}
94
95namespace chromeos_update_engine {
96
97class InstallActionTest : public ::testing::Test { };
98
99TEST(InstallActionTest, RunAsRootDiffTest) {
100 ASSERT_EQ(0, getuid());
101 string loop_dev = GetUnusedLoopDevice();
102 ScopedLoopDevUnmapper loop_dev_unmapper(loop_dev);
103 LOG(INFO) << "Using loop device: " << loop_dev;
104 const string original_image("orig.image");
105 const string original_dir("orig");
106 const string new_dir("new");
107
108 ASSERT_EQ(0, System(string("dd if=/dev/zero of=") + original_image +
109 " bs=5M count=1"));
110 ASSERT_EQ(0, System(string("mkfs.ext3 -F ") + original_image));
111 ASSERT_EQ(0, System(string("losetup ") + loop_dev + " " + original_image));
112 ASSERT_EQ(0, System(string("mkdir ") + original_dir));
113 ASSERT_EQ(0, System(string("mount ") + loop_dev + " " + original_dir));
114 ASSERT_EQ(0, System(string("mkdir ") + new_dir));
115
116 GenerateFilesAtPath(original_dir);
117 GenerateFilesAtPath(new_dir);
118
119 {
120 // Fill bigfile w/ some data in the new folder
121 vector<char> buf(100 * 1024);
122 for (unsigned int i = 0; i < buf.size(); i++) {
123 buf[i] = static_cast<char>(i);
124 }
125 EXPECT_TRUE(WriteFileVector(new_dir + "/dir/bigfile", buf));
126 }
Andrew de los Reyese5733992009-12-08 13:34:00 -0800127 const char* const new_dir_cstr = new_dir.c_str();
128 EXPECT_EQ(0, System(StringPrintf("mkdir -p '%s/newdir'", new_dir_cstr)));
129 EXPECT_EQ(0, System(StringPrintf("mkdir -p '%s/newdir/x'", new_dir_cstr)));
130 EXPECT_EQ(0, System(StringPrintf("echo -n foo > '%s/newdir/x/file'",
131 new_dir_cstr)));
132 EXPECT_EQ(0, System(StringPrintf("echo -n x >> '%s/big_file'",
133 new_dir_cstr)));
134 // Make a symlink that compresses well:
135 EXPECT_EQ(0, System(StringPrintf(
136 "ln -s "
137 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
138 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
139 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
140 "'%s/compress_sym'", new_dir_cstr)));
141
142 // Make a device that will compress
143 EXPECT_EQ(0, mknod((new_dir + "/bdev_gz").c_str(), S_IFCHR | 0644, 0));
144
adlr@google.com3defe6a2009-12-04 20:57:17 +0000145 // Make a diff
146 DeltaArchiveManifest* delta =
147 DeltaDiffGenerator::EncodeMetadataToProtoBuffer(new_dir.c_str());
148 EXPECT_TRUE(NULL != delta);
149 EXPECT_TRUE(DeltaDiffGenerator::EncodeDataToDeltaFile(delta,
150 original_dir,
151 new_dir,
Andrew de los Reyese5733992009-12-08 13:34:00 -0800152 "delta",
153 new_dir + "/bdev_gz"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000154
155 ASSERT_EQ(0, System(string("umount ") + original_dir));
156
157 ObjectFeederAction<InstallPlan> feeder_action;
158 InstallAction install_action;
159 ObjectCollectorAction<string> collector_action;
160
161 BondActions(&feeder_action, &install_action);
162 BondActions(&install_action, &collector_action);
163
164 ActionProcessor processor;
165 processor.EnqueueAction(&feeder_action);
166 processor.EnqueueAction(&install_action);
167 processor.EnqueueAction(&collector_action);
168
169 InstallPlan install_plan(false, "", "", "delta", loop_dev);
170 feeder_action.set_obj(install_plan);
171
172 processor.StartProcessing();
173 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
174
175 EXPECT_EQ(loop_dev, collector_action.object());
176
177 ASSERT_EQ(0, System(string("mount ") + loop_dev + " " + original_dir));
178
179 // Check that original_dir and new_dir are equal
180 int original_count = 0;
181 LOG(INFO) << "checking old";
182 {
183 FilesystemIterator iter(original_dir,
184 utils::SetWithValue<string>("/lost+found"));
185 for (; !iter.IsEnd(); iter.Increment()) {
186 original_count++;
187 LOG(INFO) << "checking path: " << iter.GetPartialPath();
188 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(),
189 new_dir + iter.GetPartialPath()));
190 }
191 EXPECT_FALSE(iter.IsErr());
192 }
193 LOG(INFO) << "checking new";
194 int new_count = 0;
195 {
196 FilesystemIterator iter(new_dir, set<string>());
197 for (; !iter.IsEnd(); iter.Increment()) {
198 new_count++;
199 LOG(INFO) << "checking path: " << iter.GetPartialPath();
200 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(),
201 new_dir + iter.GetPartialPath()));
202 }
203 EXPECT_FALSE(iter.IsErr());
204 }
205 LOG(INFO) << "new_count = " << new_count;
206 EXPECT_EQ(new_count, original_count);
Andrew de los Reyese5733992009-12-08 13:34:00 -0800207 EXPECT_EQ(19, original_count);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208
Andrew de los Reyese5733992009-12-08 13:34:00 -0800209 // Make sure hard-link installed properly
210 {
211 struct stat hard_link_stbuf;
212 struct stat hi_stbuf;
213 EXPECT_EQ(0, lstat((string(new_dir) + "/hard_link").c_str(),
214 &hard_link_stbuf));
215 EXPECT_EQ(0, lstat((string(new_dir) + "/hi").c_str(), &hi_stbuf));
216 EXPECT_EQ(hard_link_stbuf.st_mode, hi_stbuf.st_mode);
217 EXPECT_EQ(2, hard_link_stbuf.st_nlink);
218 EXPECT_EQ(2, hi_stbuf.st_nlink);
219 EXPECT_EQ(hi_stbuf.st_ino, hard_link_stbuf.st_ino);
220 }
221
222 EXPECT_EQ(0, System(string("umount ") + original_dir));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223
224 // Cleanup generated files
Andrew de los Reyese5733992009-12-08 13:34:00 -0800225 EXPECT_EQ(0, System(string("rm -rf ") + original_dir));
226 EXPECT_EQ(0, System(string("rm -rf ") + new_dir));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000227 EXPECT_EQ(0, System(string("rm -f ") + original_image));
Andrew de los Reyese5733992009-12-08 13:34:00 -0800228 EXPECT_EQ(0, system("rm -f delta"));
229}
230
231TEST(InstallActionTest, FullUpdateTest) {
232 ObjectFeederAction<InstallPlan> feeder_action;
233 InstallAction install_action;
234 ObjectCollectorAction<string> collector_action;
235
236 BondActions(&feeder_action, &install_action);
237 BondActions(&install_action, &collector_action);
238
239 ActionProcessor processor;
240 processor.EnqueueAction(&feeder_action);
241 processor.EnqueueAction(&install_action);
242 processor.EnqueueAction(&collector_action);
243
244 InstallPlan install_plan(true, "", "", "delta", "install_path");
245 feeder_action.set_obj(install_plan);
246
247 processor.StartProcessing();
248 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
249 EXPECT_EQ("install_path", collector_action.object());
250}
251
252TEST(InstallActionTest, InvalidDeltaFileTest) {
253 ObjectFeederAction<InstallPlan> feeder_action;
254 InstallAction install_action;
255 ObjectCollectorAction<string> collector_action;
256
257 BondActions(&feeder_action, &install_action);
258 BondActions(&install_action, &collector_action);
259
260 ActionProcessor processor;
261 processor.EnqueueAction(&feeder_action);
262 processor.EnqueueAction(&install_action);
263 processor.EnqueueAction(&collector_action);
264
265 InstallPlan install_plan(false, "", "", "no_such_file", "install_path");
266 feeder_action.set_obj(install_plan);
267
268 processor.StartProcessing();
269 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
270 EXPECT_TRUE(collector_action.object().empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000271}
272
273} // namespace chromeos_update_engine