OLD | NEW |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 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 | 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 #include <chromeos/process.h> | 5 #include <chromeos/process.h> |
6 #include <chromeos/process_mock.h> | 6 #include <chromeos/process_mock.h> |
7 #include <chromeos/test_helpers.h> | 7 #include <chromeos/test_helpers.h> |
8 | 8 |
9 #include <base/file_util.h> | 9 #include <base/file_util.h> |
10 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 TEST_F(ProcessTest, NonZeroReturnValue) { | 85 TEST_F(ProcessTest, NonZeroReturnValue) { |
86 process_.AddArg(kBinFalse); | 86 process_.AddArg(kBinFalse); |
87 EXPECT_EQ(1, process_.Run()); | 87 EXPECT_EQ(1, process_.Run()); |
88 ExpectFileEquals("", output_file_.c_str()); | 88 ExpectFileEquals("", output_file_.c_str()); |
89 EXPECT_EQ("", GetLog()); | 89 EXPECT_EQ("", GetLog()); |
90 } | 90 } |
91 | 91 |
92 TEST_F(ProcessTest, BadOutputFile) { | 92 TEST_F(ProcessTest, BadOutputFile) { |
93 process_.AddArg(kBinEcho); | 93 process_.AddArg(kBinEcho); |
94 process_.RedirectOutput("/bad/path"); | 94 process_.RedirectOutput("/bad/path"); |
95 EXPECT_EQ(127, process_.Run()); | 95 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
96 } | 96 } |
97 | 97 |
98 TEST_F(ProcessTest, ExistingOutputFile) { | 98 TEST_F(ProcessTest, ExistingOutputFile) { |
99 process_.AddArg(kBinEcho); | 99 process_.AddArg(kBinEcho); |
100 process_.AddArg("hello world"); | 100 process_.AddArg("hello world"); |
101 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); | 101 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_))); |
102 EXPECT_EQ(0, process_.Run()); | 102 EXPECT_EQ(0, process_.Run()); |
103 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); | 103 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_))); |
104 EXPECT_EQ(127, process_.Run()); | 104 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
105 } | 105 } |
106 | 106 |
107 TEST_F(ProcessTest, BadExecutable) { | 107 TEST_F(ProcessTest, BadExecutable) { |
108 process_.AddArg("false"); | 108 process_.AddArg("false"); |
109 EXPECT_EQ(127, process_.Run()); | 109 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
110 } | 110 } |
111 | 111 |
112 void ProcessTest::CheckStderrCaptured() { | 112 void ProcessTest::CheckStderrCaptured() { |
113 std::string contents; | 113 std::string contents; |
114 process_.AddArg(kBinCp); | 114 process_.AddArg(kBinCp); |
115 EXPECT_EQ(1, process_.Run()); | 115 EXPECT_EQ(1, process_.Run()); |
116 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), | 116 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), |
117 &contents)); | 117 &contents)); |
118 EXPECT_NE(std::string::npos, contents.find("missing file operand")); | 118 EXPECT_NE(std::string::npos, contents.find("missing file operand")); |
119 EXPECT_EQ("", GetLog()); | 119 EXPECT_EQ("", GetLog()); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 EXPECT_TRUE(process_.Start()); | 189 EXPECT_TRUE(process_.Start()); |
190 int write_fd = process_.GetPipe(STDIN_FILENO); | 190 int write_fd = process_.GetPipe(STDIN_FILENO); |
191 EXPECT_EQ(-1, process_.GetPipe(STDERR_FILENO)); | 191 EXPECT_EQ(-1, process_.GetPipe(STDERR_FILENO)); |
192 EXPECT_TRUE(file_util::WriteFile(GetFdPath(write_fd), kMessage, | 192 EXPECT_TRUE(file_util::WriteFile(GetFdPath(write_fd), kMessage, |
193 strlen(kMessage))); | 193 strlen(kMessage))); |
194 close(write_fd); | 194 close(write_fd); |
195 EXPECT_EQ(0, process_.Wait()); | 195 EXPECT_EQ(0, process_.Wait()); |
196 ExpectFileEquals(kMessage, output_file_.c_str()); | 196 ExpectFileEquals(kMessage, output_file_.c_str()); |
197 } | 197 } |
198 | 198 |
| 199 TEST_F(ProcessTest, WithSameUid) { |
| 200 gid_t uid = geteuid(); |
| 201 process_.AddArg(kBinEcho); |
| 202 process_.SetUid(uid); |
| 203 EXPECT_EQ(0, process_.Run()); |
| 204 } |
| 205 |
| 206 TEST_F(ProcessTest, WithSameGid) { |
| 207 gid_t gid = getegid(); |
| 208 process_.AddArg(kBinEcho); |
| 209 process_.SetGid(gid); |
| 210 EXPECT_EQ(0, process_.Run()); |
| 211 } |
| 212 |
| 213 TEST_F(ProcessTest, WithIllegalUid) { |
| 214 ASSERT_NE(0, geteuid()); |
| 215 process_.AddArg(kBinEcho); |
| 216 process_.SetUid(0); |
| 217 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
| 218 std::string contents; |
| 219 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), |
| 220 &contents)); |
| 221 EXPECT_NE(std::string::npos, |
| 222 contents.find("Unable to set UID to 0: 1\n")); |
| 223 } |
| 224 |
| 225 TEST_F(ProcessTest, WithIllegalGid) { |
| 226 ASSERT_NE(0, getegid()); |
| 227 process_.AddArg(kBinEcho); |
| 228 process_.SetGid(0); |
| 229 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
| 230 std::string contents; |
| 231 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_), |
| 232 &contents)); |
| 233 EXPECT_NE(std::string::npos, |
| 234 contents.find("Unable to set GID to 0: 1\n")); |
| 235 } |
| 236 |
199 TEST_F(ProcessTest, NoParams) { | 237 TEST_F(ProcessTest, NoParams) { |
200 EXPECT_EQ(127, process_.Run()); | 238 EXPECT_EQ(Process::kErrorExitStatus, process_.Run()); |
201 } | 239 } |
202 | 240 |
203 TEST_F(ProcessTest, SegFaultHandling) { | 241 TEST_F(ProcessTest, SegFaultHandling) { |
204 process_.AddArg(kBinBash); | 242 process_.AddArg(kBinBash); |
205 process_.AddArg("-c"); | 243 process_.AddArg("-c"); |
206 process_.AddArg("kill -SEGV $$"); | 244 process_.AddArg("kill -SEGV $$"); |
207 EXPECT_EQ(-1, process_.Run()); | 245 EXPECT_EQ(-1, process_.Run()); |
208 EXPECT_TRUE(FindLog("did not exit normally: 11")); | 246 EXPECT_TRUE(FindLog("did not exit normally: 11")); |
209 } | 247 } |
210 | 248 |
(...skipping 25 matching lines...) Expand all Loading... |
236 EXPECT_TRUE(process_.Kill(SIGTERM, 1)); | 274 EXPECT_TRUE(process_.Kill(SIGTERM, 1)); |
237 EXPECT_EQ(0, process_.pid()); | 275 EXPECT_EQ(0, process_.pid()); |
238 } | 276 } |
239 | 277 |
240 TEST_F(ProcessTest, Reset) { | 278 TEST_F(ProcessTest, Reset) { |
241 process_.AddArg(kBinFalse); | 279 process_.AddArg(kBinFalse); |
242 process_.Reset(0); | 280 process_.Reset(0); |
243 process_.AddArg(kBinEcho); | 281 process_.AddArg(kBinEcho); |
244 EXPECT_EQ(0, process_.Run()); | 282 EXPECT_EQ(0, process_.Run()); |
245 } | 283 } |
OLD | NEW |