Backed out changeset 5c7de47bcacb (bug 1927094) for causing Bug 1928689. a=backout
[gecko.git] / netwerk / test / gtest / TestNetworkLinkIdHashingDarwin.cpp
bloba07c9438bd4555e4d9a4249236fa36da4b5fa496
1 #include <arpa/inet.h>
3 #include "gtest/gtest.h"
4 #include "mozilla/SHA1.h"
5 #include "nsString.h"
6 #include "nsPrintfCString.h"
7 #include "mozilla/Logging.h"
8 #include "nsNetworkLinkService.h"
10 using namespace mozilla;
12 in6_addr StringToSockAddr(const std::string& str) {
13 sockaddr_in6 ip;
14 inet_pton(AF_INET6, str.c_str(), &(ip.sin6_addr));
15 return ip.sin6_addr;
18 TEST(TestNetworkLinkIdHashingDarwin, Single)
20 // Setup
21 SHA1Sum expected_sha1;
22 SHA1Sum::Hash expected_digest;
24 in6_addr a1 = StringToSockAddr("2001:db8:8714:3a91::1");
26 // Prefix
27 expected_sha1.update(&a1, sizeof(in6_addr));
28 // Netmask
29 expected_sha1.update(&a1, sizeof(in6_addr));
30 expected_sha1.finish(expected_digest);
32 std::vector<prefix_and_netmask> prefixNetmaskStore;
33 prefixNetmaskStore.push_back(std::make_pair(a1, a1));
34 SHA1Sum actual_sha1;
35 // Run
36 nsNetworkLinkService::HashSortedPrefixesAndNetmasks(prefixNetmaskStore,
37 &actual_sha1);
38 SHA1Sum::Hash actual_digest;
39 actual_sha1.finish(actual_digest);
41 // Assert
42 ASSERT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(SHA1Sum::Hash)));
45 TEST(TestNetworkLinkIdHashingDarwin, Multiple)
47 // Setup
48 SHA1Sum expected_sha1;
49 SHA1Sum::Hash expected_digest;
51 std::vector<in6_addr> addresses;
52 addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::1"));
53 addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::2"));
54 addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::3"));
55 addresses.push_back(StringToSockAddr("2001:db8:8714:3a91::4"));
57 for (const auto& address : addresses) {
58 // Prefix
59 expected_sha1.update(&address, sizeof(in6_addr));
60 // Netmask
61 expected_sha1.update(&address, sizeof(in6_addr));
63 expected_sha1.finish(expected_digest);
65 // Ordered
66 std::vector<prefix_and_netmask> ordered;
67 for (const auto& address : addresses) {
68 ordered.push_back(std::make_pair(address, address));
70 SHA1Sum ordered_sha1;
72 // Unordered
73 std::vector<prefix_and_netmask> reversed;
74 for (auto it = addresses.rbegin(); it != addresses.rend(); ++it) {
75 reversed.push_back(std::make_pair(*it, *it));
77 SHA1Sum reversed_sha1;
79 // Run
80 nsNetworkLinkService::HashSortedPrefixesAndNetmasks(ordered, &ordered_sha1);
81 SHA1Sum::Hash ordered_digest;
82 ordered_sha1.finish(ordered_digest);
84 nsNetworkLinkService::HashSortedPrefixesAndNetmasks(reversed, &reversed_sha1);
85 SHA1Sum::Hash reversed_digest;
86 reversed_sha1.finish(reversed_digest);
88 // Assert
89 ASSERT_EQ(0,
90 memcmp(&expected_digest, &ordered_digest, sizeof(SHA1Sum::Hash)));
91 ASSERT_EQ(0,
92 memcmp(&expected_digest, &reversed_digest, sizeof(SHA1Sum::Hash)));