3 #include "gtest/gtest.h"
4 #include "mozilla/SHA1.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
) {
14 inet_pton(AF_INET6
, str
.c_str(), &(ip
.sin6_addr
));
18 TEST(TestNetworkLinkIdHashingDarwin
, Single
)
21 SHA1Sum expected_sha1
;
22 SHA1Sum::Hash expected_digest
;
24 in6_addr a1
= StringToSockAddr("2001:db8:8714:3a91::1");
27 expected_sha1
.update(&a1
, sizeof(in6_addr
));
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
));
36 nsNetworkLinkService::HashSortedPrefixesAndNetmasks(prefixNetmaskStore
,
38 SHA1Sum::Hash actual_digest
;
39 actual_sha1
.finish(actual_digest
);
42 ASSERT_EQ(0, memcmp(&expected_digest
, &actual_digest
, sizeof(SHA1Sum::Hash
)));
45 TEST(TestNetworkLinkIdHashingDarwin
, Multiple
)
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
) {
59 expected_sha1
.update(&address
, sizeof(in6_addr
));
61 expected_sha1
.update(&address
, sizeof(in6_addr
));
63 expected_sha1
.finish(expected_digest
);
66 std::vector
<prefix_and_netmask
> ordered
;
67 for (const auto& address
: addresses
) {
68 ordered
.push_back(std::make_pair(address
, address
));
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
;
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
);
90 memcmp(&expected_digest
, &ordered_digest
, sizeof(SHA1Sum::Hash
)));
92 memcmp(&expected_digest
, &reversed_digest
, sizeof(SHA1Sum::Hash
)));