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 using chromeos::Process; | |
19 using chromeos::ProcessImpl; | |
20 using chromeos::FindLog; | |
21 using chromeos::GetLog; | |
22 | |
23 TEST(SimpleProcess, Basic) { | |
24 ProcessImpl process; | |
25 process.AddArg(kBinEcho); | |
26 EXPECT_EQ(0, process.Run()); | |
27 EXPECT_EQ("", GetLog()); | |
28 } | |
29 | |
30 class ProcessTest : public ::testing::Test { | |
31 public: | |
32 void SetUp() { | |
33 test_path_ = FilePath("test"); | |
34 output_file_ = test_path_.Append("fork_out").value(); | |
35 file_util::Delete(test_path_, true); | |
36 file_util::CreateDirectory(test_path_); | |
37 process_.RedirectOutput(output_file_); | |
38 chromeos::ClearLog(); | |
39 } | |
40 | |
41 void TearDown() { | |
42 file_util::Delete(test_path_, true); | |
43 } | |
44 | |
45 protected: | |
46 void CheckStderrCaptured(); | |
47 | |
48 ProcessImpl process_; | |
49 std::vector<const char*> args_; | |
50 std::string output_file_; | |
51 FilePath test_path_; | |
52 }; | |
53 | |
54 TEST_F(ProcessTest, Basic) { | |
55 process_.AddArg(kBinEcho); | |
56 process_.AddArg("hello world"); | |
57 EXPECT_EQ(0, process_.Run()); | |
58 ExpectFileEquals("hello world\n", output_file_.c_str()); | |
59 EXPECT_EQ("", GetLog()); | |
60 } | |
61 | |
62 TEST_F(ProcessTest, AddStringOption) { | |
63 process_.AddArg(kBinEcho); | |
64 process_.AddStringOption("--hello", "world"); | |
65 EXPECT_EQ(0, process_.Run()); | |
66 ExpectFileEquals("--hello world\n", output_file_.c_str()); | |
67 } | |
68 | |
69 TEST_F(ProcessTest, AddIntValue) { | |
70 process_.AddArg(kBinEcho); | |
71 process_.AddIntOption("--answer", 42); | |
72 EXPECT_EQ(0, process_.Run()); | |
73 ExpectFileEquals("--answer 42\n", output_file_.c_str()); | |
74 } | |
75 | |
76 TEST_F(ProcessTest, NonZeroReturnValue) { | |
77 process_.AddArg(kBinFalse); | |
78 EXPECT_EQ(1, process_.Run()); | |
79 ExpectFileEquals("", output_file_.c_str()); | |
80 EXPECT_EQ("", GetLog()); | |
81 } | |
82 | |
83 TEST_F(ProcessTest, BadOutputFile) { | |
84 process_.AddArg(kBinEcho); | |
85 process_.RedirectOutput("/bad/path"); | |
86 EXPECT_EQ(127, process_.Run()); | |
87 } | |
88 | |
89 TEST_F(ProcessTest, ExistingOutputFile) { | |
90 process_.AddArg(kBinEcho); | |
91 process_.AddArg("hello world"); | |
92 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); | |
93 EXPECT_EQ(0, process_.Run()); | |
94 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); | |
95 EXPECT_EQ(127, process_.Run()); | |
96 } | |
97 | |
98 TEST_F(ProcessTest, BadExecutable) { | |
99 process_.AddArg("false"); | |
100 EXPECT_EQ(127, process_.Run()); | |
101 } | |
102 | |
103 void ProcessTest::CheckStderrCaptured() { | |
104 std::string contents; | |
105 process_.AddArg(kBinCp); | |
106 EXPECT_EQ(1, process_.Run()); | |
107 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), | |
108 &contents)); | |
109 EXPECT_NE(std::string::npos, contents.find("missing file operand")); | |
110 EXPECT_EQ("", GetLog()); | |
111 } | |
112 | |
113 TEST_F(ProcessTest, StderrCaptured) { | |
114 CheckStderrCaptured(); | |
115 } | |
116 | |
117 TEST_F(ProcessTest, StderrCapturedWhenPreviouslyClosed) { | |
118 int saved_stderr = dup(STDERR_FILENO); | |
119 close(STDERR_FILENO); | |
120 CheckStderrCaptured(); | |
121 dup2(saved_stderr, STDERR_FILENO); | |
122 } | |
123 | |
124 TEST_F(ProcessTest, NoParams) { | |
125 EXPECT_EQ(127, process_.Run()); | |
126 } | |
127 | |
128 TEST_F(ProcessTest, SegFaultHandling) { | |
129 process_.AddArg(kBinBash); | |
130 process_.AddArg("-c"); | |
131 process_.AddArg("kill -SEGV $$"); | |
132 EXPECT_EQ(-1, process_.Run()); | |
133 EXPECT_TRUE(FindLog("did not exit normally: 11")); | |
134 } | |
135 | |
136 TEST_F(ProcessTest, KillNoPid) { | |
137 process_.Kill(SIGTERM, 0); | |
138 EXPECT_TRUE(FindLog("Process not running")); | |
139 } | |
140 | |
141 TEST_F(ProcessTest, ProcessExists) { | |
142 EXPECT_FALSE(Process::ProcessExists(0)); | |
143 EXPECT_TRUE(Process::ProcessExists(1)); | |
144 EXPECT_TRUE(Process::ProcessExists(getpid())); | |
145 } | |
146 | |
147 TEST_F(ProcessTest, ResetPidByFile) { | |
148 FilePath pid_path = test_path_.Append("pid"); | |
149 EXPECT_FALSE(process_.ResetPidByFile(pid_path.value())); | |
150 EXPECT_TRUE(file_util::WriteFile(pid_path, "456\n", 4)); | |
151 EXPECT_TRUE(process_.ResetPidByFile(pid_path.value())); | |
152 EXPECT_EQ(456, process_.pid()); | |
153 } | |
154 | |
155 TEST_F(ProcessTest, KillSleeper) { | |
156 process_.AddArg(kBinSleep); | |
157 process_.AddArg("10000"); | |
158 ASSERT_TRUE(process_.Start()); | |
159 pid_t pid = process_.pid(); | |
160 ASSERT_GT(pid, 1); | |
161 EXPECT_TRUE(process_.Kill(SIGTERM, 1)); | |
162 EXPECT_EQ(0, process_.pid()); | |
163 } | |
164 | |
165 TEST_F(ProcessTest, Reset) { | |
166 process_.AddArg(kBinFalse); | |
167 process_.Reset(0); | |
168 process_.AddArg(kBinEcho); | |
169 EXPECT_EQ(0, process_.Run()); | |
170 } | |
OLD | NEW |