Chromium Code Reviews| Index: chromeos/process_test.cc |
| diff --git a/chromeos/process_test.cc b/chromeos/process_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9713fc15123f71d0e59f8594c7e65fe8da30842a |
| --- /dev/null |
| +++ b/chromeos/process_test.cc |
| @@ -0,0 +1,159 @@ |
| +// Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <chromeos/process.h> |
| +#include <chromeos/test_helpers.h> |
| + |
| +#include <base/file_util.h> |
| +#include <gtest/gtest.h> |
| + |
| +// This test assumes the following standard binaries are installed. |
| +static const char kBinBash[] = "/bin/bash"; |
| +static const char kBinCp[] = "/bin/cp"; |
| +static const char kBinEcho[] = "/bin/echo"; |
| +static const char kBinFalse[] = "/bin/false"; |
| +static const char kBinSleep[] = "/bin/sleep"; |
| + |
| +TEST(SimpleProcess, Basic) { |
| + ProcessImpl process; |
| + process.AddArg(kBinEcho); |
| + EXPECT_EQ(0, process.Run()); |
| + EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); |
| +} |
| + |
| +class ProcessTest : public ::testing::Test { |
|
petkov
2011/02/25 19:20:16
maybe add a test that checks that you're not leaki
kmixter1
2011/02/26 00:54:31
Fixed the code. Not really sure of an easy/non-fr
|
| + public: |
| + void SetUp() { |
| + test_path_ = FilePath("test"); |
| + output_file_ = test_path_.Append("fork_out").value(); |
| + file_util::Delete(test_path_, true); |
| + file_util::CreateDirectory(test_path_); |
| + process_.SetOutput(output_file_); |
| + syslog_logging::ClearAccumulatedLog(); |
| + } |
| + |
| + void TearDown() { |
|
petkov
2011/02/25 19:20:16
delete test_path_?
kmixter1
2011/02/26 00:54:31
Done.
|
| + } |
| + |
| + protected: |
| + ProcessImpl process_; |
| + std::vector<const char*> args_; |
| + std::string output_file_; |
| + FilePath test_path_; |
| +}; |
| + |
| +TEST_F(ProcessTest, Basic) { |
| + process_.AddArg(kBinEcho); |
| + process_.AddArg("hello world"); |
| + EXPECT_EQ(0, process_.Run()); |
| + ExpectFileEquals("hello world\n", output_file_.c_str()); |
| + EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); |
| +} |
| + |
| +TEST_F(ProcessTest, AddStringValue) { |
| + process_.AddArg(kBinEcho); |
| + process_.AddStringValue("--hello", "world"); |
| + EXPECT_EQ(0, process_.Run()); |
| + ExpectFileEquals("--hello world\n", output_file_.c_str()); |
| +} |
| + |
| +TEST_F(ProcessTest, AddIntValue) { |
| + process_.AddArg(kBinEcho); |
| + process_.AddIntValue("--answer", 42); |
| + EXPECT_EQ(0, process_.Run()); |
| + ExpectFileEquals("--answer 42\n", output_file_.c_str()); |
| +} |
| + |
| +TEST_F(ProcessTest, NonZeroReturnValue) { |
| + process_.AddArg(kBinFalse); |
| + EXPECT_EQ(1, process_.Run()); |
| + ExpectFileEquals("", output_file_.c_str()); |
| + EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); |
| +} |
| + |
| +TEST_F(ProcessTest, BadOutputFile) { |
| + process_.AddArg(kBinEcho); |
| + process_.SetOutput("/bad/path"); |
| + EXPECT_EQ(127, process_.Run()); |
| +} |
| + |
| +TEST_F(ProcessTest, ExistingOutputFile) { |
| + process_.AddArg(kBinEcho); |
| + process_.AddArg("hello world"); |
| + EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); |
| + EXPECT_EQ(0, process_.Run()); |
| + EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); |
| + EXPECT_EQ(127, process_.Run()); |
| +} |
| + |
| +TEST_F(ProcessTest, BadExecutable) { |
| + process_.AddArg("false"); |
| + EXPECT_EQ(127, process_.Run()); |
| +} |
| + |
| +TEST_F(ProcessTest, StderrCaptured) { |
| + std::string contents; |
| + process_.AddArg(kBinCp); |
| + EXPECT_EQ(1, process_.Run()); |
| + EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), |
| + &contents)); |
| + EXPECT_NE(std::string::npos, contents.find("missing file operand")); |
| + EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); |
| +} |
| + |
| +TEST_F(ProcessTest, NoParams) { |
| + EXPECT_EQ(127, process_.Run()); |
| +} |
| + |
| +TEST_F(ProcessTest, SegFaultHandling) { |
| + process_.AddArg(kBinBash); |
| + process_.AddArg("-c"); |
| + process_.AddArg("kill -SEGV $$"); |
| + EXPECT_EQ(-1, process_.Run()); |
| + EXPECT_TRUE(syslog_logging::Contains("Process did not exit normally")); |
| +} |
| + |
| +TEST_F(ProcessTest, KillNoPid) { |
| + process_.Kill(SIGTERM, 0); |
| + EXPECT_TRUE(syslog_logging::Contains("Process not running")); |
| +} |
| + |
| +TEST_F(ProcessTest, ProcessExists) { |
| + EXPECT_FALSE(process_.ProcessExists(0)); |
|
petkov
2011/02/25 19:20:16
shouldn't you test Process::ProcessExists directly
kmixter1
2011/02/26 00:54:31
Done.
|
| + EXPECT_TRUE(process_.ProcessExists(1)); |
| + EXPECT_TRUE(process_.ProcessExists(getpid())); |
| +} |
| + |
| +TEST_F(ProcessTest, ResetPidByFile) { |
| + FilePath pid_path = test_path_.Append("pid"); |
| + EXPECT_FALSE(process_.ResetPidByFile(pid_path.value())); |
| + EXPECT_TRUE(file_util::WriteFile(pid_path, "456\n", 4)); |
| + EXPECT_TRUE(process_.ResetPidByFile(pid_path.value())); |
| + EXPECT_EQ(456, process_.GetPid()); |
| +} |
| + |
| +TEST_F(ProcessTest, KillSleeper) { |
| + process_.AddArg(kBinSleep); |
| + process_.AddArg("10000"); |
| + ASSERT_TRUE(process_.Start()); |
| + pid_t pid = process_.GetPid(); |
| + ASSERT_GT(pid, 1); |
| + int timeout = 100; |
| + while(timeout > 0) { |
| + if (Process::ProcessExists(pid)) |
|
petkov
2011/02/25 19:20:16
why would this return false?
kmixter1
2011/02/26 00:54:31
ugh. yeah, I was thinking about waiting until afte
|
| + break; |
| + sleep(1); |
| + --timeout; |
| + } |
| + EXPECT_GT(timeout, 0); |
| + EXPECT_TRUE(process_.Kill(SIGTERM, 1)); |
| + EXPECT_EQ(0, process_.GetPid()); |
| +} |
| + |
| +TEST_F(ProcessTest, Reset) { |
| + process_.AddArg(kBinFalse); |
| + process_.Reset(0); |
| + process_.AddArg(kBinEcho); |
| + EXPECT_EQ(0, process_.Run()); |
| +} |