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 | 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" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.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" |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 20 | #include "shill/mock_metrics.h" |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 21 | #include "shill/mock_proxy_factory.h" |
| 22 | #include "shill/mock_rtnl_handler.h" |
| 23 | #include "shill/mock_routing_table.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | #include "shill/shill_daemon.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 25 | #include "shill/shill_test_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 27 | using base::Bind; |
| 28 | using base::Callback; |
| 29 | using base::CancelableClosure; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 30 | using base::Unretained; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 31 | using base::WeakPtrFactory; |
| 32 | |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 33 | using ::testing::Expectation; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 34 | using ::testing::Gt; |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 35 | using ::testing::Mock; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 36 | using ::testing::NotNull; |
| 37 | using ::testing::Return; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 38 | using ::testing::StrictMock; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 39 | using ::testing::Test; |
| 40 | using ::testing::_; |
| 41 | |
| 42 | namespace shill { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 43 | |
| 44 | class MockEventDispatchTester { |
| 45 | public: |
| 46 | explicit MockEventDispatchTester(EventDispatcher *dispatcher) |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 47 | : dispatcher_(dispatcher), |
| 48 | triggered_(false), |
| 49 | callback_count_(0), |
| 50 | got_data_(false), |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 51 | got_ready_(false), |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 52 | input_handler_(NULL), |
| 53 | tester_factory_(this) { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 56 | void ScheduleFailSafe() { |
| 57 | // 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] | 58 | // wrong. |
| 59 | failsafe_.Reset(Bind(&MockEventDispatchTester::StopDispatcher, |
| 60 | tester_factory_.GetWeakPtr())); |
| 61 | dispatcher_->PostDelayedTask(failsafe_.callback(), 100); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 64 | void ScheduleTimedTasks() { |
| 65 | dispatcher_->PostDelayedTask( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 66 | Bind(&MockEventDispatchTester::Trigger, tester_factory_.GetWeakPtr()), |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 67 | 10); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 70 | void RescheduleUnlessTriggered() { |
| 71 | ++callback_count_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 72 | if (!triggered_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 73 | dispatcher_->PostTask( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | Bind(&MockEventDispatchTester::RescheduleUnlessTriggered, |
| 75 | tester_factory_.GetWeakPtr())); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 76 | } else { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 77 | failsafe_.Cancel(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 78 | StopDispatcher(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 82 | void StopDispatcher() { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 83 | dispatcher_->PostTask(MessageLoop::QuitClosure()); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 84 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 85 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 86 | void Trigger() { |
| 87 | LOG(INFO) << "MockEventDispatchTester handling " << callback_count_; |
| 88 | CallbackComplete(callback_count_); |
| 89 | triggered_ = true; |
| 90 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 91 | |
| 92 | void HandleData(InputData *inputData) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 93 | LOG(INFO) << "MockEventDispatchTester handling data len " |
Paul Stewart | 8322440 | 2011-11-30 14:52:30 -0800 | [diff] [blame] | 94 | << base::StringPrintf("%zd %.*s", inputData->len, |
| 95 | static_cast<int>(inputData->len), |
| 96 | inputData->buf); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 97 | got_data_ = true; |
| 98 | IOComplete(inputData->len); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 99 | StopDispatcher(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 100 | } |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 101 | |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 102 | bool GetData() { return got_data_; } |
| 103 | |
| 104 | void ListenIO(int fd) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 105 | data_callback_ = Bind(&MockEventDispatchTester::HandleData, |
| 106 | tester_factory_.GetWeakPtr()); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 107 | input_handler_.reset(dispatcher_->CreateInputHandler( |
| 108 | fd, data_callback_, IOHandler::ErrorCallback())); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void StopListenIO() { |
| 112 | got_data_ = false; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 113 | input_handler_.reset(NULL); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 116 | void HandleReady(int fd) { |
| 117 | // Stop event handling after we receive in input-ready event. We should |
| 118 | // no longer be called until events are re-enabled. |
| 119 | input_handler_->Stop(); |
| 120 | |
| 121 | if (got_ready_) { |
| 122 | // If we're still getting events after we have stopped them, something |
| 123 | // is really wrong, and we cannot just depend on ASSERT_FALSE() to get |
| 124 | // us out of it. Make sure the dispatcher is also stopped, or else we |
| 125 | // could end up never exiting. |
| 126 | StopDispatcher(); |
| 127 | ASSERT_FALSE(got_ready_) << "failed to stop Input Ready events"; |
| 128 | } |
| 129 | got_ready_ = true; |
| 130 | |
| 131 | LOG(INFO) << "MockEventDispatchTester handling ready for fd " << fd; |
| 132 | IOComplete(callback_count_); |
| 133 | |
| 134 | if (callback_count_) { |
| 135 | StopDispatcher(); |
| 136 | } else { |
| 137 | // Restart Ready events after 10 millisecond delay. |
| 138 | callback_count_++; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 139 | dispatcher_->PostDelayedTask( |
| 140 | Bind(&MockEventDispatchTester::RestartReady, |
| 141 | tester_factory_.GetWeakPtr()), |
| 142 | 10); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | void RestartReady() { |
| 147 | got_ready_ = false; |
| 148 | input_handler_->Start(); |
| 149 | } |
| 150 | |
| 151 | void ListenReady(int fd) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 152 | ready_callback_ = Bind(&MockEventDispatchTester::HandleReady, |
| 153 | tester_factory_.GetWeakPtr()); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 154 | input_handler_.reset( |
| 155 | dispatcher_->CreateReadyHandler(fd, IOHandler::kModeInput, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 156 | ready_callback_)); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void StopListenReady() { |
| 160 | got_ready_ = false; |
| 161 | input_handler_.reset(NULL); |
| 162 | } |
| 163 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 164 | MOCK_METHOD1(CallbackComplete, void(int)); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 165 | MOCK_METHOD1(IOComplete, void(int)); |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 166 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 167 | private: |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 168 | EventDispatcher *dispatcher_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 169 | bool triggered_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 170 | int callback_count_; |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 171 | bool got_data_; |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 172 | bool got_ready_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 173 | Callback<void(InputData*)> data_callback_; |
| 174 | Callback<void(int)> ready_callback_; |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 175 | scoped_ptr<IOHandler> input_handler_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 176 | WeakPtrFactory<MockEventDispatchTester> tester_factory_; |
| 177 | CancelableClosure failsafe_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | class ShillDaemonTest : public Test { |
| 181 | public: |
| 182 | ShillDaemonTest() |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 183 | : daemon_(&config_, new MockControl()), |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 184 | metrics_(new MockMetrics(&daemon_.dispatcher_)), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 185 | manager_(new MockManager(daemon_.control_, |
| 186 | &daemon_.dispatcher_, |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 187 | metrics_, |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 188 | &daemon_.glib_)), |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 189 | device_info_(daemon_.control_, dispatcher_, metrics_, |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 190 | daemon_.manager_.get()), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 191 | dispatcher_(&daemon_.dispatcher_), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 192 | dispatcher_test_(dispatcher_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 193 | } |
| 194 | virtual ~ShillDaemonTest() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 195 | virtual void SetUp() { |
| 196 | // Tests initialization done by the daemon's constructor |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 197 | ASSERT_NE(reinterpret_cast<Config*>(NULL), daemon_.config_); |
| 198 | ASSERT_NE(reinterpret_cast<ControlInterface*>(NULL), daemon_.control_); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 199 | daemon_.proxy_factory_ = &proxy_factory_; |
| 200 | daemon_.rtnl_handler_ = &rtnl_handler_; |
| 201 | daemon_.routing_table_ = &routing_table_; |
| 202 | daemon_.dhcp_provider_ = &dhcp_provider_; |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 203 | daemon_.metrics_.reset(metrics_); // Passes ownership |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 204 | daemon_.manager_.reset(manager_); // Passes ownership |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 205 | dispatcher_test_.ScheduleFailSafe(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 206 | } |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 207 | void StartDaemon() { |
| 208 | daemon_.Start(); |
| 209 | } |
| 210 | |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 211 | void StopDaemon() { |
| 212 | daemon_.Stop(); |
| 213 | } |
| 214 | |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 215 | void ResetNetlinkManager() { |
| 216 | daemon_.netlink_manager_->Reset(true); |
Darin Petkov | d581838 | 2013-01-28 16:27:07 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 219 | MOCK_METHOD0(TerminationAction, void()); |
| 220 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 221 | protected: |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 222 | TestConfig config_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 223 | Daemon daemon_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 224 | MockProxyFactory proxy_factory_; |
| 225 | MockRTNLHandler rtnl_handler_; |
| 226 | MockRoutingTable routing_table_; |
| 227 | MockDHCPProvider dhcp_provider_; |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 228 | MockMetrics *metrics_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 229 | MockManager *manager_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 230 | DeviceInfo device_info_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 231 | EventDispatcher *dispatcher_; |
| 232 | StrictMock<MockEventDispatchTester> dispatcher_test_; |
| 233 | }; |
| 234 | |
| 235 | |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 236 | TEST_F(ShillDaemonTest, StartStop) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 237 | // To ensure we do not have any stale routes, we flush a device's routes |
| 238 | // when it is started. This requires that the routing table is fully |
| 239 | // populated before we create and start devices. So test to make sure that |
| 240 | // the RoutingTable starts before the Manager (which in turn starts |
| 241 | // DeviceInfo who is responsible for creating and starting devices). |
| 242 | // The result is that we request the dump of the routing table and when that |
| 243 | // completes, we request the dump of the links. For each link found, we |
| 244 | // create and start the device. |
| 245 | EXPECT_CALL(proxy_factory_, Init()); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 246 | EXPECT_CALL(*metrics_, Start()); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 247 | EXPECT_CALL(rtnl_handler_, Start(_, _)); |
| 248 | Expectation routing_table_started = EXPECT_CALL(routing_table_, Start()); |
| 249 | EXPECT_CALL(dhcp_provider_, Init(_, _, _)); |
| 250 | EXPECT_CALL(*manager_, Start()).After(routing_table_started); |
| 251 | StartDaemon(); |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 252 | Mock::VerifyAndClearExpectations(metrics_); |
| 253 | Mock::VerifyAndClearExpectations(manager_); |
| 254 | |
| 255 | EXPECT_CALL(*manager_, Stop()); |
| 256 | EXPECT_CALL(*metrics_, Stop()); |
| 257 | StopDaemon(); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 260 | TEST_F(ShillDaemonTest, EventDispatcherTimer) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 261 | EXPECT_CALL(dispatcher_test_, CallbackComplete(Gt(0))); |
| 262 | dispatcher_test_.ScheduleTimedTasks(); |
| 263 | dispatcher_test_.RescheduleUnlessTriggered(); |
| 264 | dispatcher_->DispatchForever(); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 265 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 266 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 267 | TEST_F(ShillDaemonTest, EventDispatcherIO) { |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 268 | EXPECT_CALL(dispatcher_test_, IOComplete(16)); |
| 269 | int pipefd[2]; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 270 | ASSERT_EQ(pipe(pipefd), 0); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 271 | |
| 272 | dispatcher_test_.ListenIO(pipefd[0]); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 273 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 274 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 275 | dispatcher_->DispatchForever(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 276 | dispatcher_test_.StopListenIO(); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 279 | TEST_F(ShillDaemonTest, EventDispatcherReady) { |
| 280 | EXPECT_CALL(dispatcher_test_, IOComplete(0)) |
| 281 | .Times(1); |
| 282 | EXPECT_CALL(dispatcher_test_, IOComplete(1)) |
| 283 | .Times(1); |
| 284 | |
| 285 | int pipefd[2]; |
| 286 | ASSERT_EQ(pipe(pipefd), 0); |
| 287 | |
| 288 | dispatcher_test_.ListenReady(pipefd[0]); |
| 289 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
| 290 | |
| 291 | dispatcher_->DispatchForever(); |
| 292 | dispatcher_test_.StopListenReady(); |
| 293 | } |
| 294 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 295 | ACTION_P2(CompleteAction, manager, name) { |
| 296 | manager->TerminationActionComplete(name); |
| 297 | } |
| 298 | |
| 299 | TEST_F(ShillDaemonTest, Quit) { |
| 300 | // The following expectations are to satisfy calls in Daemon::Start(). |
| 301 | EXPECT_CALL(proxy_factory_, Init()); |
| 302 | EXPECT_CALL(rtnl_handler_, Start(_, _)); |
| 303 | EXPECT_CALL(routing_table_, Start()); |
| 304 | EXPECT_CALL(dhcp_provider_, Init(_, _, _)); |
| 305 | EXPECT_CALL(*manager_, Start()); |
| 306 | |
| 307 | // This expectation verifies that the termination actions are invoked. |
| 308 | EXPECT_CALL(*this, TerminationAction()) |
| 309 | .WillOnce(CompleteAction(manager_, "daemon test")); |
| 310 | |
| 311 | manager_->AddTerminationAction("daemon test", |
| 312 | Bind(&ShillDaemonTest::TerminationAction, |
| 313 | Unretained(this))); |
| 314 | |
| 315 | // Run Daemon::Quit() after the daemon starts running. |
| 316 | dispatcher_->PostTask(Bind(&Daemon::Quit, Unretained(&daemon_))); |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 317 | |
| 318 | // TODO(wdg): crbug.com/224712 - Need fix for "GLib-WARNING **: Invalid file |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 319 | // descriptor". This requires support for netlink_manager interface tests. |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 320 | daemon_.Run(); |
Wade Guthrie | bb9fca2 | 2013-04-10 17:21:42 -0700 | [diff] [blame] | 321 | ResetNetlinkManager(); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 324 | } // namespace shill |