1 // Copyright (c) 2012 The Chromium 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.
5 #include <gtest/gtest.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/process/kill.h"
13 #include "base/threading/thread.h"
14 #include "chromeos/process_proxy/process_proxy_registry.h"
20 // The test line must have all distinct characters.
21 const char kTestLineToSend
[] = "abcdefgh\n";
22 const char kTestLineExpected
[] = "abcdefgh\r\n";
24 const char kCatCommand
[] = "cat";
25 const char kStdoutType
[] = "stdout";
26 const int kTestLineNum
= 100;
30 virtual ~TestRunner() {}
31 virtual void SetupExpectations(pid_t pid
) = 0;
32 virtual void OnSomeRead(pid_t pid
, const std::string
& type
,
33 const std::string
& output
) = 0;
34 virtual void StartRegistryTest(ProcessProxyRegistry
* registry
) = 0;
40 class RegistryTestRunner
: public TestRunner
{
42 ~RegistryTestRunner() override
{}
44 void SetupExpectations(pid_t pid
) override
{
46 left_to_check_index_
[0] = 0;
47 left_to_check_index_
[1] = 0;
48 // We consider that a line processing has started if a value in
49 // left_to_check__[index] is set to 0, thus -2.
50 lines_left_
= 2 * kTestLineNum
- 2;
51 expected_line_
= kTestLineExpected
;
54 // Method to test validity of received input. We will receive two streams of
55 // the same data. (input will be echoed twice by the testing process). Each
56 // stream will contain the same string repeated |kTestLineNum| times. So we
57 // have to match 2 * |kTestLineNum| lines. The problem is the received lines
58 // from different streams may be interleaved (e.g. we may receive
59 // abc|abcdef|defgh|gh). To deal with that, we allow to test received text
60 // against two lines. The lines MUST NOT have two same characters for this
62 void OnSomeRead(pid_t pid
,
63 const std::string
& type
,
64 const std::string
& output
) override
{
65 EXPECT_EQ(type
, kStdoutType
);
69 for (size_t i
= 0; i
< output
.length(); i
++) {
70 // The character output[i] should be next in at least one of the lines we
72 valid
= (ProcessReceivedCharacter(output
[i
], 0) ||
73 ProcessReceivedCharacter(output
[i
], 1));
74 EXPECT_TRUE(valid
) << "Received: " << output
;
77 if (!valid
|| TestSucceeded()) {
78 base::MessageLoop::current()->PostTask(FROM_HERE
,
79 base::MessageLoop::QuitClosure());
83 void StartRegistryTest(ProcessProxyRegistry
* registry
) override
{
84 for (int i
= 0; i
< kTestLineNum
; i
++) {
85 EXPECT_TRUE(registry
->SendInput(pid_
, kTestLineToSend
));
90 bool ProcessReceivedCharacter(char received
, size_t stream
) {
91 if (stream
>= arraysize(left_to_check_index_
))
93 bool success
= left_to_check_index_
[stream
] < expected_line_
.length() &&
94 expected_line_
[left_to_check_index_
[stream
]] == received
;
96 left_to_check_index_
[stream
]++;
97 if (left_to_check_index_
[stream
] == expected_line_
.length() &&
99 // Take another line to test for this stream, if there are any lines left.
100 // If not, this stream is done.
101 left_to_check_index_
[stream
] = 0;
107 bool TestSucceeded() {
108 return left_to_check_index_
[0] == expected_line_
.length() &&
109 left_to_check_index_
[1] == expected_line_
.length() &&
113 size_t left_to_check_index_
[2];
115 std::string expected_line_
;
118 class RegistryNotifiedOnProcessExitTestRunner
: public TestRunner
{
120 ~RegistryNotifiedOnProcessExitTestRunner() override
{}
122 void SetupExpectations(pid_t pid
) override
{
123 output_received_
= false;
127 void OnSomeRead(pid_t pid
,
128 const std::string
& type
,
129 const std::string
& output
) override
{
130 EXPECT_EQ(pid_
, pid
);
131 if (!output_received_
) {
132 output_received_
= true;
133 EXPECT_EQ(type
, "stdout");
134 EXPECT_EQ(output
, "p");
135 base::KillProcess(pid_
, 0 , true);
138 EXPECT_EQ("exit", type
);
139 base::MessageLoop::current()->PostTask(FROM_HERE
,
140 base::MessageLoop::QuitClosure());
143 void StartRegistryTest(ProcessProxyRegistry
* registry
) override
{
144 EXPECT_TRUE(registry
->SendInput(pid_
, "p"));
148 bool output_received_
;
151 class SigIntTestRunner
: public TestRunner
{
153 ~SigIntTestRunner() override
{}
155 void SetupExpectations(pid_t pid
) override
{ pid_
= pid
; }
157 void OnSomeRead(pid_t pid
,
158 const std::string
& type
,
159 const std::string
& output
) override
{
160 EXPECT_EQ(pid_
, pid
);
161 // We may receive ^C on stdout, but we don't care about that, as long as we
162 // eventually received exit event.
163 if (type
== "exit") {
164 base::MessageLoop::current()->PostTask(FROM_HERE
,
165 base::MessageLoop::QuitClosure());
169 void StartRegistryTest(ProcessProxyRegistry
* registry
) override
{
170 // Send SingInt and verify the process exited.
171 EXPECT_TRUE(registry
->SendInput(pid_
, "\003"));
177 class ProcessProxyTest
: public testing::Test
{
179 ProcessProxyTest() {}
180 ~ProcessProxyTest() override
{}
183 void InitRegistryTest() {
184 registry_
= ProcessProxyRegistry::Get();
186 EXPECT_TRUE(registry_
->OpenProcess(
188 base::Bind(&TestRunner::OnSomeRead
,
189 base::Unretained(test_runner_
.get()))));
191 test_runner_
->SetupExpectations(pid_
);
192 test_runner_
->StartRegistryTest(registry_
);
195 void EndRegistryTest() {
196 registry_
->CloseProcess(pid_
);
198 base::TerminationStatus status
= base::GetTerminationStatus(pid_
, NULL
);
199 EXPECT_NE(base::TERMINATION_STATUS_STILL_RUNNING
, status
);
200 if (status
== base::TERMINATION_STATUS_STILL_RUNNING
)
201 base::KillProcess(pid_
, 0, true);
203 base::MessageLoop::current()->PostTask(FROM_HERE
,
204 base::MessageLoop::QuitClosure());
208 base::MessageLoop::current()->PostTask(
210 base::Bind(&ProcessProxyTest::InitRegistryTest
,
211 base::Unretained(this)));
213 // Wait until all data from output watcher is received (QuitTask will be
214 // fired on watcher thread).
215 base::MessageLoop::current()->Run();
217 base::MessageLoop::current()->PostTask(
219 base::Bind(&ProcessProxyTest::EndRegistryTest
,
220 base::Unretained(this)));
222 // Wait until we clean up the process proxy.
223 base::MessageLoop::current()->Run();
226 scoped_ptr
<TestRunner
> test_runner_
;
229 ProcessProxyRegistry
* registry_
;
232 base::MessageLoop message_loop_
;
235 // Test will open new process that will run cat command, and verify data we
236 // write to process gets echoed back.
237 TEST_F(ProcessProxyTest
, RegistryTest
) {
238 test_runner_
.reset(new RegistryTestRunner());
242 // Open new process, then kill it. Verifiy that we detect when the process dies.
243 TEST_F(ProcessProxyTest
, RegistryNotifiedOnProcessExit
) {
244 test_runner_
.reset(new RegistryNotifiedOnProcessExitTestRunner());
248 // Test verifies that \003 message send to process is processed as SigInt.
249 // Timing out on the waterfall: http://crbug.com/115064
250 TEST_F(ProcessProxyTest
, DISABLED_SigInt
) {
251 test_runner_
.reset(new SigIntTestRunner());
255 } // namespace chromeos