Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / chromeos / process_proxy / process_output_watcher_unittest.cc
blob529ba18433bc6a5c580b22fba84f0ef214ea5454
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>
7 #include <queue>
8 #include <string>
9 #include <vector>
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"
22 namespace chromeos {
24 struct TestCase {
25 TestCase(const std::string& input, bool send_terminating_null)
26 : input(input),
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
31 // input.
32 TestCase(const std::string& input,
33 bool send_terminating_null,
34 const std::string& expected_output)
35 : input(input),
36 should_send_terminating_null(send_terminating_null),
37 expected_output(expected_output) {}
39 std::string input;
40 bool should_send_terminating_null;
41 std::string expected_output;
44 class ProcessWatcherExpectations {
45 public:
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)
59 return false;
61 if (out_expectations_.length() == 0 && data.length() == 0)
62 return true;
64 EXPECT_LT(received_from_out_, out_expectations_.length());
65 if (received_from_out_ >= out_expectations_.length())
66 return false;
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_))
71 return false;
73 received_from_out_ += data.length();
74 return true;
77 bool IsDone() {
78 return received_from_out_ >= out_expectations_.length();
81 private:
82 std::string out_expectations_;
83 size_t received_from_out_;
86 class ProcessOutputWatcherTest : public testing::Test {
87 public:
88 ProcessOutputWatcherTest() : output_watch_thread_started_(false),
89 failed_(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());
114 return;
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();
126 protected:
127 std::string VeryLongString() {
128 std::string result = "0123456789";
129 for (int i = 0; i < 8; i++)
130 result = result.append(result);
131 return 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(
145 FROM_HERE,
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
158 // the test case.
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(),
163 test_size));
165 run_loop.Run();
166 EXPECT_TRUE(expectations_.IsDone());
167 if (failed_)
168 break;
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])));
178 private:
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_;
183 bool failed_;
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));
200 RunTest(test_cases);
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"));
208 RunTest(test_cases);
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"));
216 RunTest(test_cases);
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"));
224 RunTest(test_cases);
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"));
233 RunTest(test_cases);
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"));
240 RunTest(test_cases);
243 TEST_F(ProcessOutputWatcherTest, SoleThreeByteUTF8Character) {
244 std::vector<TestCase> test_cases;
245 test_cases.push_back(TestCase("\xe2\x82\xac", false, "\xe2\x82\xac"));
247 RunTest(test_cases);
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_"));
255 RunTest(test_cases);
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"));
262 RunTest(test_cases);
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"));
271 RunTest(test_cases);
274 TEST_F(ProcessOutputWatcherTest, ContainsInvalidUTF8) {
275 std::vector<TestCase> test_cases;
276 test_cases.push_back(TestCase("\xc2_", false, "\xc2_"));
278 RunTest(test_cases);
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"));
286 RunTest(test_cases);
289 TEST_F(ProcessOutputWatcherTest, EndsWithInvalidUTF8) {
290 std::vector<TestCase> test_cases;
291 test_cases.push_back(TestCase("\xff", false, "\xff"));
293 RunTest(test_cases);
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"));
301 RunTest(test_cases);
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));
314 RunTest(test_cases);
317 } // namespace chromeos