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" |
| 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 | |
| 20 | struct TestCase { |
| 21 | std::string str; |
| 22 | ProcessOutputType type; |
| 23 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 24 | 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.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 31 | : str(expected_string), |
| 32 | type(expected_type) { |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | class ProcessWatcherExpectations { |
| 37 | public: |
| 38 | ProcessWatcherExpectations() {} |
| 39 | |
| 40 | void Init(const std::vector<TestCase>& expectations) { |
| 41 | received_from_out_ = 0; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 42 | |
| 43 | for (size_t i = 0; i < expectations.size(); i++) { |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 44 | out_expectations_.append(expectations[i].str.c_str(), |
| 45 | expectations[i].str.length()); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 49 | 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.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_LT(received_from_out_, out_expectations_.length()); |
| 55 | if (received_from_out_ >= out_expectations_.length()) |
| 56 | return false; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 57 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 58 | EXPECT_EQ(received_from_out_, |
| 59 | out_expectations_.find(data, received_from_out_)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 60 | |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 61 | received_from_out_ += data.length(); |
| 62 | return true; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool IsDone() { |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 66 | return received_from_out_ >= out_expectations_.length(); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | private: |
| 70 | std::string out_expectations_; |
| 71 | size_t received_from_out_; |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | class ProcessOutputWatcherTest : public testing::Test { |
| 75 | public: |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 76 | void StartWatch(int pt, int stop, |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 77 | const std::vector<TestCase>& expectations) { |
| 78 | expectations_.Init(expectations); |
| 79 | |
| 80 | // This will delete itself. |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 81 | ProcessOutputWatcher* crosh_watcher = new ProcessOutputWatcher(pt, stop, |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 82 | base::Bind(&ProcessOutputWatcherTest::OnRead, base::Unretained(this))); |
| 83 | crosh_watcher->Start(); |
| 84 | } |
| 85 | |
| 86 | void OnRead(ProcessOutputType type, const std::string& output) { |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 87 | bool success = expectations_.CheckExpectations(output, type); |
| 88 | if (!success || expectations_.IsDone()) |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 89 | 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 | |
| 108 | TEST_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.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 114 | int pt_pipe[2], stop_pipe[2]; |
| 115 | ASSERT_FALSE(HANDLE_EINTR(pipe(pt_pipe))); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 116 | ASSERT_FALSE(HANDLE_EINTR(pipe(stop_pipe))); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 117 | |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 118 | // TODO(tbarzic): We don't support stderr anymore, so this can be simplified. |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 119 | std::vector<TestCase> test_cases; |
| 120 | test_cases.push_back(TestCase("testing output\n", PROCESS_OUTPUT_TYPE_OUT)); |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 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)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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)); |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 127 | test_cases.push_back(TestCase("testing error2\n", PROCESS_OUTPUT_TYPE_OUT)); |
tbarzic@chromium.org | 6866c6b | 2012-04-09 21:38:18 | [diff] [blame^] | 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)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 131 | |
| 132 | output_watch_thread.message_loop()->PostTask(FROM_HERE, |
| 133 | base::Bind(&ProcessOutputWatcherTest::StartWatch, base::Unretained(this), |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 134 | pt_pipe[0], stop_pipe[0], test_cases)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 135 | |
| 136 | for (size_t i = 0; i < test_cases.size(); i++) { |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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, |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 141 | file_util::WriteFileDescriptor(pt_pipe[1], test_str.c_str(), |
| 142 | test_size)); |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 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 | |
tbarzic@chromium.org | a504ba7 | 2012-01-25 01:42:57 | [diff] [blame] | 150 | EXPECT_NE(-1, HANDLE_EINTR(close(stop_pipe[1]))); |
| 151 | EXPECT_NE(-1, HANDLE_EINTR(close(pt_pipe[1]))); |
| 152 | |
tbarzic@chromium.org | 8f42798 | 2011-12-13 23:40:23 | [diff] [blame] | 153 | output_watch_thread.Stop(); |
| 154 | }; |
| 155 | |