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