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(const std::array<uint8_t, 6>& mac_address) {
36 uint8_t first_byte = mac_address[0];
37 return (first_byte & 0x3) == kValidUnicastLocallyAssignedMacAddressMask;
38 }
39 } // namespace
40
41 namespace android {
42 namespace hardware {
43 namespace wifi {
44 namespace V1_6 {
45 namespace implementation {
46 namespace iface_util {
47 class WifiIfaceUtilTest : public Test {
48 protected:
49 std::shared_ptr<NiceMock<wifi_system::MockInterfaceTool>> iface_tool_{
50 new NiceMock<wifi_system::MockInterfaceTool>};
51 legacy_hal::wifi_hal_fn fake_func_table_;
52 std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
53 new NiceMock<legacy_hal::MockWifiLegacyHal>(iface_tool_, fake_func_table_, true)};
54 WifiIfaceUtil* iface_util_ = new WifiIfaceUtil(iface_tool_, legacy_hal_);
55 };
56
TEST_F(WifiIfaceUtilTest,GetOrCreateRandomMacAddress)57 TEST_F(WifiIfaceUtilTest, GetOrCreateRandomMacAddress) {
58 auto mac_address = iface_util_->getOrCreateRandomMacAddress();
59 ASSERT_TRUE(isValidUnicastLocallyAssignedMacAddress(mac_address));
60
61 // All further calls should return the same MAC address.
62 ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
63 ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
64 }
65
TEST_F(WifiIfaceUtilTest,IfaceEventHandlers_SetMacAddress)66 TEST_F(WifiIfaceUtilTest, IfaceEventHandlers_SetMacAddress) {
67 std::array<uint8_t, 6> mac_address = {};
68 std::copy(std::begin(kMacAddress), std::end(kMacAddress), std::begin(mac_address));
69 EXPECT_CALL(*iface_tool_, SetMacAddress(testing::_, testing::_))
70 .WillRepeatedly(testing::Return(true));
71 EXPECT_CALL(*iface_tool_, SetUpState(testing::_, testing::_))
72 .WillRepeatedly(testing::Return(true));
73
74 // Register for iface state toggle events.
75 bool callback_invoked = false;
76 iface_util::IfaceEventHandlers event_handlers = {};
77 event_handlers.on_state_toggle_off_on =
78 [&callback_invoked](const std::string& /* iface_name */) { callback_invoked = true; };
79 iface_util_->registerIfaceEventHandlers(kIfaceName, event_handlers);
80 // Invoke setMacAddress and ensure that the cb is invoked.
81 ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address));
82 ASSERT_TRUE(callback_invoked);
83
84 // Unregister for iface state toggle events.
85 callback_invoked = false;
86 iface_util_->unregisterIfaceEventHandlers(kIfaceName);
87 // Invoke setMacAddress and ensure that the cb is not invoked.
88 ASSERT_TRUE(iface_util_->setMacAddress(kIfaceName, mac_address));
89 ASSERT_FALSE(callback_invoked);
90 }
91 } // namespace iface_util
92 } // namespace implementation
93 } // namespace V1_6
94 } // namespace wifi
95 } // namespace hardware
96 } // namespace android
97