-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81b6651
commit 3bc810b
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "gtest/gtest.h" | ||
#include "utilities.h" | ||
#include "common.h" | ||
|
||
void test_cipaddress_pton(const char* peer_ip, int family, const uint32_t (&ip)[4]) | ||
{ | ||
const int port = 4200; | ||
|
||
// Peer | ||
sockaddr_storage ss; | ||
ss.ss_family = family; | ||
|
||
void* sin_addr = nullptr; | ||
if (family == AF_INET) | ||
{ | ||
sockaddr_in* const sa = (sockaddr_in*)&ss; | ||
sa->sin_port = htons(port); | ||
sin_addr = &sa->sin_addr; | ||
} | ||
else // IPv6 | ||
{ | ||
sockaddr_in6* const sa = (sockaddr_in6*)&ss; | ||
sa->sin6_port = htons(port); | ||
sin_addr = &sa->sin6_addr; | ||
} | ||
|
||
ASSERT_EQ(inet_pton(family, peer_ip, sin_addr), 1); | ||
const sockaddr_any peer(ss); | ||
|
||
// HOST | ||
sockaddr_any host(family); | ||
host.hport(port); | ||
|
||
CIPAddress::pton(host, ip, peer); | ||
EXPECT_EQ(peer, host) << "Peer " << peer.str() << " host " << host.str(); | ||
} | ||
|
||
// Example IPv4 address: 192.168.0.1 | ||
TEST(CIPAddress, IPv4_pton) | ||
{ | ||
const char* peer_ip = "192.168.0.1"; | ||
const uint32_t ip[4] = {htobe32(0xC0A80001), 0, 0, 0}; | ||
test_cipaddress_pton(peer_ip, AF_INET, ip); | ||
} | ||
|
||
// Example IPv6 address: 2001:db8:85a3:8d3:1319:8a2e:370:7348 | ||
TEST(CIPAddress, IPv6_pton) | ||
{ | ||
const char* peer_ip = "2001:db8:85a3:8d3:1319:8a2e:370:7348"; | ||
const uint32_t ip[4] = {htobe32(0x20010db8), htobe32(0x85a308d3), htobe32(0x13198a2e), htobe32(0x03707348)}; | ||
|
||
test_cipaddress_pton(peer_ip, AF_INET6, ip); | ||
} | ||
|
||
// Example IPv4 address: 192.168.0.1 | ||
// Maps to IPv6 address: 0:0:0:0:0:FFFF:192.168.0.1 | ||
// Simplified: ::FFFF:192.168.0.1 | ||
TEST(CIPAddress, IPv4_in_IPv6_pton) | ||
{ | ||
const char* peer_ip = "::ffff:192.168.0.1"; | ||
const uint32_t ip[4] = {0, 0, htobe32(0x0000FFFF), htobe32(0xC0A80001)}; | ||
|
||
test_cipaddress_pton(peer_ip, AF_INET6, ip); | ||
} |