blob: 5d6736930c3192ee64d5b49145072e0202f8f032 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// 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 Stewart75897df2011-04-27 09:05:53 -07005#include <stdint.h>
Paul Stewart75897df2011-04-27 09:05:53 -07006
Chris Masone487b8bf2011-05-13 16:27:57 -07007#include <base/callback_old.h>
Chris Masoneee929b72011-05-10 10:02:18 -07008#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -07009#include <base/memory/ref_counted.h>
Chris Masone0e1d1042011-05-09 18:07:03 -070010#include <base/message_loop_proxy.h>
11#include <base/stringprintf.h>
Chris Masoneee929b72011-05-10 10:02:18 -070012#include <gmock/gmock.h>
Darin Petkov887f2982011-07-14 16:10:17 -070013#include <gtest/gtest.h>
Chris Masoneee929b72011-05-10 10:02:18 -070014
Darin Petkov67d8ecf2011-07-26 16:03:30 -070015#include "shill/io_handler.h"
Darin Petkov887f2982011-07-14 16:10:17 -070016#include "shill/mock_control.h"
Darin Petkov633ac6f2011-07-08 13:56:13 -070017#include "shill/shill_config.h"
Paul Stewart75897df2011-04-27 09:05:53 -070018#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019
20namespace shill {
21using ::testing::Test;
22using ::testing::_;
Chris Masone0e1d1042011-05-09 18:07:03 -070023using ::testing::Gt;
Paul Stewart75897df2011-04-27 09:05:53 -070024using ::testing::NotNull;
25using ::testing::Return;
Paul Stewart75897df2011-04-27 09:05:53 -070026using ::testing::StrictMock;
27
28class MockEventDispatchTester {
29 public:
30 explicit MockEventDispatchTester(EventDispatcher *dispatcher)
Chris Masone0e1d1042011-05-09 18:07:03 -070031 : 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 Stewart75897df2011-04-27 09:05:53 -070038 }
39
Chris Masone0e1d1042011-05-09 18:07:03 -070040 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 Stewart75897df2011-04-27 09:05:53 -070049 }
50
Chris Masone0e1d1042011-05-09 18:07:03 -070051 void RescheduleUnlessTriggered() {
52 ++callback_count_;
Paul Stewart75897df2011-04-27 09:05:53 -070053 if (!triggered_) {
Chris Masone0e1d1042011-05-09 18:07:03 -070054 dispatcher_->PostTask(
55 tester_factory_.NewRunnableMethod(
56 &MockEventDispatchTester::RescheduleUnlessTriggered));
57 } else {
58 failsafe_->Cancel();
59 QuitRegardless();
Paul Stewart75897df2011-04-27 09:05:53 -070060 }
61 }
62
Chris Masone0e1d1042011-05-09 18:07:03 -070063 void QuitRegardless() {
64 dispatcher_->PostTask(new MessageLoop::QuitTask);
65 }
Paul Stewart75897df2011-04-27 09:05:53 -070066
Chris Masone0e1d1042011-05-09 18:07:03 -070067 void Trigger() {
68 LOG(INFO) << "MockEventDispatchTester handling " << callback_count_;
69 CallbackComplete(callback_count_);
70 triggered_ = true;
71 }
Paul Stewarta43d9232011-05-10 11:40:22 -070072
73 void HandleData(InputData *inputData) {
Chris Masone0e1d1042011-05-09 18:07:03 -070074 LOG(INFO) << "MockEventDispatchTester handling data len "
75 << base::StringPrintf("%d %.*s", inputData->len,
76 inputData->len, inputData->buf);
Paul Stewarta43d9232011-05-10 11:40:22 -070077 got_data_ = true;
78 IOComplete(inputData->len);
Chris Masone0e1d1042011-05-09 18:07:03 -070079 QuitRegardless();
Paul Stewarta43d9232011-05-10 11:40:22 -070080 }
Chris Masone0e1d1042011-05-09 18:07:03 -070081
Paul Stewarta43d9232011-05-10 11:40:22 -070082 bool GetData() { return got_data_; }
83
84 void ListenIO(int fd) {
Chris Masone0e1d1042011-05-09 18:07:03 -070085 data_callback_.reset(NewCallback(this,
86 &MockEventDispatchTester::HandleData));
87 input_handler_.reset(dispatcher_->CreateInputHandler(fd,
88 data_callback_.get()));
Paul Stewarta43d9232011-05-10 11:40:22 -070089 }
90
91 void StopListenIO() {
92 got_data_ = false;
Chris Masone0e1d1042011-05-09 18:07:03 -070093 input_handler_.reset(NULL);
Paul Stewarta43d9232011-05-10 11:40:22 -070094 }
95
Paul Stewart75897df2011-04-27 09:05:53 -070096 MOCK_METHOD1(CallbackComplete, void(int));
Paul Stewarta43d9232011-05-10 11:40:22 -070097 MOCK_METHOD1(IOComplete, void(int));
Paul Stewart75897df2011-04-27 09:05:53 -070098 private:
Paul Stewarta43d9232011-05-10 11:40:22 -070099 EventDispatcher *dispatcher_;
Paul Stewart75897df2011-04-27 09:05:53 -0700100 bool triggered_;
Chris Masone0e1d1042011-05-09 18:07:03 -0700101 int callback_count_;
Paul Stewarta43d9232011-05-10 11:40:22 -0700102 bool got_data_;
Chris Masone0e1d1042011-05-09 18:07:03 -0700103 scoped_ptr<Callback1<InputData*>::Type> data_callback_;
104 scoped_ptr<IOInputHandler> input_handler_;
105 ScopedRunnableMethodFactory<MockEventDispatchTester> tester_factory_;
106 CancelableTask* failsafe_;
Paul Stewart75897df2011-04-27 09:05:53 -0700107};
108
109class ShillDaemonTest : public Test {
110 public:
111 ShillDaemonTest()
Darin Petkova7b89492011-07-27 12:48:17 -0700112 : daemon_(&config_, new MockControl()),
Darin Petkov887f2982011-07-14 16:10:17 -0700113 device_info_(daemon_.control_, dispatcher_, &daemon_.manager_),
114 dispatcher_(&daemon_.dispatcher_),
115 dispatcher_test_(dispatcher_),
116 factory_(this) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700117 }
118 virtual ~ShillDaemonTest() {}
Paul Stewart75897df2011-04-27 09:05:53 -0700119 virtual void SetUp() {
120 // Tests initialization done by the daemon's constructor
Chris Masone0e1d1042011-05-09 18:07:03 -0700121 ASSERT_NE(reinterpret_cast<Config*>(NULL), daemon_.config_);
122 ASSERT_NE(reinterpret_cast<ControlInterface*>(NULL), daemon_.control_);
Paul Stewart75897df2011-04-27 09:05:53 -0700123 }
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700124 protected:
Paul Stewart75897df2011-04-27 09:05:53 -0700125 Config config_;
126 Daemon daemon_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700127 DeviceInfo device_info_;
Paul Stewart75897df2011-04-27 09:05:53 -0700128 EventDispatcher *dispatcher_;
129 StrictMock<MockEventDispatchTester> dispatcher_test_;
Chris Masone0e1d1042011-05-09 18:07:03 -0700130 ScopedRunnableMethodFactory<ShillDaemonTest> factory_;
Paul Stewart75897df2011-04-27 09:05:53 -0700131};
132
133
134TEST_F(ShillDaemonTest, EventDispatcher) {
Chris Masone0e1d1042011-05-09 18:07:03 -0700135 EXPECT_CALL(dispatcher_test_, CallbackComplete(Gt(0)));
136 dispatcher_test_.ScheduleTimedTasks();
137 dispatcher_test_.RescheduleUnlessTriggered();
138 dispatcher_->DispatchForever();
Paul Stewarta43d9232011-05-10 11:40:22 -0700139
140 EXPECT_CALL(dispatcher_test_, IOComplete(16));
141 int pipefd[2];
Chris Masone0e1d1042011-05-09 18:07:03 -0700142 ASSERT_EQ(pipe(pipefd), 0);
Paul Stewarta43d9232011-05-10 11:40:22 -0700143
144 dispatcher_test_.ListenIO(pipefd[0]);
Chris Masone0e1d1042011-05-09 18:07:03 -0700145 ASSERT_EQ(write(pipefd[1], "This is a test?!", 16), 16);
Paul Stewarta43d9232011-05-10 11:40:22 -0700146
Chris Masone0e1d1042011-05-09 18:07:03 -0700147 dispatcher_->DispatchForever();
Paul Stewarta43d9232011-05-10 11:40:22 -0700148 dispatcher_test_.StopListenIO();
Chris Masone0e1d1042011-05-09 18:07:03 -0700149}
150
Chris Masone9be4a9d2011-05-16 15:44:09 -0700151} // namespace shill