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