summaryrefslogtreecommitdiff
path: root/hostapd_monitor_unittest.cc
blob: 44f0304924ff2b2ffaa9bcaf084ac35990e1b618 (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
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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::_;

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(), "", ""),
        event_dispatcher_(MockEventDispatcher::GetInstance()) {}

  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();

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

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