-
Notifications
You must be signed in to change notification settings - Fork 856
/
test_common.cpp
73 lines (60 loc) · 1.95 KB
/
test_common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include "gtest/gtest.h"
#include "test_env.h"
#include "utilities.h"
#include "common.h"
using namespace srt;
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);
srt::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)
{
srt::TestInit srtinit;
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)
{
srt::TestInit srtinit;
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)
{
srt::TestInit srtinit;
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);
}