2
* Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
35
#include "test_utils.h"
37
void IpTest::testIpAddr()
41
IpAddr ip = {"8.8.8.8"};
43
CPPUNIT_ASSERT(ip.toString() == "8.8.8.8");
44
CPPUNIT_ASSERT(ip.getPort() == 0);
45
CPPUNIT_ASSERT(ip.isIpv4());
46
CPPUNIT_ASSERT(not ip.isIpv6());
47
CPPUNIT_ASSERT(not ip.isLoopback());
48
CPPUNIT_ASSERT(not ip.isPrivate());
49
CPPUNIT_ASSERT(not ip.isUnspecified());
51
IpAddr ip_2 = ip.toString();
52
CPPUNIT_ASSERT(ip_2 == ip);
55
CPPUNIT_ASSERT(not ip);
56
CPPUNIT_ASSERT(ip.isUnspecified());
57
CPPUNIT_ASSERT(not ip.isIpv4());
58
CPPUNIT_ASSERT(not ip.isIpv6());
59
CPPUNIT_ASSERT(ip.getPort() == 0);
61
ip = IpAddr("8.8.8.8:42");
63
CPPUNIT_ASSERT(ip.toString() == "8.8.8.8");
64
CPPUNIT_ASSERT(ip.getPort() == 42);
66
pj_sockaddr_set_port(ip.pjPtr(), 5042);
67
CPPUNIT_ASSERT(ip.getPort() == 5042);
70
const in6_addr native_ip = {{
71
0x3f, 0xfe, 0x05, 0x01,
72
0x00, 0x08, 0x00, 0x00,
73
0x02, 0x60, 0x97, 0xff,
74
0xfe, 0x40, 0xef, 0xab
76
ip = IpAddr(native_ip);
78
CPPUNIT_ASSERT(ip == IpAddr("3ffe:0501:0008:0000:0260:97ff:fe40:efab"));
79
CPPUNIT_ASSERT(not ip.isIpv4());
80
CPPUNIT_ASSERT(ip.isIpv6());
82
CPPUNIT_ASSERT(IpAddr::isValid("3ffe:0501:0008:0000:0260:97ff:fe40:efab"));
83
CPPUNIT_ASSERT(IpAddr::isValid("[3ffe:0501:0008:0000:0260:97ff:fe40:efab]"));
84
CPPUNIT_ASSERT(IpAddr::isValid("[3ffe:0501:0008:0000:0260:97ff:fe40:efab]:4242"));
85
CPPUNIT_ASSERT(IpAddr::isValid("[3ffe:501:8::260:97ff:fe40:efab]:4242"));
89
void IpTest::testIpAddrOst()
93
const IpAddr ipv4 = {"8.8.8.8"};
95
ost::IPV4Host v4host = ipv4;
96
IpAddr ip = v4host.getAddress();
97
CPPUNIT_ASSERT(ipv4 == ip);
100
const IpAddr ipv6 = {"2001:4860:4860::8888"};
102
ost::IPV6Host v6host = ipv6;
103
ip = v6host.getAddress();
104
CPPUNIT_ASSERT(ipv6 == ip);
106
ost::IPV6Host v6host2 = v6host;
107
ip = v6host2.getAddress();
108
CPPUNIT_ASSERT(ipv6 == ip);
110
ost::IPV6Host v6host3 = {};
111
ip = v6host3.getAddress();
113
CPPUNIT_ASSERT(v6host3.getAddressCount() > 0 && v6host3.getAddressCount() < 256);
116
CPPUNIT_ASSERT(IpAddr(v6host3.getAddress()) == ipv6);
117
CPPUNIT_ASSERT(v6host3.getAddressCount() == 1);
119
v6host3 = v6host2.getAddress();
120
CPPUNIT_ASSERT(IpAddr(v6host3.getAddress()) == ipv6);
121
CPPUNIT_ASSERT(v6host3.getAddressCount() == 1);
125
IpTest::IpTest() : CppUnit::TestCase("IP Tests") {}