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