mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Fixs Mac address containing non-hex digit.
PiperOrigin-RevId: 536562911
This commit is contained in:
committed by
Copybara-Service
parent
72f74f3698
commit
6060bc1856
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "internal/platform/bluetooth_utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
|
||||
@@ -39,17 +41,15 @@ std::string BluetoothUtils::ToString(const ByteArray& bluetooth_mac_address) {
|
||||
ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) {
|
||||
std::string bt_mac_address(bluetooth_mac_address);
|
||||
|
||||
// Remove the colon delimiters.
|
||||
bt_mac_address.erase(
|
||||
std::remove(bt_mac_address.begin(), bt_mac_address.end(), ':'),
|
||||
bt_mac_address.end());
|
||||
|
||||
// If the bluetooth mac address is invalid (wrong size), return a null byte
|
||||
// array.
|
||||
if (bt_mac_address.length() != kBluetoothMacAddressLength * 2) {
|
||||
if (bt_mac_address.length() != kBluetoothMacAddressLength * 2 ||
|
||||
!std::all_of(bt_mac_address.begin(), bt_mac_address.end(),
|
||||
absl::ascii_isxdigit)) {
|
||||
return ByteArray();
|
||||
}
|
||||
// Convert to bytes. If MAC Address bytes are unset, return a null byte array.
|
||||
auto bt_mac_address_string(absl::HexStringToBytes(bt_mac_address));
|
||||
auto bt_mac_address_bytes =
|
||||
ByteArray(bt_mac_address_string.data(), bt_mac_address_string.size());
|
||||
|
||||
@@ -82,6 +82,10 @@ TEST(BluetoothUtilsTest, InvalidStringReturnsEmptyByteArray) {
|
||||
std::string bad_bt_mac_address_4 = "BLUETOOTHCHIP";
|
||||
bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_4);
|
||||
EXPECT_TRUE(bytes_result.Empty());
|
||||
|
||||
std::string bad_bt_mac_address_5 = "G1:F2:F3:F4:F5:F6";
|
||||
bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_5);
|
||||
EXPECT_TRUE(bytes_result.Empty());
|
||||
}
|
||||
|
||||
TEST(BluetoothUtilsTest, FromNumber) {
|
||||
|
||||
Reference in New Issue
Block a user