Chris Weir | 5031a04 | 2024-10-21 13:31:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 <cstring> |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | #include "../hostapd.cpp" |
| 21 | |
| 22 | namespace aidl::android::hardware::wifi::hostapd { |
| 23 | |
| 24 | /** |
| 25 | * Null hostapd_data* and null mac address (u8*) |
| 26 | * There's an || check on these that should return nullopt |
| 27 | */ |
| 28 | TEST(getStaInfoByMacAddrTest, NullArguments) { |
| 29 | EXPECT_EQ(std::nullopt, getStaInfoByMacAddr(nullptr, nullptr)); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * We pass valid arguments to get past the nullptr check, but hostapd_data->sta_list is nullptr. |
| 35 | * Don't loop through the sta_info* list, just return nullopt. |
| 36 | */ |
| 37 | TEST(getStaInfoByMacAddrTest, NullStaList) { |
| 38 | struct hostapd_data iface_hapd = {}; |
| 39 | u8 mac_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xD0, 0x0D}; |
| 40 | EXPECT_EQ(std::nullopt, getStaInfoByMacAddr(&iface_hapd, mac_addr)); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Mac doesn't match, and we hit the end of the sta_info list. |
| 45 | * Don't run over the end of the list and return nullopt. |
| 46 | */ |
| 47 | TEST(getStaInfoByMacAddrTest, NoMatchingMac) { |
| 48 | struct hostapd_data iface_hapd = {}; |
| 49 | struct sta_info sta0 = {}; |
| 50 | struct sta_info sta1 = {}; |
| 51 | struct sta_info sta2 = {}; |
| 52 | iface_hapd.sta_list = &sta0; |
| 53 | sta0.next = &sta1; |
| 54 | sta1.next = &sta2; |
| 55 | u8 mac_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xD0, 0x0D}; |
| 56 | EXPECT_EQ(std::nullopt, getStaInfoByMacAddr(&iface_hapd, mac_addr)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * There is a matching address and we return it. |
| 61 | */ |
| 62 | TEST(getStaInfoByMacAddr, MatchingMac) { |
| 63 | struct hostapd_data iface_hapd = {}; |
| 64 | struct sta_info sta0 = {}; |
| 65 | struct sta_info sta1 = {}; |
| 66 | struct sta_info sta2 = {}; |
| 67 | iface_hapd.sta_list = &sta0; |
| 68 | sta0.next = &sta1; |
| 69 | sta1.next = &sta2; |
| 70 | u8 sta0_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xD0, 0x0C}; // off by 1 bit |
| 71 | std::memcpy(sta0.addr, sta0_addr, ETH_ALEN); |
| 72 | u8 sta1_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xD0, 0x0D}; |
| 73 | std::memcpy(sta1.addr, sta1_addr, ETH_ALEN); |
| 74 | u8 mac_addr[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xD0, 0x0D}; |
| 75 | auto sta_ptr_optional = getStaInfoByMacAddr(&iface_hapd, mac_addr); |
| 76 | EXPECT_TRUE(sta_ptr_optional.has_value()); |
| 77 | EXPECT_EQ(0, std::memcmp(sta_ptr_optional.value()->addr, sta1_addr, ETH_ALEN)); |
| 78 | } |
| 79 | |
| 80 | } // namespace aidl::android::hardware::wifi::hostapd |