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

Side by Side Diff: filesystem_copier_action_unittest.cc

Issue 5516009: AU: Split applied update verification into a separate step. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/update_engine.git@master
Patch Set: review comments Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « filesystem_copier_action.cc ('k') | generate_delta_main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <fcntl.h> 5 #include <fcntl.h>
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include <base/eintr_wrapper.h> 11 #include <base/eintr_wrapper.h>
12 #include <base/string_util.h> 12 #include <base/string_util.h>
13 #include <glib.h> 13 #include <glib.h>
14 #include <gtest/gtest.h> 14 #include <gtest/gtest.h>
15 15
16 #include "update_engine/filesystem_copier_action.h" 16 #include "update_engine/filesystem_copier_action.h"
17 #include "update_engine/filesystem_iterator.h" 17 #include "update_engine/filesystem_iterator.h"
18 #include "update_engine/omaha_hash_calculator.h" 18 #include "update_engine/omaha_hash_calculator.h"
19 #include "update_engine/test_utils.h" 19 #include "update_engine/test_utils.h"
20 #include "update_engine/utils.h" 20 #include "update_engine/utils.h"
21 21
22 using std::set; 22 using std::set;
23 using std::string; 23 using std::string;
24 using std::vector; 24 using std::vector;
25 25
26 namespace chromeos_update_engine { 26 namespace chromeos_update_engine {
27 27
28 class FilesystemCopierActionTest : public ::testing::Test { 28 class FilesystemCopierActionTest : public ::testing::Test {
29 protected: 29 protected:
30 // |verify_hash|: 0 - no hash verification, 1 -- successful hash verification,
31 // 2 -- hash verification failure.
30 void DoTest(bool run_out_of_space, 32 void DoTest(bool run_out_of_space,
31 bool terminate_early, 33 bool terminate_early,
32 bool use_kernel_partition); 34 bool use_kernel_partition,
35 int verify_hash);
33 void SetUp() { 36 void SetUp() {
34 } 37 }
35 void TearDown() { 38 void TearDown() {
36 } 39 }
37 }; 40 };
38 41
39 class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { 42 class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate {
40 public: 43 public:
41 FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {} 44 FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {}
42 void ExitMainLoop() { 45 void ExitMainLoop() {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 processor->StartProcessing(); 86 processor->StartProcessing();
84 if (args->terminate_early) { 87 if (args->terminate_early) {
85 EXPECT_TRUE(args->filesystem_copier_action); 88 EXPECT_TRUE(args->filesystem_copier_action);
86 args->processor->StopProcessing(); 89 args->processor->StopProcessing();
87 } 90 }
88 return FALSE; 91 return FALSE;
89 } 92 }
90 93
91 TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) { 94 TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) {
92 ASSERT_EQ(0, getuid()); 95 ASSERT_EQ(0, getuid());
93 DoTest(false, false, false); 96 DoTest(false, false, false, 0);
97 DoTest(false, false, true, 0);
98 }
94 99
95 DoTest(false, false, true);
96 }
97 void FilesystemCopierActionTest::DoTest(bool run_out_of_space, 100 void FilesystemCopierActionTest::DoTest(bool run_out_of_space,
98 bool terminate_early, 101 bool terminate_early,
99 bool use_kernel_partition) { 102 bool use_kernel_partition,
103 int verify_hash) {
100 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); 104 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
101 105
102 string a_loop_file; 106 string a_loop_file;
103 string b_loop_file; 107 string b_loop_file;
104 108
105 EXPECT_TRUE(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", 109 EXPECT_TRUE(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX",
106 &a_loop_file, 110 &a_loop_file,
107 NULL)); 111 NULL));
108 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); 112 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file);
109 EXPECT_TRUE(utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", 113 EXPECT_TRUE(utils::MakeTempFile("/tmp/b_loop_file.XXXXXX",
(...skipping 25 matching lines...) Expand all
135 string b_dev = GetUnusedLoopDevice(); 139 string b_dev = GetUnusedLoopDevice();
136 EXPECT_FALSE(b_dev.empty()); 140 EXPECT_FALSE(b_dev.empty());
137 EXPECT_EQ(0, System(StringPrintf("losetup %s %s", 141 EXPECT_EQ(0, System(StringPrintf("losetup %s %s",
138 b_dev.c_str(), 142 b_dev.c_str(),
139 b_loop_file.c_str()))); 143 b_loop_file.c_str())));
140 ScopedLoopbackDeviceReleaser b_dev_releaser(b_dev); 144 ScopedLoopbackDeviceReleaser b_dev_releaser(b_dev);
141 145
142 // Set up the action objects 146 // Set up the action objects
143 InstallPlan install_plan; 147 InstallPlan install_plan;
144 install_plan.is_full_update = false; 148 install_plan.is_full_update = false;
145 if (use_kernel_partition) 149 if (verify_hash) {
146 install_plan.kernel_install_path = b_dev; 150 if (use_kernel_partition) {
147 else 151 install_plan.kernel_install_path = a_dev;
148 install_plan.install_path = b_dev; 152 install_plan.kernel_size =
153 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
154 EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(
155 a_loop_data,
156 &install_plan.kernel_hash));
157 } else {
158 install_plan.install_path = a_dev;
159 install_plan.rootfs_size =
160 kLoopFileSize - ((verify_hash == 2) ? 1 : 0);
161 EXPECT_TRUE(OmahaHashCalculator::RawHashOfData(
162 a_loop_data,
163 &install_plan.rootfs_hash));
164 }
165 } else {
166 if (use_kernel_partition) {
167 install_plan.kernel_install_path = b_dev;
168 } else {
169 install_plan.install_path = b_dev;
170 }
171 }
149 172
150 ActionProcessor processor; 173 ActionProcessor processor;
151 FilesystemCopierActionTestDelegate delegate; 174 FilesystemCopierActionTestDelegate delegate;
152 delegate.set_loop(loop); 175 delegate.set_loop(loop);
153 processor.set_delegate(&delegate); 176 processor.set_delegate(&delegate);
154 177
155 ObjectFeederAction<InstallPlan> feeder_action; 178 ObjectFeederAction<InstallPlan> feeder_action;
156 FilesystemCopierAction copier_action(use_kernel_partition); 179 FilesystemCopierAction copier_action(use_kernel_partition,
180 verify_hash != 0);
157 ObjectCollectorAction<InstallPlan> collector_action; 181 ObjectCollectorAction<InstallPlan> collector_action;
158 182
159 BondActions(&feeder_action, &copier_action); 183 BondActions(&feeder_action, &copier_action);
160 BondActions(&copier_action, &collector_action); 184 BondActions(&copier_action, &collector_action);
161 185
162 processor.EnqueueAction(&feeder_action); 186 processor.EnqueueAction(&feeder_action);
163 processor.EnqueueAction(&copier_action); 187 processor.EnqueueAction(&copier_action);
164 processor.EnqueueAction(&collector_action); 188 processor.EnqueueAction(&collector_action);
165 189
166 copier_action.set_copy_source(a_dev); 190 if (!verify_hash) {
191 copier_action.set_copy_source(a_dev);
192 }
167 feeder_action.set_obj(install_plan); 193 feeder_action.set_obj(install_plan);
168 194
169 StartProcessorCallbackArgs start_callback_args; 195 StartProcessorCallbackArgs start_callback_args;
170 start_callback_args.processor = &processor; 196 start_callback_args.processor = &processor;
171 start_callback_args.filesystem_copier_action = &copier_action; 197 start_callback_args.filesystem_copier_action = &copier_action;
172 start_callback_args.terminate_early = terminate_early; 198 start_callback_args.terminate_early = terminate_early;
173 199
174 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); 200 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args);
175 g_main_loop_run(loop); 201 g_main_loop_run(loop);
176 g_main_loop_unref(loop); 202 g_main_loop_unref(loop);
177 203
178 if (!terminate_early) 204 if (!terminate_early)
179 EXPECT_TRUE(delegate.ran()); 205 EXPECT_TRUE(delegate.ran());
180 if (run_out_of_space || terminate_early) { 206 if (run_out_of_space || terminate_early) {
181 EXPECT_EQ(kActionCodeError, delegate.code()); 207 EXPECT_EQ(kActionCodeError, delegate.code());
182 return; 208 return;
183 } 209 }
210 if (verify_hash == 2) {
211 EXPECT_EQ(use_kernel_partition ?
212 kActionCodeNewKernelVerificationError :
213 kActionCodeNewRootfsVerificationError,
214 delegate.code());
215 return;
216 }
184 EXPECT_EQ(kActionCodeSuccess, delegate.code()); 217 EXPECT_EQ(kActionCodeSuccess, delegate.code());
185 218
186 // Make sure everything in the out_image is there 219 // Make sure everything in the out_image is there
187 vector<char> a_out; 220 vector<char> a_out;
188 vector<char> b_out;
189 EXPECT_TRUE(utils::ReadFile(a_dev, &a_out)); 221 EXPECT_TRUE(utils::ReadFile(a_dev, &a_out));
190 EXPECT_TRUE(utils::ReadFile(b_dev, &b_out)); 222 if (!verify_hash) {
191 EXPECT_TRUE(ExpectVectorsEq(a_out, b_out)); 223 vector<char> b_out;
224 EXPECT_TRUE(utils::ReadFile(b_dev, &b_out));
225 EXPECT_TRUE(ExpectVectorsEq(a_out, b_out));
226 }
192 EXPECT_TRUE(ExpectVectorsEq(a_loop_data, a_out)); 227 EXPECT_TRUE(ExpectVectorsEq(a_loop_data, a_out));
193 228
194 EXPECT_TRUE(collector_action.object() == install_plan); 229 EXPECT_TRUE(collector_action.object() == install_plan);
195 if (terminate_early) {
196 // sleep so OS can clean up
197 sleep(1);
198 }
199 } 230 }
200 231
201 class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { 232 class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate {
202 public: 233 public:
203 void ActionCompleted(ActionProcessor* processor, 234 void ActionCompleted(ActionProcessor* processor,
204 AbstractAction* action, 235 AbstractAction* action,
205 ActionExitCode code) { 236 ActionExitCode code) {
206 if (action->Type() == FilesystemCopierAction::StaticType()) { 237 if (action->Type() == FilesystemCopierAction::StaticType()) {
207 ran_ = true; 238 ran_ = true;
208 code_ = code; 239 code_ = code;
209 } 240 }
210 } 241 }
211 GMainLoop *loop_; 242 GMainLoop *loop_;
212 bool ran_; 243 bool ran_;
213 ActionExitCode code_; 244 ActionExitCode code_;
214 }; 245 };
215 246
216 TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { 247 TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) {
217 ActionProcessor processor; 248 ActionProcessor processor;
218 FilesystemCopierActionTest2Delegate delegate; 249 FilesystemCopierActionTest2Delegate delegate;
219 250
220 processor.set_delegate(&delegate); 251 processor.set_delegate(&delegate);
221 252
222 FilesystemCopierAction copier_action(false); 253 FilesystemCopierAction copier_action(false, false);
223 ObjectCollectorAction<InstallPlan> collector_action; 254 ObjectCollectorAction<InstallPlan> collector_action;
224 255
225 BondActions(&copier_action, &collector_action); 256 BondActions(&copier_action, &collector_action);
226 257
227 processor.EnqueueAction(&copier_action); 258 processor.EnqueueAction(&copier_action);
228 processor.EnqueueAction(&collector_action); 259 processor.EnqueueAction(&collector_action);
229 processor.StartProcessing(); 260 processor.StartProcessing();
230 EXPECT_FALSE(processor.IsRunning()); 261 EXPECT_FALSE(processor.IsRunning());
231 EXPECT_TRUE(delegate.ran_); 262 EXPECT_TRUE(delegate.ran_);
232 EXPECT_EQ(kActionCodeError, delegate.code_); 263 EXPECT_EQ(kActionCodeError, delegate.code_);
233 } 264 }
234 265
235 TEST_F(FilesystemCopierActionTest, FullUpdateTest) { 266 TEST_F(FilesystemCopierActionTest, FullUpdateTest) {
236 ActionProcessor processor; 267 ActionProcessor processor;
237 FilesystemCopierActionTest2Delegate delegate; 268 FilesystemCopierActionTest2Delegate delegate;
238 269
239 processor.set_delegate(&delegate); 270 processor.set_delegate(&delegate);
240 271
241 ObjectFeederAction<InstallPlan> feeder_action; 272 ObjectFeederAction<InstallPlan> feeder_action;
242 const char* kUrl = "http://some/url"; 273 const char* kUrl = "http://some/url";
243 InstallPlan install_plan(true, false, kUrl, 0, "", "", ""); 274 InstallPlan install_plan(true, false, kUrl, 0, "", "", "");
244 feeder_action.set_obj(install_plan); 275 feeder_action.set_obj(install_plan);
245 FilesystemCopierAction copier_action(false); 276 FilesystemCopierAction copier_action(false, false);
246 ObjectCollectorAction<InstallPlan> collector_action; 277 ObjectCollectorAction<InstallPlan> collector_action;
247 278
248 BondActions(&feeder_action, &copier_action); 279 BondActions(&feeder_action, &copier_action);
249 BondActions(&copier_action, &collector_action); 280 BondActions(&copier_action, &collector_action);
250 281
251 processor.EnqueueAction(&feeder_action); 282 processor.EnqueueAction(&feeder_action);
252 processor.EnqueueAction(&copier_action); 283 processor.EnqueueAction(&copier_action);
253 processor.EnqueueAction(&collector_action); 284 processor.EnqueueAction(&collector_action);
254 processor.StartProcessing(); 285 processor.StartProcessing();
255 EXPECT_FALSE(processor.IsRunning()); 286 EXPECT_FALSE(processor.IsRunning());
256 EXPECT_TRUE(delegate.ran_); 287 EXPECT_TRUE(delegate.ran_);
257 EXPECT_EQ(kActionCodeSuccess, delegate.code_); 288 EXPECT_EQ(kActionCodeSuccess, delegate.code_);
258 EXPECT_EQ(kUrl, collector_action.object().download_url); 289 EXPECT_EQ(kUrl, collector_action.object().download_url);
259 } 290 }
260 291
261 TEST_F(FilesystemCopierActionTest, ResumeTest) { 292 TEST_F(FilesystemCopierActionTest, ResumeTest) {
262 ActionProcessor processor; 293 ActionProcessor processor;
263 FilesystemCopierActionTest2Delegate delegate; 294 FilesystemCopierActionTest2Delegate delegate;
264 295
265 processor.set_delegate(&delegate); 296 processor.set_delegate(&delegate);
266 297
267 ObjectFeederAction<InstallPlan> feeder_action; 298 ObjectFeederAction<InstallPlan> feeder_action;
268 const char* kUrl = "http://some/url"; 299 const char* kUrl = "http://some/url";
269 InstallPlan install_plan(false, true, kUrl, 0, "", "", ""); 300 InstallPlan install_plan(false, true, kUrl, 0, "", "", "");
270 feeder_action.set_obj(install_plan); 301 feeder_action.set_obj(install_plan);
271 FilesystemCopierAction copier_action(false); 302 FilesystemCopierAction copier_action(false, false);
272 ObjectCollectorAction<InstallPlan> collector_action; 303 ObjectCollectorAction<InstallPlan> collector_action;
273 304
274 BondActions(&feeder_action, &copier_action); 305 BondActions(&feeder_action, &copier_action);
275 BondActions(&copier_action, &collector_action); 306 BondActions(&copier_action, &collector_action);
276 307
277 processor.EnqueueAction(&feeder_action); 308 processor.EnqueueAction(&feeder_action);
278 processor.EnqueueAction(&copier_action); 309 processor.EnqueueAction(&copier_action);
279 processor.EnqueueAction(&collector_action); 310 processor.EnqueueAction(&collector_action);
280 processor.StartProcessing(); 311 processor.StartProcessing();
281 EXPECT_FALSE(processor.IsRunning()); 312 EXPECT_FALSE(processor.IsRunning());
(...skipping 10 matching lines...) Expand all
292 323
293 ObjectFeederAction<InstallPlan> feeder_action; 324 ObjectFeederAction<InstallPlan> feeder_action;
294 InstallPlan install_plan(false, 325 InstallPlan install_plan(false,
295 false, 326 false,
296 "", 327 "",
297 0, 328 0,
298 "", 329 "",
299 "/no/such/file", 330 "/no/such/file",
300 "/no/such/file"); 331 "/no/such/file");
301 feeder_action.set_obj(install_plan); 332 feeder_action.set_obj(install_plan);
302 FilesystemCopierAction copier_action(false); 333 FilesystemCopierAction copier_action(false, false);
303 ObjectCollectorAction<InstallPlan> collector_action; 334 ObjectCollectorAction<InstallPlan> collector_action;
304 335
305 BondActions(&copier_action, &collector_action); 336 BondActions(&copier_action, &collector_action);
306 337
307 processor.EnqueueAction(&feeder_action); 338 processor.EnqueueAction(&feeder_action);
308 processor.EnqueueAction(&copier_action); 339 processor.EnqueueAction(&copier_action);
309 processor.EnqueueAction(&collector_action); 340 processor.EnqueueAction(&collector_action);
310 processor.StartProcessing(); 341 processor.StartProcessing();
311 EXPECT_FALSE(processor.IsRunning()); 342 EXPECT_FALSE(processor.IsRunning());
312 EXPECT_TRUE(delegate.ran_); 343 EXPECT_TRUE(delegate.ran_);
313 EXPECT_EQ(kActionCodeError, delegate.code_); 344 EXPECT_EQ(kActionCodeError, delegate.code_);
314 } 345 }
315 346
347 TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashTest) {
348 ASSERT_EQ(0, getuid());
349 DoTest(false, false, false, 1);
350 DoTest(false, false, true, 1);
351 }
352
353 TEST_F(FilesystemCopierActionTest, RunAsRootVerifyHashFailTest) {
354 ASSERT_EQ(0, getuid());
355 DoTest(false, false, false, 2);
356 DoTest(false, false, true, 2);
357 }
358
316 TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { 359 TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) {
317 ASSERT_EQ(0, getuid()); 360 ASSERT_EQ(0, getuid());
318 DoTest(true, false, false); 361 DoTest(true, false, false, 0);
319 } 362 }
320 363
321 TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { 364 TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) {
322 ASSERT_EQ(0, getuid()); 365 ASSERT_EQ(0, getuid());
323 DoTest(false, true, false); 366 DoTest(false, true, false, 0);
324 } 367 }
325 368
326 TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) { 369 TEST_F(FilesystemCopierActionTest, RunAsRootDetermineFilesystemSizeTest) {
327 string img; 370 string img;
328 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL)); 371 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL));
329 ScopedPathUnlinker img_unlinker(img); 372 ScopedPathUnlinker img_unlinker(img);
330 CreateExtImageAtPath(img, NULL); 373 CreateExtImageAtPath(img, NULL);
331 // Extend the "partition" holding the file system from 10MiB to 20MiB. 374 // Extend the "partition" holding the file system from 10MiB to 20MiB.
332 EXPECT_EQ(0, System(StringPrintf( 375 EXPECT_EQ(0, System(StringPrintf(
333 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1", 376 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
334 img.c_str()))); 377 img.c_str())));
335 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img)); 378 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
336 379
337 for (int i = 0; i < 2; ++i) { 380 for (int i = 0; i < 2; ++i) {
338 bool is_kernel = i == 1; 381 bool is_kernel = i == 1;
339 FilesystemCopierAction action(is_kernel); 382 FilesystemCopierAction action(is_kernel, false);
340 EXPECT_EQ(kint64max, action.filesystem_size_); 383 EXPECT_EQ(kint64max, action.filesystem_size_);
341 { 384 {
342 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY)); 385 int fd = HANDLE_EINTR(open(img.c_str(), O_RDONLY));
343 EXPECT_TRUE(fd > 0); 386 EXPECT_TRUE(fd > 0);
344 ScopedFdCloser fd_closer(&fd); 387 ScopedFdCloser fd_closer(&fd);
345 action.DetermineFilesystemSize(fd); 388 action.DetermineFilesystemSize(fd);
346 } 389 }
347 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024, 390 EXPECT_EQ(is_kernel ? kint64max : 10 * 1024 * 1024,
348 action.filesystem_size_); 391 action.filesystem_size_);
349 } 392 }
350 } 393 }
351 394
352 395
353 } // namespace chromeos_update_engine 396 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « filesystem_copier_action.cc ('k') | generate_delta_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698