OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <gtest/gtest.h> | 5 #include <gtest/gtest.h> |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include <sys/wait.h> | 11 #include <sys/wait.h> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/eintr_wrapper.h" | 14 #include "base/eintr_wrapper.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "chrome/browser/chromeos/process_proxy/process_output_watcher.h" | 18 #include "chrome/browser/chromeos/process_proxy/process_output_watcher.h" |
19 | 19 |
20 struct TestCase { | 20 struct TestCase { |
21 std::string str; | 21 std::string str; |
22 ProcessOutputType type; | 22 bool should_send_terminating_null; |
23 | 23 |
24 TestCase(const char* expected_string, | 24 TestCase(const std::string& expected_string, |
25 size_t expected_string_length, | 25 bool send_terminating_null) |
26 ProcessOutputType expected_type) | |
27 : str(expected_string, expected_string_length), | |
28 type(expected_type) { | |
29 } | |
30 TestCase(const std::string& expected_string, ProcessOutputType expected_type) | |
31 : str(expected_string), | 26 : str(expected_string), |
32 type(expected_type) { | 27 should_send_terminating_null(send_terminating_null) { |
33 } | 28 } |
34 }; | 29 }; |
35 | 30 |
36 class ProcessWatcherExpectations { | 31 class ProcessWatcherExpectations { |
37 public: | 32 public: |
38 ProcessWatcherExpectations() {} | 33 ProcessWatcherExpectations() {} |
39 | 34 |
40 void Init(const std::vector<TestCase>& expectations) { | 35 void Init(const std::vector<TestCase>& expectations) { |
41 received_from_out_ = 0; | 36 received_from_out_ = 0; |
42 | 37 |
43 for (size_t i = 0; i < expectations.size(); i++) { | 38 for (size_t i = 0; i < expectations.size(); i++) { |
44 out_expectations_.append(expectations[i].str.c_str(), | 39 out_expectations_.append(expectations[i].str); |
45 expectations[i].str.length()); | 40 if (expectations[i].should_send_terminating_null) |
| 41 out_expectations_.append(std::string("", 1)); |
46 } | 42 } |
47 } | 43 } |
48 | 44 |
49 bool CheckExpectations(const std::string& data, ProcessOutputType type) { | 45 bool CheckExpectations(const std::string& data, ProcessOutputType type) { |
50 EXPECT_EQ(PROCESS_OUTPUT_TYPE_OUT, type); | 46 EXPECT_EQ(PROCESS_OUTPUT_TYPE_OUT, type); |
51 if (!type == PROCESS_OUTPUT_TYPE_OUT) | 47 if (!type == PROCESS_OUTPUT_TYPE_OUT) |
52 return false; | 48 return false; |
53 | 49 |
54 EXPECT_LT(received_from_out_, out_expectations_.length()); | 50 EXPECT_LT(received_from_out_, out_expectations_.length()); |
55 if (received_from_out_ >= out_expectations_.length()) | 51 if (received_from_out_ >= out_expectations_.length()) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 86 } |
91 | 87 |
92 protected: | 88 protected: |
93 std::string VeryLongString() { | 89 std::string VeryLongString() { |
94 std::string result = "0123456789"; | 90 std::string result = "0123456789"; |
95 for (int i = 0; i < 8; i++) | 91 for (int i = 0; i < 8; i++) |
96 result = result.append(result); | 92 result = result.append(result); |
97 return result; | 93 return result; |
98 } | 94 } |
99 | 95 |
| 96 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 |
100 scoped_ptr<base::WaitableEvent> all_data_received_; | 134 scoped_ptr<base::WaitableEvent> all_data_received_; |
101 | 135 |
102 private: | 136 private: |
103 ProcessWatcherExpectations expectations_; | 137 ProcessWatcherExpectations expectations_; |
104 std::vector<TestCase> exp; | 138 std::vector<TestCase> exp; |
105 }; | 139 }; |
106 | 140 |
107 | 141 |
108 TEST_F(ProcessOutputWatcherTest, OutputWatcher) { | 142 TEST_F(ProcessOutputWatcherTest, OutputWatcher) { |
109 all_data_received_.reset(new base::WaitableEvent(true, false)); | 143 std::vector<TestCase> test_cases; |
| 144 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)); |
110 | 152 |
111 base::Thread output_watch_thread("ProcessOutpuWatchThread"); | 153 RunTest(test_cases); |
112 ASSERT_TRUE(output_watch_thread.Start()); | |
113 | |
114 int pt_pipe[2], stop_pipe[2]; | |
115 ASSERT_FALSE(HANDLE_EINTR(pipe(pt_pipe))); | |
116 ASSERT_FALSE(HANDLE_EINTR(pipe(stop_pipe))); | |
117 | |
118 // TODO(tbarzic): We don't support stderr anymore, so this can be simplified. | |
119 std::vector<TestCase> test_cases; | |
120 test_cases.push_back(TestCase("testing output\n", PROCESS_OUTPUT_TYPE_OUT)); | |
121 test_cases.push_back(TestCase("testing error\n", PROCESS_OUTPUT_TYPE_OUT)); | |
122 test_cases.push_back(TestCase("testing error1\n", PROCESS_OUTPUT_TYPE_OUT)); | |
123 test_cases.push_back(TestCase("testing output1\n", PROCESS_OUTPUT_TYPE_OUT)); | |
124 test_cases.push_back(TestCase("testing output2\n", PROCESS_OUTPUT_TYPE_OUT)); | |
125 test_cases.push_back(TestCase("testing output3\n", PROCESS_OUTPUT_TYPE_OUT)); | |
126 test_cases.push_back(TestCase(VeryLongString(), PROCESS_OUTPUT_TYPE_OUT)); | |
127 test_cases.push_back(TestCase("testing error2\n", PROCESS_OUTPUT_TYPE_OUT)); | |
128 test_cases.push_back(TestCase("line with \0 in it\n", | |
129 arraysize("line with \0 in it \n"), | |
130 PROCESS_OUTPUT_TYPE_OUT)); | |
131 | |
132 output_watch_thread.message_loop()->PostTask(FROM_HERE, | |
133 base::Bind(&ProcessOutputWatcherTest::StartWatch, base::Unretained(this), | |
134 pt_pipe[0], stop_pipe[0], test_cases)); | |
135 | |
136 for (size_t i = 0; i < test_cases.size(); i++) { | |
137 // Let's make inputs not NULL terminated. | |
138 const std::string& test_str = test_cases[i].str; | |
139 ssize_t test_size = test_str.length() * sizeof(*test_str.c_str()); | |
140 EXPECT_EQ(test_size, | |
141 file_util::WriteFileDescriptor(pt_pipe[1], test_str.c_str(), | |
142 test_size)); | |
143 } | |
144 | |
145 all_data_received_->Wait(); | |
146 | |
147 // Send stop signal. It is not important which string we send. | |
148 EXPECT_EQ(1, file_util::WriteFileDescriptor(stop_pipe[1], "q", 1)); | |
149 | |
150 EXPECT_NE(-1, HANDLE_EINTR(close(stop_pipe[1]))); | |
151 EXPECT_NE(-1, HANDLE_EINTR(close(pt_pipe[1]))); | |
152 | |
153 output_watch_thread.Stop(); | |
154 }; | 154 }; |
155 | 155 |
| 156 // Verifies that sending '\0' generates PROCESS_OUTPUT_TYPE_OUT event and does |
| 157 // not terminate output watcher. |
| 158 TEST_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 }; |
OLD | NEW |