summaryrefslogtreecommitdiff
path: root/hostapd_monitor_unittest.cc
blob: a5ac3733dd15d41e144536e84fbcffec9b7d111f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "apmanager/hostapd_monitor.h"

#include <base/bind.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <shill/net/io_handler.h>

#include "apmanager/mock_event_dispatcher.h"

using base::Bind;
using base::Unretained;
using ::testing::_;
using ::testing::Mock;

namespace {
  const char kStationMac[] = "00:11:22:33:44:55";
  const char kHostapdEventStationConnected[] =
      "<2>AP-STA-CONNECTED 00:11:22:33:44:55";
  const char kHostapdEventStationDisconnected[] =
      "<2>AP-STA-DISCONNECTED 00:11:22:33:44:55";
}  // namespace

namespace apmanager {

class HostapdEventCallbackObserver {
 public:
  HostapdEventCallbackObserver()
      : event_callback_(
          Bind(&HostapdEventCallbackObserver::OnEventCallback,
               Unretained(this))) {}
  virtual ~HostapdEventCallbackObserver() {}

  MOCK_METHOD2(OnEventCallback,
               void(HostapdMonitor::Event event, const std::string& data));

  const HostapdMonitor::EventCallback event_callback() {
    return event_callback_;
  }

 private:
  HostapdMonitor::EventCallback event_callback_;

  DISALLOW_COPY_AND_ASSIGN(HostapdEventCallbackObserver);
};

class HostapdMonitorTest : public testing::Test {
 public:
  HostapdMonitorTest()
      : hostapd_monitor_(observer_.event_callback(), "", "") {}

  virtual void SetUp() {
    hostapd_monitor_.event_dispatcher_ = &event_dispatcher_;
  }

  void Start() {
    hostapd_monitor_.Start();
  }

  void ParseMessage(shill::InputData* data) {
    hostapd_monitor_.ParseMessage(data);
  }

 protected:
  HostapdEventCallbackObserver observer_;
  HostapdMonitor hostapd_monitor_;
  MockEventDispatcher event_dispatcher_;
};

TEST_F(HostapdMonitorTest, Start) {
  EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(1);
  Start();
  Mock::VerifyAndClearExpectations(&event_dispatcher_);

  // Monitor already started, nothing to be done.
  EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(0);
  Start();
  Mock::VerifyAndClearExpectations(&event_dispatcher_);
}

TEST_F(HostapdMonitorTest, StationConnected) {
  shill::InputData data;
  data.buf = reinterpret_cast<unsigned char*>(
      const_cast<char*>(kHostapdEventStationConnected));
  data.len = strlen(kHostapdEventStationConnected);
  EXPECT_CALL(observer_,
              OnEventCallback(HostapdMonitor::kStationConnected,
                              kStationMac)).Times(1);
  ParseMessage(&data);
}

TEST_F(HostapdMonitorTest, StationDisconnected) {
  shill::InputData data;
  data.buf = reinterpret_cast<unsigned char*>(
      const_cast<char*>(kHostapdEventStationDisconnected));
  data.len = strlen(kHostapdEventStationDisconnected);
  EXPECT_CALL(observer_,
              OnEventCallback(HostapdMonitor::kStationDisconnected,
                              kStationMac)).Times(1);
  ParseMessage(&data);
}

}  // namespace apmanager