process_utils_unittest.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromecast/base/process_utils.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace chromecast {
  7. // Verify that a simple process works as expected.
  8. TEST(ProcessUtilsTest, SimpleProcess) {
  9. // Create a simple command.
  10. std::vector<std::string> args;
  11. args.push_back("echo");
  12. args.push_back("Hello World");
  13. // Execute the command and collect the output.
  14. std::string stdout_result;
  15. ASSERT_TRUE(GetAppOutput(args, &stdout_result));
  16. // Echo will append a newline to the stdout.
  17. EXPECT_EQ("Hello World\n", stdout_result);
  18. }
  19. // Verify that false is returned for an invalid command.
  20. TEST(ProcessUtilsTest, InvalidCommand) {
  21. // Create a command which is not valid.
  22. std::vector<std::string> args;
  23. args.push_back("invalid_command");
  24. // The command should not run.
  25. std::string stdout_result;
  26. ASSERT_FALSE(GetAppOutput(args, &stdout_result));
  27. ASSERT_TRUE(stdout_result.empty());
  28. }
  29. // Verify that false is returned when a command an error code.
  30. TEST(ProcessUtilsTest, ProcessReturnsError) {
  31. // Create a simple command.
  32. std::vector<std::string> args;
  33. args.push_back("cd");
  34. args.push_back("path/to/invalid/directory");
  35. args.push_back("2>&1"); // Pipe the stderr into stdout.
  36. // Execute the command and collect the output. Verify that the output of the
  37. // process is collected, even when the process returns an error code.
  38. std::string stderr_result;
  39. ASSERT_FALSE(GetAppOutput(args, &stderr_result));
  40. ASSERT_FALSE(stderr_result.empty());
  41. }
  42. } // namespace chromecast