Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [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 | |
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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
| 8 | #include <base/cancelable_callback.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 10 | #include <base/memory/ref_counted.h> |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 11 | #include <base/message_loop_proxy.h> |
| 12 | #include <base/stringprintf.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 13 | #include <gmock/gmock.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 16 | #include "shill/io_handler.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 17 | #include "shill/mock_control.h" |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 18 | #include "shill/mock_dhcp_provider.h" |
| 19 | #include "shill/mock_manager.h" |
| 20 | #include "shill/mock_proxy_factory.h" |
| 21 | #include "shill/mock_rtnl_handler.h" |
| 22 | #include "shill/mock_routing_table.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/shill_daemon.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 24 | #include "shill/shill_test_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 26 | using base::Bind; |
| 27 | using base::Callback; |
| 28 | using base::CancelableClosure; |
| 29 | using base::WeakPtrFactory; |
| 30 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 31 | using ::testing::Expectation; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 32 | using ::testing::Gt; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 33 | using ::testing::NotNull; |
| 34 | using ::testing::Return; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 35 | using ::testing::StrictMock; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 36 | using ::testing::Test; |
| 37 | using ::testing::_; |
| 38 | |
| 39 | namespace shill { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 40 | |
| 41 | class MockEventDispatchTester { |
| 42 | public: |
| 43 | explicit MockEventDispatchTester(EventDispatcher *dispatcher) |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 44 | : dispatcher_(dispatcher), |
| 45 | triggered_(false), |
| 46 | callback_count_(0), |
| 47 | got_data_(false), |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 48 | got_ready_(false), |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 49 | input_handler_(NULL), |
| 50 | tester_factory_(this) { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 53 | void ScheduleFailSafe() { |
| 54 | // Set up a failsafe, so the test still exits even if something goes |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 55 | // wrong. |
| 56 | failsafe_.Reset(Bind(&MockEventDispatchTester::StopDispatcher, |
| 57 | tester_factory_.GetWeakPtr())); |
| 58 | dispatcher_->PostDelayedTask(failsafe_.callback(), 100); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 61 | void ScheduleTimedTasks() { |
| 62 | dispatcher_->PostDelayedTask( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 63 | Bind(&MockEventDispatchTester::Trigger, tester_factory_.GetWeakPtr()), |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 64 | 10); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 67 | void RescheduleUnlessTriggered() { |
| 68 | ++callback_count_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 69 | if (!triggered_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 70 | dispatcher_->PostTask( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 71 | Bind(&MockEventDispatchTester::RescheduleUnlessTriggered, |
| 72 | tester_factory_.GetWeakPtr())); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 73 | } else { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | failsafe_.Cancel(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 75 | StopDispatcher(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 79 | void StopDispatcher() { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 80 | dispatcher_->PostTask(MessageLoop::QuitClosure()); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 81 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 82 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 83 | void Trigger() { |
| 84 | LOG(INFO) << "MockEventDispatchTester handling " << callback_count_; |
| 85 | CallbackComplete(callback_count_); |
| 86 | triggered_ = true; |
| 87 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 88 | |
| 89 | void HandleData(InputData *inputData) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 90 | LOG(INFO) << "MockEventDispatchTester handling data len " |
Paul Stewart | 8322440 | 2011-11-30 14:52:30 -0800 | [diff] [blame] | 91 | << base::StringPrintf("%zd %.*s", inputData->len, |
| 92 | static_cast<int>(inputData->len), |
| 93 | inputData->buf); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 94 | got_data_ = true; |
| 95 | IOComplete(inputData->len); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 96 | StopDispatcher(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 97 | } |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 98 | |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 99 | bool GetData() { return got_data_; } |
| 100 | |
| 101 | void ListenIO(int fd) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 102 | data_callback_ = Bind(&MockEventDispatchTester::HandleData, |
| 103 | tester_factory_.GetWeakPtr()); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 104 | input_handler_.reset( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 105 | dispatcher_->CreateInputHandler(fd, data_callback_)); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void StopListenIO() { |
| 109 | got_data_ = false; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 110 | input_handler_.reset(NULL); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 113 | void HandleReady(int fd) { |
| 114 | // Stop event handling after we receive in input-ready event. We should |
| 115 | // no longer be called until events are re-enabled. |
| 116 | input_handler_->Stop(); |
| 117 | |
| 118 | if (got_ready_) { |
| 119 | // If we're still getting events after we have stopped them, something |
| 120 | // is really wrong, and we cannot just depend on ASSERT_FALSE() to get |
| 121 | // us out of it. Make sure the dispatcher is also stopped, or else we |
| 122 | // could end up never exiting. |
| 123 | StopDispatcher(); |
| 124 | ASSERT_FALSE(got_ready_) << "failed to stop Input Ready events"; |
| 125 | } |
| 126 | got_ready_ = true; |
| 127 | |
| 128 | LOG(INFO) << "MockEventDispatchTester handling ready for fd " << fd; |
| 129 | IOComplete(callback_count_); |
| 130 | |
| 131 | if (callback_count_) { |
| 132 | StopDispatcher(); |
| 133 | } else { |
| 134 | // Restart Ready events after 10 millisecond delay. |
| 135 | callback_count_++; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 136 | dispatcher_->PostDelayedTask( |
| 137 | Bind(&MockEventDispatchTester::RestartReady, |
| 138 | tester_factory_.GetWeakPtr()), |
| 139 | 10); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | void RestartReady() { |
| 144 | got_ready_ = false; |
| 145 | input_handler_->Start(); |
| 146 | } |
| 147 | |
| 148 | void ListenReady(int fd) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 149 | ready_callback_ = Bind(&MockEventDispatchTester::HandleReady, |
| 150 | tester_factory_.GetWeakPtr()); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 151 | input_handler_.reset( |
| 152 | dispatcher_->CreateReadyHandler(fd, IOHandler::kModeInput, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 153 | ready_callback_)); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void StopListenReady() { |
| 157 | got_ready_ = false; |
| 158 | input_handler_.reset(NULL); |
| 159 | } |
| 160 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 161 | MOCK_METHOD1(CallbackComplete, void(int)); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 162 | MOCK_METHOD1(IOComplete, void(int)); |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 163 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 164 | private: |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 165 | EventDispatcher *dispatcher_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 166 | bool triggered_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 167 | int callback_count_; |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 168 | bool got_data_; |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 169 | bool got_ready_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 170 | Callback<void(InputData*)> data_callback_; |
| 171 | Callback<void(int)> ready_callback_; |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 172 | scoped_ptr<IOHandler> input_handler_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 173 | WeakPtrFactory<MockEventDispatchTester> tester_factory_; |
| 174 | CancelableClosure failsafe_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | class ShillDaemonTest : public Test { |
| 178 | public: |
| 179 | ShillDaemonTest() |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 180 | : daemon_(&config_, new MockControl()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 181 | manager_(new MockManager(daemon_.control_, |
| 182 | &daemon_.dispatcher_, |
| 183 | &daemon_.metrics_, |
| 184 | &daemon_.glib_)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 185 | device_info_(daemon_.control_, dispatcher_, &daemon_.metrics_, |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 186 | daemon_.manager_.get()), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 187 | dispatcher_(&daemon_.dispatcher_), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 188 | dispatcher_test_(dispatcher_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 189 | } |
| 190 | virtual ~ShillDaemonTest() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 191 | virtual void SetUp() { |
| 192 | // Tests initialization done by the daemon's constructor |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 193 | ASSERT_NE(reinterpret_cast<Config*>(NULL), daemon_.config_); |
| 194 | ASSERT_NE(reinterpret_cast<ControlInterface*>(NULL), daemon_.control_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 195 | daemon_.proxy_factory_ = &proxy_factory_; |
| 196 | daemon_.rtnl_handler_ = &rtnl_handler_; |
| 197 | daemon_.routing_table_ = &routing_table_; |
| 198 | daemon_.dhcp_provider_ = &dhcp_provider_; |
| 199 | daemon_.manager_.reset(manager_); // Passes ownership |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 200 | dispatcher_test_.ScheduleFailSafe(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 201 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 202 | void StartDaemon() { |
| 203 | daemon_.Start(); |
| 204 | } |
| 205 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 206 | protected: |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 207 | TestConfig config_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 208 | Daemon daemon_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 209 | MockProxyFactory proxy_factory_; |
| 210 | MockRTNLHandler rtnl_handler_; |
| 211 | MockRoutingTable routing_table_; |
| 212 | MockDHCPProvider dhcp_provider_; |
| 213 | MockManager *manager_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 214 | DeviceInfo device_info_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 215 | EventDispatcher *dispatcher_; |
| 216 | StrictMock<MockEventDispatchTester> dispatcher_test_; |
| 217 | }; |
| 218 | |
| 219 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 220 | TEST_F(ShillDaemonTest, Start) { |
| 221 | // To ensure we do not have any stale routes, we flush a device's routes |
| 222 | // when it is started. This requires that the routing table is fully |
| 223 | // populated before we create and start devices. So test to make sure that |
| 224 | // the RoutingTable starts before the Manager (which in turn starts |
| 225 | // DeviceInfo who is responsible for creating and starting devices). |
| 226 | // The result is that we request the dump of the routing table and when that |
| 227 | // completes, we request the dump of the links. For each link found, we |
| 228 | // create and start the device. |
| 229 | EXPECT_CALL(proxy_factory_, Init()); |
| 230 | EXPECT_CALL(rtnl_handler_, Start(_, _)); |
| 231 | Expectation routing_table_started = EXPECT_CALL(routing_table_, Start()); |
| 232 | EXPECT_CALL(dhcp_provider_, Init(_, _, _)); |
| 233 | EXPECT_CALL(*manager_, Start()).After(routing_table_started); |
| 234 | StartDaemon(); |
| 235 | } |
| 236 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 237 | TEST_F(ShillDaemonTest, EventDispatcherTimer) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 238 | EXPECT_CALL(dispatcher_test_, CallbackComplete(Gt(0))); |
| 239 | dispatcher_test_.ScheduleTimedTasks(); |
| 240 | dispatcher_test_.RescheduleUnlessTriggered(); |
| 241 | dispatcher_->DispatchForever(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 242 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 243 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 244 | TEST_F(ShillDaemonTest, EventDispatcherIO) { |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 245 | EXPECT_CALL(dispatcher_test_, IOComplete(16)); |
| 246 | int pipefd[2]; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 247 | ASSERT_EQ(pipe(pipefd), 0); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 248 | |
| 249 | dispatcher_test_.ListenIO(pipefd[0]); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 250 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 251 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 252 | dispatcher_->DispatchForever(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 253 | dispatcher_test_.StopListenIO(); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 256 | TEST_F(ShillDaemonTest, EventDispatcherReady) { |
| 257 | EXPECT_CALL(dispatcher_test_, IOComplete(0)) |
| 258 | .Times(1); |
| 259 | EXPECT_CALL(dispatcher_test_, IOComplete(1)) |
| 260 | .Times(1); |
| 261 | |
| 262 | int pipefd[2]; |
| 263 | ASSERT_EQ(pipe(pipefd), 0); |
| 264 | |
| 265 | dispatcher_test_.ListenReady(pipefd[0]); |
| 266 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
| 267 | |
| 268 | dispatcher_->DispatchForever(); |
| 269 | dispatcher_test_.StopListenReady(); |
| 270 | } |
| 271 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 272 | } // namespace shill |