Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS 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 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 5 | #include <stdint.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 6 | |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 7 | #include <base/callback_old.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 9 | #include <base/memory/ref_counted.h> |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 10 | #include <base/message_loop_proxy.h> |
| 11 | #include <base/stringprintf.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 12 | #include <gmock/gmock.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 14 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 15 | #include "shill/io_handler.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 16 | #include "shill/mock_control.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 17 | #include "shill/shill_daemon.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 18 | #include "shill/shill_test_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 19 | |
| 20 | namespace shill { |
| 21 | using ::testing::Test; |
| 22 | using ::testing::_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 23 | using ::testing::Gt; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | using ::testing::NotNull; |
| 25 | using ::testing::Return; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | using ::testing::StrictMock; |
| 27 | |
| 28 | class MockEventDispatchTester { |
| 29 | public: |
| 30 | explicit MockEventDispatchTester(EventDispatcher *dispatcher) |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 31 | : dispatcher_(dispatcher), |
| 32 | triggered_(false), |
| 33 | callback_count_(0), |
| 34 | got_data_(false), |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 35 | got_ready_(false), |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 36 | data_callback_(NULL), |
| 37 | input_handler_(NULL), |
| 38 | tester_factory_(this) { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 41 | void ScheduleFailSafe() { |
| 42 | // Set up a failsafe, so the test still exits even if something goes |
| 43 | // wrong. The Factory owns the RunnableMethod, but we get a pointer to it. |
| 44 | failsafe_ = tester_factory_.NewRunnableMethod( |
| 45 | &MockEventDispatchTester::StopDispatcher); |
| 46 | dispatcher_->PostDelayedTask(failsafe_, 100); |
| 47 | } |
| 48 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 49 | void ScheduleTimedTasks() { |
| 50 | dispatcher_->PostDelayedTask( |
| 51 | tester_factory_.NewRunnableMethod(&MockEventDispatchTester::Trigger), |
| 52 | 10); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 55 | void RescheduleUnlessTriggered() { |
| 56 | ++callback_count_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 57 | if (!triggered_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 58 | dispatcher_->PostTask( |
| 59 | tester_factory_.NewRunnableMethod( |
| 60 | &MockEventDispatchTester::RescheduleUnlessTriggered)); |
| 61 | } else { |
| 62 | failsafe_->Cancel(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 63 | StopDispatcher(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 67 | void StopDispatcher() { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 68 | dispatcher_->PostTask(new MessageLoop::QuitTask); |
| 69 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 70 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 71 | void Trigger() { |
| 72 | LOG(INFO) << "MockEventDispatchTester handling " << callback_count_; |
| 73 | CallbackComplete(callback_count_); |
| 74 | triggered_ = true; |
| 75 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 76 | |
| 77 | void HandleData(InputData *inputData) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 78 | LOG(INFO) << "MockEventDispatchTester handling data len " |
| 79 | << base::StringPrintf("%d %.*s", inputData->len, |
| 80 | inputData->len, inputData->buf); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 81 | got_data_ = true; |
| 82 | IOComplete(inputData->len); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 83 | StopDispatcher(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 84 | } |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 85 | |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 86 | bool GetData() { return got_data_; } |
| 87 | |
| 88 | void ListenIO(int fd) { |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 89 | data_callback_.reset( |
| 90 | NewCallback(this, &MockEventDispatchTester::HandleData)); |
| 91 | input_handler_.reset( |
| 92 | dispatcher_->CreateInputHandler(fd, data_callback_.get())); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void StopListenIO() { |
| 96 | got_data_ = false; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 97 | input_handler_.reset(NULL); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 100 | void HandleReady(int fd) { |
| 101 | // Stop event handling after we receive in input-ready event. We should |
| 102 | // no longer be called until events are re-enabled. |
| 103 | input_handler_->Stop(); |
| 104 | |
| 105 | if (got_ready_) { |
| 106 | // If we're still getting events after we have stopped them, something |
| 107 | // is really wrong, and we cannot just depend on ASSERT_FALSE() to get |
| 108 | // us out of it. Make sure the dispatcher is also stopped, or else we |
| 109 | // could end up never exiting. |
| 110 | StopDispatcher(); |
| 111 | ASSERT_FALSE(got_ready_) << "failed to stop Input Ready events"; |
| 112 | } |
| 113 | got_ready_ = true; |
| 114 | |
| 115 | LOG(INFO) << "MockEventDispatchTester handling ready for fd " << fd; |
| 116 | IOComplete(callback_count_); |
| 117 | |
| 118 | if (callback_count_) { |
| 119 | StopDispatcher(); |
| 120 | } else { |
| 121 | // Restart Ready events after 10 millisecond delay. |
| 122 | callback_count_++; |
| 123 | dispatcher_->PostDelayedTask(tester_factory_.NewRunnableMethod( |
| 124 | &MockEventDispatchTester::RestartReady), 10); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void RestartReady() { |
| 129 | got_ready_ = false; |
| 130 | input_handler_->Start(); |
| 131 | } |
| 132 | |
| 133 | void ListenReady(int fd) { |
| 134 | ready_callback_.reset( |
| 135 | NewCallback(this, &MockEventDispatchTester::HandleReady)); |
| 136 | input_handler_.reset( |
| 137 | dispatcher_->CreateReadyHandler(fd, IOHandler::kModeInput, |
| 138 | ready_callback_.get())); |
| 139 | } |
| 140 | |
| 141 | void StopListenReady() { |
| 142 | got_ready_ = false; |
| 143 | input_handler_.reset(NULL); |
| 144 | } |
| 145 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 146 | MOCK_METHOD1(CallbackComplete, void(int)); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 147 | MOCK_METHOD1(IOComplete, void(int)); |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 148 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 149 | private: |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 150 | EventDispatcher *dispatcher_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 151 | bool triggered_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 152 | int callback_count_; |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 153 | bool got_data_; |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 154 | bool got_ready_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 155 | scoped_ptr<Callback1<InputData*>::Type> data_callback_; |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 156 | scoped_ptr<Callback1<int>::Type> ready_callback_; |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 157 | scoped_ptr<IOHandler> input_handler_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 158 | ScopedRunnableMethodFactory<MockEventDispatchTester> tester_factory_; |
| 159 | CancelableTask* failsafe_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | class ShillDaemonTest : public Test { |
| 163 | public: |
| 164 | ShillDaemonTest() |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 165 | : daemon_(&config_, new MockControl()), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 166 | device_info_(daemon_.control_, dispatcher_, &daemon_.manager_), |
| 167 | dispatcher_(&daemon_.dispatcher_), |
| 168 | dispatcher_test_(dispatcher_), |
| 169 | factory_(this) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 170 | } |
| 171 | virtual ~ShillDaemonTest() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 172 | virtual void SetUp() { |
| 173 | // Tests initialization done by the daemon's constructor |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 174 | ASSERT_NE(reinterpret_cast<Config*>(NULL), daemon_.config_); |
| 175 | ASSERT_NE(reinterpret_cast<ControlInterface*>(NULL), daemon_.control_); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 176 | dispatcher_test_.ScheduleFailSafe(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 177 | } |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 178 | protected: |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 179 | TestConfig config_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 180 | Daemon daemon_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 181 | DeviceInfo device_info_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 182 | EventDispatcher *dispatcher_; |
| 183 | StrictMock<MockEventDispatchTester> dispatcher_test_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 184 | ScopedRunnableMethodFactory<ShillDaemonTest> factory_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 188 | TEST_F(ShillDaemonTest, EventDispatcherTimer) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 189 | EXPECT_CALL(dispatcher_test_, CallbackComplete(Gt(0))); |
| 190 | dispatcher_test_.ScheduleTimedTasks(); |
| 191 | dispatcher_test_.RescheduleUnlessTriggered(); |
| 192 | dispatcher_->DispatchForever(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 193 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 194 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 195 | TEST_F(ShillDaemonTest, EventDispatcherIO) { |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 196 | EXPECT_CALL(dispatcher_test_, IOComplete(16)); |
| 197 | int pipefd[2]; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 198 | ASSERT_EQ(pipe(pipefd), 0); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 199 | |
| 200 | dispatcher_test_.ListenIO(pipefd[0]); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 201 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 202 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 203 | dispatcher_->DispatchForever(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 204 | dispatcher_test_.StopListenIO(); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 207 | TEST_F(ShillDaemonTest, EventDispatcherReady) { |
| 208 | EXPECT_CALL(dispatcher_test_, IOComplete(0)) |
| 209 | .Times(1); |
| 210 | EXPECT_CALL(dispatcher_test_, IOComplete(1)) |
| 211 | .Times(1); |
| 212 | |
| 213 | int pipefd[2]; |
| 214 | ASSERT_EQ(pipe(pipefd), 0); |
| 215 | |
| 216 | dispatcher_test_.ListenReady(pipefd[0]); |
| 217 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
| 218 | |
| 219 | dispatcher_->DispatchForever(); |
| 220 | dispatcher_test_.StopListenReady(); |
| 221 | } |
| 222 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 223 | } // namespace shill |