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