[go: nahoru, domu]

blob: 0dfc3321e3f5313d756b1c3b8e976e07f858cd4d [file] [log] [blame]
tbarzic@chromium.orga504ba72012-01-25 01:42:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
tbarzic@chromium.org8f427982011-12-13 23:40:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6
7#include <queue>
8#include <string>
9#include <vector>
10
11#include <sys/wait.h>
12
13#include "base/bind.h"
tbarzic@chromium.org8f427982011-12-13 23:40:2314#include "base/file_util.h"
brettw@chromium.org2025d002012-11-14 20:54:3515#include "base/posix/eintr_wrapper.h"
tbarzic@chromium.org8f427982011-12-13 23:40:2316#include "base/synchronization/waitable_event.h"
17#include "base/threading/thread.h"
18#include "chrome/browser/chromeos/process_proxy/process_output_watcher.h"
19
20struct TestCase {
21 std::string str;
tbarzic@chromium.org250f7f892012-04-11 02:46:4322 bool should_send_terminating_null;
tbarzic@chromium.org8f427982011-12-13 23:40:2323
tbarzic@chromium.org250f7f892012-04-11 02:46:4324 TestCase(const std::string& expected_string,
25 bool send_terminating_null)
tbarzic@chromium.org8f427982011-12-13 23:40:2326 : str(expected_string),
tbarzic@chromium.org250f7f892012-04-11 02:46:4327 should_send_terminating_null(send_terminating_null) {
tbarzic@chromium.org8f427982011-12-13 23:40:2328 }
29};
30
31class ProcessWatcherExpectations {
32 public:
33 ProcessWatcherExpectations() {}
34
35 void Init(const std::vector<TestCase>& expectations) {
36 received_from_out_ = 0;
tbarzic@chromium.org8f427982011-12-13 23:40:2337
38 for (size_t i = 0; i < expectations.size(); i++) {
tbarzic@chromium.org250f7f892012-04-11 02:46:4339 out_expectations_.append(expectations[i].str);
40 if (expectations[i].should_send_terminating_null)
41 out_expectations_.append(std::string("", 1));
tbarzic@chromium.org8f427982011-12-13 23:40:2342 }
43 }
44
tbarzic@chromium.org6866c6b2012-04-09 21:38:1845 bool CheckExpectations(const std::string& data, ProcessOutputType type) {
46 EXPECT_EQ(PROCESS_OUTPUT_TYPE_OUT, type);
47 if (!type == PROCESS_OUTPUT_TYPE_OUT)
48 return false;
tbarzic@chromium.org8f427982011-12-13 23:40:2349
tbarzic@chromium.org6866c6b2012-04-09 21:38:1850 EXPECT_LT(received_from_out_, out_expectations_.length());
51 if (received_from_out_ >= out_expectations_.length())
52 return false;
tbarzic@chromium.org8f427982011-12-13 23:40:2353
tbarzic@chromium.org6866c6b2012-04-09 21:38:1854 EXPECT_EQ(received_from_out_,
55 out_expectations_.find(data, received_from_out_));
tbarzic@chromium.org8f427982011-12-13 23:40:2356
tbarzic@chromium.org6866c6b2012-04-09 21:38:1857 received_from_out_ += data.length();
58 return true;
tbarzic@chromium.org8f427982011-12-13 23:40:2359 }
60
61 bool IsDone() {
tbarzic@chromium.org6866c6b2012-04-09 21:38:1862 return received_from_out_ >= out_expectations_.length();
tbarzic@chromium.org8f427982011-12-13 23:40:2363 }
64
65 private:
66 std::string out_expectations_;
67 size_t received_from_out_;
tbarzic@chromium.org8f427982011-12-13 23:40:2368};
69
70class ProcessOutputWatcherTest : public testing::Test {
71public:
tbarzic@chromium.orga504ba72012-01-25 01:42:5772 void StartWatch(int pt, int stop,
tbarzic@chromium.org8f427982011-12-13 23:40:2373 const std::vector<TestCase>& expectations) {
74 expectations_.Init(expectations);
75
76 // This will delete itself.
tbarzic@chromium.orga504ba72012-01-25 01:42:5777 ProcessOutputWatcher* crosh_watcher = new ProcessOutputWatcher(pt, stop,
tbarzic@chromium.org8f427982011-12-13 23:40:2378 base::Bind(&ProcessOutputWatcherTest::OnRead, base::Unretained(this)));
79 crosh_watcher->Start();
80 }
81
82 void OnRead(ProcessOutputType type, const std::string& output) {
tbarzic@chromium.org6866c6b2012-04-09 21:38:1883 bool success = expectations_.CheckExpectations(output, type);
84 if (!success || expectations_.IsDone())
tbarzic@chromium.org8f427982011-12-13 23:40:2385 all_data_received_->Signal();
86 }
87
88 protected:
89 std::string VeryLongString() {
90 std::string result = "0123456789";
91 for (int i = 0; i < 8; i++)
92 result = result.append(result);
93 return result;
94 }
95
tbarzic@chromium.org250f7f892012-04-11 02:46:4396 void RunTest(const std::vector<TestCase>& test_cases) {
97 all_data_received_.reset(new base::WaitableEvent(true, false));
98
99 base::Thread output_watch_thread("ProcessOutpuWatchThread");
100 ASSERT_TRUE(output_watch_thread.Start());
101
102 int pt_pipe[2], stop_pipe[2];
103 ASSERT_FALSE(HANDLE_EINTR(pipe(pt_pipe)));
104 ASSERT_FALSE(HANDLE_EINTR(pipe(stop_pipe)));
105
106 output_watch_thread.message_loop()->PostTask(FROM_HERE,
107 base::Bind(&ProcessOutputWatcherTest::StartWatch,
108 base::Unretained(this),
109 pt_pipe[0], stop_pipe[0], test_cases));
110
111 for (size_t i = 0; i < test_cases.size(); i++) {
112 const std::string& test_str = test_cases[i].str;
113 // Let's make inputs not NULL terminated, unless other is specified in
114 // the test case.
115 ssize_t test_size = test_str.length() * sizeof(*test_str.c_str());
116 if (test_cases[i].should_send_terminating_null)
117 test_size += sizeof(*test_str.c_str());
118 EXPECT_EQ(test_size,
119 file_util::WriteFileDescriptor(pt_pipe[1], test_str.c_str(),
120 test_size));
121 }
122
123 all_data_received_->Wait();
124
125 // Send stop signal. It is not important which string we send.
126 EXPECT_EQ(1, file_util::WriteFileDescriptor(stop_pipe[1], "q", 1));
127
128 EXPECT_NE(-1, HANDLE_EINTR(close(stop_pipe[1])));
129 EXPECT_NE(-1, HANDLE_EINTR(close(pt_pipe[1])));
130
131 output_watch_thread.Stop();
132 }
133
tbarzic@chromium.org8f427982011-12-13 23:40:23134 scoped_ptr<base::WaitableEvent> all_data_received_;
135
136 private:
137 ProcessWatcherExpectations expectations_;
138 std::vector<TestCase> exp;
139};
140
141
142TEST_F(ProcessOutputWatcherTest, OutputWatcher) {
tbarzic@chromium.org8f427982011-12-13 23:40:23143 std::vector<TestCase> test_cases;
tbarzic@chromium.org250f7f892012-04-11 02:46:43144 test_cases.push_back(TestCase("testing output\n", false));
145 test_cases.push_back(TestCase("testing error\n", false));
146 test_cases.push_back(TestCase("testing error1\n", false));
147 test_cases.push_back(TestCase("testing output1\n", false));
148 test_cases.push_back(TestCase("testing output2\n", false));
149 test_cases.push_back(TestCase("testing output3\n", false));
150 test_cases.push_back(TestCase(VeryLongString(), false));
151 test_cases.push_back(TestCase("testing error2\n", false));
tbarzic@chromium.org8f427982011-12-13 23:40:23152
tbarzic@chromium.org250f7f892012-04-11 02:46:43153 RunTest(test_cases);
tbarzic@chromium.org8f427982011-12-13 23:40:23154};
155
tbarzic@chromium.org250f7f892012-04-11 02:46:43156// Verifies that sending '\0' generates PROCESS_OUTPUT_TYPE_OUT event and does
157// not terminate output watcher.
158TEST_F(ProcessOutputWatcherTest, SendNull) {
159 std::vector<TestCase> test_cases;
160 // This will send '\0' to output wathcer.
161 test_cases.push_back(TestCase("", true));
162 // Let's verify that next input also gets detected (i.e. output watcher does
163 // not exit after seeing '\0' from previous test case).
164 test_cases.push_back(TestCase("a", true));
165
166 RunTest(test_cases);
167};