1 //
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 //
16
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
26 using base::Bind;
27 using base::Unretained;
28 using ::testing::_;
29 using ::testing::Mock;
30
31 namespace {
32 const char kStationMac[] = "00:11:22:33:44:55";
33 const char kHostapdEventStationConnected[] =
34 "<2>AP-STA-CONNECTED 00:11:22:33:44:55";
35 const char kHostapdEventStationDisconnected[] =
36 "<2>AP-STA-DISCONNECTED 00:11:22:33:44:55";
37 } // namespace
38
39 namespace apmanager {
40
41 class HostapdEventCallbackObserver {
42 public:
HostapdEventCallbackObserver()43 HostapdEventCallbackObserver()
44 : event_callback_(
45 Bind(&HostapdEventCallbackObserver::OnEventCallback,
46 Unretained(this))) {}
~HostapdEventCallbackObserver()47 virtual ~HostapdEventCallbackObserver() {}
48
49 MOCK_METHOD2(OnEventCallback,
50 void(HostapdMonitor::Event event, const std::string& data));
51
event_callback()52 const HostapdMonitor::EventCallback event_callback() {
53 return event_callback_;
54 }
55
56 private:
57 HostapdMonitor::EventCallback event_callback_;
58
59 DISALLOW_COPY_AND_ASSIGN(HostapdEventCallbackObserver);
60 };
61
62 class HostapdMonitorTest : public testing::Test {
63 public:
HostapdMonitorTest()64 HostapdMonitorTest()
65 : hostapd_monitor_(observer_.event_callback(), "", "") {}
66
SetUp()67 virtual void SetUp() {
68 hostapd_monitor_.event_dispatcher_ = &event_dispatcher_;
69 }
70
Start()71 void Start() {
72 hostapd_monitor_.Start();
73 }
74
ParseMessage(shill::InputData * data)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
TEST_F(HostapdMonitorTest,Start)85 TEST_F(HostapdMonitorTest, Start) {
86 EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(1);
87 Start();
88 Mock::VerifyAndClearExpectations(&event_dispatcher_);
89
90 // Monitor already started, nothing to be done.
91 EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(0);
92 Start();
93 Mock::VerifyAndClearExpectations(&event_dispatcher_);
94 }
95
TEST_F(HostapdMonitorTest,StationConnected)96 TEST_F(HostapdMonitorTest, StationConnected) {
97 shill::InputData data;
98 data.buf = reinterpret_cast<unsigned char*>(
99 const_cast<char*>(kHostapdEventStationConnected));
100 data.len = strlen(kHostapdEventStationConnected);
101 EXPECT_CALL(observer_,
102 OnEventCallback(HostapdMonitor::kStationConnected,
103 kStationMac)).Times(1);
104 ParseMessage(&data);
105 }
106
TEST_F(HostapdMonitorTest,StationDisconnected)107 TEST_F(HostapdMonitorTest, StationDisconnected) {
108 shill::InputData data;
109 data.buf = reinterpret_cast<unsigned char*>(
110 const_cast<char*>(kHostapdEventStationDisconnected));
111 data.len = strlen(kHostapdEventStationDisconnected);
112 EXPECT_CALL(observer_,
113 OnEventCallback(HostapdMonitor::kStationDisconnected,
114 kStationMac)).Times(1);
115 ParseMessage(&data);
116 }
117
118 } // namespace apmanager
119