blob: a5ac3733dd15d41e144536e84fbcffec9b7d111f [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::_;
Peter Qiu6789bcb2015-10-26 15:45:13 -070029using ::testing::Mock;
Peter Qiufda548c2015-01-13 14:39:19 -080030
31namespace {
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
39namespace apmanager {
40
41class HostapdEventCallbackObserver {
42 public:
43 HostapdEventCallbackObserver()
44 : event_callback_(
45 Bind(&HostapdEventCallbackObserver::OnEventCallback,
46 Unretained(this))) {}
47 virtual ~HostapdEventCallbackObserver() {}
48
49 MOCK_METHOD2(OnEventCallback,
50 void(HostapdMonitor::Event event, const std::string& data));
51
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
62class HostapdMonitorTest : public testing::Test {
63 public:
64 HostapdMonitorTest()
Peter Qiu6789bcb2015-10-26 15:45:13 -070065 : hostapd_monitor_(observer_.event_callback(), "", "") {}
Peter Qiufda548c2015-01-13 14:39:19 -080066
67 virtual void SetUp() {
Peter Qiu6789bcb2015-10-26 15:45:13 -070068 hostapd_monitor_.event_dispatcher_ = &event_dispatcher_;
Peter Qiufda548c2015-01-13 14:39:19 -080069 }
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_;
Peter Qiu6789bcb2015-10-26 15:45:13 -070082 MockEventDispatcher event_dispatcher_;
Peter Qiufda548c2015-01-13 14:39:19 -080083};
84
85TEST_F(HostapdMonitorTest, Start) {
Peter Qiu6789bcb2015-10-26 15:45:13 -070086 EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(1);
Peter Qiufda548c2015-01-13 14:39:19 -080087 Start();
Peter Qiu6789bcb2015-10-26 15:45:13 -070088 Mock::VerifyAndClearExpectations(&event_dispatcher_);
Peter Qiufda548c2015-01-13 14:39:19 -080089
90 // Monitor already started, nothing to be done.
Peter Qiu6789bcb2015-10-26 15:45:13 -070091 EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(0);
Peter Qiufda548c2015-01-13 14:39:19 -080092 Start();
Peter Qiu6789bcb2015-10-26 15:45:13 -070093 Mock::VerifyAndClearExpectations(&event_dispatcher_);
Peter Qiufda548c2015-01-13 14:39:19 -080094}
95
96TEST_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
107TEST_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