| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 826 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 | 837 |
| 838 std::vector<std::string> argv; | 838 std::vector<std::string> argv; |
| 839 argv.push_back("/bin/echo"); | 839 argv.push_back("/bin/echo"); |
| 840 argv.push_back("-n"); | 840 argv.push_back("-n"); |
| 841 argv.push_back("foobar42"); | 841 argv.push_back("foobar42"); |
| 842 EXPECT_TRUE(base::GetAppOutput(base::CommandLine(argv), &output)); | 842 EXPECT_TRUE(base::GetAppOutput(base::CommandLine(argv), &output)); |
| 843 EXPECT_STREQ("foobar42", output.c_str()); | 843 EXPECT_STREQ("foobar42", output.c_str()); |
| 844 #endif // defined(OS_ANDROID) | 844 #endif // defined(OS_ANDROID) |
| 845 } | 845 } |
| 846 | 846 |
| 847 // Flakes on Android, crbug.com/375840 | |
| 848 #if defined(OS_ANDROID) | |
| 849 #define MAYBE_GetAppOutputRestricted DISABLED_GetAppOutputRestricted | |
| 850 #else | |
| 851 #define MAYBE_GetAppOutputRestricted GetAppOutputRestricted | |
| 852 #endif | |
| 853 TEST_F(ProcessUtilTest, MAYBE_GetAppOutputRestricted) { | |
| 854 // Unfortunately, since we can't rely on the path, we need to know where | |
| 855 // everything is. So let's use /bin/sh, which is on every POSIX system, and | |
| 856 // its built-ins. | |
| 857 std::vector<std::string> argv; | |
| 858 argv.push_back(std::string(kShellPath)); // argv[0] | |
| 859 argv.push_back("-c"); // argv[1] | |
| 860 | |
| 861 // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of | |
| 862 // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we | |
| 863 // need absolute paths). | |
| 864 argv.push_back("exit 0"); // argv[2]; equivalent to "true" | |
| 865 std::string output = "abc"; | |
| 866 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 867 100)); | |
| 868 EXPECT_STREQ("", output.c_str()); | |
| 869 | |
| 870 argv[2] = "exit 1"; // equivalent to "false" | |
| 871 output = "before"; | |
| 872 EXPECT_FALSE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 873 100)); | |
| 874 EXPECT_STREQ("", output.c_str()); | |
| 875 | |
| 876 // Amount of output exactly equal to space allowed. | |
| 877 argv[2] = "echo 123456789"; // (the sh built-in doesn't take "-n") | |
| 878 output.clear(); | |
| 879 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 880 10)); | |
| 881 EXPECT_STREQ("123456789\n", output.c_str()); | |
| 882 | |
| 883 // Amount of output greater than space allowed. | |
| 884 output.clear(); | |
| 885 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 886 5)); | |
| 887 EXPECT_STREQ("12345", output.c_str()); | |
| 888 | |
| 889 // Amount of output less than space allowed. | |
| 890 output.clear(); | |
| 891 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 892 15)); | |
| 893 EXPECT_STREQ("123456789\n", output.c_str()); | |
| 894 | |
| 895 // Zero space allowed. | |
| 896 output = "abc"; | |
| 897 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 898 0)); | |
| 899 EXPECT_STREQ("", output.c_str()); | |
| 900 } | |
| 901 | |
| 902 #if !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
| 903 // TODO(benwells): GetAppOutputRestricted should terminate applications | |
| 904 // with SIGPIPE when we have enough output. http://crbug.com/88502 | |
| 905 TEST_F(ProcessUtilTest, GetAppOutputRestrictedSIGPIPE) { | |
| 906 std::vector<std::string> argv; | |
| 907 std::string output; | |
| 908 | |
| 909 argv.push_back(std::string(kShellPath)); // argv[0] | |
| 910 argv.push_back("-c"); | |
| 911 #if defined(OS_ANDROID) | |
| 912 argv.push_back("while echo 12345678901234567890; do :; done"); | |
| 913 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 914 10)); | |
| 915 EXPECT_STREQ("1234567890", output.c_str()); | |
| 916 #else // defined(OS_ANDROID) | |
| 917 argv.push_back("yes"); | |
| 918 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 919 10)); | |
| 920 EXPECT_STREQ("y\ny\ny\ny\ny\n", output.c_str()); | |
| 921 #endif // !defined(OS_ANDROID) | |
| 922 } | |
| 923 #endif // !defined(OS_MACOSX) && !defined(OS_OPENBSD) | |
| 924 | |
| 925 #if defined(ADDRESS_SANITIZER) && defined(OS_MACOSX) && \ | |
| 926 defined(ARCH_CPU_64_BITS) | |
| 927 // Times out under AddressSanitizer on 64-bit OS X, see | |
| 928 // http://crbug.com/298197. | |
| 929 #define MAYBE_GetAppOutputRestrictedNoZombies \ | |
| 930 DISABLED_GetAppOutputRestrictedNoZombies | |
| 931 #else | |
| 932 #define MAYBE_GetAppOutputRestrictedNoZombies GetAppOutputRestrictedNoZombies | |
| 933 #endif | |
| 934 TEST_F(ProcessUtilTest, MAYBE_GetAppOutputRestrictedNoZombies) { | |
| 935 std::vector<std::string> argv; | |
| 936 | |
| 937 argv.push_back(std::string(kShellPath)); // argv[0] | |
| 938 argv.push_back("-c"); // argv[1] | |
| 939 argv.push_back("echo 123456789012345678901234567890"); // argv[2] | |
| 940 | |
| 941 // Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS | |
| 942 // 10.5) times with an output buffer big enough to capture all output. | |
| 943 for (int i = 0; i < 300; i++) { | |
| 944 std::string output; | |
| 945 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 946 100)); | |
| 947 EXPECT_STREQ("123456789012345678901234567890\n", output.c_str()); | |
| 948 } | |
| 949 | |
| 950 // Ditto, but with an output buffer too small to capture all output. | |
| 951 for (int i = 0; i < 300; i++) { | |
| 952 std::string output; | |
| 953 EXPECT_TRUE(base::GetAppOutputRestricted(base::CommandLine(argv), &output, | |
| 954 10)); | |
| 955 EXPECT_STREQ("1234567890", output.c_str()); | |
| 956 } | |
| 957 } | |
| 958 | |
| 959 TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) { | 847 TEST_F(ProcessUtilTest, GetAppOutputWithExitCode) { |
| 960 // Test getting output from a successful application. | 848 // Test getting output from a successful application. |
| 961 std::vector<std::string> argv; | 849 std::vector<std::string> argv; |
| 962 std::string output; | 850 std::string output; |
| 963 int exit_code; | 851 int exit_code; |
| 964 argv.push_back(std::string(kShellPath)); // argv[0] | 852 argv.push_back(std::string(kShellPath)); // argv[0] |
| 965 argv.push_back("-c"); // argv[1] | 853 argv.push_back("-c"); // argv[1] |
| 966 argv.push_back("echo foo"); // argv[2]; | 854 argv.push_back("echo foo"); // argv[2]; |
| 967 EXPECT_TRUE(base::GetAppOutputWithExitCode(base::CommandLine(argv), &output, | 855 EXPECT_TRUE(base::GetAppOutputWithExitCode(base::CommandLine(argv), &output, |
| 968 &exit_code)); | 856 &exit_code)); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1137 options.current_directory = base::FilePath("/dev/null"); | 1025 options.current_directory = base::FilePath("/dev/null"); |
| 1138 | 1026 |
| 1139 base::Process process(SpawnChildWithOptions("SimpleChildProcess", options)); | 1027 base::Process process(SpawnChildWithOptions("SimpleChildProcess", options)); |
| 1140 ASSERT_TRUE(process.IsValid()); | 1028 ASSERT_TRUE(process.IsValid()); |
| 1141 | 1029 |
| 1142 int exit_code = kSuccess; | 1030 int exit_code = kSuccess; |
| 1143 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 1031 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 1144 EXPECT_NE(kSuccess, exit_code); | 1032 EXPECT_NE(kSuccess, exit_code); |
| 1145 } | 1033 } |
| 1146 #endif // defined(OS_LINUX) | 1034 #endif // defined(OS_LINUX) |
| OLD | NEW |