Index: base/process_util_unittest.cc |
diff --git a/base/process_util_unittest.cc b/base/process_util_unittest.cc |
index 0dfcd2b76cf5f6a0ce4764ad089de44d2bb1b4fe..c6605163b1367626f668986c63256c14bba4b29f 100644 |
--- a/base/process_util_unittest.cc |
+++ b/base/process_util_unittest.cc |
@@ -273,6 +273,51 @@ TEST_F(ProcessUtilTest, GetAppOutput) { |
EXPECT_STREQ("foobar42", output.c_str()); |
} |
+TEST_F(ProcessUtilTest, GetAppOutputRestricted) { |
+ // Unfortunately, since we can't rely on the path, we need to know where |
+ // everything is. So let's use /bin/sh, which is on every POSIX system, and |
+ // its built-ins. |
+ std::vector<std::string> argv; |
+ argv.push_back("/bin/sh"); // argv[0] |
+ argv.push_back("-c"); // argv[1] |
+ |
+ // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of |
+ // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we |
+ // need absolute paths). |
+ argv.push_back("exit 0"); // argv[2]; equivalent to "true" |
+ std::string output = "abc"; |
+ EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 100)); |
+ EXPECT_STREQ("", output.c_str()); |
+ |
+ // On failure, should not touch |output|. As above, but for |false|. |
+ argv[2] = "exit 1"; // equivalent to "false" |
+ output = "abc"; |
+ EXPECT_FALSE(GetAppOutputRestricted(CommandLine(argv), |
+ &output, 100)); |
+ EXPECT_STREQ("abc", output.c_str()); |
+ |
+ // Amount of output exactly equal to space allowed. |
+ argv[2] = "echo 123456789"; // (the sh built-in doesn't take "-n") |
+ output.clear(); |
+ EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 10)); |
+ EXPECT_STREQ("123456789\n", output.c_str()); |
+ |
+ // Amount of output greater than space allowed. |
+ output.clear(); |
+ EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 5)); |
+ EXPECT_STREQ("12345", output.c_str()); |
+ |
+ // Amount of output less than space allowed. |
+ output.clear(); |
+ EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 15)); |
+ EXPECT_STREQ("123456789\n", output.c_str()); |
+ |
+ // Zero space allowed. |
+ output = "abc"; |
+ EXPECT_TRUE(GetAppOutputRestricted(CommandLine(argv), &output, 0)); |
+ EXPECT_STREQ("", output.c_str()); |
+} |
+ |
#if defined(OS_LINUX) |
TEST_F(ProcessUtilTest, GetParentProcessId) { |
base::ProcessId ppid = GetParentProcessId(GetCurrentProcId()); |