OSDN Git Service

Support scan plan attribute for scheduled scan
[android-x86/system-connectivity-wificond.git] / tests / server_unittest.cpp
1 /*
2  * Copyright (C) 2016, 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  */
16
17 #include <memory>
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <wifi_system_test/mock_hostapd_manager.h>
22 #include <wifi_system_test/mock_interface_tool.h>
23 #include <wifi_system_test/mock_supplicant_manager.h>
24
25 #include "android/net/wifi/IApInterface.h"
26 #include "wificond/tests/mock_netlink_manager.h"
27 #include "wificond/tests/mock_netlink_utils.h"
28 #include "wificond/tests/mock_scan_utils.h"
29 #include "wificond/server.h"
30
31 using android::net::wifi::IApInterface;
32 using android::wifi_system::HostapdManager;
33 using android::wifi_system::InterfaceTool;
34 using android::wifi_system::MockHostapdManager;
35 using android::wifi_system::MockInterfaceTool;
36 using android::wifi_system::MockSupplicantManager;
37 using android::wifi_system::SupplicantManager;
38 using std::unique_ptr;
39 using std::vector;
40 using testing::Invoke;
41 using testing::NiceMock;
42 using testing::Return;
43 using testing::Sequence;
44 using testing::_;
45
46 using namespace std::placeholders;
47
48 namespace android {
49 namespace wificond {
50 namespace {
51
52 const char kFakeInterfaceName[] = "testif0";
53 const uint32_t kFakeInterfaceIndex = 34;
54 const uint32_t kFakeInterfaceIndex1 = 36;
55 const uint8_t kFakeInterfaceMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
56 const uint8_t kFakeInterfaceMacAddress1[] = {0x05, 0x04, 0xef, 0x27, 0x12, 0xff};
57
58 // This is a helper function to mock the behavior of
59 // NetlinkUtils::GetInterfaces().
60 // |wiphy_index| is mapped to first parameters of GetInterfaces().
61 // |response| is mapped to second parameters of GetInterfaces().
62 // |mock_response| and |mock_return_value| are additional parameters used
63 // for specifying expected results,
64 bool MockGetInterfacesResponse(
65     const vector<InterfaceInfo>& mock_response,
66     bool mock_return_value,
67     uint32_t wiphy_index,
68     vector<InterfaceInfo>* response) {
69   for (auto interface : mock_response) {
70     response->emplace_back(interface);
71   }
72   return mock_return_value;
73 }
74
75 class ServerTest : public ::testing::Test {
76  protected:
77   void SetUp() override {
78     ON_CALL(*if_tool_, SetWifiUpState(_)).WillByDefault(Return(true));
79     ON_CALL(*netlink_utils_, GetWiphyIndex(_)).WillByDefault(Return(true));
80     ON_CALL(*netlink_utils_, GetInterfaces(_, _))
81       .WillByDefault(Invoke(bind(
82           MockGetInterfacesResponse, mock_interfaces, true, _1, _2)));
83   }
84
85   NiceMock<MockInterfaceTool>* if_tool_ = new NiceMock<MockInterfaceTool>;
86   NiceMock<MockSupplicantManager>* supplicant_manager_ =
87       new NiceMock<MockSupplicantManager>;
88   NiceMock<MockHostapdManager>* hostapd_manager_ =
89       new NiceMock<MockHostapdManager>;
90
91   unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
92       new NiceMock<MockNetlinkManager>()};
93
94   unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
95       new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
96   unique_ptr<NiceMock<MockScanUtils>> scan_utils_{
97       new NiceMock<MockScanUtils>(netlink_manager_.get())};
98   const vector<InterfaceInfo> mock_interfaces = {
99       // Client interface
100       InterfaceInfo(
101           kFakeInterfaceIndex,
102           std::string(kFakeInterfaceName),
103           vector<uint8_t>(
104               kFakeInterfaceMacAddress,
105               kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress))),
106       // p2p interface
107       InterfaceInfo(
108           kFakeInterfaceIndex1,
109           "p2p0",
110            vector<uint8_t>(
111                kFakeInterfaceMacAddress1,
112                kFakeInterfaceMacAddress1 + sizeof(kFakeInterfaceMacAddress1)))
113   };
114
115   Server server_{unique_ptr<InterfaceTool>(if_tool_),
116                  unique_ptr<SupplicantManager>(supplicant_manager_),
117                  unique_ptr<HostapdManager>(hostapd_manager_),
118                  netlink_utils_.get(),
119                  scan_utils_.get()};
120 };  // class ServerTest
121
122 }  // namespace
123
124 TEST_F(ServerTest, CanSetUpApInterface) {
125   sp<IApInterface> ap_if;
126   EXPECT_CALL(*netlink_utils_, SubscribeRegDomainChange(_, _));
127
128   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
129   EXPECT_NE(nullptr, ap_if.get());
130 }
131
132 TEST_F(ServerTest, DoesNotSupportMultipleInterfaces) {
133   sp<IApInterface> ap_if;
134
135   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
136   EXPECT_NE(nullptr, ap_if.get());
137
138   sp<IApInterface> second_ap_if;
139   // We won't throw on a second interface request.
140   EXPECT_TRUE(server_.createApInterface(&second_ap_if).isOk());
141   // But this time we won't get an interface back.
142   EXPECT_EQ(nullptr, second_ap_if.get());
143 }
144
145 TEST_F(ServerTest, CanDestroyInterfaces) {
146   sp<IApInterface> ap_if;
147
148   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
149
150   // When we tear down the interface, we expect the driver to be unloaded.
151   EXPECT_CALL(*netlink_utils_, UnsubscribeRegDomainChange(_));
152   EXPECT_TRUE(server_.tearDownInterfaces().isOk());
153   // After a teardown, we should be able to create another interface.
154   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
155 }
156
157 }  // namespace wificond
158 }  // namespace android