Peter Qiu | 326b6cf | 2015-09-02 11:11:42 -0700 | [diff] [blame] | 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 | // |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 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::_; |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 29 | using ::testing::Mock; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 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: |
| 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 | |
| 62 | class HostapdMonitorTest : public testing::Test { |
| 63 | public: |
| 64 | HostapdMonitorTest() |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 65 | : hostapd_monitor_(observer_.event_callback(), "", "") {} |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 66 | |
| 67 | virtual void SetUp() { |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 68 | hostapd_monitor_.event_dispatcher_ = &event_dispatcher_; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 69 | } |
| 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 Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 82 | MockEventDispatcher event_dispatcher_; |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | TEST_F(HostapdMonitorTest, Start) { |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 86 | EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(1); |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 87 | Start(); |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 88 | Mock::VerifyAndClearExpectations(&event_dispatcher_); |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 89 | |
| 90 | // Monitor already started, nothing to be done. |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 91 | EXPECT_CALL(event_dispatcher_, PostTask(_)).Times(0); |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 92 | Start(); |
Peter Qiu | 6789bcb | 2015-10-26 15:45:13 -0700 | [diff] [blame] | 93 | Mock::VerifyAndClearExpectations(&event_dispatcher_); |
Peter Qiu | fda548c | 2015-01-13 14:39:19 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 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 | |
| 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 |