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

Side by Side Diff: base/process/process_util_unittest.cc

Issue 2950153002: Improve process launch handle sharing API. (Closed)
Patch Set: Fix Created 3 years, 5 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #define _CRT_SECURE_NO_WARNINGS 5 #define _CRT_SECURE_NO_WARNINGS
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 TEST_F(ProcessUtilTest, InheritSpecifiedHandles) { 477 TEST_F(ProcessUtilTest, InheritSpecifiedHandles) {
478 // Manually create the event, so that it can be inheritable. 478 // Manually create the event, so that it can be inheritable.
479 SECURITY_ATTRIBUTES security_attributes = {}; 479 SECURITY_ATTRIBUTES security_attributes = {};
480 security_attributes.nLength = static_cast<DWORD>(sizeof(security_attributes)); 480 security_attributes.nLength = static_cast<DWORD>(sizeof(security_attributes));
481 security_attributes.lpSecurityDescriptor = NULL; 481 security_attributes.lpSecurityDescriptor = NULL;
482 security_attributes.bInheritHandle = true; 482 security_attributes.bInheritHandle = true;
483 483
484 // Takes ownership of the event handle. 484 // Takes ownership of the event handle.
485 base::WaitableEvent event(base::win::ScopedHandle( 485 base::WaitableEvent event(base::win::ScopedHandle(
486 CreateEvent(&security_attributes, true, false, NULL))); 486 CreateEvent(&security_attributes, true, false, NULL)));
487 base::HandlesToInheritVector handles_to_inherit;
488 handles_to_inherit.push_back(event.handle());
489 base::LaunchOptions options; 487 base::LaunchOptions options;
490 options.handles_to_inherit = &handles_to_inherit; 488 options.handles_to_inherit.push_back(event.handle());
491 489
492 base::CommandLine cmd_line = MakeCmdLine("TriggerEventChildProcess"); 490 base::CommandLine cmd_line = MakeCmdLine("TriggerEventChildProcess");
493 cmd_line.AppendSwitchASCII( 491 cmd_line.AppendSwitchASCII(
494 kEventToTriggerHandleSwitch, 492 kEventToTriggerHandleSwitch,
495 base::Uint64ToString(reinterpret_cast<uint64_t>(event.handle()))); 493 base::Uint64ToString(reinterpret_cast<uint64_t>(event.handle())));
496 494
497 // This functionality actually requires Vista or later. Make sure that it 495 // This functionality actually requires Vista or later. Make sure that it
498 // fails properly on XP. 496 // fails properly on XP.
499 if (base::win::GetVersion() < base::win::VERSION_VISTA) { 497 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
500 EXPECT_FALSE(base::LaunchProcess(cmd_line, options).IsValid()); 498 EXPECT_FALSE(base::LaunchProcess(cmd_line, options).IsValid());
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 DPCHECK(ret == 0); 622 DPCHECK(ret == 0);
625 623
626 return 0; 624 return 0;
627 } 625 }
628 626
629 int ProcessUtilTest::CountOpenFDsInChild() { 627 int ProcessUtilTest::CountOpenFDsInChild() {
630 int fds[2]; 628 int fds[2];
631 if (pipe(fds) < 0) 629 if (pipe(fds) < 0)
632 NOTREACHED(); 630 NOTREACHED();
633 631
634 base::FileHandleMappingVector fd_mapping_vec;
635 fd_mapping_vec.push_back(std::pair<int, int>(fds[1], kChildPipe));
636 base::LaunchOptions options; 632 base::LaunchOptions options;
637 options.fds_to_remap = &fd_mapping_vec; 633 options.fds_to_remap.push_back(std::pair<int, int>(fds[1], kChildPipe));
638 base::SpawnChildResult spawn_child = 634 base::SpawnChildResult spawn_child =
639 SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options); 635 SpawnChildWithOptions("ProcessUtilsLeakFDChildProcess", options);
640 CHECK(spawn_child.process.IsValid()); 636 CHECK(spawn_child.process.IsValid());
641 int ret = IGNORE_EINTR(close(fds[1])); 637 int ret = IGNORE_EINTR(close(fds[1]));
642 DPCHECK(ret == 0); 638 DPCHECK(ret == 0);
643 639
644 // Read number of open files in client process from pipe; 640 // Read number of open files in client process from pipe;
645 int num_open_files = -1; 641 int num_open_files = -1;
646 ssize_t bytes_read = 642 ssize_t bytes_read =
647 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files))); 643 HANDLE_EINTR(read(fds[0], &num_open_files, sizeof(num_open_files)));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 ret = IGNORE_EINTR(close(dev_null)); 687 ret = IGNORE_EINTR(close(dev_null));
692 DPCHECK(ret == 0); 688 DPCHECK(ret == 0);
693 } 689 }
694 690
695 namespace { 691 namespace {
696 692
697 std::string TestLaunchProcess(const std::vector<std::string>& args, 693 std::string TestLaunchProcess(const std::vector<std::string>& args,
698 const base::EnvironmentMap& env_changes, 694 const base::EnvironmentMap& env_changes,
699 const bool clear_environ, 695 const bool clear_environ,
700 const int clone_flags) { 696 const int clone_flags) {
701 base::FileHandleMappingVector fds_to_remap;
702
703 int fds[2]; 697 int fds[2];
704 PCHECK(pipe(fds) == 0); 698 PCHECK(pipe(fds) == 0);
705 699
706 fds_to_remap.push_back(std::make_pair(fds[1], 1));
707 base::LaunchOptions options; 700 base::LaunchOptions options;
708 options.wait = true; 701 options.wait = true;
709 options.environ = env_changes; 702 options.environ = env_changes;
710 options.clear_environ = clear_environ; 703 options.clear_environ = clear_environ;
711 options.fds_to_remap = &fds_to_remap; 704 options.fds_to_remap.push_back(std::make_pair(fds[1], 1));
712 #if defined(OS_LINUX) 705 #if defined(OS_LINUX)
713 options.clone_flags = clone_flags; 706 options.clone_flags = clone_flags;
714 #else 707 #else
715 CHECK_EQ(0, clone_flags); 708 CHECK_EQ(0, clone_flags);
716 #endif // defined(OS_LINUX) 709 #endif // defined(OS_LINUX)
717 EXPECT_TRUE(base::LaunchProcess(args, options).IsValid()); 710 EXPECT_TRUE(base::LaunchProcess(args, options).IsValid());
718 PCHECK(IGNORE_EINTR(close(fds[1])) == 0); 711 PCHECK(IGNORE_EINTR(close(fds[1])) == 0);
719 712
720 char buf[512]; 713 char buf[512];
721 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); 714 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf)));
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 int fd_; 938 int fd_;
946 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate); 939 DISALLOW_COPY_AND_ASSIGN(ReadFromPipeDelegate);
947 }; 940 };
948 941
949 TEST_F(ProcessUtilTest, PreExecHook) { 942 TEST_F(ProcessUtilTest, PreExecHook) {
950 int pipe_fds[2]; 943 int pipe_fds[2];
951 ASSERT_EQ(0, pipe(pipe_fds)); 944 ASSERT_EQ(0, pipe(pipe_fds));
952 945
953 base::ScopedFD read_fd(pipe_fds[0]); 946 base::ScopedFD read_fd(pipe_fds[0]);
954 base::ScopedFD write_fd(pipe_fds[1]); 947 base::ScopedFD write_fd(pipe_fds[1]);
955 base::FileHandleMappingVector fds_to_remap;
956 fds_to_remap.push_back(std::make_pair(read_fd.get(), read_fd.get()));
957 948
958 ReadFromPipeDelegate read_from_pipe_delegate(read_fd.get()); 949 ReadFromPipeDelegate read_from_pipe_delegate(read_fd.get());
959 base::LaunchOptions options; 950 base::LaunchOptions options;
960 options.fds_to_remap = &fds_to_remap; 951 options.fds_to_remap.push_back(std::make_pair(read_fd.get(), read_fd.get()));
961 options.pre_exec_delegate = &read_from_pipe_delegate; 952 options.pre_exec_delegate = &read_from_pipe_delegate;
962 base::SpawnChildResult spawn_child = 953 base::SpawnChildResult spawn_child =
963 SpawnChildWithOptions("SimpleChildProcess", options); 954 SpawnChildWithOptions("SimpleChildProcess", options);
964 ASSERT_TRUE(spawn_child.process.IsValid()); 955 ASSERT_TRUE(spawn_child.process.IsValid());
965 956
966 read_fd.reset(); 957 read_fd.reset();
967 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd.get(), &kPipeValue, 1))); 958 ASSERT_EQ(1, HANDLE_EINTR(write(write_fd.get(), &kPipeValue, 1)));
968 959
969 int exit_code = 42; 960 int exit_code = 42;
970 EXPECT_TRUE(spawn_child.process.WaitForExit(&exit_code)); 961 EXPECT_TRUE(spawn_child.process.WaitForExit(&exit_code));
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 1030
1040 base::SpawnChildResult spawn_child = 1031 base::SpawnChildResult spawn_child =
1041 SpawnChildWithOptions("SimpleChildProcess", options); 1032 SpawnChildWithOptions("SimpleChildProcess", options);
1042 ASSERT_TRUE(spawn_child.process.IsValid()); 1033 ASSERT_TRUE(spawn_child.process.IsValid());
1043 1034
1044 int exit_code = kSuccess; 1035 int exit_code = kSuccess;
1045 EXPECT_TRUE(spawn_child.process.WaitForExit(&exit_code)); 1036 EXPECT_TRUE(spawn_child.process.WaitForExit(&exit_code));
1046 EXPECT_NE(kSuccess, exit_code); 1037 EXPECT_NE(kSuccess, exit_code);
1047 } 1038 }
1048 #endif // defined(OS_LINUX) 1039 #endif // defined(OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698