From f2f95ef48ccd5f79312bd6d287c0bfee42d973f2 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 9 Jul 2025 15:30:52 -0700 Subject: [PATCH] Add more test cases to windows utils PiperOrigin-RevId: 781235840 --- .../platform/implementation/windows/utils.cc | 4 + .../implementation/windows/utils_test.cc | 128 ++++++++++++++++-- 2 files changed, 117 insertions(+), 15 deletions(-) diff --git a/internal/platform/implementation/windows/utils.cc b/internal/platform/implementation/windows/utils.cc index 23ea29cd..a5b287fe 100644 --- a/internal/platform/implementation/windows/utils.cc +++ b/internal/platform/implementation/windows/utils.cc @@ -66,6 +66,10 @@ uint64_t mac_address_string_to_uint64(absl::string_view mac_address) { std::string ipaddr_4bytes_to_dotdecimal_string( absl::string_view ipaddr_4bytes) { + if (ipaddr_4bytes.size() != 4) { + return {}; + } + in_addr address; address.S_un.S_un_b.s_b1 = ipaddr_4bytes[0]; address.S_un.S_un_b.s_b2 = ipaddr_4bytes[1]; diff --git a/internal/platform/implementation/windows/utils_test.cc b/internal/platform/implementation/windows/utils_test.cc index 3dc0cd16..53de19af 100644 --- a/internal/platform/implementation/windows/utils_test.cc +++ b/internal/platform/implementation/windows/utils_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2020-2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,49 +16,68 @@ #include +#include +#include +#include #include +#include #include "gtest/gtest.h" +#include "absl/strings/string_view.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/uuid.h" +#include "winrt/Windows.Foundation.h" #include "winrt/base.h" namespace nearby { namespace windows { +namespace { +using ::winrt::Windows::Foundation::IInspectable; +using ::winrt::Windows::Foundation::PropertyValue; + +constexpr absl::string_view kIpDotdecimal{"192.168.1.37"}; +constexpr char kIp4Bytes[] = {(char)192, (char)168, (char)1, (char)37}; + +} // namespace TEST(UtilsTests, MacAddressToString) { - // Arrange const uint64_t input = 0x000034363bc70c71; std::string expected = "34:36:3B:C7:0C:71"; - - // Act std::string result = uint64_to_mac_address_string(input); - - // Assert EXPECT_EQ(result, expected); } +TEST(UtilsTests, MacAddressToStringInvalid) { + const uint64_t input = 0x1234567890ABCDEF; // Invalid MAC address + std::string result = uint64_to_mac_address_string(input); + EXPECT_TRUE(result.empty()); +} + TEST(UtilsTests, StringToMacAddress) { - // Arrange std::string input = "34:36:3B:C7:8C:71"; const uint64_t expected = 0x000034363bc78c71; - - // Act uint64_t result = mac_address_string_to_uint64(input); - - // Assert EXPECT_EQ(result, expected); } -constexpr absl::string_view kIpDotdecimal{"192.168.1.37"}; - -constexpr char kIp4Bytes[] = {(char)192, (char)168, (char)1, (char)37}; +TEST(UtilsTests, StringToMacAddressInvalid) { + std::string input = "34:36:3B:C7:8C:7Z"; // Invalid hex character + uint64_t result = mac_address_string_to_uint64(input); + EXPECT_EQ(result, 0); +} TEST(UtilsTests, Ip4BytesToDotdecimal) { std::string result = - ipaddr_4bytes_to_dotdecimal_string(absl::string_view(kIp4Bytes)); + ipaddr_4bytes_to_dotdecimal_string(absl::string_view(kIp4Bytes, 4)); EXPECT_EQ(result, kIpDotdecimal); } +TEST(UtilsTests, Ip4BytesToDotdecimalInvalid) { + std::string result = ipaddr_4bytes_to_dotdecimal_string(absl::string_view()); + EXPECT_TRUE(result.empty()); +} + TEST(UtilsTests, IpDotdecimalTo4Bytes) { std::string result = ipaddr_dotdecimal_to_4bytes_string(std::string(kIpDotdecimal)); @@ -66,6 +85,38 @@ TEST(UtilsTests, IpDotdecimalTo4Bytes) { EXPECT_EQ(result, std::string(kIp4Bytes, 4)); } +TEST(UtilsTests, IpDotdecimalTo4BytesEmpty) { + std::string result = ipaddr_dotdecimal_to_4bytes_string(""); + EXPECT_TRUE(result.empty()); +} + +TEST(UtilsTests, IpDotdecimalTo4BytesInvalid) { + std::string result = ipaddr_dotdecimal_to_4bytes_string("192.168.1.256"); + // inet_addr returns INADDR_NONE for invalid address. + char expected[] = {(char)255, (char)255, (char)255, (char)255}; + EXPECT_EQ(result, std::string(expected, 4)); +} + +TEST(UtilsTests, Sha256) { + std::string input = "Hello World"; + // sha256("Hello World") + const char expected_sha256[] = { + (char)0xa5, (char)0x91, (char)0xa6, (char)0xd4, (char)0x0b, (char)0xf4, + (char)0x20, (char)0x40, (char)0x4a, (char)0x01, (char)0x17, (char)0x33, + (char)0xcf, (char)0xb7, (char)0xb1, (char)0x90, (char)0xd6, (char)0x2c, + (char)0x65, (char)0xbf, (char)0x0b, (char)0xcd, (char)0xa3, (char)0x2b, + (char)0x57, (char)0xb2, (char)0x77, (char)0xd9, (char)0xad, (char)0x9f, + (char)0x14, (char)0x6e}; + + ByteArray result = Sha256(input, 32); + EXPECT_EQ(result.size(), 32); + EXPECT_EQ(memcmp(result.data(), expected_sha256, 32), 0); + + result = Sha256(input, 16); + EXPECT_EQ(result.size(), 16); + EXPECT_EQ(memcmp(result.data(), expected_sha256, 16), 0); +} + TEST(UtilsTests, ConvertBetweenWinrtGuidAndNearbyUuidSuccessfully) { Uuid uuid(0x123e4567e89b12d3, 0xa456426614174000); winrt::guid guid("{123e4567-e89b-12d3-a456-426614174000}"); @@ -82,5 +133,52 @@ TEST(UtilsTests, CompareWinrtGuidAndNearbyUuidSuccessfully) { EXPECT_NE(uuid, winrt_guid_to_nearby_uuid(guid)); } +TEST(UtilsTests, InspectableReader_ReadBoolean) { + EXPECT_TRUE( + InspectableReader::ReadBoolean(PropertyValue::CreateBoolean(true))); + EXPECT_FALSE( + InspectableReader::ReadBoolean(PropertyValue::CreateBoolean(false))); + EXPECT_FALSE(InspectableReader::ReadBoolean(nullptr)); + EXPECT_THROW(InspectableReader::ReadBoolean(PropertyValue::CreateString(L"")), + std::invalid_argument); +} + +TEST(UtilsTests, InspectableReader_ReadUint16) { + EXPECT_EQ(InspectableReader::ReadUint16(PropertyValue::CreateUInt16(123)), + 123); + EXPECT_EQ(InspectableReader::ReadUint16(nullptr), 0); + EXPECT_THROW(InspectableReader::ReadUint16(PropertyValue::CreateString(L"")), + std::invalid_argument); +} + +TEST(UtilsTests, InspectableReader_ReadUint32) { + EXPECT_EQ(InspectableReader::ReadUint32(PropertyValue::CreateUInt32(456)), + 456); + EXPECT_EQ(InspectableReader::ReadUint32(nullptr), 0); + EXPECT_THROW(InspectableReader::ReadUint32(PropertyValue::CreateString(L"")), + std::invalid_argument); +} + +TEST(UtilsTests, InspectableReader_ReadString) { + EXPECT_EQ(InspectableReader::ReadString( + PropertyValue::CreateString(L"test string")), + "test string"); + EXPECT_EQ(InspectableReader::ReadString(nullptr), ""); + EXPECT_THROW( + InspectableReader::ReadString(PropertyValue::CreateBoolean(true)), + std::invalid_argument); +} + +TEST(UtilsTests, InspectableReader_ReadStringArray) { + winrt::com_array string_array = {L"a", L"b", L"c"}; + std::vector expected = {"a", "b", "c"}; + IInspectable inspectable = PropertyValue::CreateStringArray(string_array); + EXPECT_EQ(InspectableReader::ReadStringArray(inspectable), expected); + EXPECT_TRUE(InspectableReader::ReadStringArray(nullptr).empty()); + EXPECT_THROW( + InspectableReader::ReadStringArray(PropertyValue::CreateBoolean(true)), + std::invalid_argument); +} + } // namespace windows } // namespace nearby