| 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 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 ret = HANDLE_EINTR(close(sockets[0])); | 588 ret = HANDLE_EINTR(close(sockets[0])); |
| 589 DPCHECK(ret == 0); | 589 DPCHECK(ret == 0); |
| 590 ret = HANDLE_EINTR(close(sockets[1])); | 590 ret = HANDLE_EINTR(close(sockets[1])); |
| 591 DPCHECK(ret == 0); | 591 DPCHECK(ret == 0); |
| 592 ret = HANDLE_EINTR(close(dev_null)); | 592 ret = HANDLE_EINTR(close(dev_null)); |
| 593 DPCHECK(ret == 0); | 593 DPCHECK(ret == 0); |
| 594 } | 594 } |
| 595 | 595 |
| 596 namespace { | 596 namespace { |
| 597 | 597 |
| 598 std::string TestLaunchProcess(const base::EnvironmentVector& env_changes, | 598 std::string TestLaunchProcess(const base::EnvironmentMap& env_changes, |
| 599 const int clone_flags) { | 599 const int clone_flags) { |
| 600 std::vector<std::string> args; | 600 std::vector<std::string> args; |
| 601 base::FileHandleMappingVector fds_to_remap; | 601 base::FileHandleMappingVector fds_to_remap; |
| 602 | 602 |
| 603 args.push_back(kPosixShell); | 603 args.push_back(kPosixShell); |
| 604 args.push_back("-c"); | 604 args.push_back("-c"); |
| 605 args.push_back("echo $BASE_TEST"); | 605 args.push_back("echo $BASE_TEST"); |
| 606 | 606 |
| 607 int fds[2]; | 607 int fds[2]; |
| 608 PCHECK(pipe(fds) == 0); | 608 PCHECK(pipe(fds) == 0); |
| 609 | 609 |
| 610 fds_to_remap.push_back(std::make_pair(fds[1], 1)); | 610 fds_to_remap.push_back(std::make_pair(fds[1], 1)); |
| 611 base::LaunchOptions options; | 611 base::LaunchOptions options; |
| 612 options.wait = true; | 612 options.wait = true; |
| 613 options.environ = &env_changes; | 613 options.environ = env_changes; |
| 614 options.fds_to_remap = &fds_to_remap; | 614 options.fds_to_remap = &fds_to_remap; |
| 615 #if defined(OS_LINUX) | 615 #if defined(OS_LINUX) |
| 616 options.clone_flags = clone_flags; | 616 options.clone_flags = clone_flags; |
| 617 #else | 617 #else |
| 618 CHECK_EQ(0, clone_flags); | 618 CHECK_EQ(0, clone_flags); |
| 619 #endif // OS_LINUX | 619 #endif // OS_LINUX |
| 620 EXPECT_TRUE(base::LaunchProcess(args, options, NULL)); | 620 EXPECT_TRUE(base::LaunchProcess(args, options, NULL)); |
| 621 PCHECK(HANDLE_EINTR(close(fds[1])) == 0); | 621 PCHECK(HANDLE_EINTR(close(fds[1])) == 0); |
| 622 | 622 |
| 623 char buf[512]; | 623 char buf[512]; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 634 "0123456789012345678901234567890123456789012345678901234567890123456789" | 634 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 635 "0123456789012345678901234567890123456789012345678901234567890123456789" | 635 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 636 "0123456789012345678901234567890123456789012345678901234567890123456789" | 636 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 637 "0123456789012345678901234567890123456789012345678901234567890123456789" | 637 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 638 "0123456789012345678901234567890123456789012345678901234567890123456789" | 638 "0123456789012345678901234567890123456789012345678901234567890123456789" |
| 639 "0123456789012345678901234567890123456789012345678901234567890123456789"; | 639 "0123456789012345678901234567890123456789012345678901234567890123456789"; |
| 640 | 640 |
| 641 } // namespace | 641 } // namespace |
| 642 | 642 |
| 643 TEST_F(ProcessUtilTest, LaunchProcess) { | 643 TEST_F(ProcessUtilTest, LaunchProcess) { |
| 644 base::EnvironmentVector env_changes; | 644 base::EnvironmentMap env_changes; |
| 645 const int no_clone_flags = 0; | 645 const int no_clone_flags = 0; |
| 646 | 646 |
| 647 env_changes.push_back(std::make_pair(std::string("BASE_TEST"), | 647 const char kBaseTest[] = "BASE_TEST"; |
| 648 std::string("bar"))); | 648 |
| 649 env_changes[kBaseTest] = "bar"; |
| 649 EXPECT_EQ("bar\n", TestLaunchProcess(env_changes, no_clone_flags)); | 650 EXPECT_EQ("bar\n", TestLaunchProcess(env_changes, no_clone_flags)); |
| 650 env_changes.clear(); | 651 env_changes.clear(); |
| 651 | 652 |
| 652 EXPECT_EQ(0, setenv("BASE_TEST", "testing", 1 /* override */)); | 653 EXPECT_EQ(0, setenv(kBaseTest, "testing", 1 /* override */)); |
| 653 EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags)); | 654 EXPECT_EQ("testing\n", TestLaunchProcess(env_changes, no_clone_flags)); |
| 654 | 655 |
| 655 env_changes.push_back( | 656 env_changes[kBaseTest] = std::string(); |
| 656 std::make_pair(std::string("BASE_TEST"), std::string())); | |
| 657 EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags)); | 657 EXPECT_EQ("\n", TestLaunchProcess(env_changes, no_clone_flags)); |
| 658 | 658 |
| 659 env_changes[0].second = "foo"; | 659 env_changes[kBaseTest] = "foo"; |
| 660 EXPECT_EQ("foo\n", TestLaunchProcess(env_changes, no_clone_flags)); | 660 EXPECT_EQ("foo\n", TestLaunchProcess(env_changes, no_clone_flags)); |
| 661 | 661 |
| 662 env_changes.clear(); | 662 env_changes.clear(); |
| 663 EXPECT_EQ(0, setenv("BASE_TEST", kLargeString, 1 /* override */)); | 663 EXPECT_EQ(0, setenv(kBaseTest, kLargeString, 1 /* override */)); |
| 664 EXPECT_EQ(std::string(kLargeString) + "\n", | 664 EXPECT_EQ(std::string(kLargeString) + "\n", |
| 665 TestLaunchProcess(env_changes, no_clone_flags)); | 665 TestLaunchProcess(env_changes, no_clone_flags)); |
| 666 | 666 |
| 667 env_changes.push_back(std::make_pair(std::string("BASE_TEST"), | 667 env_changes[kBaseTest] = "wibble"; |
| 668 std::string("wibble"))); | |
| 669 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, no_clone_flags)); | 668 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, no_clone_flags)); |
| 670 | 669 |
| 671 #if defined(OS_LINUX) | 670 #if defined(OS_LINUX) |
| 672 // Test a non-trival value for clone_flags. | 671 // Test a non-trival value for clone_flags. |
| 673 // Don't test on Valgrind as it has limited support for clone(). | 672 // Don't test on Valgrind as it has limited support for clone(). |
| 674 if (!RunningOnValgrind()) { | 673 if (!RunningOnValgrind()) { |
| 675 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, CLONE_FS | SIGCHLD)); | 674 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes, CLONE_FS | SIGCHLD)); |
| 676 } | 675 } |
| 677 #endif | 676 #endif |
| 678 } | 677 } |
| 679 | 678 |
| 680 TEST_F(ProcessUtilTest, AlterEnvironment) { | |
| 681 const char* const empty[] = { NULL }; | |
| 682 const char* const a2[] = { "A=2", NULL }; | |
| 683 base::EnvironmentVector changes; | |
| 684 char** e; | |
| 685 | |
| 686 e = base::AlterEnvironment(changes, empty); | |
| 687 EXPECT_TRUE(e[0] == NULL); | |
| 688 delete[] e; | |
| 689 | |
| 690 changes.push_back(std::make_pair(std::string("A"), std::string("1"))); | |
| 691 e = base::AlterEnvironment(changes, empty); | |
| 692 EXPECT_EQ(std::string("A=1"), e[0]); | |
| 693 EXPECT_TRUE(e[1] == NULL); | |
| 694 delete[] e; | |
| 695 | |
| 696 changes.clear(); | |
| 697 changes.push_back(std::make_pair(std::string("A"), std::string())); | |
| 698 e = base::AlterEnvironment(changes, empty); | |
| 699 EXPECT_TRUE(e[0] == NULL); | |
| 700 delete[] e; | |
| 701 | |
| 702 changes.clear(); | |
| 703 e = base::AlterEnvironment(changes, a2); | |
| 704 EXPECT_EQ(std::string("A=2"), e[0]); | |
| 705 EXPECT_TRUE(e[1] == NULL); | |
| 706 delete[] e; | |
| 707 | |
| 708 changes.clear(); | |
| 709 changes.push_back(std::make_pair(std::string("A"), std::string("1"))); | |
| 710 e = base::AlterEnvironment(changes, a2); | |
| 711 EXPECT_EQ(std::string("A=1"), e[0]); | |
| 712 EXPECT_TRUE(e[1] == NULL); | |
| 713 delete[] e; | |
| 714 | |
| 715 changes.clear(); | |
| 716 changes.push_back(std::make_pair(std::string("A"), std::string())); | |
| 717 e = base::AlterEnvironment(changes, a2); | |
| 718 EXPECT_TRUE(e[0] == NULL); | |
| 719 delete[] e; | |
| 720 } | |
| 721 | |
| 722 TEST_F(ProcessUtilTest, GetAppOutput) { | 679 TEST_F(ProcessUtilTest, GetAppOutput) { |
| 723 std::string output; | 680 std::string output; |
| 724 | 681 |
| 725 #if defined(OS_ANDROID) | 682 #if defined(OS_ANDROID) |
| 726 std::vector<std::string> argv; | 683 std::vector<std::string> argv; |
| 727 argv.push_back("sh"); // Instead of /bin/sh, force path search to find it. | 684 argv.push_back("sh"); // Instead of /bin/sh, force path search to find it. |
| 728 argv.push_back("-c"); | 685 argv.push_back("-c"); |
| 729 | 686 |
| 730 argv.push_back("exit 0"); | 687 argv.push_back("exit 0"); |
| 731 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output)); | 688 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output)); |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 // Check that process was really killed. | 909 // Check that process was really killed. |
| 953 EXPECT_TRUE(IsProcessDead(child_process)); | 910 EXPECT_TRUE(IsProcessDead(child_process)); |
| 954 base::CloseProcessHandle(child_process); | 911 base::CloseProcessHandle(child_process); |
| 955 } | 912 } |
| 956 | 913 |
| 957 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 914 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
| 958 return 0; | 915 return 0; |
| 959 } | 916 } |
| 960 | 917 |
| 961 #endif // defined(OS_POSIX) | 918 #endif // defined(OS_POSIX) |
| OLD | NEW |