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 #include <vector>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <wifi_system_test/mock_interface_tool.h>
23
24 #include "wificond/client_interface_impl.h"
25 #include "wificond/tests/mock_netlink_manager.h"
26 #include "wificond/tests/mock_netlink_utils.h"
27 #include "wificond/tests/mock_scan_utils.h"
28
29 using android::wifi_system::MockInterfaceTool;
30 using std::unique_ptr;
31 using std::vector;
32 using testing::NiceMock;
33 using testing::Return;
34 using testing::_;
35
36 namespace android {
37 namespace wificond {
38 namespace {
39
40 const uint32_t kTestWiphyIndex = 2;
41 const char kTestInterfaceName[] = "testwifi0";
42 const uint32_t kTestInterfaceIndex = 42;
43 const size_t kMacAddrLenBytes = ETH_ALEN;
44
45 class ClientInterfaceImplTest : public ::testing::Test {
46 protected:
47
SetUp()48 void SetUp() override {
49 EXPECT_CALL(*netlink_utils_,
50 SubscribeMlmeEvent(kTestInterfaceIndex, _));
51 EXPECT_CALL(*netlink_utils_,
52 GetWiphyInfo(kTestWiphyIndex, _, _, _));
53 client_interface_.reset(new ClientInterfaceImpl{
54 kTestWiphyIndex,
55 kTestInterfaceName,
56 kTestInterfaceIndex,
57 vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
58 if_tool_.get(),
59 netlink_utils_.get(),
60 scan_utils_.get()});
61 }
62
TearDown()63 void TearDown() override {
64 EXPECT_CALL(*netlink_utils_,
65 UnsubscribeMlmeEvent(kTestInterfaceIndex));
66 }
67
68 unique_ptr<NiceMock<MockInterfaceTool>> if_tool_{
69 new NiceMock<MockInterfaceTool>};
70 unique_ptr<NiceMock<MockNetlinkManager>> netlink_manager_{
71 new NiceMock<MockNetlinkManager>()};
72 unique_ptr<NiceMock<MockNetlinkUtils>> netlink_utils_{
73 new NiceMock<MockNetlinkUtils>(netlink_manager_.get())};
74 unique_ptr<NiceMock<MockScanUtils>> scan_utils_{
75 new NiceMock<MockScanUtils>(netlink_manager_.get())};
76 unique_ptr<ClientInterfaceImpl> client_interface_;
77 }; // class ClientInterfaceImplTest
78
79 } // namespace
80
TEST_F(ClientInterfaceImplTest,SetMacAddressFailsOnInvalidInput)81 TEST_F(ClientInterfaceImplTest, SetMacAddressFailsOnInvalidInput) {
82 EXPECT_FALSE(client_interface_->SetMacAddress(
83 std::vector<uint8_t>(kMacAddrLenBytes - 1)));
84 EXPECT_FALSE(client_interface_->SetMacAddress(
85 std::vector<uint8_t>(kMacAddrLenBytes + 1)));
86 }
87
TEST_F(ClientInterfaceImplTest,SetMacAddressFailsOnInterfaceDownFailure)88 TEST_F(ClientInterfaceImplTest, SetMacAddressFailsOnInterfaceDownFailure) {
89 EXPECT_CALL(*if_tool_, SetWifiUpState(false)).WillOnce(Return(false));
90 EXPECT_FALSE(
91 client_interface_->SetMacAddress(std::vector<uint8_t>(kMacAddrLenBytes)));
92 }
93
TEST_F(ClientInterfaceImplTest,SetMacAddressFailsOnAddressChangeFailure)94 TEST_F(ClientInterfaceImplTest, SetMacAddressFailsOnAddressChangeFailure) {
95 EXPECT_CALL(*if_tool_, SetWifiUpState(false)).WillOnce(Return(true));
96 EXPECT_CALL(*if_tool_, SetMacAddress(_, _)).WillOnce(Return(false));
97 EXPECT_FALSE(
98 client_interface_->SetMacAddress(std::vector<uint8_t>(kMacAddrLenBytes)));
99 }
100
TEST_F(ClientInterfaceImplTest,SetMacAddressFailsOnInterfaceUpFailure)101 TEST_F(ClientInterfaceImplTest, SetMacAddressFailsOnInterfaceUpFailure) {
102 EXPECT_CALL(*if_tool_, SetWifiUpState(false)).WillOnce(Return(true));
103 EXPECT_CALL(*if_tool_, SetMacAddress(_, _)).WillOnce(Return(true));
104 EXPECT_CALL(*if_tool_, SetWifiUpState(true)).WillOnce(Return(false));
105 EXPECT_FALSE(
106 client_interface_->SetMacAddress(std::vector<uint8_t>(kMacAddrLenBytes)));
107 }
108
TEST_F(ClientInterfaceImplTest,SetMacAddressReturnsTrueOnSuccess)109 TEST_F(ClientInterfaceImplTest, SetMacAddressReturnsTrueOnSuccess) {
110 EXPECT_CALL(*if_tool_, SetWifiUpState(false)).WillOnce(Return(true));
111 EXPECT_CALL(*if_tool_, SetMacAddress(_, _)).WillOnce(Return(true));
112 EXPECT_CALL(*if_tool_, SetWifiUpState(true)).WillOnce(Return(true));
113 EXPECT_TRUE(
114 client_interface_->SetMacAddress(std::vector<uint8_t>(kMacAddrLenBytes)));
115 }
116
TEST_F(ClientInterfaceImplTest,SetMacAddressPassesCorrectAddressToIfTool)117 TEST_F(ClientInterfaceImplTest, SetMacAddressPassesCorrectAddressToIfTool) {
118 EXPECT_CALL(*if_tool_, SetWifiUpState(false)).WillOnce(Return(true));
119 EXPECT_CALL(*if_tool_, SetMacAddress(_,
120 std::array<uint8_t, kMacAddrLenBytes>{{1, 2, 3, 4, 5, 6}}))
121 .WillOnce(Return(true));
122 EXPECT_CALL(*if_tool_, SetWifiUpState(true)).WillOnce(Return(true));
123 EXPECT_TRUE(client_interface_->SetMacAddress(
124 std::vector<uint8_t>{1, 2, 3, 4, 5, 6}));
125 }
126
127 } // namespace wificond
128 } // namespace android
129