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" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 17 | #include "shill/shill_config.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 18 | #include "shill/shill_daemon.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), |
| 35 | data_callback_(NULL), |
| 36 | input_handler_(NULL), |
| 37 | tester_factory_(this) { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 40 | void ScheduleTimedTasks() { |
| 41 | dispatcher_->PostDelayedTask( |
| 42 | tester_factory_.NewRunnableMethod(&MockEventDispatchTester::Trigger), |
| 43 | 10); |
| 44 | // also set up a failsafe, so the test still exits even if something goes |
| 45 | // wrong. The Factory owns the RunnableMethod, but we get a pointer to it. |
| 46 | failsafe_ = tester_factory_.NewRunnableMethod( |
| 47 | &MockEventDispatchTester::QuitRegardless); |
| 48 | dispatcher_->PostDelayedTask(failsafe_, 100); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 51 | void RescheduleUnlessTriggered() { |
| 52 | ++callback_count_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 53 | if (!triggered_) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 54 | dispatcher_->PostTask( |
| 55 | tester_factory_.NewRunnableMethod( |
| 56 | &MockEventDispatchTester::RescheduleUnlessTriggered)); |
| 57 | } else { |
| 58 | failsafe_->Cancel(); |
| 59 | QuitRegardless(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 63 | void QuitRegardless() { |
| 64 | dispatcher_->PostTask(new MessageLoop::QuitTask); |
| 65 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 66 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 67 | void Trigger() { |
| 68 | LOG(INFO) << "MockEventDispatchTester handling " << callback_count_; |
| 69 | CallbackComplete(callback_count_); |
| 70 | triggered_ = true; |
| 71 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 72 | |
| 73 | void HandleData(InputData *inputData) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 74 | LOG(INFO) << "MockEventDispatchTester handling data len " |
| 75 | << base::StringPrintf("%d %.*s", inputData->len, |
| 76 | inputData->len, inputData->buf); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 77 | got_data_ = true; |
| 78 | IOComplete(inputData->len); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 79 | QuitRegardless(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 80 | } |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 81 | |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 82 | bool GetData() { return got_data_; } |
| 83 | |
| 84 | void ListenIO(int fd) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 85 | data_callback_.reset(NewCallback(this, |
| 86 | &MockEventDispatchTester::HandleData)); |
| 87 | input_handler_.reset(dispatcher_->CreateInputHandler(fd, |
| 88 | data_callback_.get())); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void StopListenIO() { |
| 92 | got_data_ = false; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 93 | input_handler_.reset(NULL); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 96 | MOCK_METHOD1(CallbackComplete, void(int)); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 97 | MOCK_METHOD1(IOComplete, void(int)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 98 | private: |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 99 | EventDispatcher *dispatcher_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 100 | bool triggered_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 101 | int callback_count_; |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 102 | bool got_data_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 103 | scoped_ptr<Callback1<InputData*>::Type> data_callback_; |
| 104 | scoped_ptr<IOInputHandler> input_handler_; |
| 105 | ScopedRunnableMethodFactory<MockEventDispatchTester> tester_factory_; |
| 106 | CancelableTask* failsafe_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | class ShillDaemonTest : public Test { |
| 110 | public: |
| 111 | ShillDaemonTest() |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 112 | : daemon_(&config_, new MockControl()), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 113 | device_info_(daemon_.control_, dispatcher_, &daemon_.manager_), |
| 114 | dispatcher_(&daemon_.dispatcher_), |
| 115 | dispatcher_test_(dispatcher_), |
| 116 | factory_(this) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 117 | } |
| 118 | virtual ~ShillDaemonTest() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 119 | virtual void SetUp() { |
| 120 | // Tests initialization done by the daemon's constructor |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 121 | ASSERT_NE(reinterpret_cast<Config*>(NULL), daemon_.config_); |
| 122 | ASSERT_NE(reinterpret_cast<ControlInterface*>(NULL), daemon_.control_); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 123 | } |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 124 | protected: |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 125 | Config config_; |
| 126 | Daemon daemon_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 127 | DeviceInfo device_info_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 128 | EventDispatcher *dispatcher_; |
| 129 | StrictMock<MockEventDispatchTester> dispatcher_test_; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 130 | ScopedRunnableMethodFactory<ShillDaemonTest> factory_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | |
| 134 | TEST_F(ShillDaemonTest, EventDispatcher) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 135 | EXPECT_CALL(dispatcher_test_, CallbackComplete(Gt(0))); |
| 136 | dispatcher_test_.ScheduleTimedTasks(); |
| 137 | dispatcher_test_.RescheduleUnlessTriggered(); |
| 138 | dispatcher_->DispatchForever(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 139 | |
| 140 | EXPECT_CALL(dispatcher_test_, IOComplete(16)); |
| 141 | int pipefd[2]; |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 142 | ASSERT_EQ(pipe(pipefd), 0); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 143 | |
| 144 | dispatcher_test_.ListenIO(pipefd[0]); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 145 | ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 146 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 147 | dispatcher_->DispatchForever(); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 148 | dispatcher_test_.StopListenIO(); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 151 | } // namespace shill |