1 /*
2  * Copyright (C) 2019, 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 <android-base/logging.h>
18 #include <android-base/macros.h>
19 #include <gmock/gmock.h>
20 
21 #undef NAN
22 #include "wifi_iface_util.h"
23 
24 #include "mock_interface_tool.h"
25 #include "mock_wifi_legacy_hal.h"
26 
27 using testing::NiceMock;
28 using testing::Test;
29 
30 namespace {
31 constexpr uint8_t kValidUnicastLocallyAssignedMacAddressMask = 0x02;
32 constexpr uint8_t kMacAddress[] = {0x02, 0x12, 0x45, 0x56, 0xab, 0xcc};
33 constexpr char kIfaceName[] = "test-wlan0";
34 
isValidUnicastLocallyAssignedMacAddress(const std::array<uint8_t,6> & mac_address)35 bool isValidUnicastLocallyAssignedMacAddress(
36     const std::array<uint8_t, 6>& mac_address) {
37     uint8_t first_byte = mac_address[0];
38     return (first_byte & 0x3) == kValidUnicastLocallyAssignedMacAddressMask;
39 }
40 }  // namespace
41 
42 namespace android {
43 namespace hardware {
44 namespace wifi {
45 namespace V1_5 {
46 namespace implementation {
47 namespace iface_util {
48 class WifiIfaceUtilTest : public Test {
49    protected:
50     std::shared_ptr<NiceMock<wifi_system::MockInterfaceTool>> iface_tool_{
51         new NiceMock<wifi_system::MockInterfaceTool>};
52     legacy_hal::wifi_hal_fn fake_func_table_;
53     std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
54         new NiceMock<legacy_hal::MockWifiLegacyHal>(iface_tool_,
55                                                     fake_func_table_, true)};
56     WifiIfaceUtil* iface_util_ = new WifiIfaceUtil(iface_tool_, legacy_hal_);
57 };
58 
TEST_F(WifiIfaceUtilTest,GetOrCreateRandomMacAddress)59 TEST_F(WifiIfaceUtilTest, GetOrCreateRandomMacAddress) {
60     auto mac_address = iface_util_->getOrCreateRandomMacAddress();
61     ASSERT_TRUE(isValidUnicastLocallyAssignedMacAddress(mac_address));
62 
63     // All further calls should return the same MAC address.
64     ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
65     ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
66 }
67 
TEST_F(WifiIfaceUtilTest,IfaceEventHandlers_SetMacAddress)68 TEST_F(WifiIfaceUtilTest, IfaceEventHandlers_SetMacAddress) {
69     std::array<uint8_t, 6> mac_address = {};
70     std::copy(std::begin(kMacAddress), std::end(kMacAddress),
71               std::begin(mac_address));
72     EXPECT_CALL(*iface_tool_, SetMacAddress(testing::_, testing::_))
73         .WillRepeatedly(testing::Return(true));
74     EXPECT_CALL(*iface_tool_, SetUpState(testing::_, testing::_))
75         .WillRepeatedly(testing::Return(true));
76 
77     // Register for iface state toggle events.
78     bool callback_invoked = false;
79     iface_util::IfaceEventHandlers event_handlers = {};
80     event_handlers.on_state_toggle_off_on =
81         [&callback_invoked](const std::string& /* iface_name */) {
82             callback_invoked = true;
83         };
84     iface_util_->registerIfaceEventHandlers(kIfaceName, event_handlers);
85     // Invoke setMacAddress and ensure that the cb is invoked.
86     ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address));
87     ASSERT_TRUE(callback_invoked);
88 
89     // Unregister for iface state toggle events.
90     callback_invoked = false;
91     iface_util_->unregisterIfaceEventHandlers(kIfaceName);
92     // Invoke setMacAddress and ensure that the cb is not invoked.
93     ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address));
94     ASSERT_FALSE(callback_invoked);
95 }
96 }  // namespace iface_util
97 }  // namespace implementation
98 }  // namespace V1_5
99 }  // namespace wifi
100 }  // namespace hardware
101 }  // namespace android
102