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>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/files/file_util.h"
14 #include "base/location.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/string_util.h"
19 #include "base/threading/thread.h"
20 #include "chromeos/process_proxy/process_output_watcher.h"
25 TestCase(const std::string
& input
, bool send_terminating_null
)
27 should_send_terminating_null(send_terminating_null
),
28 expected_output(input
) {}
30 // Conctructor for cases where the output is not expected to be the same as
32 TestCase(const std::string
& input
,
33 bool send_terminating_null
,
34 const std::string
& expected_output
)
36 should_send_terminating_null(send_terminating_null
),
37 expected_output(expected_output
) {}
40 bool should_send_terminating_null
;
41 std::string expected_output
;
44 class ProcessWatcherExpectations
{
46 ProcessWatcherExpectations() {}
48 void SetTestCase(const TestCase
& test_case
) {
49 received_from_out_
= 0;
51 out_expectations_
= test_case
.expected_output
;
52 if (test_case
.should_send_terminating_null
)
53 out_expectations_
.append(std::string("", 1));
56 bool CheckExpectations(const std::string
& data
, ProcessOutputType type
) {
57 EXPECT_EQ(PROCESS_OUTPUT_TYPE_OUT
, type
);
58 if (type
!= PROCESS_OUTPUT_TYPE_OUT
)
61 if (out_expectations_
.length() == 0 && data
.length() == 0)
64 EXPECT_LT(received_from_out_
, out_expectations_
.length());
65 if (received_from_out_
>= out_expectations_
.length())
68 EXPECT_EQ(received_from_out_
,
69 out_expectations_
.find(data
, received_from_out_
));
70 if (received_from_out_
!= out_expectations_
.find(data
, received_from_out_
))
73 received_from_out_
+= data
.length();
78 return received_from_out_
>= out_expectations_
.length();
82 std::string out_expectations_
;
83 size_t received_from_out_
;
86 class ProcessOutputWatcherTest
: public testing::Test
{
88 ProcessOutputWatcherTest() : output_watch_thread_started_(false),
92 ~ProcessOutputWatcherTest() override
{}
94 void TearDown() override
{
95 if (output_watch_thread_started_
)
96 output_watch_thread_
->Stop();
99 void StartWatch(int pt
, int stop
) {
100 // This will delete itself.
101 ProcessOutputWatcher
* crosh_watcher
= new ProcessOutputWatcher(pt
, stop
,
102 base::Bind(&ProcessOutputWatcherTest::OnRead
, base::Unretained(this)));
103 crosh_watcher
->Start();
106 void OnRead(ProcessOutputType type
, const std::string
& output
) {
107 ASSERT_FALSE(failed_
);
108 // There may be an EXIT signal sent during test tear down (which is sent
109 // by process output watcher when master end of test pseudo-terminal is
110 // closed). If this happens, ignore it. If EXIT is seen before test
111 // expectations are met, fall through in order to fail the test.
112 if (type
== PROCESS_OUTPUT_TYPE_EXIT
&& expectations_
.IsDone()) {
113 ASSERT_TRUE(test_case_done_callback_
.is_null());
117 failed_
= !expectations_
.CheckExpectations(output
, type
);
118 if (failed_
|| expectations_
.IsDone()) {
119 ASSERT_FALSE(test_case_done_callback_
.is_null());
120 message_loop_
.task_runner()->PostTask(FROM_HERE
,
121 test_case_done_callback_
);
122 test_case_done_callback_
.Reset();
127 std::string
VeryLongString() {
128 std::string result
= "0123456789";
129 for (int i
= 0; i
< 8; i
++)
130 result
= result
.append(result
);
134 void RunTest(const std::vector
<TestCase
>& test_cases
) {
135 ASSERT_FALSE(output_watch_thread_started_
);
136 output_watch_thread_
.reset(new base::Thread("ProcessOutpuWatchThread"));
137 output_watch_thread_started_
= output_watch_thread_
->Start();
138 ASSERT_TRUE(output_watch_thread_started_
);
140 int pt_pipe
[2], stop_pipe
[2];
141 ASSERT_FALSE(HANDLE_EINTR(pipe(pt_pipe
)));
142 ASSERT_FALSE(HANDLE_EINTR(pipe(stop_pipe
)));
144 output_watch_thread_
->task_runner()->PostTask(
146 base::Bind(&ProcessOutputWatcherTest::StartWatch
,
147 base::Unretained(this), pt_pipe
[0], stop_pipe
[0]));
149 for (size_t i
= 0; i
< test_cases
.size(); i
++) {
150 expectations_
.SetTestCase(test_cases
[i
]);
152 base::RunLoop run_loop
;
153 ASSERT_TRUE(test_case_done_callback_
.is_null());
154 test_case_done_callback_
= run_loop
.QuitClosure();
156 const std::string
& test_str
= test_cases
[i
].input
;
157 // Let's make inputs not NULL terminated, unless other is specified in
159 ssize_t test_size
= test_str
.length() * sizeof(*test_str
.c_str());
160 if (test_cases
[i
].should_send_terminating_null
)
161 test_size
+= sizeof(*test_str
.c_str());
162 EXPECT_TRUE(base::WriteFileDescriptor(pt_pipe
[1], test_str
.c_str(),
166 EXPECT_TRUE(expectations_
.IsDone());
171 // Send stop signal. It is not important which string we send.
172 EXPECT_TRUE(base::WriteFileDescriptor(stop_pipe
[1], "q", 1));
174 EXPECT_NE(-1, IGNORE_EINTR(close(stop_pipe
[1])));
175 EXPECT_NE(-1, IGNORE_EINTR(close(pt_pipe
[1])));
179 base::Closure test_case_done_callback_
;
180 base::MessageLoop message_loop_
;
181 scoped_ptr
<base::Thread
> output_watch_thread_
;
182 bool output_watch_thread_started_
;
184 ProcessWatcherExpectations expectations_
;
185 std::vector
<TestCase
> exp
;
188 TEST_F(ProcessOutputWatcherTest
, OutputWatcher
) {
189 std::vector
<TestCase
> test_cases
;
190 test_cases
.push_back(TestCase("t", false));
191 test_cases
.push_back(TestCase("testing output\n", false));
192 test_cases
.push_back(TestCase("testing error\n", false));
193 test_cases
.push_back(TestCase("testing error1\n", false));
194 test_cases
.push_back(TestCase("testing output1\n", false));
195 test_cases
.push_back(TestCase("testing output2\n", false));
196 test_cases
.push_back(TestCase("testing output3\n", false));
197 test_cases
.push_back(TestCase(VeryLongString(), false));
198 test_cases
.push_back(TestCase("testing error2\n", false));
203 TEST_F(ProcessOutputWatcherTest
, SplitUTF8Character
) {
204 std::vector
<TestCase
> test_cases
;
205 test_cases
.push_back(TestCase("test1\xc2", false, "test1"));
206 test_cases
.push_back(TestCase("\xb5test1", false, "\xc2\xb5test1"));
211 TEST_F(ProcessOutputWatcherTest
, SplitSoleUTF8Character
) {
212 std::vector
<TestCase
> test_cases
;
213 test_cases
.push_back(TestCase("\xc2", false, ""));
214 test_cases
.push_back(TestCase("\xb5", false, "\xc2\xb5"));
219 TEST_F(ProcessOutputWatcherTest
, SplitUTF8CharacterLength3
) {
220 std::vector
<TestCase
> test_cases
;
221 test_cases
.push_back(TestCase("test3\xe2\x82", false, "test3"));
222 test_cases
.push_back(TestCase("\xac", false, "\xe2\x82\xac"));
227 TEST_F(ProcessOutputWatcherTest
, SplitSoleUTF8CharacterThreeWays
) {
228 std::vector
<TestCase
> test_cases
;
229 test_cases
.push_back(TestCase("\xe2", false, ""));
230 test_cases
.push_back(TestCase("\x82", false, ""));
231 test_cases
.push_back(TestCase("\xac", false, "\xe2\x82\xac"));
236 TEST_F(ProcessOutputWatcherTest
, EndsWithThreeByteUTF8Character
) {
237 std::vector
<TestCase
> test_cases
;
238 test_cases
.push_back(TestCase("test\xe2\x82\xac", false, "test\xe2\x82\xac"));
243 TEST_F(ProcessOutputWatcherTest
, SoleThreeByteUTF8Character
) {
244 std::vector
<TestCase
> test_cases
;
245 test_cases
.push_back(TestCase("\xe2\x82\xac", false, "\xe2\x82\xac"));
250 TEST_F(ProcessOutputWatcherTest
, HasThreeByteUTF8Character
) {
251 std::vector
<TestCase
> test_cases
;
252 test_cases
.push_back(
253 TestCase("test\xe2\x82\xac_", false, "test\xe2\x82\xac_"));
258 TEST_F(ProcessOutputWatcherTest
, MulitByteUTF8CharNullTerminated
) {
259 std::vector
<TestCase
> test_cases
;
260 test_cases
.push_back(TestCase("test\xe2\x82\xac", true, "test\xe2\x82\xac"));
265 TEST_F(ProcessOutputWatcherTest
, MultipleMultiByteUTF8Characters
) {
266 std::vector
<TestCase
> test_cases
;
267 test_cases
.push_back(
268 TestCase("test\xe2\x82\xac\xc2", false, "test\xe2\x82\xac"));
269 test_cases
.push_back(TestCase("\xb5", false, "\xc2\xb5"));
274 TEST_F(ProcessOutputWatcherTest
, ContainsInvalidUTF8
) {
275 std::vector
<TestCase
> test_cases
;
276 test_cases
.push_back(TestCase("\xc2_", false, "\xc2_"));
281 TEST_F(ProcessOutputWatcherTest
, InvalidUTF8SeriesOfTrailingBytes
) {
282 std::vector
<TestCase
> test_cases
;
283 test_cases
.push_back(TestCase("\x82\x82\x82", false, "\x82\x82\x82"));
284 test_cases
.push_back(TestCase("\x82\x82\x82", false, "\x82\x82\x82"));
289 TEST_F(ProcessOutputWatcherTest
, EndsWithInvalidUTF8
) {
290 std::vector
<TestCase
> test_cases
;
291 test_cases
.push_back(TestCase("\xff", false, "\xff"));
296 TEST_F(ProcessOutputWatcherTest
, FourByteUTF8
) {
297 std::vector
<TestCase
> test_cases
;
298 test_cases
.push_back(TestCase("\xf0\xa4\xad", false, ""));
299 test_cases
.push_back(TestCase("\xa2", false, "\xf0\xa4\xad\xa2"));
304 // Verifies that sending '\0' generates PROCESS_OUTPUT_TYPE_OUT event and does
305 // not terminate output watcher.
306 TEST_F(ProcessOutputWatcherTest
, SendNull
) {
307 std::vector
<TestCase
> test_cases
;
308 // This will send '\0' to output watcher.
309 test_cases
.push_back(TestCase("", true));
310 // Let's verify that next input also gets detected (i.e. output watcher does
311 // not exit after seeing '\0' from previous test case).
312 test_cases
.push_back(TestCase("a", true));
317 } // namespace chromeos