blob: dd6b82f13e5b7aff9cf6d316b7f00f431b09f817 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Peter Qiufda548c2015-01-13 14:39:19 -080016
17#include "apmanager/hostapd_monitor.h"
18
19#include <base/bind.h>
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22#include <shill/net/io_handler.h>
23
24#include "apmanager/mock_event_dispatcher.h"
25
26using base::Bind;
27using base::Unretained;
28using ::testing::_;
29
30namespace {
31 const char kStationMac[] = "00:11:22:33:44:55";
32 const char kHostapdEventStationConnected[] =
33 "<2>AP-STA-CONNECTED 00:11:22:33:44:55";
34 const char kHostapdEventStationDisconnected[] =
35 "<2>AP-STA-DISCONNECTED 00:11:22:33:44:55";
36} // namespace
37
38namespace apmanager {
39
40class HostapdEventCallbackObserver {
41 public:
42 HostapdEventCallbackObserver()
43 : event_callback_(
44 Bind(&HostapdEventCallbackObserver::OnEventCallback,
45 Unretained(this))) {}
46 virtual ~HostapdEventCallbackObserver() {}
47
48 MOCK_METHOD2(OnEventCallback,
49 void(HostapdMonitor::Event event, const std::string& data));
50
51 const HostapdMonitor::EventCallback event_callback() {
52 return event_callback_;
53 }
54
55 private:
56 HostapdMonitor::EventCallback event_callback_;
57
58 DISALLOW_COPY_AND_ASSIGN(HostapdEventCallbackObserver);
59};
60
61class HostapdMonitorTest : public testing::Test {
62 public:
63 HostapdMonitorTest()
64 : hostapd_monitor_(observer_.event_callback(), "", ""),
65 event_dispatcher_(MockEventDispatcher::GetInstance()) {}
66
67 virtual void SetUp() {
68 hostapd_monitor_.event_dispatcher_ = event_dispatcher_;
69 }
70
71 void Start() {
72 hostapd_monitor_.Start();
73 }
74
75 void ParseMessage(shill::InputData* data) {
76 hostapd_monitor_.ParseMessage(data);
77 }
78
79 protected:
80 HostapdEventCallbackObserver observer_;
81 HostapdMonitor hostapd_monitor_;
82 MockEventDispatcher* event_dispatcher_;
83};
84
85TEST_F(HostapdMonitorTest, Start) {
86 EXPECT_CALL(*event_dispatcher_, PostTask(_)).Times(1);
87 Start();
88
89 // Monitor already started, nothing to be done.
90 EXPECT_CALL(*event_dispatcher_, PostTask(_)).Times(0);
91 Start();
92}
93
94TEST_F(HostapdMonitorTest, StationConnected) {
95 shill::InputData data;
96 data.buf = reinterpret_cast<unsigned char*>(
97 const_cast<char*>(kHostapdEventStationConnected));
98 data.len = strlen(kHostapdEventStationConnected);
99 EXPECT_CALL(observer_,
100 OnEventCallback(HostapdMonitor::kStationConnected,
101 kStationMac)).Times(1);
102 ParseMessage(&data);
103}
104
105TEST_F(HostapdMonitorTest, StationDisconnected) {
106 shill::InputData data;
107 data.buf = reinterpret_cast<unsigned char*>(
108 const_cast<char*>(kHostapdEventStationDisconnected));
109 data.len = strlen(kHostapdEventStationDisconnected);
110 EXPECT_CALL(observer_,
111 OnEventCallback(HostapdMonitor::kStationDisconnected,
112 kStationMac)).Times(1);
113 ParseMessage(&data);
114}
115
116} // namespace apmanager