Skip to content

Commit

Permalink
[tests] Check CIPAddress::pton(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed Nov 24, 2020
1 parent 81b6651 commit 3bc810b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/filelist.maf
Expand Up @@ -2,6 +2,7 @@
SOURCES
test_buffer.cpp
test_bonding.cpp
test_common.cpp
test_connection_timeout.cpp
test_many_connections.cpp
test_cryspr.cpp
Expand Down
67 changes: 67 additions & 0 deletions test/test_common.cpp
@@ -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);
}

0 comments on commit 3bc810b

Please sign in to comment.