OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 EXPECT_TRUE(e[1] == NULL); | 429 EXPECT_TRUE(e[1] == NULL); |
430 delete[] e; | 430 delete[] e; |
431 | 431 |
432 changes.clear(); | 432 changes.clear(); |
433 changes.push_back(std::make_pair(std::string("A"), std::string(""))); | 433 changes.push_back(std::make_pair(std::string("A"), std::string(""))); |
434 e = base::AlterEnvironment(changes, a2); | 434 e = base::AlterEnvironment(changes, a2); |
435 EXPECT_TRUE(e[0] == NULL); | 435 EXPECT_TRUE(e[0] == NULL); |
436 delete[] e; | 436 delete[] e; |
437 } | 437 } |
438 | 438 |
439 TEST_F(ProcessUtilTest, GetAppOutput_Success) { | 439 TEST_F(ProcessUtilTest, GetAppOutput) { |
440 std::string output; | 440 std::string output; |
441 EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath( | 441 EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output)); |
442 FILE_PATH_LITERAL("true"))), &output)); | |
443 EXPECT_STREQ("", output.c_str()); | 442 EXPECT_STREQ("", output.c_str()); |
444 | 443 |
445 CommandLine command_line(FilePath(FILE_PATH_LITERAL("echo"))); | 444 EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath("false")), &output)); |
446 command_line.AppendLooseValue(L"foobar42"); | |
447 EXPECT_TRUE(base::GetAppOutput(command_line, &output)); | |
448 EXPECT_STREQ("foobar42\n", output.c_str()); | |
449 } | |
450 | 445 |
451 TEST_F(ProcessUtilTest, GetAppOutput_Failure) { | |
452 std::string output; | |
453 EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath( | |
454 FILE_PATH_LITERAL("false"))), &output)); | |
455 EXPECT_STREQ("", output.c_str()); | |
456 | |
457 // Make sure we capture output in case of failure. | |
458 std::vector<std::string> argv; | 446 std::vector<std::string> argv; |
459 argv.push_back("/bin/sh"); | 447 argv.push_back("/bin/echo"); |
460 argv.push_back("-c"); | 448 argv.push_back("-n"); |
461 argv.push_back("echo hello && false"); | 449 argv.push_back("foobar42"); |
462 EXPECT_FALSE(base::GetAppOutput(CommandLine(argv), &output)); | 450 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv), &output)); |
463 EXPECT_STREQ("hello\n", output.c_str()); | 451 EXPECT_STREQ("foobar42", output.c_str()); |
464 } | |
465 | |
466 TEST_F(ProcessUtilTest, GetAppOutputWithTimeout_SuccessWithinTimeout) { | |
467 CommandLine command_line(FilePath(FILE_PATH_LITERAL("echo"))); | |
468 command_line.AppendLooseValue(L"hello"); | |
469 std::string output; | |
470 bool timed_out; | |
471 | |
472 EXPECT_TRUE(base::GetAppOutputWithTimeout( | |
473 command_line, &output, &timed_out, | |
474 std::numeric_limits<int>::max())); | |
475 EXPECT_EQ("hello\n", output); | |
476 EXPECT_FALSE(timed_out); | |
477 } | |
478 | |
479 TEST_F(ProcessUtilTest, GetAppOutputWithTimeout_FailureWithinTimeout) { | |
480 std::vector<std::string> argv; | |
481 argv.push_back("/bin/sh"); | |
482 argv.push_back("-c"); | |
483 argv.push_back("echo hello && false"); | |
484 std::string output; | |
485 bool timed_out; | |
486 | |
487 EXPECT_FALSE(base::GetAppOutputWithTimeout( | |
488 CommandLine(argv), &output, &timed_out, | |
489 std::numeric_limits<int>::max())); | |
490 EXPECT_EQ("hello\n", output); | |
491 EXPECT_FALSE(timed_out); | |
492 } | |
493 | |
494 TEST_F(ProcessUtilTest, WaitForExitCodeWithTimeout) { | |
495 CommandLine command_line(FilePath(FILE_PATH_LITERAL("sleep"))); | |
496 command_line.AppendLooseValue(L"10000"); | |
497 | |
498 base::ProcessHandle process_handle; | |
499 EXPECT_TRUE(base::LaunchApp(command_line, false, false, | |
500 &process_handle)); | |
501 int exit_code = 42; | |
502 EXPECT_FALSE(base::WaitForExitCodeWithTimeout(process_handle, &exit_code, 1)); | |
503 EXPECT_EQ(42, exit_code); // exit_code is unchanged if timeout triggers. | |
504 } | |
505 | |
506 TEST_F(ProcessUtilTest, GetAppOutputWithTimeout_TimedOutWhileOutputing) { | |
507 std::vector<std::string> argv; | |
508 argv.push_back("/bin/sh"); | |
509 argv.push_back("-c"); | |
510 argv.push_back("echo asleep && sleep 10 && echo awake"); | |
511 std::string output; | |
512 bool timed_out; | |
513 | |
514 EXPECT_FALSE(base::GetAppOutputWithTimeout(CommandLine(argv), &output, | |
515 &timed_out, 1000)); | |
516 EXPECT_EQ("asleep\n", output); // Timed out before printing "awake". | |
517 EXPECT_TRUE(timed_out); | |
518 } | 452 } |
519 | 453 |
520 TEST_F(ProcessUtilTest, GetAppOutputRestricted) { | 454 TEST_F(ProcessUtilTest, GetAppOutputRestricted) { |
521 // Unfortunately, since we can't rely on the path, we need to know where | 455 // Unfortunately, since we can't rely on the path, we need to know where |
522 // everything is. So let's use /bin/sh, which is on every POSIX system, and | 456 // everything is. So let's use /bin/sh, which is on every POSIX system, and |
523 // its built-ins. | 457 // its built-ins. |
524 std::vector<std::string> argv; | 458 std::vector<std::string> argv; |
525 argv.push_back("/bin/sh"); // argv[0] | 459 argv.push_back("/bin/sh"); // argv[0] |
526 argv.push_back("-c"); // argv[1] | 460 argv.push_back("-c"); // argv[1] |
527 | 461 |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 | 719 |
786 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) { | 720 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) { |
787 ASSERT_DEATH(while ((value_ = | 721 ASSERT_DEATH(while ((value_ = |
788 base::AllocatePsychoticallyBigObjCObject())) {}, ""); | 722 base::AllocatePsychoticallyBigObjCObject())) {}, ""); |
789 } | 723 } |
790 | 724 |
791 #endif // !ARCH_CPU_64_BITS | 725 #endif // !ARCH_CPU_64_BITS |
792 #endif // OS_MACOSX | 726 #endif // OS_MACOSX |
793 | 727 |
794 #endif // !defined(OS_WIN) | 728 #endif // !defined(OS_WIN) |
OLD | NEW |