OLD | NEW |
| (Empty) |
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. | |
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" | |
14 #include "base/file_util.h" | |
15 #include "base/posix/eintr_wrapper.h" | |
16 #include "base/synchronization/waitable_event.h" | |
17 #include "base/threading/thread.h" | |
18 #include "chrome/browser/chromeos/process_proxy/process_output_watcher.h" | |
19 | |
20 struct TestCase { | |
21 std::string str; | |
22 bool should_send_terminating_null; | |
23 | |
24 TestCase(const std::string& expected_string, | |
25 bool send_terminating_null) | |
26 : str(expected_string), | |
27 should_send_terminating_null(send_terminating_null) { | |
28 } | |
29 }; | |
30 | |
31 class ProcessWatcherExpectations { | |
32 public: | |
33 ProcessWatcherExpectations() {} | |
34 | |
35 void Init(const std::vector<TestCase>& expectations) { | |
36 received_from_out_ = 0; | |
37 | |
38 for (size_t i = 0; i < expectations.size(); i++) { | |
39 out_expectations_.append(expectations[i].str); | |
40 if (expectations[i].should_send_terminating_null) | |
41 out_expectations_.append(std::string("", 1)); | |
42 } | |
43 } | |
44 | |
45 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; | |
49 | |
50 EXPECT_LT(received_from_out_, out_expectations_.length()); | |
51 if (received_from_out_ >= out_expectations_.length()) | |
52 return false; | |
53 | |
54 EXPECT_EQ(received_from_out_, | |
55 out_expectations_.find(data, received_from_out_)); | |
56 | |
57 received_from_out_ += data.length(); | |
58 return true; | |
59 } | |
60 | |
61 bool IsDone() { | |
62 return received_from_out_ >= out_expectations_.length(); | |
63 } | |
64 | |
65 private: | |
66 std::string out_expectations_; | |
67 size_t received_from_out_; | |
68 }; | |
69 | |
70 class ProcessOutputWatcherTest : public testing::Test { | |
71 public: | |
72 void StartWatch(int pt, int stop, | |
73 const std::vector<TestCase>& expectations) { | |
74 expectations_.Init(expectations); | |
75 | |
76 // This will delete itself. | |
77 ProcessOutputWatcher* crosh_watcher = new ProcessOutputWatcher(pt, stop, | |
78 base::Bind(&ProcessOutputWatcherTest::OnRead, base::Unretained(this))); | |
79 crosh_watcher->Start(); | |
80 } | |
81 | |
82 void OnRead(ProcessOutputType type, const std::string& output) { | |
83 bool success = expectations_.CheckExpectations(output, type); | |
84 if (!success || expectations_.IsDone()) | |
85 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 | |
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 | |
134 scoped_ptr<base::WaitableEvent> all_data_received_; | |
135 | |
136 private: | |
137 ProcessWatcherExpectations expectations_; | |
138 std::vector<TestCase> exp; | |
139 }; | |
140 | |
141 | |
142 TEST_F(ProcessOutputWatcherTest, OutputWatcher) { | |
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)); | |
152 | |
153 RunTest(test_cases); | |
154 }; | |
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 |