| OLD | NEW |
| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 ret = IGNORE_EINTR(close(sockets[0])); | 555 ret = IGNORE_EINTR(close(sockets[0])); |
| 556 DPCHECK(ret == 0); | 556 DPCHECK(ret == 0); |
| 557 ret = IGNORE_EINTR(close(sockets[1])); | 557 ret = IGNORE_EINTR(close(sockets[1])); |
| 558 DPCHECK(ret == 0); | 558 DPCHECK(ret == 0); |
| 559 ret = IGNORE_EINTR(close(dev_null)); | 559 ret = IGNORE_EINTR(close(dev_null)); |
| 560 DPCHECK(ret == 0); | 560 DPCHECK(ret == 0); |
| 561 } | 561 } |
| 562 | 562 |
| 563 namespace { | 563 namespace { |
| 564 | 564 |
| 565 std::string TestLaunchProcess(const base::EnvironmentMap& env_changes, | 565 std::string TestLaunchProcess(const std::vector<std::string>& args, |
| 566 const base::EnvironmentMap& env_changes, |
| 567 const bool clear_environ, |
| 566 const int clone_flags) { | 568 const int clone_flags) { |
| 567 std::vector<std::string> args; | |
| 568 base::FileHandleMappingVector fds_to_remap; | 569 base::FileHandleMappingVector fds_to_remap; |
| 569 | 570 |
| 570 args.push_back(kPosixShell); | |
| 571 args.push_back("-c"); | |
| 572 args.push_back("echo $BASE_TEST"); | |
| 573 | |
| 574 int fds[2]; | 571 int fds[2]; |
| 575 PCHECK(pipe(fds) == 0); | 572 PCHECK(pipe(fds) == 0); |
| 576 | 573 |
| 577 fds_to_remap.push_back(std::make_pair(fds[1], 1)); | 574 fds_to_remap.push_back(std::make_pair(fds[1], 1)); |
| 578 base::LaunchOptions options; | 575 base::LaunchOptions options; |
| 579 options.wait = true; | 576 options.wait = true; |
| 580 options.environ = env_changes; | 577 options.environ = env_changes; |
| 578 options.clear_environ = clear_environ; |
| 581 options.fds_to_remap = &fds_to_remap; | 579 options.fds_to_remap = &fds_to_remap; |
| 582 #if defined(OS_LINUX) | 580 #if defined(OS_LINUX) |
| 583 options.clone_flags = clone_flags; | 581 options.clone_flags = clone_flags; |
| 584 #else | 582 #else |
| 585 CHECK_EQ(0, clone_flags); | 583 CHECK_EQ(0, clone_flags); |
| 586 #endif // OS_LINUX | 584 #endif // OS_LINUX |
| 587 EXPECT_TRUE(base::LaunchProcess(args, options, NULL)); | 585 EXPECT_TRUE(base::LaunchProcess(args, options, NULL)); |
| 588 PCHECK(IGNORE_EINTR(close(fds[1])) == 0); | 586 PCHECK(IGNORE_EINTR(close(fds[1])) == 0); |
| 589 | 587 |
| 590 char buf[512]; | 588 char buf[512]; |
| 591 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); | 589 const ssize_t n = HANDLE_EINTR(read(fds[0], buf, sizeof(buf))); |
| 592 PCHECK(n > 0); | |
| 593 | 590 |
| 594 PCHECK(IGNORE_EINTR(close(fds[0])) == 0); | 591 PCHECK(IGNORE_EINTR(close(fds[0])) == 0); |
| 595 | 592 |
| 596 return std::string(buf, n); | 593 return std::string(buf, n); |
| 597 } | 594 } |
| 598 | 595 |
| 599 const char kLargeString[] = | 596 const char kLargeString[] = |
| 600 "0123456789012345678901234567890123456789012345678901234567890123456789" | 597 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 601 "0123456789012345678901234567890123456789012345678901234567890123456789" | 598 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 602 "0123456789012345678901234567890123456789012345678901234567890123456789" | 599 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 603 "0123456789012345678901234567890123456789012345678901234567890123456789" | 600 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 604 "0123456789012345678901234567890123456789012345678901234567890123456789" | 601 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 605 "0123456789012345678901234567890123456789012345678901234567890123456789" | 602 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 606 "0123456789012345678901234567890123456789012345678901234567890123456789"; | 603 "0123456789012345678901234567890123456789012345678901234567890123456789"; |
| 607 | 604 |
| 608 } // namespace | 605 } // namespace |
| 609 | 606 |
| 610 TEST_F(ProcessUtilTest, LaunchProcess) { | 607 TEST_F(ProcessUtilTest, LaunchProcess) { |
| 611 base::EnvironmentMap env_changes; | 608 base::EnvironmentMap env_changes; |
| 609 std::vector<std::string> echo_base_test; |
| 610 echo_base_test.push_back(kPosixShell); |
| 611 echo_base_test.push_back("-c"); |
| 612 echo_base_test.push_back("echo $BASE_TEST"); |
| 613 |
| 614 std::vector<std::string> print_env; |
| 615 print_env.push_back("/usr/bin/env"); |
| 612 const int no_clone_flags = 0; | 616 const int no_clone_flags = 0; |
| 617 const bool no_clear_environ = false; |
| 613 | 618 |
| 614 const char kBaseTest[] = "BASE_TEST"; | 619 const char kBaseTest[] = "BASE_TEST"; |
| 615 | 620 |
| 616 env_changes[kBaseTest] = "bar"; | 621 env_changes[kBaseTest] = "bar"; |
| 617 EXPECT_EQ("bar\n", TestLaunchProcess(env_changes, no_clone_flags)); | 622 EXPECT_EQ("bar\n", |
| 623 TestLaunchProcess( |
| 624 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 618 env_changes.clear(); | 625 env_changes.clear(); |
| 619 | 626 |
| 620 EXPECT_EQ(0, setenv(kBaseTest, "testing", 1 /* override */)); | 627 EXPECT_EQ(0, setenv(kBaseTest, "testing", 1 /* override */)); |
| 621 EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags)); | 628 EXPECT_EQ("testing\n", |
| 629 TestLaunchProcess( |
| 630 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 622 | 631 |
| 623 env_changes[kBaseTest] = std::string(); | 632 env_changes[kBaseTest] = std::string(); |
| 624 EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags)); | 633 EXPECT_EQ("\n", |
| 634 TestLaunchProcess( |
| 635 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 625 | 636 |
| 626 env_changes[kBaseTest] = "foo"; | 637 env_changes[kBaseTest] = "foo"; |
| 627 EXPECT_EQ("foo\n", TestLaunchProcess(env_changes, no_clone_flags)); | 638 EXPECT_EQ("foo\n", |
| 639 TestLaunchProcess( |
| 640 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 628 | 641 |
| 629 env_changes.clear(); | 642 env_changes.clear(); |
| 630 EXPECT_EQ(0, setenv(kBaseTest, kLargeString, 1 /* override */)); | 643 EXPECT_EQ(0, setenv(kBaseTest, kLargeString, 1 /* override */)); |
| 631 EXPECT_EQ(std::string(kLargeString) + "\n", | 644 EXPECT_EQ(std::string(kLargeString) + "\n", |
| 632 TestLaunchProcess(env_changes, no_clone_flags)); | 645 TestLaunchProcess( |
| 646 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 633 | 647 |
| 634 env_changes[kBaseTest] = "wibble"; | 648 env_changes[kBaseTest] = "wibble"; |
| 635 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, no_clone_flags)); | 649 EXPECT_EQ("wibble\n", |
| 650 TestLaunchProcess( |
| 651 echo_base_test, env_changes, no_clear_environ, no_clone_flags)); |
| 636 | 652 |
| 637 #if defined(OS_LINUX) | 653 #if defined(OS_LINUX) |
| 638 // Test a non-trival value for clone_flags. | 654 // Test a non-trival value for clone_flags. |
| 639 // Don't test on Valgrind as it has limited support for clone(). | 655 // Don't test on Valgrind as it has limited support for clone(). |
| 640 if (!RunningOnValgrind()) { | 656 if (!RunningOnValgrind()) { |
| 641 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, CLONE_FS | SIGCHLD)); | 657 EXPECT_EQ( |
| 658 "wibble\n", |
| 659 TestLaunchProcess( |
| 660 echo_base_test, env_changes, no_clear_environ, CLONE_FS | SIGCHLD)); |
| 642 } | 661 } |
| 662 |
| 663 EXPECT_EQ( |
| 664 "BASE_TEST=wibble\n", |
| 665 TestLaunchProcess( |
| 666 print_env, env_changes, true /* clear_environ */, no_clone_flags)); |
| 667 env_changes.clear(); |
| 668 EXPECT_EQ( |
| 669 "", |
| 670 TestLaunchProcess( |
| 671 print_env, env_changes, true /* clear_environ */, no_clone_flags)); |
| 643 #endif | 672 #endif |
| 644 } | 673 } |
| 645 | 674 |
| 646 TEST_F(ProcessUtilTest, GetAppOutput) { | 675 TEST_F(ProcessUtilTest, GetAppOutput) { |
| 647 std::string output; | 676 std::string output; |
| 648 | 677 |
| 649 #if defined(OS_ANDROID) | 678 #if defined(OS_ANDROID) |
| 650 std::vector<std::string> argv; | 679 std::vector<std::string> argv; |
| 651 argv.push_back("sh"); // Instead of /bin/sh, force path search to find it. | 680 argv.push_back("sh"); // Instead of /bin/sh, force path search to find it. |
| 652 argv.push_back("-c"); | 681 argv.push_back("-c"); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 // Check that process was really killed. | 875 // Check that process was really killed. |
| 847 EXPECT_TRUE(IsProcessDead(child_process)); | 876 EXPECT_TRUE(IsProcessDead(child_process)); |
| 848 base::CloseProcessHandle(child_process); | 877 base::CloseProcessHandle(child_process); |
| 849 } | 878 } |
| 850 | 879 |
| 851 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 880 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
| 852 return 0; | 881 return 0; |
| 853 } | 882 } |
| 854 | 883 |
| 855 #endif // defined(OS_POSIX) | 884 #endif // defined(OS_POSIX) |
| OLD | NEW |