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

Side by Side Diff: chrome/installer/util/move_tree_work_item_unittest.cc

Issue 9700038: ScopedProcessInformation protects against process/thread handle leaks from CreateProcess calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove changes from another CL. Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium 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 <windows.h> 5 #include <windows.h>
6 6
7 #include <fstream> 7 #include <fstream>
8 8
9 #include "base/base_paths.h" 9 #include "base/base_paths.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/process_util.h" 13 #include "base/process_util.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/win/scoped_process_information.h"
15 #include "chrome/installer/util/work_item.h" 16 #include "chrome/installer/util/work_item.h"
16 #include "chrome/installer/util/move_tree_work_item.h" 17 #include "chrome/installer/util/move_tree_work_item.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace { 20 namespace {
20 class MoveTreeWorkItemTest : public testing::Test { 21 class MoveTreeWorkItemTest : public testing::Test {
21 protected: 22 protected:
22 virtual void SetUp() { 23 virtual void SetUp() {
23 ASSERT_TRUE(temp_from_dir_.CreateUniqueTempDir()); 24 ASSERT_TRUE(temp_from_dir_.CreateUniqueTempDir());
24 ASSERT_TRUE(temp_to_dir_.CreateUniqueTempDir()); 25 ASSERT_TRUE(temp_to_dir_.CreateUniqueTempDir());
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 wchar_t exe_full_path_str[MAX_PATH]; 274 wchar_t exe_full_path_str[MAX_PATH];
274 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); 275 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH);
275 FilePath exe_full_path(exe_full_path_str); 276 FilePath exe_full_path(exe_full_path_str);
276 FilePath to_file(to_dir); 277 FilePath to_file(to_dir);
277 to_file = to_file.AppendASCII("To_File"); 278 to_file = to_file.AppendASCII("To_File");
278 file_util::CopyFile(exe_full_path, to_file); 279 file_util::CopyFile(exe_full_path, to_file);
279 ASSERT_TRUE(file_util::PathExists(to_file)); 280 ASSERT_TRUE(file_util::PathExists(to_file));
280 281
281 // Run the executable in destination path 282 // Run the executable in destination path
282 STARTUPINFOW si = {sizeof(si)}; 283 STARTUPINFOW si = {sizeof(si)};
283 PROCESS_INFORMATION pi = {0}; 284 base::win::ScopedProcessInformation pi;
284 ASSERT_TRUE(::CreateProcess(NULL, 285 ASSERT_TRUE(::CreateProcess(NULL,
285 const_cast<wchar_t*>(to_file.value().c_str()), 286 const_cast<wchar_t*>(to_file.value().c_str()),
286 NULL, NULL, FALSE, 287 NULL, NULL, FALSE,
287 CREATE_NO_WINDOW | CREATE_SUSPENDED, 288 CREATE_NO_WINDOW | CREATE_SUSPENDED,
288 NULL, NULL, &si, &pi)); 289 NULL, NULL, &si, pi.Receive()));
289 290
290 // test Do() 291 // test Do()
291 scoped_ptr<MoveTreeWorkItem> work_item( 292 scoped_ptr<MoveTreeWorkItem> work_item(
292 WorkItem::CreateMoveTreeWorkItem(from_file, 293 WorkItem::CreateMoveTreeWorkItem(from_file,
293 to_file, 294 to_file,
294 temp_to_dir_.path(), 295 temp_to_dir_.path(),
295 WorkItem::ALWAYS_MOVE)); 296 WorkItem::ALWAYS_MOVE));
296 EXPECT_TRUE(work_item->Do()); 297 EXPECT_TRUE(work_item->Do());
297 298
298 EXPECT_TRUE(file_util::PathExists(from_dir)); 299 EXPECT_TRUE(file_util::PathExists(from_dir));
299 EXPECT_FALSE(file_util::PathExists(from_file)); 300 EXPECT_FALSE(file_util::PathExists(from_file));
300 EXPECT_TRUE(file_util::PathExists(to_dir)); 301 EXPECT_TRUE(file_util::PathExists(to_dir));
301 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1)); 302 EXPECT_EQ(0, ReadTextFile(to_file).compare(kTextContent1));
302 303
303 // test rollback() 304 // test rollback()
304 work_item->Rollback(); 305 work_item->Rollback();
305 306
306 EXPECT_TRUE(file_util::PathExists(from_dir)); 307 EXPECT_TRUE(file_util::PathExists(from_dir));
307 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 308 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
308 EXPECT_TRUE(file_util::PathExists(to_dir)); 309 EXPECT_TRUE(file_util::PathExists(to_dir));
309 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); 310 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file));
310 311
311 TerminateProcess(pi.hProcess, 0); 312 TerminateProcess(pi.Get().hProcess, 0);
312 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); 313 EXPECT_TRUE(WaitForSingleObject(pi.Get().hProcess, 10000) == WAIT_OBJECT_0);
313 CloseHandle(pi.hProcess);
314 CloseHandle(pi.hThread);
315 } 314 }
316 315
317 // Move one file that is in use to destination. 316 // Move one file that is in use to destination.
318 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { 317 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) {
319 // Create an executable for source by copying ourself to a new source dir. 318 // Create an executable for source by copying ourself to a new source dir.
320 FilePath from_dir(temp_from_dir_.path()); 319 FilePath from_dir(temp_from_dir_.path());
321 from_dir = from_dir.AppendASCII("From_Dir"); 320 from_dir = from_dir.AppendASCII("From_Dir");
322 file_util::CreateDirectory(from_dir); 321 file_util::CreateDirectory(from_dir);
323 ASSERT_TRUE(file_util::PathExists(from_dir)); 322 ASSERT_TRUE(file_util::PathExists(from_dir));
324 323
(...skipping 11 matching lines...) Expand all
336 file_util::CreateDirectory(to_dir); 335 file_util::CreateDirectory(to_dir);
337 ASSERT_TRUE(file_util::PathExists(to_dir)); 336 ASSERT_TRUE(file_util::PathExists(to_dir));
338 337
339 FilePath to_file(to_dir); 338 FilePath to_file(to_dir);
340 to_file = to_file.AppendASCII("To_File"); 339 to_file = to_file.AppendASCII("To_File");
341 CreateTextFile(to_file.value(), kTextContent1); 340 CreateTextFile(to_file.value(), kTextContent1);
342 ASSERT_TRUE(file_util::PathExists(to_file)); 341 ASSERT_TRUE(file_util::PathExists(to_file));
343 342
344 // Run the executable in source path 343 // Run the executable in source path
345 STARTUPINFOW si = {sizeof(si)}; 344 STARTUPINFOW si = {sizeof(si)};
346 PROCESS_INFORMATION pi = {0}; 345 base::win::ScopedProcessInformation pi;
347 ASSERT_TRUE(::CreateProcess(NULL, 346 ASSERT_TRUE(::CreateProcess(NULL,
348 const_cast<wchar_t*>(from_file.value().c_str()), 347 const_cast<wchar_t*>(from_file.value().c_str()),
349 NULL, NULL, FALSE, 348 NULL, NULL, FALSE,
350 CREATE_NO_WINDOW | CREATE_SUSPENDED, 349 CREATE_NO_WINDOW | CREATE_SUSPENDED,
351 NULL, NULL, &si, &pi)); 350 NULL, NULL, &si, pi.Receive()));
352 351
353 // test Do() 352 // test Do()
354 scoped_ptr<MoveTreeWorkItem> work_item( 353 scoped_ptr<MoveTreeWorkItem> work_item(
355 WorkItem::CreateMoveTreeWorkItem(from_file, 354 WorkItem::CreateMoveTreeWorkItem(from_file,
356 to_file, 355 to_file,
357 temp_to_dir_.path(), 356 temp_to_dir_.path(),
358 WorkItem::ALWAYS_MOVE)); 357 WorkItem::ALWAYS_MOVE));
359 EXPECT_TRUE(work_item->Do()); 358 EXPECT_TRUE(work_item->Do());
360 359
361 EXPECT_TRUE(file_util::PathExists(from_dir)); 360 EXPECT_TRUE(file_util::PathExists(from_dir));
362 EXPECT_FALSE(file_util::PathExists(from_file)); 361 EXPECT_FALSE(file_util::PathExists(from_file));
363 EXPECT_TRUE(file_util::PathExists(to_dir)); 362 EXPECT_TRUE(file_util::PathExists(to_dir));
364 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); 363 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file));
365 364
366 // Close the process and make sure all the conditions after Do() are 365 // Close the process and make sure all the conditions after Do() are
367 // still true. 366 // still true.
368 TerminateProcess(pi.hProcess, 0); 367 TerminateProcess(pi.Get().hProcess, 0);
369 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); 368 EXPECT_TRUE(WaitForSingleObject(pi.Get().hProcess, 10000) == WAIT_OBJECT_0);
370 CloseHandle(pi.hProcess);
371 CloseHandle(pi.hThread);
372 369
373 EXPECT_TRUE(file_util::PathExists(from_dir)); 370 EXPECT_TRUE(file_util::PathExists(from_dir));
374 EXPECT_FALSE(file_util::PathExists(from_file)); 371 EXPECT_FALSE(file_util::PathExists(from_file));
375 EXPECT_TRUE(file_util::PathExists(to_dir)); 372 EXPECT_TRUE(file_util::PathExists(to_dir));
376 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); 373 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file));
377 374
378 // test rollback() 375 // test rollback()
379 work_item->Rollback(); 376 work_item->Rollback();
380 377
381 EXPECT_TRUE(file_util::PathExists(from_dir)); 378 EXPECT_TRUE(file_util::PathExists(from_dir));
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 // the source files. 540 // the source files.
544 EXPECT_TRUE(file_util::PathExists(from_dir1)); 541 EXPECT_TRUE(file_util::PathExists(from_dir1));
545 EXPECT_TRUE(file_util::PathExists(to_dir)); 542 EXPECT_TRUE(file_util::PathExists(to_dir));
546 EXPECT_TRUE(file_util::PathExists(orig_to_file)); 543 EXPECT_TRUE(file_util::PathExists(orig_to_file));
547 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1)); 544 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(kTextContent1));
548 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1)); 545 EXPECT_EQ(0, ReadTextFile(from_file).compare(kTextContent1));
549 546
550 // Also, after rollback the new "to" file should be gone. 547 // Also, after rollback the new "to" file should be gone.
551 EXPECT_FALSE(file_util::PathExists(new_to_file2)); 548 EXPECT_FALSE(file_util::PathExists(new_to_file2));
552 } 549 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698