Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium OS 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 | |
| 5 #include <chromeos/process.h> | |
| 6 #include <chromeos/test_helpers.h> | |
| 7 | |
| 8 #include <base/file_util.h> | |
| 9 #include <gtest/gtest.h> | |
| 10 | |
| 11 // This test assumes the following standard binaries are installed. | |
| 12 static const char kBinBash[] = "/bin/bash"; | |
| 13 static const char kBinCp[] = "/bin/cp"; | |
| 14 static const char kBinEcho[] = "/bin/echo"; | |
| 15 static const char kBinFalse[] = "/bin/false"; | |
| 16 static const char kBinSleep[] = "/bin/sleep"; | |
| 17 | |
| 18 TEST(SimpleProcess, Basic) { | |
| 19 ProcessImpl process; | |
| 20 process.AddArg(kBinEcho); | |
| 21 EXPECT_EQ(0, process.Run()); | |
| 22 EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); | |
| 23 } | |
| 24 | |
| 25 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
| |
| 26 public: | |
| 27 void SetUp() { | |
| 28 test_path_ = FilePath("test"); | |
| 29 output_file_ = test_path_.Append("fork_out").value(); | |
| 30 file_util::Delete(test_path_, true); | |
| 31 file_util::CreateDirectory(test_path_); | |
| 32 process_.SetOutput(output_file_); | |
| 33 syslog_logging::ClearAccumulatedLog(); | |
| 34 } | |
| 35 | |
| 36 void TearDown() { | |
|
petkov
2011/02/25 19:20:16
delete test_path_?
kmixter1
2011/02/26 00:54:31
Done.
| |
| 37 } | |
| 38 | |
| 39 protected: | |
| 40 ProcessImpl process_; | |
| 41 std::vector<const char*> args_; | |
| 42 std::string output_file_; | |
| 43 FilePath test_path_; | |
| 44 }; | |
| 45 | |
| 46 TEST_F(ProcessTest, Basic) { | |
| 47 process_.AddArg(kBinEcho); | |
| 48 process_.AddArg("hello world"); | |
| 49 EXPECT_EQ(0, process_.Run()); | |
| 50 ExpectFileEquals("hello world\n", output_file_.c_str()); | |
| 51 EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); | |
| 52 } | |
| 53 | |
| 54 TEST_F(ProcessTest, AddStringValue) { | |
| 55 process_.AddArg(kBinEcho); | |
| 56 process_.AddStringValue("--hello", "world"); | |
| 57 EXPECT_EQ(0, process_.Run()); | |
| 58 ExpectFileEquals("--hello world\n", output_file_.c_str()); | |
| 59 } | |
| 60 | |
| 61 TEST_F(ProcessTest, AddIntValue) { | |
| 62 process_.AddArg(kBinEcho); | |
| 63 process_.AddIntValue("--answer", 42); | |
| 64 EXPECT_EQ(0, process_.Run()); | |
| 65 ExpectFileEquals("--answer 42\n", output_file_.c_str()); | |
| 66 } | |
| 67 | |
| 68 TEST_F(ProcessTest, NonZeroReturnValue) { | |
| 69 process_.AddArg(kBinFalse); | |
| 70 EXPECT_EQ(1, process_.Run()); | |
| 71 ExpectFileEquals("", output_file_.c_str()); | |
| 72 EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); | |
| 73 } | |
| 74 | |
| 75 TEST_F(ProcessTest, BadOutputFile) { | |
| 76 process_.AddArg(kBinEcho); | |
| 77 process_.SetOutput("/bad/path"); | |
| 78 EXPECT_EQ(127, process_.Run()); | |
| 79 } | |
| 80 | |
| 81 TEST_F(ProcessTest, ExistingOutputFile) { | |
| 82 process_.AddArg(kBinEcho); | |
| 83 process_.AddArg("hello world"); | |
| 84 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); | |
| 85 EXPECT_EQ(0, process_.Run()); | |
| 86 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); | |
| 87 EXPECT_EQ(127, process_.Run()); | |
| 88 } | |
| 89 | |
| 90 TEST_F(ProcessTest, BadExecutable) { | |
| 91 process_.AddArg("false"); | |
| 92 EXPECT_EQ(127, process_.Run()); | |
| 93 } | |
| 94 | |
| 95 TEST_F(ProcessTest, StderrCaptured) { | |
| 96 std::string contents; | |
| 97 process_.AddArg(kBinCp); | |
| 98 EXPECT_EQ(1, process_.Run()); | |
| 99 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), | |
| 100 &contents)); | |
| 101 EXPECT_NE(std::string::npos, contents.find("missing file operand")); | |
| 102 EXPECT_EQ("", syslog_logging::GetAccumulatedLogging()); | |
| 103 } | |
| 104 | |
| 105 TEST_F(ProcessTest, NoParams) { | |
| 106 EXPECT_EQ(127, process_.Run()); | |
| 107 } | |
| 108 | |
| 109 TEST_F(ProcessTest, SegFaultHandling) { | |
| 110 process_.AddArg(kBinBash); | |
| 111 process_.AddArg("-c"); | |
| 112 process_.AddArg("kill -SEGV $$"); | |
| 113 EXPECT_EQ(-1, process_.Run()); | |
| 114 EXPECT_TRUE(syslog_logging::Contains("Process did not exit normally")); | |
| 115 } | |
| 116 | |
| 117 TEST_F(ProcessTest, KillNoPid) { | |
| 118 process_.Kill(SIGTERM, 0); | |
| 119 EXPECT_TRUE(syslog_logging::Contains("Process not running")); | |
| 120 } | |
| 121 | |
| 122 TEST_F(ProcessTest, ProcessExists) { | |
| 123 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.
| |
| 124 EXPECT_TRUE(process_.ProcessExists(1)); | |
| 125 EXPECT_TRUE(process_.ProcessExists(getpid())); | |
| 126 } | |
| 127 | |
| 128 TEST_F(ProcessTest, ResetPidByFile) { | |
| 129 FilePath pid_path = test_path_.Append("pid"); | |
| 130 EXPECT_FALSE(process_.ResetPidByFile(pid_path.value())); | |
| 131 EXPECT_TRUE(file_util::WriteFile(pid_path, "456\n", 4)); | |
| 132 EXPECT_TRUE(process_.ResetPidByFile(pid_path.value())); | |
| 133 EXPECT_EQ(456, process_.GetPid()); | |
| 134 } | |
| 135 | |
| 136 TEST_F(ProcessTest, KillSleeper) { | |
| 137 process_.AddArg(kBinSleep); | |
| 138 process_.AddArg("10000"); | |
| 139 ASSERT_TRUE(process_.Start()); | |
| 140 pid_t pid = process_.GetPid(); | |
| 141 ASSERT_GT(pid, 1); | |
| 142 int timeout = 100; | |
| 143 while(timeout > 0) { | |
| 144 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
| |
| 145 break; | |
| 146 sleep(1); | |
| 147 --timeout; | |
| 148 } | |
| 149 EXPECT_GT(timeout, 0); | |
| 150 EXPECT_TRUE(process_.Kill(SIGTERM, 1)); | |
| 151 EXPECT_EQ(0, process_.GetPid()); | |
| 152 } | |
| 153 | |
| 154 TEST_F(ProcessTest, Reset) { | |
| 155 process_.AddArg(kBinFalse); | |
| 156 process_.Reset(0); | |
| 157 process_.AddArg(kBinEcho); | |
| 158 EXPECT_EQ(0, process_.Run()); | |
| 159 } | |
| OLD | NEW |