[go: nahoru, domu]

blob: 778e905bdc2c50bb0e25e4b83b3ed2ce455a4455 [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"
14#include "base/eintr_wrapper.h"
15#include "base/file_util.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
20struct TestCase {
21 std::string str;
22 ProcessOutputType type;
23
tbarzic@chromium.org6866c6b2012-04-09 21:38:1824 TestCase(const char* expected_string,
25 size_t expected_string_length,
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)
tbarzic@chromium.org8f427982011-12-13 23:40:2331 : str(expected_string),
32 type(expected_type) {
33 }
34};
35
36class ProcessWatcherExpectations {
37 public:
38 ProcessWatcherExpectations() {}
39
40 void Init(const std::vector<TestCase>& expectations) {
41 received_from_out_ = 0;
tbarzic@chromium.org8f427982011-12-13 23:40:2342
43 for (size_t i = 0; i < expectations.size(); i++) {
tbarzic@chromium.org6866c6b2012-04-09 21:38:1844 out_expectations_.append(expectations[i].str.c_str(),
45 expectations[i].str.length());
tbarzic@chromium.org8f427982011-12-13 23:40:2346 }
47 }
48
tbarzic@chromium.org6866c6b2012-04-09 21:38:1849 bool CheckExpectations(const std::string& data, ProcessOutputType type) {
50 EXPECT_EQ(PROCESS_OUTPUT_TYPE_OUT, type);
51 if (!type == PROCESS_OUTPUT_TYPE_OUT)
52 return false;
tbarzic@chromium.org8f427982011-12-13 23:40:2353
tbarzic@chromium.org6866c6b2012-04-09 21:38:1854 EXPECT_LT(received_from_out_, out_expectations_.length());
55 if (received_from_out_ >= out_expectations_.length())
56 return false;
tbarzic@chromium.org8f427982011-12-13 23:40:2357
tbarzic@chromium.org6866c6b2012-04-09 21:38:1858 EXPECT_EQ(received_from_out_,
59 out_expectations_.find(data, received_from_out_));
tbarzic@chromium.org8f427982011-12-13 23:40:2360
tbarzic@chromium.org6866c6b2012-04-09 21:38:1861 received_from_out_ += data.length();
62 return true;
tbarzic@chromium.org8f427982011-12-13 23:40:2363 }
64
65 bool IsDone() {
tbarzic@chromium.org6866c6b2012-04-09 21:38:1866 return received_from_out_ >= out_expectations_.length();
tbarzic@chromium.org8f427982011-12-13 23:40:2367 }
68
69 private:
70 std::string out_expectations_;
71 size_t received_from_out_;
tbarzic@chromium.org8f427982011-12-13 23:40:2372};
73
74class ProcessOutputWatcherTest : public testing::Test {
75public:
tbarzic@chromium.orga504ba72012-01-25 01:42:5776 void StartWatch(int pt, int stop,
tbarzic@chromium.org8f427982011-12-13 23:40:2377 const std::vector<TestCase>& expectations) {
78 expectations_.Init(expectations);
79
80 // This will delete itself.
tbarzic@chromium.orga504ba72012-01-25 01:42:5781 ProcessOutputWatcher* crosh_watcher = new ProcessOutputWatcher(pt, stop,
tbarzic@chromium.org8f427982011-12-13 23:40:2382 base::Bind(&ProcessOutputWatcherTest::OnRead, base::Unretained(this)));
83 crosh_watcher->Start();
84 }
85
86 void OnRead(ProcessOutputType type, const std::string& output) {
tbarzic@chromium.org6866c6b2012-04-09 21:38:1887 bool success = expectations_.CheckExpectations(output, type);
88 if (!success || expectations_.IsDone())
tbarzic@chromium.org8f427982011-12-13 23:40:2389 all_data_received_->Signal();
90 }
91
92 protected:
93 std::string VeryLongString() {
94 std::string result = "0123456789";
95 for (int i = 0; i < 8; i++)
96 result = result.append(result);
97 return result;
98 }
99
100 scoped_ptr<base::WaitableEvent> all_data_received_;
101
102 private:
103 ProcessWatcherExpectations expectations_;
104 std::vector<TestCase> exp;
105};
106
107
108TEST_F(ProcessOutputWatcherTest, OutputWatcher) {
109 all_data_received_.reset(new base::WaitableEvent(true, false));
110
111 base::Thread output_watch_thread("ProcessOutpuWatchThread");
112 ASSERT_TRUE(output_watch_thread.Start());
113
tbarzic@chromium.orga504ba72012-01-25 01:42:57114 int pt_pipe[2], stop_pipe[2];
115 ASSERT_FALSE(HANDLE_EINTR(pipe(pt_pipe)));
tbarzic@chromium.org8f427982011-12-13 23:40:23116 ASSERT_FALSE(HANDLE_EINTR(pipe(stop_pipe)));
tbarzic@chromium.org8f427982011-12-13 23:40:23117
tbarzic@chromium.orga504ba72012-01-25 01:42:57118 // TODO(tbarzic): We don't support stderr anymore, so this can be simplified.
tbarzic@chromium.org8f427982011-12-13 23:40:23119 std::vector<TestCase> test_cases;
120 test_cases.push_back(TestCase("testing output\n", PROCESS_OUTPUT_TYPE_OUT));
tbarzic@chromium.orga504ba72012-01-25 01:42:57121 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));
tbarzic@chromium.org8f427982011-12-13 23:40:23123 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));
tbarzic@chromium.orga504ba72012-01-25 01:42:57127 test_cases.push_back(TestCase("testing error2\n", PROCESS_OUTPUT_TYPE_OUT));
tbarzic@chromium.org6866c6b2012-04-09 21:38:18128 test_cases.push_back(TestCase("line with \0 in it\n",
129 arraysize("line with \0 in it \n"),
130 PROCESS_OUTPUT_TYPE_OUT));
tbarzic@chromium.org8f427982011-12-13 23:40:23131
132 output_watch_thread.message_loop()->PostTask(FROM_HERE,
133 base::Bind(&ProcessOutputWatcherTest::StartWatch, base::Unretained(this),
tbarzic@chromium.orga504ba72012-01-25 01:42:57134 pt_pipe[0], stop_pipe[0], test_cases));
tbarzic@chromium.org8f427982011-12-13 23:40:23135
136 for (size_t i = 0; i < test_cases.size(); i++) {
tbarzic@chromium.org8f427982011-12-13 23:40:23137 // 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,
tbarzic@chromium.orga504ba72012-01-25 01:42:57141 file_util::WriteFileDescriptor(pt_pipe[1], test_str.c_str(),
142 test_size));
tbarzic@chromium.org8f427982011-12-13 23:40:23143 }
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
tbarzic@chromium.orga504ba72012-01-25 01:42:57150 EXPECT_NE(-1, HANDLE_EINTR(close(stop_pipe[1])));
151 EXPECT_NE(-1, HANDLE_EINTR(close(pt_pipe[1])));
152
tbarzic@chromium.org8f427982011-12-13 23:40:23153 output_watch_thread.Stop();
154};
155