1//
2// SocketAddressTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "SocketAddressTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/SocketAddress.h"
15#include "Poco/Net/NetException.h"
16
17
18using Poco::Net::SocketAddress;
19using Poco::Net::IPAddress;
20using Poco::Net::InvalidAddressException;
21using Poco::Net::HostNotFoundException;
22using Poco::Net::ServiceNotFoundException;
23using Poco::Net::NoAddressFoundException;
24using Poco::Net::AddressFamilyMismatchException;
25using Poco::InvalidArgumentException;
26
27
28SocketAddressTest::SocketAddressTest(const std::string& name): CppUnit::TestCase(name)
29{
30}
31
32
33SocketAddressTest::~SocketAddressTest()
34{
35}
36
37
38void SocketAddressTest::testSocketAddress()
39{
40 SocketAddress wild;
41 assertTrue (wild.host().isWildcard());
42 assertTrue (wild.port() == 0);
43
44 SocketAddress sa1("192.168.1.100", 100);
45 assertTrue (sa1.af() == AF_INET);
46 assertTrue (sa1.family() == SocketAddress::IPv4);
47 assertTrue (sa1.host().toString() == "192.168.1.100");
48 assertTrue (sa1.port() == 100);
49 assertTrue (sa1.toString() == "192.168.1.100:100");
50
51 SocketAddress sa2("192.168.1.100", "100");
52 assertTrue (sa2.host().toString() == "192.168.1.100");
53 assertTrue (sa2.port() == 100);
54
55#if !defined(_WIN32_WCE)
56 SocketAddress sa3("192.168.1.100", "ftp");
57 assertTrue (sa3.host().toString() == "192.168.1.100");
58 assertTrue (sa3.port() == 21);
59#endif
60
61 try
62 {
63 SocketAddress sa3("192.168.1.100", "f00bar");
64 fail("bad service name - must throw");
65 }
66 catch (ServiceNotFoundException&)
67 {
68 }
69
70 SocketAddress sa4("pocoproject.org", 80);
71 assertTrue (sa4.host().toString() == "104.130.199.50");
72 assertTrue (sa4.port() == 80);
73
74 try
75 {
76 SocketAddress sa5("192.168.2.260", 80);
77 fail("invalid address - must throw");
78 }
79 catch (HostNotFoundException&)
80 {
81 }
82 catch (NoAddressFoundException&)
83 {
84 }
85
86 try
87 {
88 SocketAddress sa6("192.168.2.120", "80000");
89 fail("invalid port - must throw");
90 }
91 catch (ServiceNotFoundException&)
92 {
93 }
94
95 SocketAddress sa7("192.168.2.120:88");
96 assertTrue (sa7.host().toString() == "192.168.2.120");
97 assertTrue (sa7.port() == 88);
98
99 SocketAddress sa8("[192.168.2.120]:88");
100 assertTrue (sa8.host().toString() == "192.168.2.120");
101 assertTrue (sa8.port() == 88);
102
103 try
104 {
105 SocketAddress sa9("[192.168.2.260]");
106 fail("invalid address - must throw");
107 }
108 catch (InvalidArgumentException&)
109 {
110 }
111
112 try
113 {
114 SocketAddress sa9("[192.168.2.260:88");
115 fail("invalid address - must throw");
116 }
117 catch (InvalidArgumentException&)
118 {
119 }
120
121 SocketAddress sa10("www6.pocoproject.org", 80);
122 assertTrue (sa10.host().toString() == "104.130.199.50" || sa10.host().toString() == "[2001:4801:7828:101:be76:4eff:fe10:1455]");
123
124 SocketAddress sa11(SocketAddress::IPv4, "www6.pocoproject.org", 80);
125 assertTrue (sa11.host().toString() == "104.130.199.50");
126
127#ifdef POCO_HAVE_IPv6
128 try
129 {
130 SocketAddress sa12(SocketAddress::IPv6, "www6.pocoproject.org", 80);
131 assertTrue (sa12.host().toString() == "2001:4801:7828:101:be76:4eff:fe10:1455");
132 }
133 catch (AddressFamilyMismatchException&)
134 {
135 // may happen if no IPv6 address is configured on the system
136 }
137#endif
138}
139
140
141void SocketAddressTest::testSocketRelationals()
142{
143 SocketAddress sa1("192.168.1.100", 100);
144 SocketAddress sa2("192.168.1.100:100");
145 assertTrue (sa1 == sa2);
146
147 SocketAddress sa3("192.168.1.101", "99");
148 assertTrue (sa2 < sa3);
149
150 SocketAddress sa4("192.168.1.101", "102");
151 assertTrue (sa3 < sa4);
152}
153
154
155void SocketAddressTest::testSocketAddress6()
156{
157#ifdef POCO_HAVE_IPv6
158 SocketAddress sa1("FE80::E6CE:8FFF:FE4A:EDD0", 100);
159 assertTrue (sa1.af() == AF_INET6);
160 assertTrue (sa1.family() == SocketAddress::IPv6);
161 assertTrue (sa1.host().toString() == "fe80::e6ce:8fff:fe4a:edd0");
162 assertTrue (sa1.port() == 100);
163 assertTrue (sa1.toString() == "[fe80::e6ce:8fff:fe4a:edd0]:100");
164
165 SocketAddress sa2("[FE80::E6CE:8FFF:FE4A:EDD0]:100");
166 assertTrue (sa2.af() == AF_INET6);
167 assertTrue (sa2.family() == SocketAddress::IPv6);
168 assertTrue (sa2.host().toString() == "fe80::e6ce:8fff:fe4a:edd0");
169 assertTrue (sa2.port() == 100);
170 assertTrue (sa2.toString() == "[fe80::e6ce:8fff:fe4a:edd0]:100");
171#endif
172}
173
174
175void SocketAddressTest::testSocketAddressUnixLocal()
176{
177#ifdef POCO_OS_FAMILY_UNIX
178 SocketAddress sa1(SocketAddress::UNIX_LOCAL, "/tmp/sock1");
179 assertTrue (sa1.af() == AF_UNIX);
180 assertTrue (sa1.family() == SocketAddress::UNIX_LOCAL);
181 assertTrue (sa1.toString() == "/tmp/sock1");
182
183 SocketAddress sa2(SocketAddress::UNIX_LOCAL, "/tmp/sock2");
184 assertTrue (sa1 != sa2);
185 assertTrue (sa1 < sa2);
186
187 SocketAddress sa3(SocketAddress::UNIX_LOCAL, "/tmp/sock1");
188 assertTrue (sa1 == sa3);
189 assertTrue (!(sa1 < sa3));
190
191 SocketAddress sa4("/tmp/sock1");
192 assertTrue (sa1 == sa4);
193 assertTrue (sa4.toString() == "/tmp/sock1");
194#endif
195}
196
197
198void SocketAddressTest::testSocketAddressLinuxAbstract()
199{
200#if POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID
201 std::string path("sock1.pocoproject.org");
202 path.insert(0, 1, '\0');
203 SocketAddress sa1(SocketAddress::UNIX_LOCAL, path);
204 assertTrue(sa1.af() == AF_UNIX);
205 assertTrue(sa1.family() == SocketAddress::UNIX_LOCAL);
206 assertTrue(sa1.toString() == path);
207
208 std::string path2("sock2.pocoproject.org");
209 path2.insert(0, 1, '\0');
210
211 SocketAddress sa2(SocketAddress::UNIX_LOCAL, path2);
212 assertTrue(sa1 != sa2);
213 assertTrue(sa1 < sa2);
214
215 SocketAddress sa3(SocketAddress::UNIX_LOCAL, path);
216 assertTrue(sa1 == sa3);
217 assertTrue(!(sa1 < sa3));
218
219 SocketAddress sa4(path);
220 assertTrue(sa1 == sa4);
221 assertTrue(sa4.toString() == path);
222
223 SocketAddress sa5(sa1);
224 assertTrue(sa1 == sa5);
225 assertTrue(sa1.length() == sa5.length());
226
227 sa5 = sa1;
228 assertTrue(sa1 == sa5);
229 assertTrue(sa1.length() == sa5.length());
230#endif
231}
232
233
234void SocketAddressTest::setUp()
235{
236}
237
238
239void SocketAddressTest::tearDown()
240{
241}
242
243
244CppUnit::Test* SocketAddressTest::suite()
245{
246 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SocketAddressTest");
247
248 CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress);
249 CppUnit_addTest(pSuite, SocketAddressTest, testSocketRelationals);
250 CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddress6);
251 CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddressUnixLocal);
252 CppUnit_addTest(pSuite, SocketAddressTest, testSocketAddressLinuxAbstract);
253
254 return pSuite;
255}
256