]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/test-misc_hh.cc
Merge pull request #7945 from pieterlexis/syncres-CNAME-cache-cleanup
[thirdparty/pdns.git] / pdns / test-misc_hh.cc
CommitLineData
9bacbe09
PD
1#define BOOST_TEST_DYN_LINK
2#define BOOST_TEST_NO_MAIN
3
870a0fe4
AT
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
9bacbe09 7#include <boost/test/unit_test.hpp>
8fb5b29a 8#include <boost/assign/list_of.hpp>
fa8fd4d2 9
8fb5b29a 10#include <boost/tuple/tuple.hpp>
9bacbe09 11#include "misc.hh"
6e427101 12#include "dns.hh"
c6e47b32 13#include <arpa/inet.h>
9bacbe09
PD
14#include <utility>
15
16using std::string;
17
c7f29d3e 18BOOST_AUTO_TEST_SUITE(test_misc_hh)
9bacbe09
PD
19typedef pair<std::string, uint16_t> typedns_t;
20
21BOOST_AUTO_TEST_CASE(test_CIStringCompare) {
232f0877
CH
22 set<std::string, CIStringCompare> nsset;
23 nsset.insert("abc");
24 nsset.insert("ns.example.com");
25 nsset.insert("");
26 nsset.insert("def");
27 nsset.insert("aBc");
28 nsset.insert("ns.example.com");
29 BOOST_CHECK_EQUAL(nsset.size(), 4);
9bacbe09 30
232f0877
CH
31 ostringstream s;
32 for(set<std::string, CIStringCompare>::const_iterator i=nsset.begin();i!=nsset.end();++i) {
33 s<<"("<<*i<<")";
34 }
35 BOOST_CHECK_EQUAL(s.str(), "()(abc)(def)(ns.example.com)");
9bacbe09
PD
36}
37
38BOOST_AUTO_TEST_CASE(test_CIStringPairCompare) {
232f0877
CH
39 set<typedns_t, CIStringPairCompare> nsset2;
40 nsset2.insert(make_pair("ns.example.com", 1));
41 nsset2.insert(make_pair("abc", 1));
42 nsset2.insert(make_pair("", 1));
43 nsset2.insert(make_pair("def", 1));
44 nsset2.insert(make_pair("abc", 2));
45 nsset2.insert(make_pair("abc", 1));
46 nsset2.insert(make_pair("ns.example.com", 0));
47 nsset2.insert(make_pair("abc", 2));
48 nsset2.insert(make_pair("ABC", 2));
49 BOOST_CHECK_EQUAL(nsset2.size(), 6);
9bacbe09 50
232f0877
CH
51 ostringstream s;
52 for(set<typedns_t, CIStringPairCompare>::const_iterator i=nsset2.begin();i!=nsset2.end();++i) {
53 s<<"("<<i->first<<"|"<<i->second<<")";
54 }
55 BOOST_CHECK_EQUAL(s.str(), "(|1)(abc|1)(abc|2)(def|1)(ns.example.com|0)(ns.example.com|1)");
9bacbe09
PD
56}
57
8fb5b29a
KM
58BOOST_AUTO_TEST_CASE(test_pdns_ilexicographical_compare) {
59 typedef boost::tuple<const std::string, const std::string, bool> case_t;
60 typedef std::list<case_t> cases_t;
61
62 cases_t cases = boost::assign::list_of
63 (case_t(std::string(""), std::string(""), false))
64 (case_t(std::string(""), std::string("abc"), true))
65 (case_t(std::string("abc"), std::string(""), false))
66 (case_t(std::string("abc"), std::string("abcd"), true))
67 (case_t(std::string("abcd"), std::string("abc"), false))
68 (case_t(std::string("abd"), std::string("abc"), false))
69 (case_t(std::string("abc"), std::string("abd"), true))
70 (case_t(std::string("abc"), std::string("Abc"), false))
71 (case_t(std::string("Abc"), std::string("abc"), false))
72 ;
73
ef7cd021 74 for(const case_t& val : cases) {
8fb5b29a
KM
75 bool res;
76 res = pdns_ilexicographical_compare(val.get<0>(), val.get<1>());
77 BOOST_CHECK_EQUAL(res, val.get<2>());
78 }
79}
80
81BOOST_AUTO_TEST_CASE(test_pdns_iequals) {
82 typedef boost::tuple<const std::string, const std::string, bool> case_t;
83 typedef std::list<case_t> cases_t;
84
85 cases_t cases = boost::assign::list_of
86 (case_t(std::string(""), std::string(""), true))
87 (case_t(std::string(""), std::string("abc"), false))
88 (case_t(std::string("abc"), std::string(""), false))
89 (case_t(std::string("abc"), std::string("abcd"), false))
90 (case_t(std::string("abcd"), std::string("abc"), false))
91 (case_t(std::string("abd"), std::string("abc"), false))
92 (case_t(std::string("abc"), std::string("abd"), false))
93 (case_t(std::string("abc"), std::string("Abc"), true))
94 (case_t(std::string("Abc"), std::string("abc"), true))
95 ;
96
ef7cd021 97 for(const case_t& val : cases) {
8fb5b29a
KM
98 bool res;
99 res = pdns_iequals(val.get<0>(), val.get<1>());
100 BOOST_CHECK_EQUAL(res, val.get<2>());
101 }
102}
103
76cd6661 104BOOST_AUTO_TEST_CASE(test_stripDot) {
8fb5b29a
KM
105 BOOST_CHECK_EQUAL(stripDot("."), "");
106 BOOST_CHECK_EQUAL(stripDot(""), "");
107 BOOST_CHECK_EQUAL(stripDot("www.powerdns.com."), "www.powerdns.com");
108 BOOST_CHECK_EQUAL(stripDot("www.powerdns.com"), "www.powerdns.com");
76cd6661 109}
110
111BOOST_AUTO_TEST_CASE(test_labelReverse) {
7a59dc5a 112 BOOST_CHECK_EQUAL(DNSName("www.powerdns.com").labelReverse().toString(" ", false), "com powerdns www");
76cd6661 113}
114
76cd6661 115
1df6d212 116BOOST_AUTO_TEST_CASE(test_AtomicCounter) {
c3064e57 117 AtomicCounter ac(0);
76cd6661 118 ++ac;
119 ++ac;
120 BOOST_CHECK_EQUAL(ac, 2);
121}
122
6e427101 123BOOST_AUTO_TEST_CASE(test_endianness) {
124 uint32_t i = 1;
125#if BYTE_ORDER == BIG_ENDIAN
126 BOOST_CHECK_EQUAL(i, htonl(i));
127#elif BYTE_ORDER == LITTLE_ENDIAN
128 uint32_t j=0x01000000;
129 BOOST_CHECK_EQUAL(i, ntohl(j));
130#else
131 BOOST_FAIL("Did not detect endianness at all");
132#endif
133}
76cd6661 134
135BOOST_AUTO_TEST_CASE(test_parseService) {
136 ServiceTuple tp;
137 parseService("smtp.powerdns.com:25", tp);
138 BOOST_CHECK_EQUAL(tp.host, "smtp.powerdns.com");
139 BOOST_CHECK_EQUAL(tp.port, 25);
140 parseService("smtp.powerdns.com", tp);
141 BOOST_CHECK_EQUAL(tp.port, 25);
142}
143
83e5035d 144BOOST_AUTO_TEST_CASE(test_ternary) {
145 int maxqps=1024;
146 BOOST_CHECK_EQUAL(defTer(maxqps, 16384), maxqps);
147 BOOST_CHECK_EQUAL(defTer(0, 16384), 16384);
148
149 int* qps=0;
150 BOOST_CHECK_EQUAL(*defTer(qps, &maxqps), 1024);
151}
152
ac84f00c
AT
153BOOST_AUTO_TEST_CASE(test_SimpleMatch) {
154 BOOST_CHECK_EQUAL(SimpleMatch("").match(std::string("")), true);
155 BOOST_CHECK_EQUAL(SimpleMatch("?").match(std::string("")), false);
156 BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("")), true);
157
158 BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("abc")), true);
159 BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("ab")), false);
160 BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("bc")), false);
161
162 BOOST_CHECK_EQUAL(SimpleMatch("?").match(std::string("a")), true);
163 BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("abc")), true);
164 BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("ab")), false);
165 BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("bc")), false);
166
167 BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("*")), true);
168 BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("abc")), true);
169 BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("ab")), false);
170 BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("bc")), false);
171
172 BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("abcdefghj")), true);
173 BOOST_CHECK_EQUAL(SimpleMatch("*a").match(std::string("abca")), true);
174 BOOST_CHECK_EQUAL(SimpleMatch("*a").match(std::string("abcb")), false);
175 BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abcabcabcabacabac")), true);
176 BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abc")), true);
177}
178
9bacbe09
PD
179BOOST_AUTO_TEST_SUITE_END()
180