• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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(
35     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_3 {
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     WifiIfaceUtil* iface_util_ = new WifiIfaceUtil(iface_tool_);
52 };
53 
TEST_F(WifiIfaceUtilTest,GetOrCreateRandomMacAddress)54 TEST_F(WifiIfaceUtilTest, GetOrCreateRandomMacAddress) {
55     auto mac_address = iface_util_->getOrCreateRandomMacAddress();
56     ASSERT_TRUE(isValidUnicastLocallyAssignedMacAddress(mac_address));
57 
58     // All further calls should return the same MAC address.
59     ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
60     ASSERT_EQ(mac_address, iface_util_->getOrCreateRandomMacAddress());
61 }
62 
TEST_F(WifiIfaceUtilTest,IfaceEventHandlers_SetMacAddress)63 TEST_F(WifiIfaceUtilTest, IfaceEventHandlers_SetMacAddress) {
64     std::array<uint8_t, 6> mac_address = {};
65     std::copy(std::begin(kMacAddress), std::end(kMacAddress),
66               std::begin(mac_address));
67     EXPECT_CALL(*iface_tool_, SetMacAddress(testing::_, testing::_))
68         .WillRepeatedly(testing::Return(true));
69     EXPECT_CALL(*iface_tool_, SetUpState(testing::_, testing::_))
70         .WillRepeatedly(testing::Return(true));
71 
72     // Register for iface state toggle events.
73     bool callback_invoked = false;
74     iface_util::IfaceEventHandlers event_handlers = {};
75     event_handlers.on_state_toggle_off_on =
76         [&callback_invoked](const std::string& /* iface_name */) {
77             callback_invoked = true;
78         };
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_3
94 }  // namespace wifi
95 }  // namespace hardware
96 }  // namespace android
97