]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/remotebackend/test-remotebackend.cc
Standardize license text in all PDNS files
[thirdparty/pdns.git] / modules / remotebackend / test-remotebackend.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #define BOOST_TEST_DYN_LINK
23 #define BOOST_TEST_NO_MAIN
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 #include <boost/test/unit_test.hpp>
29 #include <boost/assign/list_of.hpp>
30
31 #include <boost/tuple/tuple.hpp>
32 #include "pdns/namespaces.hh"
33 #include "pdns/dns.hh"
34 #include "pdns/dnsbackend.hh"
35 #include "pdns/dnspacket.hh"
36 #include "pdns/ueberbackend.hh"
37 #include "pdns/pdnsexception.hh"
38 #include "pdns/logger.hh"
39 #include "pdns/arguments.hh"
40 #include "pdns/json.hh"
41 #include "pdns/statbag.hh"
42 #include "pdns/packetcache.hh"
43
44 #include "test-remotebackend-keys.hh"
45
46 extern DNSBackend *be;
47
48 BOOST_AUTO_TEST_SUITE(test_remotebackend_so)
49
50 BOOST_AUTO_TEST_CASE(test_method_lookup) {
51 BOOST_TEST_MESSAGE("Testing lookup method");
52 DNSResourceRecord rr;
53 be->lookup(QType(QType::SOA), DNSName("unit.test."));
54 // then try to get()
55 BOOST_CHECK(be->get(rr)); // and this should be TRUE.
56 // then we check rr contains what we expect
57 BOOST_CHECK_EQUAL(rr.qname.toString(), "unit.test.");
58 BOOST_CHECK_MESSAGE(rr.qtype == QType::SOA, "returned qtype was not SOA");
59 BOOST_CHECK_EQUAL(rr.content, "ns.unit.test. hostmaster.unit.test. 1 2 3 4 5 6");
60 BOOST_CHECK_EQUAL(rr.ttl, 300);
61 }
62
63 BOOST_AUTO_TEST_CASE(test_method_lookup_empty) {
64 BOOST_TEST_MESSAGE("Testing lookup method with empty result");
65 DNSResourceRecord rr;
66 be->lookup(QType(QType::SOA), DNSName("empty.unit.test."));
67 // then try to get()
68 BOOST_CHECK(!be->get(rr)); // and this should be FALSE
69 }
70
71 BOOST_AUTO_TEST_CASE(test_method_list) {
72 int record_count = 0;
73 DNSResourceRecord rr;
74
75 BOOST_TEST_MESSAGE("Testing list method");
76 be->list(DNSName("unit.test."), -1);
77 while(be->get(rr)) record_count++;
78
79 BOOST_CHECK_EQUAL(record_count, 5); // number of records our test domain has
80 }
81
82 BOOST_AUTO_TEST_CASE(test_method_doesDNSSEC) {
83 BOOST_TEST_MESSAGE("Testing doesDNSSEC method");
84 BOOST_CHECK(be->doesDNSSEC()); // should be true
85 }
86
87 BOOST_AUTO_TEST_CASE(test_method_setDomainMetadata) {
88 std::vector<std::string> meta;
89 meta.push_back("VALUE");
90 BOOST_TEST_MESSAGE("Testing setDomainMetadata method");
91 BOOST_CHECK(be->setDomainMetadata(DNSName("unit.test."),"TEST", meta));
92 }
93
94 BOOST_AUTO_TEST_CASE(test_method_getDomainMetadata) {
95 std::vector<std::string> meta;
96 BOOST_TEST_MESSAGE("Testing getDomainMetadata method");
97 be->getDomainMetadata(DNSName("unit.test."),"TEST", meta);
98 BOOST_CHECK_EQUAL(meta.size(), 1);
99 // in case we got more than one value, which would be unexpected
100 // but not fatal
101 if (meta.size() > 0)
102 BOOST_CHECK_EQUAL(meta[0], "VALUE");
103 }
104
105 BOOST_AUTO_TEST_CASE(test_method_getAllDomainMetadata) {
106 std::map<std::string, std::vector<std::string> > meta;
107 BOOST_TEST_MESSAGE("Testing getAllDomainMetadata method");
108 be->getAllDomainMetadata(DNSName("unit.test."), meta);
109 BOOST_CHECK_EQUAL(meta.size(), 1);
110 // in case we got more than one value, which would be unexpected
111 // but not fatal
112 if (meta.size() > 0)
113 BOOST_CHECK_EQUAL(meta["TEST"][0], "VALUE");
114 }
115
116 BOOST_AUTO_TEST_CASE(test_method_addDomainKey) {
117 BOOST_TEST_MESSAGE("Testing addDomainKey method");
118 BOOST_CHECK_EQUAL(be->addDomainKey(DNSName("unit.test."),k1), 1);
119 BOOST_CHECK_EQUAL(be->addDomainKey(DNSName("unit.test."),k2), 2);
120 }
121
122 BOOST_AUTO_TEST_CASE(test_method_getDomainKeys) {
123 std::vector<DNSBackend::KeyData> keys;
124 BOOST_TEST_MESSAGE("Testing getDomainKeys method");
125 // we expect to get two keys
126 be->getDomainKeys(DNSName("unit.test."),0,keys);
127 BOOST_CHECK_EQUAL(keys.size(), 2);
128 // in case we got more than 2 keys, which would be unexpected
129 // but not fatal
130 if (keys.size() > 1) {
131 // check that we have two keys
132 for(DNSBackend::KeyData &kd : keys) {
133 BOOST_CHECK(kd.id > 0);
134 BOOST_CHECK(kd.flags == 256 || kd.flags == 257);
135 BOOST_CHECK(kd.active == true);
136 BOOST_CHECK(kd.content.size() > 500);
137 }
138 }
139 }
140
141 BOOST_AUTO_TEST_CASE(test_method_deactivateDomainKey) {
142 BOOST_TEST_MESSAGE("Testing deactivateDomainKey method");
143 BOOST_CHECK(be->deactivateDomainKey(DNSName("unit.test."),1));
144 }
145
146 BOOST_AUTO_TEST_CASE(test_method_activateDomainKey) {
147 BOOST_TEST_MESSAGE("Testing activateDomainKey method");
148 BOOST_CHECK(be->activateDomainKey(DNSName("unit.test."),1));
149 }
150
151 BOOST_AUTO_TEST_CASE(test_method_removeDomainKey) {
152 BOOST_CHECK(be->removeDomainKey(DNSName("unit.test."),2));
153 BOOST_CHECK(be->removeDomainKey(DNSName("unit.test."),1));
154 }
155
156 BOOST_AUTO_TEST_CASE(test_method_getBeforeAndAfterNamesAbsolute) {
157 DNSName unhashed;
158 std::string before,after;
159 BOOST_TEST_MESSAGE("Testing getBeforeAndAfterNamesAbsolute method");
160
161 be->getBeforeAndAfterNamesAbsolute(-1, "middle.unit.test.", unhashed, before, after);
162 BOOST_CHECK_EQUAL(unhashed.toString(), "middle.");
163 BOOST_CHECK_EQUAL(before, "begin.");
164 BOOST_CHECK_EQUAL(after, "stop.");
165 }
166
167 BOOST_AUTO_TEST_CASE(test_method_setTSIGKey) {
168 std::string algorithm, content;
169 BOOST_TEST_MESSAGE("Testing setTSIGKey method");
170 BOOST_CHECK_MESSAGE(be->setTSIGKey(DNSName("unit.test."),DNSName("hmac-md5."),"kp4/24gyYsEzbuTVJRUMoqGFmN3LYgVDzJ/3oRSP7ys="), "did not return true");
171 }
172
173 BOOST_AUTO_TEST_CASE(test_method_getTSIGKey) {
174 DNSName algorithm;
175 std::string content;
176 BOOST_TEST_MESSAGE("Testing getTSIGKey method");
177 be->getTSIGKey(DNSName("unit.test."),&algorithm,&content);
178 BOOST_CHECK_EQUAL(algorithm.toString(), "hmac-md5.");
179 BOOST_CHECK_EQUAL(content, "kp4/24gyYsEzbuTVJRUMoqGFmN3LYgVDzJ/3oRSP7ys=");
180 }
181
182 BOOST_AUTO_TEST_CASE(test_method_deleteTSIGKey) {
183 std::string algorithm, content;
184 BOOST_TEST_MESSAGE("Testing deleteTSIGKey method");
185 BOOST_CHECK_MESSAGE(be->deleteTSIGKey(DNSName("unit.test.")), "did not return true");
186 }
187
188 BOOST_AUTO_TEST_CASE(test_method_getTSIGKeys) {
189 std::vector<struct TSIGKey> keys;
190 BOOST_TEST_MESSAGE("Testing getTSIGKeys method");
191 be->getTSIGKeys(keys);
192 BOOST_CHECK(keys.size() > 0);
193 if (keys.size() > 0) {
194 BOOST_CHECK_EQUAL(keys[0].name.toString(), "test.");
195 BOOST_CHECK_EQUAL(keys[0].algorithm.toString(), "NULL.");
196 BOOST_CHECK_EQUAL(keys[0].key, "NULL");
197 }
198 }
199
200 BOOST_AUTO_TEST_CASE(test_method_setNotified) {
201 BOOST_TEST_MESSAGE("Testing setNotified method");
202 be->setNotified(1, 2);
203 BOOST_CHECK(true); // we check this on next step
204 }
205
206 BOOST_AUTO_TEST_CASE(test_method_getDomainInfo) {
207 DomainInfo di;
208 BOOST_TEST_MESSAGE("Testing getDomainInfo method");
209 be->getDomainInfo(DNSName("unit.test."), di);
210 BOOST_CHECK_EQUAL(di.zone.toString(), "unit.test.");
211 BOOST_CHECK_EQUAL(di.serial, 2);
212 BOOST_CHECK_EQUAL(di.notified_serial, 2);
213 BOOST_CHECK_EQUAL(di.kind, DomainInfo::Native);
214 BOOST_CHECK_EQUAL(di.backend, be);
215 }
216
217 BOOST_AUTO_TEST_CASE(test_method_getAllDomains) {
218 DomainInfo di;
219 BOOST_TEST_MESSAGE("Testing getAllDomains method");
220 vector<DomainInfo> result;
221
222 be->getAllDomains(&result, true);
223
224 di = result[0];
225 BOOST_CHECK_EQUAL(di.zone.toString(), "unit.test.");
226 BOOST_CHECK_EQUAL(di.serial, 2);
227 BOOST_CHECK_EQUAL(di.notified_serial, 2);
228 BOOST_CHECK_EQUAL(di.kind, DomainInfo::Native);
229 BOOST_CHECK_EQUAL(di.backend, be);
230 }
231
232 BOOST_AUTO_TEST_CASE(test_method_isMaster) {
233 BOOST_TEST_MESSAGE("Testing isMaster method");
234 BOOST_CHECK(be->isMaster(DNSName("ns1.unit.test."), "10.0.0.1"));
235 BOOST_CHECK(!be->isMaster(DNSName("ns2.unit.test."), "10.0.0.2"));
236 }
237
238 BOOST_AUTO_TEST_CASE(test_method_superMasterBackend) {
239 DNSResourceRecord rr;
240 std::vector<DNSResourceRecord> nsset;
241 DNSBackend *dbd;
242 BOOST_TEST_MESSAGE("Testing superMasterBackend method");
243
244 rr.qname = DNSName("example.com.");
245 rr.qtype = QType::NS;
246 rr.qclass = QClass::IN;
247 rr.ttl = 300;
248 rr.content = "ns1.example.com.";
249 nsset.push_back(rr);
250 rr.qname = DNSName("example.com.");
251 rr.qtype = QType::NS;
252 rr.qclass = QClass::IN;
253 rr.ttl = 300;
254 rr.content = "ns2.example.com.";
255 nsset.push_back(rr);
256
257 BOOST_CHECK(be->superMasterBackend("10.0.0.1", DNSName("example.com."), nsset, NULL, NULL, &dbd));
258
259 // let's see what we got
260 BOOST_CHECK_EQUAL(dbd, be);
261 }
262
263 BOOST_AUTO_TEST_CASE(test_method_createSlaveDomain) {
264 BOOST_TEST_MESSAGE("Testing createSlaveDomain method");
265 BOOST_CHECK(be->createSlaveDomain("10.0.0.1", DNSName("pirate.unit.test."), "", ""));
266 }
267
268 BOOST_AUTO_TEST_CASE(test_method_feedRecord) {
269 DNSResourceRecord rr;
270 BOOST_TEST_MESSAGE("Testing feedRecord method");
271 be->startTransaction(DNSName("example.com."),2);
272 rr.qname = DNSName("example.com.");
273 rr.qtype = QType::SOA;
274 rr.qclass = QClass::IN;
275 rr.ttl = 300;
276 rr.content = "ns1.example.com. hostmaster.example.com. 2013013441 7200 3600 1209600 300";
277 BOOST_CHECK(be->feedRecord(rr, NULL));
278 rr.qname = DNSName("replace.example.com.");
279 rr.qtype = QType::A;
280 rr.qclass = QClass::IN;
281 rr.ttl = 300;
282 rr.content = "127.0.0.1";
283 BOOST_CHECK(be->feedRecord(rr, NULL));
284 be->commitTransaction();
285 }
286
287 BOOST_AUTO_TEST_CASE(test_method_replaceRRSet) {
288 be->startTransaction(DNSName("example.com."),2);
289 DNSResourceRecord rr;
290 std::vector<DNSResourceRecord> rrset;
291 BOOST_TEST_MESSAGE("Testing replaceRRSet method");
292 rr.qname = DNSName("replace.example.com.");
293 rr.qtype = QType::A;
294 rr.qclass = QClass::IN;
295 rr.ttl = 300;
296 rr.content = "1.1.1.1";
297 rrset.push_back(rr);
298 BOOST_CHECK(be->replaceRRSet(2, DNSName("replace.example.com."), QType(QType::A), rrset));
299 be->commitTransaction();
300 }
301
302 BOOST_AUTO_TEST_CASE(test_method_feedEnts) {
303 BOOST_TEST_MESSAGE("Testing feedEnts method");
304 be->startTransaction(DNSName("example.com."),2);
305 map<DNSName, bool> nonterm = boost::assign::map_list_of(DNSName("_udp"), true)(DNSName("_sip._udp"), true);
306 BOOST_CHECK(be->feedEnts(2, nonterm));
307 be->commitTransaction();
308 }
309
310 BOOST_AUTO_TEST_CASE(test_method_feedEnts3) {
311 BOOST_TEST_MESSAGE("Testing feedEnts3 method");
312 be->startTransaction(DNSName("example.com"),2);
313 NSEC3PARAMRecordContent ns3prc;
314 ns3prc.d_iterations=1;
315 ns3prc.d_salt="\u00aa\u00bb\u00cc\u00dd";
316 map<DNSName, bool> nonterm = boost::assign::map_list_of(DNSName("_udp"), true)(DNSName("_sip._udp"), true);
317 BOOST_CHECK(be->feedEnts3(2, DNSName("example.com."), nonterm, ns3prc, 0));
318 be->commitTransaction();
319 }
320
321 BOOST_AUTO_TEST_CASE(test_method_abortTransaction) {
322 BOOST_TEST_MESSAGE("Testing abortTransaction method");
323 be->startTransaction(DNSName("example.com."),2);
324 BOOST_CHECK(be->abortTransaction());
325 }
326
327 BOOST_AUTO_TEST_CASE(test_method_calculateSOASerial) {
328 SOAData sd;
329 time_t serial;
330
331 be->getSOA(DNSName("unit.test."),sd);
332 BOOST_CHECK(be->calculateSOASerial(DNSName("unit.test."),sd,serial));
333
334 BOOST_CHECK_EQUAL(serial, 2013060300);
335 }
336
337 BOOST_AUTO_TEST_CASE(test_method_directBackendCmd) {
338 BOOST_TEST_MESSAGE("Testing directBackendCmd method");
339 BOOST_CHECK_EQUAL(be->directBackendCmd("PING 1234"), "PING 1234");
340 }
341
342 BOOST_AUTO_TEST_SUITE_END();