From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../test/gtest/TestNetworkLinkIdHashingDarwin.cpp | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 netwerk/test/gtest/TestNetworkLinkIdHashingDarwin.cpp (limited to 'netwerk/test/gtest/TestNetworkLinkIdHashingDarwin.cpp') diff --git a/netwerk/test/gtest/TestNetworkLinkIdHashingDarwin.cpp b/netwerk/test/gtest/TestNetworkLinkIdHashingDarwin.cpp new file mode 100644 index 0000000000..a07c9438bd --- /dev/null +++ b/netwerk/test/gtest/TestNetworkLinkIdHashingDarwin.cpp @@ -0,0 +1,93 @@ +#include + +#include "gtest/gtest.h" +#include "mozilla/SHA1.h" +#include "nsString.h" +#include "nsPrintfCString.h" +#include "mozilla/Logging.h" +#include "nsNetworkLinkService.h" + +using namespace mozilla; + +in6_addr StringToSockAddr(const std::string& str) { + sockaddr_in6 ip; + inet_pton(AF_INET6, str.c_str(), &(ip.sin6_addr)); + return ip.sin6_addr; +} + +TEST(TestNetworkLinkIdHashingDarwin, Single) +{ + // Setup + SHA1Sum expected_sha1; + SHA1Sum::Hash expected_digest; + + in6_addr a1 = StringToSockAddr("2001:db8:8714:3a91::1"); + + // Prefix + expected_sha1.update(&a1, sizeof(in6_addr)); + // Netmask + expected_sha1.update(&a1, sizeof(in6_addr)); + expected_sha1.finish(expected_digest); + + std::vector prefixNetmaskStore; + prefixNetmaskStore.push_back(std::make_pair(a1, a1)); + SHA1Sum actual_sha1; + // Run + nsNetworkLinkService::HashSortedPrefixesAndNetmasks(prefixNetmaskStore, + &actual_sha1); + SHA1Sum::Hash actual_digest; + actual_sha1.finish(actual_digest); + + // Assert + ASSERT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(SHA1Sum::Hash))); +} + +TEST(TestNetworkLinkIdHashingDarwin, Multiple) +{ + // Setup + SHA1Sum expected_sha1; + SHA1Sum::Hash expected_digest; + + std::vector addresses; + addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::1")); + addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::2")); + addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::3")); + addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::4")); + + for (const auto& address : addresses) { + // Prefix + expected_sha1.update(&address, sizeof(in6_addr)); + // Netmask + expected_sha1.update(&address, sizeof(in6_addr)); + } + expected_sha1.finish(expected_digest); + + // Ordered + std::vector ordered; + for (const auto& address : addresses) { + ordered.push_back(std::make_pair(address, address)); + } + SHA1Sum ordered_sha1; + + // Unordered + std::vector reversed; + for (auto it = addresses.rbegin(); it != addresses.rend(); ++it) { + reversed.push_back(std::make_pair(*it, *it)); + } + SHA1Sum reversed_sha1; + + // Run + nsNetworkLinkService::HashSortedPrefixesAndNetmasks(ordered, &ordered_sha1); + SHA1Sum::Hash ordered_digest; + ordered_sha1.finish(ordered_digest); + + nsNetworkLinkService::HashSortedPrefixesAndNetmasks(reversed, &reversed_sha1); + SHA1Sum::Hash reversed_digest; + reversed_sha1.finish(reversed_digest); + + // Assert + ASSERT_EQ(0, + memcmp(&expected_digest, &ordered_digest, sizeof(SHA1Sum::Hash))); + ASSERT_EQ(0, + memcmp(&expected_digest, &reversed_digest, sizeof(SHA1Sum::Hash))); +} -- cgit v1.2.3