tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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" |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 14 | #include "base/file_util.h" |
brettw@chromium.org | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame^] | 15 | #include "base/posix/eintr_wrapper.h" |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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; |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 22 | bool should_send_terminating_null; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 23 | |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 24 | TestCase(const std::string& expected_string, |
| 25 | bool send_terminating_null) |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 26 | : str(expected_string), |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 27 | should_send_terminating_null(send_terminating_null) { |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 28 | } |
| 29 | }; |
| 30 | |
| 31 | class ProcessWatcherExpectations { |
| 32 | public: |
| 33 | ProcessWatcherExpectations() {} |
| 34 | |
| 35 | void Init(const std::vector<TestCase>& expectations) { |
| 36 | received_from_out_ = 0; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 37 | |
| 38 | for (size_t i = 0; i < expectations.size(); i++) { |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 39 | out_expectations_.append(expectations[i].str); |
| 40 | if (expectations[i].should_send_terminating_null) |
| 41 | out_expectations_.append(std::string("", 1)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 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; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 49 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 50 | EXPECT_LT(received_from_out_, out_expectations_.length()); |
| 51 | if (received_from_out_ >= out_expectations_.length()) |
| 52 | return false; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 53 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 54 | EXPECT_EQ(received_from_out_, |
| 55 | out_expectations_.find(data, received_from_out_)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 56 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 57 | received_from_out_ += data.length(); |
| 58 | return true; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool IsDone() { |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 62 | return received_from_out_ >= out_expectations_.length(); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
| 66 | std::string out_expectations_; |
| 67 | size_t received_from_out_; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | class ProcessOutputWatcherTest : public testing::Test { |
| 71 | public: |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 72 | void StartWatch(int pt, int stop, |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 73 | const std::vector<TestCase>& expectations) { |
| 74 | expectations_.Init(expectations); |
| 75 | |
| 76 | // This will delete itself. |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 77 | ProcessOutputWatcher* crosh_watcher = new ProcessOutputWatcher(pt, stop, |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 78 | base::Bind(&ProcessOutputWatcherTest::OnRead, base::Unretained(this))); |
| 79 | crosh_watcher->Start(); |
| 80 | } |
| 81 | |
| 82 | void OnRead(ProcessOutputType type, const std::string& output) { |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame] | 83 | bool success = expectations_.CheckExpectations(output, type); |
| 84 | if (!success || expectations_.IsDone()) |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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 | |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 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 | |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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) { |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 143 | std::vector<TestCase> test_cases; |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 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)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 152 | |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 153 | RunTest(test_cases); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 154 | }; |
| 155 | |
tbarzic@chromium.org | 250f7f89 | 2012-04-11 02:46:43 | [diff] [blame] | 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 | }; |