Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: chromeos/process_test.cc

Issue 6509006: libchromeos: add process control library, syslog logging capability, and run unit tests (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/common.git@master
Patch Set: respond to petkov, update interface Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ProcessImpl process_;
47 std::vector<const char*> args_;
48 std::string output_file_;
49 FilePath test_path_;
50 };
51
52 TEST_F(ProcessTest, Basic) {
53 process_.AddArg(kBinEcho);
54 process_.AddArg("hello world");
55 EXPECT_EQ(0, process_.Run());
56 ExpectFileEquals("hello world\n", output_file_.c_str());
57 EXPECT_EQ("", GetLog());
58 }
59
60 TEST_F(ProcessTest, AddStringOption) {
61 process_.AddArg(kBinEcho);
62 process_.AddStringOption("--hello", "world");
63 EXPECT_EQ(0, process_.Run());
64 ExpectFileEquals("--hello world\n", output_file_.c_str());
65 }
66
67 TEST_F(ProcessTest, AddIntValue) {
68 process_.AddArg(kBinEcho);
69 process_.AddIntOption("--answer", 42);
70 EXPECT_EQ(0, process_.Run());
71 ExpectFileEquals("--answer 42\n", output_file_.c_str());
72 }
73
74 TEST_F(ProcessTest, NonZeroReturnValue) {
75 process_.AddArg(kBinFalse);
76 EXPECT_EQ(1, process_.Run());
77 ExpectFileEquals("", output_file_.c_str());
78 EXPECT_EQ("", GetLog());
79 }
80
81 TEST_F(ProcessTest, BadOutputFile) {
82 process_.AddArg(kBinEcho);
83 process_.RedirectOutput("/bad/path");
84 EXPECT_EQ(127, process_.Run());
85 }
86
87 TEST_F(ProcessTest, ExistingOutputFile) {
88 process_.AddArg(kBinEcho);
89 process_.AddArg("hello world");
90 EXPECT_FALSE(file_util::PathExists(FilePath(output_file_)));
91 EXPECT_EQ(0, process_.Run());
92 EXPECT_TRUE(file_util::PathExists(FilePath(output_file_)));
93 EXPECT_EQ(127, process_.Run());
94 }
95
96 TEST_F(ProcessTest, BadExecutable) {
97 process_.AddArg("false");
98 EXPECT_EQ(127, process_.Run());
99 }
100
101 TEST_F(ProcessTest, StderrCaptured) {
102 std::string contents;
103 process_.AddArg(kBinCp);
104 EXPECT_EQ(1, process_.Run());
105 EXPECT_TRUE(file_util::ReadFileToString(FilePath(output_file_),
106 &contents));
107 EXPECT_NE(std::string::npos, contents.find("missing file operand"));
108 EXPECT_EQ("", GetLog());
109 }
110
111 TEST_F(ProcessTest, NoParams) {
112 EXPECT_EQ(127, process_.Run());
113 }
114
115 TEST_F(ProcessTest, SegFaultHandling) {
116 process_.AddArg(kBinBash);
117 process_.AddArg("-c");
118 process_.AddArg("kill -SEGV $$");
119 EXPECT_EQ(-1, process_.Run());
120 EXPECT_TRUE(FindLog("did not exit normally: 11"));
121 }
122
123 TEST_F(ProcessTest, KillNoPid) {
124 process_.Kill(SIGTERM, 0);
125 EXPECT_TRUE(FindLog("Process not running"));
126 }
127
128 TEST_F(ProcessTest, ProcessExists) {
129 EXPECT_FALSE(Process::ProcessExists(0));
130 EXPECT_TRUE(Process::ProcessExists(1));
131 EXPECT_TRUE(Process::ProcessExists(getpid()));
132 }
133
134 TEST_F(ProcessTest, ResetPidByFile) {
135 FilePath pid_path = test_path_.Append("pid");
136 EXPECT_FALSE(process_.ResetPidByFile(pid_path.value()));
137 EXPECT_TRUE(file_util::WriteFile(pid_path, "456\n", 4));
138 EXPECT_TRUE(process_.ResetPidByFile(pid_path.value()));
139 EXPECT_EQ(456, process_.GetPid());
140 }
141
142 TEST_F(ProcessTest, KillSleeper) {
143 process_.AddArg(kBinSleep);
144 process_.AddArg("10000");
145 ASSERT_TRUE(process_.Start());
146 pid_t pid = process_.GetPid();
147 ASSERT_GT(pid, 1);
148 EXPECT_TRUE(process_.Kill(SIGTERM, 1));
149 EXPECT_EQ(0, process_.GetPid());
150 }
151
152 TEST_F(ProcessTest, HandleSigChild) {
153 process_.AddArg(kBinFalse);
154 ASSERT_TRUE(process_.Start());
155 pid_t pid = process_.GetPid();
156 ASSERT_GT(pid, 1);
157 int timeout = 100;
158 int status = 0;
159 while(timeout > 0) {
160 if (waitpid(pid, &status, WNOHANG) > 0)
161 break;
162 sleep(1);
163 --timeout;
164 }
165 EXPECT_GT(timeout, 0);
166 EXPECT_TRUE(process_.HandleSigChild(pid, status));
167 EXPECT_EQ(0, process_.GetPid());
168 }
169
170 TEST_F(ProcessTest, Reset) {
171 process_.AddArg(kBinFalse);
172 process_.Reset(0);
173 process_.AddArg(kBinEcho);
174 EXPECT_EQ(0, process_.Run());
175 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698