]>
Commit | Line | Data |
---|---|---|
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 KM |
8 | #include <boost/assign/list_of.hpp> |
9 | #include <boost/foreach.hpp> | |
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 | ||
16 | using std::string; | |
17 | ||
18 | BOOST_AUTO_TEST_SUITE(misc_hh) | |
19 | typedef pair<std::string, uint16_t> typedns_t; | |
20 | ||
21 | BOOST_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 | ||
38 | BOOST_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 |
58 | BOOST_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 | ||
81 | BOOST_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 | 104 | BOOST_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 | ||
111 | BOOST_AUTO_TEST_CASE(test_labelReverse) { | |
112 | BOOST_CHECK_EQUAL(labelReverse("www.powerdns.com"), "com powerdns www"); | |
113 | } | |
114 | ||
115 | BOOST_AUTO_TEST_CASE(test_makeRelative) { | |
116 | BOOST_CHECK_EQUAL(makeRelative("www.powerdns.com", "powerdns.com"), "www"); | |
bd414f9b | 117 | BOOST_CHECK_EQUAL(makeRelative("PoWeRdNs.CoM", "powerdns.com"), ""); |
76cd6661 | 118 | } |
119 | ||
1df6d212 | 120 | BOOST_AUTO_TEST_CASE(test_AtomicCounter) { |
76cd6661 | 121 | AtomicCounter ac; |
122 | ++ac; | |
123 | ++ac; | |
124 | BOOST_CHECK_EQUAL(ac, 2); | |
125 | } | |
126 | ||
6e427101 | 127 | BOOST_AUTO_TEST_CASE(test_endianness) { |
128 | uint32_t i = 1; | |
129 | #if BYTE_ORDER == BIG_ENDIAN | |
130 | BOOST_CHECK_EQUAL(i, htonl(i)); | |
131 | #elif BYTE_ORDER == LITTLE_ENDIAN | |
132 | uint32_t j=0x01000000; | |
133 | BOOST_CHECK_EQUAL(i, ntohl(j)); | |
134 | #else | |
135 | BOOST_FAIL("Did not detect endianness at all"); | |
136 | #endif | |
137 | } | |
76cd6661 | 138 | |
139 | BOOST_AUTO_TEST_CASE(test_parseService) { | |
140 | ServiceTuple tp; | |
141 | parseService("smtp.powerdns.com:25", tp); | |
142 | BOOST_CHECK_EQUAL(tp.host, "smtp.powerdns.com"); | |
143 | BOOST_CHECK_EQUAL(tp.port, 25); | |
144 | parseService("smtp.powerdns.com", tp); | |
145 | BOOST_CHECK_EQUAL(tp.port, 25); | |
146 | } | |
147 | ||
83e5035d | 148 | BOOST_AUTO_TEST_CASE(test_ternary) { |
149 | int maxqps=1024; | |
150 | BOOST_CHECK_EQUAL(defTer(maxqps, 16384), maxqps); | |
151 | BOOST_CHECK_EQUAL(defTer(0, 16384), 16384); | |
152 | ||
153 | int* qps=0; | |
154 | BOOST_CHECK_EQUAL(*defTer(qps, &maxqps), 1024); | |
155 | } | |
156 | ||
ac84f00c AT |
157 | BOOST_AUTO_TEST_CASE(test_SimpleMatch) { |
158 | BOOST_CHECK_EQUAL(SimpleMatch("").match(std::string("")), true); | |
159 | BOOST_CHECK_EQUAL(SimpleMatch("?").match(std::string("")), false); | |
160 | BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("")), true); | |
161 | ||
162 | BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("abc")), true); | |
163 | BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("ab")), false); | |
164 | BOOST_CHECK_EQUAL(SimpleMatch("abc").match(std::string("bc")), false); | |
165 | ||
166 | BOOST_CHECK_EQUAL(SimpleMatch("?").match(std::string("a")), true); | |
167 | BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("abc")), true); | |
168 | BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("ab")), false); | |
169 | BOOST_CHECK_EQUAL(SimpleMatch("a?c").match(std::string("bc")), false); | |
170 | ||
171 | BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("*")), true); | |
172 | BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("abc")), true); | |
173 | BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("ab")), false); | |
174 | BOOST_CHECK_EQUAL(SimpleMatch("a*c").match(std::string("bc")), false); | |
175 | ||
176 | BOOST_CHECK_EQUAL(SimpleMatch("*").match(std::string("abcdefghj")), true); | |
177 | BOOST_CHECK_EQUAL(SimpleMatch("*a").match(std::string("abca")), true); | |
178 | BOOST_CHECK_EQUAL(SimpleMatch("*a").match(std::string("abcb")), false); | |
179 | BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abcabcabcabacabac")), true); | |
180 | BOOST_CHECK_EQUAL(SimpleMatch("abc*").match(std::string("abc")), true); | |
181 | } | |
182 | ||
9bacbe09 PD |
183 | BOOST_AUTO_TEST_SUITE_END() |
184 |