Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: src/platform/update_engine/install_action_unittest.cc

Issue 545072: AU: Gut code for old updater. New protobuf for v2 updater. (Closed)
Patch Set: better comments Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // 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>
8 #include <utime.h>
9 #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
20 using chromeos_update_engine::DeltaDiffGenerator;
21 using chromeos_update_engine::kRandomString;
22 using chromeos_update_engine::System;
23 using chromeos_update_engine::utils::WriteFile;
24 using std::set;
25 using std::string;
26 using std::vector;
27
28 namespace {
29 void GenerateFilesAtPath(const string& base) {
30 EXPECT_EQ(0, System(StringPrintf("echo hi > %s/hi", base.c_str())));
31 EXPECT_EQ(0, System(StringPrintf("ln %s/hi %s/hard_link", base.c_str(),
32 base.c_str())));
33 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())));
50 EXPECT_TRUE(WriteFile((base + "/big_file").c_str(),
51 reinterpret_cast<const char*>(kRandomString),
52 sizeof(kRandomString)));
53 }
54
55 // Returns true if files at paths a, b are equal and there are no errors.
56 bool 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);
68 if (!S_ISDIR(a_stbuf.st_mode))
69 TEST_AND_RETURN_FALSE(a_stbuf.st_nlink == b_stbuf.st_nlink);
70 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
83 class 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
95 namespace chromeos_update_engine {
96
97 class InstallActionTest : public ::testing::Test { };
98
99 TEST(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 }
127 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("chmod 03755 '%s/newdir'", new_dir_cstr)));
130 EXPECT_EQ(0, System(StringPrintf("mkdir -p '%s/newdir/x'", new_dir_cstr)));
131 EXPECT_EQ(0, System(StringPrintf("echo -n foo > '%s/newdir/x/file'",
132 new_dir_cstr)));
133 EXPECT_EQ(0, System(StringPrintf("touch '%s/new_empty'", new_dir_cstr)));
134 EXPECT_EQ(0, System(StringPrintf("chmod 04644 '%s/new_empty'",
135 new_dir_cstr)));
136 EXPECT_EQ(0, System(StringPrintf("echo -n x >> '%s/big_file'",
137 new_dir_cstr)));
138 EXPECT_EQ(0, System(StringPrintf("chmod 02644 '%s/big_file'", new_dir_cstr)));
139 // Make a symlink that compresses well:
140 EXPECT_EQ(0, System(StringPrintf(
141 "ln -s "
142 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
143 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
144 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx "
145 "'%s/compress_sym'", new_dir_cstr)));
146
147 // Make a device that will compress
148 EXPECT_EQ(0, mknod((new_dir + "/bdev_gz").c_str(), S_IFCHR | 0644, 0));
149
150 // Make a diff
151 DeltaArchiveManifest* delta =
152 DeltaDiffGenerator::EncodeMetadataToProtoBuffer(new_dir.c_str());
153 EXPECT_TRUE(NULL != delta);
154 EXPECT_TRUE(DeltaDiffGenerator::EncodeDataToDeltaFile(delta,
155 original_dir,
156 new_dir,
157 "delta",
158 set<string>(),
159 new_dir + "/bdev_gz"));
160
161 ASSERT_EQ(0, System(string("umount ") + original_dir));
162
163 ObjectFeederAction<InstallPlan> feeder_action;
164 InstallAction install_action;
165 ObjectCollectorAction<string> collector_action;
166
167 BondActions(&feeder_action, &install_action);
168 BondActions(&install_action, &collector_action);
169
170 ActionProcessor processor;
171 processor.EnqueueAction(&feeder_action);
172 processor.EnqueueAction(&install_action);
173 processor.EnqueueAction(&collector_action);
174
175 InstallPlan install_plan(false, "", "", "delta", loop_dev);
176 feeder_action.set_obj(install_plan);
177
178 processor.StartProcessing();
179 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
180
181 EXPECT_EQ(loop_dev, collector_action.object());
182
183 ASSERT_EQ(0, System(string("mount ") + loop_dev + " " + original_dir));
184
185 // Check that original_dir and new_dir are equal
186 int original_count = 0;
187 LOG(INFO) << "checking old";
188 {
189 FilesystemIterator iter(original_dir,
190 utils::SetWithValue<string>("/lost+found"));
191 for (; !iter.IsEnd(); iter.Increment()) {
192 original_count++;
193 LOG(INFO) << "checking path: " << iter.GetPartialPath();
194 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(),
195 new_dir + iter.GetPartialPath()));
196 }
197 EXPECT_FALSE(iter.IsErr());
198 }
199 LOG(INFO) << "checking new";
200 int new_count = 0;
201 {
202 FilesystemIterator iter(new_dir, set<string>());
203 for (; !iter.IsEnd(); iter.Increment()) {
204 new_count++;
205 LOG(INFO) << "checking path: " << iter.GetPartialPath();
206 EXPECT_TRUE(FilesEqual(original_dir + iter.GetPartialPath(),
207 new_dir + iter.GetPartialPath()));
208 }
209 EXPECT_FALSE(iter.IsErr());
210 }
211 LOG(INFO) << "new_count = " << new_count;
212 EXPECT_EQ(new_count, original_count);
213 EXPECT_EQ(20, original_count);
214
215 // Make sure hard-link installed properly
216 {
217 struct stat hard_link_stbuf;
218 struct stat hi_stbuf;
219 EXPECT_EQ(0, lstat((string(new_dir) + "/hard_link").c_str(),
220 &hard_link_stbuf));
221 EXPECT_EQ(0, lstat((string(new_dir) + "/hi").c_str(), &hi_stbuf));
222 EXPECT_EQ(hard_link_stbuf.st_mode, hi_stbuf.st_mode);
223 EXPECT_EQ(2, hard_link_stbuf.st_nlink);
224 EXPECT_EQ(2, hi_stbuf.st_nlink);
225 EXPECT_EQ(hi_stbuf.st_ino, hard_link_stbuf.st_ino);
226 }
227
228 EXPECT_EQ(0, System(string("umount ") + original_dir));
229
230 // Cleanup generated files
231 EXPECT_EQ(0, System(string("rm -rf ") + original_dir));
232 EXPECT_EQ(0, System(string("rm -rf ") + new_dir));
233 EXPECT_EQ(0, System(string("rm -f ") + original_image));
234 EXPECT_EQ(0, system("rm -f delta"));
235 }
236
237 TEST(InstallActionTest, FullUpdateTest) {
238 ObjectFeederAction<InstallPlan> feeder_action;
239 InstallAction install_action;
240 ObjectCollectorAction<string> collector_action;
241
242 BondActions(&feeder_action, &install_action);
243 BondActions(&install_action, &collector_action);
244
245 ActionProcessor processor;
246 processor.EnqueueAction(&feeder_action);
247 processor.EnqueueAction(&install_action);
248 processor.EnqueueAction(&collector_action);
249
250 InstallPlan install_plan(true, "", "", "delta", "install_path");
251 feeder_action.set_obj(install_plan);
252
253 processor.StartProcessing();
254 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
255 EXPECT_EQ("install_path", collector_action.object());
256 }
257
258 TEST(InstallActionTest, InvalidDeltaFileTest) {
259 ObjectFeederAction<InstallPlan> feeder_action;
260 InstallAction install_action;
261 ObjectCollectorAction<string> collector_action;
262
263 BondActions(&feeder_action, &install_action);
264 BondActions(&install_action, &collector_action);
265
266 ActionProcessor processor;
267 processor.EnqueueAction(&feeder_action);
268 processor.EnqueueAction(&install_action);
269 processor.EnqueueAction(&collector_action);
270
271 InstallPlan install_plan(false, "", "", "no_such_file", "install_path");
272 feeder_action.set_obj(install_plan);
273
274 processor.StartProcessing();
275 EXPECT_FALSE(processor.IsRunning()) << "Update to handle async actions";
276 EXPECT_TRUE(collector_action.object().empty());
277 }
278
279 } // namespace chromeos_update_engine
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698