2 * Copyright (C) 2016, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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"
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;
40 using testing::Invoke;
41 using testing::NiceMock;
42 using testing::Return;
43 using testing::Sequence;
46 using namespace std::placeholders;
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};
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,
68 vector<InterfaceInfo>* response) {
69 for (auto interface : mock_response) {
70 response->emplace_back(interface);
72 return mock_return_value;
75 class ServerTest : public ::testing::Test {
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)));
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>;
91 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
92 new NiceMock<MockNetlinkManager>()};
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 = {
102 std::string(kFakeInterfaceName),
104 kFakeInterfaceMacAddress,
105 kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress))),
108 kFakeInterfaceIndex1,
111 kFakeInterfaceMacAddress1,
112 kFakeInterfaceMacAddress1 + sizeof(kFakeInterfaceMacAddress1)))
115 Server server_{unique_ptr<InterfaceTool>(if_tool_),
116 unique_ptr<SupplicantManager>(supplicant_manager_),
117 unique_ptr<HostapdManager>(hostapd_manager_),
118 netlink_utils_.get(),
120 }; // class ServerTest
124 TEST_F(ServerTest, CanSetUpApInterface) {
125 sp<IApInterface> ap_if;
126 EXPECT_CALL(*netlink_utils_, SubscribeRegDomainChange(_, _));
128 EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
129 EXPECT_NE(nullptr, ap_if.get());
132 TEST_F(ServerTest, DoesNotSupportMultipleInterfaces) {
133 sp<IApInterface> ap_if;
135 EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
136 EXPECT_NE(nullptr, ap_if.get());
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());
145 TEST_F(ServerTest, CanDestroyInterfaces) {
146 sp<IApInterface> ap_if;
148 EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
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());
157 } // namespace wificond
158 } // namespace android