Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "apmanager/hostapd_monitor.h" |
| 6 | |
| 7 | #include <base/bind.h> |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include <shill/net/io_handler.h> |
| 11 | |
| 12 | #include "apmanager/mock_event_dispatcher.h" |
| 13 | |
| 14 | using base::Bind; |
| 15 | using base::Unretained; |
| 16 | using ::testing::_; |
| 17 | |
| 18 | namespace { |
| 19 | const char kStationMac[] = "00:11:22:33:44:55"; |
| 20 | const char kHostapdEventStationConnected[] = |
| 21 | "<2>AP-STA-CONNECTED 00:11:22:33:44:55"; |
| 22 | const char kHostapdEventStationDisconnected[] = |
| 23 | "<2>AP-STA-DISCONNECTED 00:11:22:33:44:55"; |
| 24 | } // namespace |
| 25 | |
| 26 | namespace apmanager { |
| 27 | |
| 28 | class HostapdEventCallbackObserver { |
| 29 | public: |
| 30 | HostapdEventCallbackObserver() |
| 31 | : event_callback_( |
| 32 | Bind(&HostapdEventCallbackObserver::OnEventCallback, |
| 33 | Unretained(this))) {} |
| 34 | virtual ~HostapdEventCallbackObserver() {} |
| 35 | |
| 36 | MOCK_METHOD2(OnEventCallback, |
| 37 | void(HostapdMonitor::Event event, const std::string& data)); |
| 38 | |
| 39 | const HostapdMonitor::EventCallback event_callback() { |
| 40 | return event_callback_; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | HostapdMonitor::EventCallback event_callback_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(HostapdEventCallbackObserver); |
| 47 | }; |
| 48 | |
| 49 | class HostapdMonitorTest : public testing::Test { |
| 50 | public: |
| 51 | HostapdMonitorTest() |
| 52 | : hostapd_monitor_(observer_.event_callback(), "", ""), |
| 53 | event_dispatcher_(MockEventDispatcher::GetInstance()) {} |
| 54 | |
| 55 | virtual void SetUp() { |
| 56 | hostapd_monitor_.event_dispatcher_ = event_dispatcher_; |
| 57 | } |
| 58 | |
| 59 | void Start() { |
| 60 | hostapd_monitor_.Start(); |
| 61 | } |
| 62 | |
| 63 | void ParseMessage(shill::InputData* data) { |
| 64 | hostapd_monitor_.ParseMessage(data); |
| 65 | } |
| 66 | |
| 67 | protected: |
| 68 | HostapdEventCallbackObserver observer_; |
| 69 | HostapdMonitor hostapd_monitor_; |
| 70 | MockEventDispatcher* event_dispatcher_; |
| 71 | }; |
| 72 | |
| 73 | TEST_F(HostapdMonitorTest, Start) { |
| 74 | EXPECT_CALL(*event_dispatcher_, PostTask(_)).Times(1); |
| 75 | Start(); |
| 76 | |
| 77 | // Monitor already started, nothing to be done. |
| 78 | EXPECT_CALL(*event_dispatcher_, PostTask(_)).Times(0); |
| 79 | Start(); |
| 80 | } |
| 81 | |
| 82 | TEST_F(HostapdMonitorTest, StationConnected) { |
| 83 | shill::InputData data; |
| 84 | data.buf = reinterpret_cast<unsigned char*>( |
| 85 | const_cast<char*>(kHostapdEventStationConnected)); |
| 86 | data.len = strlen(kHostapdEventStationConnected); |
| 87 | EXPECT_CALL(observer_, |
| 88 | OnEventCallback(HostapdMonitor::kStationConnected, |
| 89 | kStationMac)).Times(1); |
| 90 | ParseMessage(&data); |
| 91 | } |
| 92 | |
| 93 | TEST_F(HostapdMonitorTest, StationDisconnected) { |
| 94 | shill::InputData data; |
| 95 | data.buf = reinterpret_cast<unsigned char*>( |
| 96 | const_cast<char*>(kHostapdEventStationDisconnected)); |
| 97 | data.len = strlen(kHostapdEventStationDisconnected); |
| 98 | EXPECT_CALL(observer_, |
| 99 | OnEventCallback(HostapdMonitor::kStationDisconnected, |
| 100 | kStationMac)).Times(1); |
| 101 | ParseMessage(&data); |
| 102 | } |
| 103 | |
| 104 | } // namespace apmanager |