1//
2// DNSTest.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 "DNSTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/DNS.h"
15#include "Poco/Net/HostEntry.h"
16#include "Poco/Net/NetException.h"
17
18
19using Poco::Net::DNS;
20using Poco::Net::IPAddress;
21using Poco::Net::HostEntry;
22using Poco::Net::InvalidAddressException;
23using Poco::Net::HostNotFoundException;
24using Poco::Net::ServiceNotFoundException;
25using Poco::Net::NoAddressFoundException;
26
27
28DNSTest::DNSTest(const std::string& name): CppUnit::TestCase(name)
29{
30}
31
32
33DNSTest::~DNSTest()
34{
35}
36
37
38void DNSTest::testHostByName()
39{
40 HostEntry he1 = DNS::hostByName("aliastest.pocoproject.org");
41 // different systems report different canonical names, unfortunately.
42 assertTrue (he1.name() == "dnstest.pocoproject.org" || he1.name() == "aliastest.pocoproject.org");
43#if !defined(POCO_HAVE_ADDRINFO)
44 // getaddrinfo() does not report any aliases
45 assertTrue (!he1.aliases().empty());
46 assertTrue (he1.aliases()[0] == "aliastest.pocoproject.org");
47#endif
48 assertTrue (he1.addresses().size() >= 1);
49 assertTrue (he1.addresses()[0].toString() == "1.2.3.4");
50
51 try
52 {
53 HostEntry he1 = DNS::hostByName("nohost.pocoproject.org");
54 fail("host not found - must throw");
55 }
56 catch (HostNotFoundException&)
57 {
58 }
59 catch (NoAddressFoundException&)
60 {
61 }
62}
63
64
65void DNSTest::testHostByAddress()
66{
67 IPAddress ip1("80.122.195.86");
68 HostEntry he1 = DNS::hostByAddress(ip1);
69 assertTrue (he1.name() == "mailhost.appinf.com");
70 assertTrue (he1.aliases().empty());
71 assertTrue (he1.addresses().size() >= 1);
72 assertTrue (he1.addresses()[0].toString() == "80.122.195.86");
73
74 IPAddress ip2("10.0.244.253");
75 try
76 {
77 HostEntry he2 = DNS::hostByAddress(ip2);
78 fail("host not found - must throw");
79 }
80 catch (HostNotFoundException&)
81 {
82 }
83 catch (NoAddressFoundException&)
84 {
85 }
86}
87
88
89void DNSTest::testResolve()
90{
91}
92
93
94void DNSTest::testEncodeIDN()
95{
96 std::string idn("d\xc3\xb6m\xc3\xa4in.example"); // d"om"ain.example
97 assertTrue (DNS::isIDN(idn));
98 assertTrue (DNS::encodeIDN(idn) == "xn--dmin-moa0i.example");
99
100 idn = ".d\xc3\xb6m\xc3\xa4in.example"; // .d"om"ain.example
101 assertTrue (DNS::isIDN(idn));
102 assertTrue (DNS::encodeIDN(idn) == ".xn--dmin-moa0i.example");
103
104 idn = "d\xc3\xb6m\xc3\xa4in.example."; // .d"om"ain.example.
105 assertTrue (DNS::isIDN(idn));
106 assertTrue (DNS::encodeIDN(idn) == "xn--dmin-moa0i.example.");
107
108 idn = "d\xc3\xb6m\xc3\xa4in"; // d"om"ain
109 assertTrue (DNS::isIDN(idn));
110 assertTrue (DNS::encodeIDN(idn) == "xn--dmin-moa0i");
111
112 idn = "\xc3\xa4""aaa.example"; // "aaaa.example
113 assertTrue (DNS::isIDN(idn));
114 assertTrue (DNS::encodeIDN(idn) == "xn--aaa-pla.example");
115
116 idn = "a\xc3\xa4""aa.example"; // a"aaa.example
117 assertTrue (DNS::isIDN(idn));
118 assertTrue (DNS::encodeIDN(idn) == "xn--aaa-qla.example");
119
120 idn = "foo.\xc3\xa2""bcd\xc3\xa9""f.example"; // foo.^abcd'ef.example
121 assertTrue (DNS::isIDN(idn));
122 assertTrue (DNS::encodeIDN(idn) == "foo.xn--bcdf-9na9b.example");
123
124 idn = "\xe2\x98\x83.example"; // <snowman>.example
125 assertTrue (DNS::isIDN(idn));
126 assertTrue (DNS::encodeIDN(idn) == "xn--n3h.example");
127
128 idn = "\xe2\x98\x83."; // <snowman>.
129 assertTrue (DNS::isIDN(idn));
130 assertTrue (DNS::encodeIDN(idn) == "xn--n3h.");
131
132 idn = "\xe2\x98\x83"; // <snowman>
133 assertTrue (DNS::isIDN(idn));
134 assertTrue (DNS::encodeIDN(idn) == "xn--n3h");
135
136 std::string dn = "www.pocoproject.org";
137 assertTrue (!DNS::isIDN(dn));
138 assertTrue (DNS::encodeIDN(dn) == "www.pocoproject.org");
139}
140
141
142void DNSTest::testDecodeIDN()
143{
144 std::string enc("xn--dmin-moa0i.example");
145 assertTrue (DNS::isEncodedIDN(enc));
146 assertTrue (DNS::decodeIDN(enc) == "d\xc3\xb6m\xc3\xa4in.example"); // d"om"ain.example
147
148 enc = ".xn--dmin-moa0i.example";
149 assertTrue (DNS::isEncodedIDN(enc));
150 assertTrue (DNS::decodeIDN(enc) == ".d\xc3\xb6m\xc3\xa4in.example"); // .d"om"ain.example
151
152 enc = "xn--dmin-moa0i.example.";
153 assertTrue (DNS::isEncodedIDN(enc));
154 assertTrue (DNS::decodeIDN(enc) == "d\xc3\xb6m\xc3\xa4in.example."); // d"om"ain.example.
155
156 enc = "xn--dmin-moa0i";
157 assertTrue (DNS::isEncodedIDN(enc));
158 assertTrue (DNS::decodeIDN(enc) == "d\xc3\xb6m\xc3\xa4in"); // d"om"ain
159
160 enc = "foo.xn--bcdf-9na9b.example";
161 assertTrue (DNS::isEncodedIDN(enc));
162 assertTrue (DNS::decodeIDN(enc) == "foo.\xc3\xa2""bcd\xc3\xa9""f.example"); // foo.^abcd'ef.example
163
164 enc = "xn--n3h.example";
165 assertTrue (DNS::isEncodedIDN(enc));
166 assertTrue (DNS::decodeIDN(enc) == "\xe2\x98\x83.example"); // <snowman>.example
167
168 enc = "xn--n3h.";
169 assertTrue (DNS::isEncodedIDN(enc));
170 assertTrue (DNS::decodeIDN(enc) == "\xe2\x98\x83."); // <snowman>.
171
172 enc = "xn--n3h";
173 assertTrue (DNS::isEncodedIDN(enc));
174 assertTrue (DNS::decodeIDN(enc) == "\xe2\x98\x83"); // <snowman>
175
176 std::string dn = "www.pocoproject.org";
177 assertTrue (!DNS::isEncodedIDN(dn));
178 assertTrue (DNS::decodeIDN(dn) == "www.pocoproject.org");
179}
180
181
182void DNSTest::setUp()
183{
184}
185
186
187void DNSTest::tearDown()
188{
189}
190
191
192CppUnit::Test* DNSTest::suite()
193{
194 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DNSTest");
195
196 CppUnit_addTest(pSuite, DNSTest, testHostByName);
197 CppUnit_addTest(pSuite, DNSTest, testHostByAddress);
198 CppUnit_addTest(pSuite, DNSTest, testResolve);
199 CppUnit_addTest(pSuite, DNSTest, testEncodeIDN);
200 CppUnit_addTest(pSuite, DNSTest, testDecodeIDN);
201
202 return pSuite;
203}
204