rdata_serialization_unittest.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <util/buffer.h>
  16. #include <dns/name.h>
  17. #include <dns/labelsequence.h>
  18. #include <dns/messagerenderer.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <dns/rrclass.h>
  22. #include <dns/rrtype.h>
  23. #include <datasrc/memory/rdata_serialization.h>
  24. #include <util/unittests/wiredata.h>
  25. #include <gtest/gtest.h>
  26. #include <boost/bind.hpp>
  27. #include <boost/foreach.hpp>
  28. #include <cstring>
  29. #include <algorithm>
  30. #include <set>
  31. #include <stdexcept>
  32. #include <string>
  33. #include <vector>
  34. using namespace isc::dns;
  35. using namespace isc::dns::rdata;
  36. using namespace isc::datasrc::memory;
  37. using isc::util::unittests::matchWireData;
  38. using std::string;
  39. using std::vector;
  40. // A trick to steal some private definitions of the implementation we use here
  41. namespace isc {
  42. namespace datasrc{
  43. namespace memory {
  44. #include <datasrc/memory/rdata_serialization_priv.cc>
  45. }
  46. }
  47. }
  48. namespace {
  49. // This defines a tuple of test data used in test_rdata_list below.
  50. struct TestRdata {
  51. const char* const rrclass; // RR class, textual form
  52. const char* const rrtype; // RR type, textual form
  53. const char* const rdata; // textual RDATA
  54. const size_t n_varlen_fields; // expected # of variable-len fields
  55. };
  56. // This test data consist of (almost) all supported types of RDATA (+ some
  57. // unusual and corner cases).
  58. const TestRdata test_rdata_list[] = {
  59. {"IN", "A", "192.0.2.1", 0},
  60. {"IN", "NS", "ns.example.com.", 0},
  61. {"IN", "CNAME", "cname.example.com.", 0},
  62. {"IN", "SOA", "ns.example.com. root.example.com. 0 0 0 0 0", 0},
  63. {"IN", "PTR", "reverse.example.com.", 0},
  64. {"IN", "HINFO", "\"cpu-info\" \"OS-info\"", 1},
  65. {"IN", "MINFO", "root.example.com. mbox.example.com.", 0},
  66. {"IN", "MX", "10 mx.example.com.", 0},
  67. {"IN", "TXT", "\"test1\" \"test 2\"", 1},
  68. {"IN", "RP", "root.example.com. rp-text.example.com.", 0},
  69. {"IN", "AFSDB", "1 afsdb.example.com.", 0},
  70. {"IN", "AAAA", "2001:db8::1", 0},
  71. {"IN", "SRV", "1 0 10 target.example.com.", 0},
  72. {"IN", "NAPTR", "100 50 \"s\" \"http\" \"\" _http._tcp.example.com.", 1},
  73. {"IN", "DNAME", "dname.example.com.", 0},
  74. {"IN", "DS", "12892 5 2 5F0EB5C777586DE18DA6B5", 1},
  75. {"IN", "SSHFP", "1 1 dd465c09cfa51fb45020cc83316fff", 1},
  76. // We handle RRSIG separately, so it's excluded from the list
  77. {"IN", "NSEC", "next.example.com. A AAAA NSEC RRSIG", 1},
  78. {"IN", "DNSKEY", "256 3 5 FAKEFAKE", 1},
  79. {"IN", "DHCID", "FAKEFAKE", 1},
  80. {"IN", "NSEC3", "1 1 12 AABBCCDD FAKEFAKE A RRSIG", 1},
  81. {"IN", "NSEC3PARAM", "1 0 12 AABBCCDD", 1},
  82. {"IN", "SPF", "v=spf1 +mx a:colo.example.com/28 -all", 1},
  83. {"IN", "DLV", "12892 5 2 5F0EB5C777586DE18DA6B5", 1},
  84. {"IN", "TYPE65000", "\\# 3 010203", 1}, // some "custom" type
  85. {"IN", "TYPE65535", "\\# 0", 1}, // max RR type, 0-length RDATA
  86. {"CH", "A", "\\# 2 0102", 1}, // A RR for non-IN class; varlen data
  87. {"CH", "NS", "ns.example.com.", 0}, // class CH, generic data
  88. {"CH", "TXT", "BIND10", 1}, // ditto
  89. {"HS", "A", "\\# 5 0102030405", 1}, // A RR for non-IN class; varlen data
  90. {NULL, NULL, NULL, 0}
  91. };
  92. // The following two functions will be used to generate wire format data
  93. // from encoded representation of each RDATA.
  94. void
  95. renderNameField(MessageRenderer* renderer, bool additional_required,
  96. const LabelSequence& labels, RdataNameAttributes attributes)
  97. {
  98. EXPECT_EQ(additional_required, (attributes & NAMEATTR_ADDITIONAL) != 0);
  99. renderer->writeName(labels, (attributes & NAMEATTR_COMPRESSIBLE) != 0);
  100. }
  101. void
  102. renderDataField(MessageRenderer* renderer, const void* data, size_t data_len) {
  103. renderer->writeData(data, data_len);
  104. }
  105. class RdataSerializationTest : public ::testing::Test {
  106. protected:
  107. RdataSerializationTest() : a_rdata_(createRdata(RRType::A(), RRClass::IN(),
  108. "192.0.2.53")),
  109. aaaa_rdata_(createRdata(RRType::AAAA(), RRClass::IN(),
  110. "2001:db8::53")),
  111. rrsig_rdata_(createRdata(
  112. RRType::RRSIG(), RRClass::IN(),
  113. "A 5 2 3600 20120814220826 "
  114. "20120715220826 12345 com. FAKE"))
  115. {}
  116. // A wrapper for RdataEncoder::encode() with buffer overrun check.
  117. void encodeWrapper(size_t data_len);
  118. // Some commonly used RDATA
  119. const ConstRdataPtr a_rdata_;
  120. const ConstRdataPtr aaaa_rdata_;
  121. const ConstRdataPtr rrsig_rdata_;
  122. RdataEncoder encoder_;
  123. vector<uint8_t> encoded_data_;
  124. MessageRenderer expected_renderer_;
  125. MessageRenderer actual_renderer_;
  126. vector<ConstRdataPtr> rdata_list_;
  127. };
  128. // There are several ways to decode the data. For one, there are
  129. // more interfaces uses for RdataReader, and we use our own decoder,
  130. // to check the actual encoded data.
  131. //
  132. // These decoding ways are provided by the template parameter.
  133. template<class DecoderStyle>
  134. class RdataEncodeDecodeTest : public RdataSerializationTest {
  135. public:
  136. // This helper test method encodes the given list of RDATAs
  137. // (in rdata_list), and then iterates over the data, rendering the fields
  138. // in the wire format. It then compares the wire data with the one
  139. // generated by the normal libdns++ interface to see the encoding/decoding
  140. // works as intended.
  141. // By default it encodes the given RDATAs from the scratch; if old_data
  142. // is non NULL, the test case assumes it points to previously encoded data
  143. // and the given RDATAs are to be merged with it. old_rdata/rrsig_count
  144. // will be set to the number of RDATAs and RRSIGs encoded in old_data.
  145. // These "count" variables must not be set to non 0 unless old_data is
  146. // non NULL, but it's not checked in this methods; it's the caller's
  147. // responsibility to ensure that. rdata_list and rrsig_list should contain
  148. // all RDATAs and RRSIGs included those stored in old_data.
  149. void checkEncode(RRClass rrclass, RRType rrtype,
  150. const vector<ConstRdataPtr>& rdata_list,
  151. size_t expected_varlen_fields,
  152. const vector<ConstRdataPtr>& rrsig_list =
  153. vector<ConstRdataPtr>(),
  154. const void* old_data = NULL,
  155. size_t old_rdata_count = 0,
  156. size_t old_rrsig_count = 0);
  157. void addRdataCommon(const vector<ConstRdataPtr>& rrsigs);
  158. void addRdataMultiCommon(const vector<ConstRdataPtr>& rrsigs,
  159. bool duplicate = false);
  160. void mergeRdataCommon(const vector<ConstRdataPtr>& old_rrsigs,
  161. const vector<ConstRdataPtr>& rrsigs);
  162. };
  163. // Used across more classes and scopes. But it's just uninteresting
  164. // constant.
  165. const Name& dummyName2() {
  166. static const Name result("example.com");
  167. return (result);
  168. }
  169. bool
  170. additionalRequired(const RRType& type) {
  171. // The set of RR types that require additional section processing.
  172. // We'll use it to determine what value should the renderNameField get
  173. // and, if the stored attributes are as expected.
  174. static std::set<RRType> need_additionals;
  175. if (need_additionals.empty()) {
  176. need_additionals.insert(RRType::NS());
  177. need_additionals.insert(RRType::MX());
  178. need_additionals.insert(RRType::SRV());
  179. }
  180. return (need_additionals.find(type) != need_additionals.end());
  181. }
  182. // A decoder that does not use RdataReader. Not recommended for use,
  183. // but it allows the tests to check the internals of the data.
  184. class ManualDecoderStyle {
  185. public:
  186. static void foreachRdataField(RRClass rrclass, RRType rrtype,
  187. size_t rdata_count,
  188. const vector<uint8_t>& encoded_data,
  189. const vector<uint16_t>& varlen_list,
  190. RdataReader::NameAction name_callback,
  191. RdataReader::DataAction data_callback)
  192. {
  193. const RdataEncodeSpec& encode_spec = getRdataEncodeSpec(rrclass,
  194. rrtype);
  195. size_t off = 0;
  196. size_t varlen_count = 0;
  197. size_t name_count = 0;
  198. for (size_t count = 0; count < rdata_count; ++count) {
  199. for (size_t i = 0; i < encode_spec.field_count; ++i) {
  200. const RdataFieldSpec& field_spec = encode_spec.fields[i];
  201. switch (field_spec.type) {
  202. case RdataFieldSpec::FIXEDLEN_DATA:
  203. if (data_callback) {
  204. data_callback(&encoded_data.at(off),
  205. field_spec.fixeddata_len);
  206. }
  207. off += field_spec.fixeddata_len;
  208. break;
  209. case RdataFieldSpec::VARLEN_DATA:
  210. {
  211. const size_t varlen = varlen_list.at(varlen_count);
  212. if (data_callback && varlen > 0) {
  213. data_callback(&encoded_data.at(off), varlen);
  214. }
  215. off += varlen;
  216. ++varlen_count;
  217. break;
  218. }
  219. case RdataFieldSpec::DOMAIN_NAME:
  220. {
  221. ++name_count;
  222. const LabelSequence labels(&encoded_data.at(off));
  223. if (name_callback) {
  224. name_callback(labels,
  225. field_spec.name_attributes);
  226. }
  227. off += labels.getSerializedLength();
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. assert(name_count == encode_spec.name_count * rdata_count);
  234. assert(varlen_count == encode_spec.varlen_count * rdata_count);
  235. }
  236. static void foreachRRSig(const vector<uint8_t>& encoded_data,
  237. const vector<uint16_t>& rrsiglen_list,
  238. RdataReader::DataAction data_callback)
  239. {
  240. size_t rrsig_totallen = 0;
  241. for (vector<uint16_t>::const_iterator it = rrsiglen_list.begin();
  242. it != rrsiglen_list.end();
  243. ++it) {
  244. rrsig_totallen += *it;
  245. }
  246. assert(encoded_data.size() >= rrsig_totallen);
  247. const uint8_t* dp = &encoded_data[encoded_data.size() -
  248. rrsig_totallen];
  249. for (size_t i = 0; i < rrsiglen_list.size(); ++i) {
  250. data_callback(dp, rrsiglen_list[i]);
  251. dp += rrsiglen_list[i];
  252. }
  253. }
  254. static void decode(const isc::dns::RRClass& rrclass,
  255. const isc::dns::RRType& rrtype,
  256. size_t rdata_count,
  257. size_t rrsig_count,
  258. size_t expected_varlen_fields,
  259. const vector<uint8_t>& encoded_data_orig, size_t,
  260. MessageRenderer& renderer)
  261. {
  262. // Make a manual copy, which we're going to modify.
  263. vector<uint8_t> encoded_data = encoded_data_orig;
  264. // If this type of RDATA is expected to contain variable-length fields,
  265. // we brute force the encoded data, exploiting our knowledge of actual
  266. // encoding, then adjust the encoded data excluding the list of length
  267. // fields. This is ugly, but for tests only.
  268. vector<uint16_t> varlen_list;
  269. if (expected_varlen_fields > 0) {
  270. const size_t varlen_list_size =
  271. rdata_count * expected_varlen_fields * sizeof(uint16_t);
  272. ASSERT_LE(varlen_list_size, encoded_data.size());
  273. varlen_list.resize(rdata_count * expected_varlen_fields);
  274. std::memcpy(&varlen_list[0], &encoded_data[0], varlen_list_size);
  275. encoded_data.assign(encoded_data.begin() + varlen_list_size,
  276. encoded_data.end());
  277. }
  278. // If RRSIGs are given, we need to extract the list of the RRSIG
  279. // lengths and adjust encoded_data_ further.
  280. vector<uint16_t> rrsiglen_list;
  281. if (rrsig_count > 0) {
  282. const size_t rrsig_len_size = rrsig_count * sizeof(uint16_t);
  283. ASSERT_LE(rrsig_len_size, encoded_data.size());
  284. rrsiglen_list.resize(rrsig_count * rrsig_len_size);
  285. std::memcpy(&rrsiglen_list[0], &encoded_data[0], rrsig_len_size);
  286. encoded_data.assign(encoded_data.begin() + rrsig_len_size,
  287. encoded_data.end());
  288. }
  289. // Create wire-format data from the encoded data
  290. foreachRdataField(rrclass, rrtype, rdata_count, encoded_data,
  291. varlen_list,
  292. boost::bind(renderNameField, &renderer,
  293. additionalRequired(rrtype), _1, _2),
  294. boost::bind(renderDataField, &renderer, _1, _2));
  295. // 2nd dummy name
  296. renderer.writeName(dummyName2());
  297. // Finally, dump any RRSIGs in wire format.
  298. foreachRRSig(encoded_data, rrsiglen_list,
  299. boost::bind(renderDataField, &renderer, _1, _2));
  300. }
  301. };
  302. // Check using callbacks and calling next until the end.
  303. class CallbackDecoder {
  304. public:
  305. static void decode(const isc::dns::RRClass& rrclass,
  306. const isc::dns::RRType& rrtype,
  307. size_t rdata_count, size_t sig_count, size_t,
  308. const vector<uint8_t>& encoded_data, size_t,
  309. MessageRenderer& renderer)
  310. {
  311. RdataReader reader(rrclass, rrtype, &encoded_data[0], rdata_count,
  312. sig_count,
  313. boost::bind(renderNameField, &renderer,
  314. additionalRequired(rrtype), _1, _2),
  315. boost::bind(renderDataField, &renderer, _1, _2));
  316. while (reader.next() != RdataReader::RRSET_BOUNDARY) {}
  317. renderer.writeName(dummyName2());
  318. while (reader.nextSig() != RdataReader::RRSET_BOUNDARY) {}
  319. }
  320. };
  321. // Check using callbacks and calling iterate.
  322. class IterateDecoder {
  323. public:
  324. static void decode(const isc::dns::RRClass& rrclass,
  325. const isc::dns::RRType& rrtype,
  326. size_t rdata_count, size_t sig_count, size_t,
  327. const vector<uint8_t>& encoded_data, size_t,
  328. MessageRenderer& renderer)
  329. {
  330. RdataReader reader(rrclass, rrtype, &encoded_data[0],
  331. rdata_count, sig_count,
  332. boost::bind(renderNameField, &renderer,
  333. additionalRequired(rrtype), _1, _2),
  334. boost::bind(renderDataField, &renderer, _1, _2));
  335. reader.iterate();
  336. renderer.writeName(dummyName2());
  337. reader.iterateAllSigs();
  338. }
  339. };
  340. namespace {
  341. // Render the data to renderer, if one is set, or put it inside
  342. // a data buffer.
  343. void
  344. appendOrRenderData(vector<uint8_t>* where, MessageRenderer** renderer,
  345. const void* data, size_t size)
  346. {
  347. if (*renderer != NULL) {
  348. (*renderer)->writeData(data, size);
  349. } else {
  350. where->insert(where->end(), reinterpret_cast<const uint8_t*>(data),
  351. reinterpret_cast<const uint8_t*>(data) + size);
  352. }
  353. }
  354. }
  355. // Similar to IterateDecoder, but it first iterates a little and rewinds
  356. // before actual rendering.
  357. class RewindAndDecode {
  358. private:
  359. static void writeName(MessageRenderer** renderer,
  360. const LabelSequence& labels,
  361. RdataNameAttributes attributes)
  362. {
  363. (*renderer)->writeName(labels,
  364. (attributes & NAMEATTR_COMPRESSIBLE) != 0);
  365. }
  366. public:
  367. static void decode(const isc::dns::RRClass& rrclass,
  368. const isc::dns::RRType& rrtype,
  369. size_t rdata_count, size_t sig_count, size_t,
  370. const vector<uint8_t>& encoded_data, size_t,
  371. MessageRenderer& renderer)
  372. {
  373. MessageRenderer dump; // A place to dump the extra data from before
  374. // actual rendering.
  375. MessageRenderer* current = &dump;
  376. vector<uint8_t> placeholder; // boost::bind does not like NULL
  377. RdataReader reader(rrclass, rrtype, &encoded_data[0],
  378. rdata_count, sig_count,
  379. boost::bind(writeName, &current, _1, _2),
  380. boost::bind(appendOrRenderData, &placeholder,
  381. &current, _1, _2));
  382. // Iterate a little and rewind
  383. reader.next();
  384. reader.nextSig();
  385. reader.rewind();
  386. // Do the actual rendering
  387. // cppcheck-suppress unreadVariable
  388. current = &renderer;
  389. reader.iterate();
  390. renderer.writeName(dummyName2());
  391. reader.iterateAllSigs();
  392. }
  393. };
  394. // Decode using the iteration over one rdata each time.
  395. // We also count there's the correct count of Rdatas.
  396. class SingleIterateDecoder {
  397. public:
  398. static void decode(const isc::dns::RRClass& rrclass,
  399. const isc::dns::RRType& rrtype,
  400. size_t rdata_count, size_t sig_count, size_t,
  401. const vector<uint8_t>& encoded_data, size_t,
  402. MessageRenderer& renderer)
  403. {
  404. RdataReader reader(rrclass, rrtype, &encoded_data[0],
  405. rdata_count, sig_count,
  406. boost::bind(renderNameField, &renderer,
  407. additionalRequired(rrtype), _1, _2),
  408. boost::bind(renderDataField, &renderer, _1, _2));
  409. size_t actual_count = 0;
  410. while (reader.iterateRdata()) {
  411. ++actual_count;
  412. }
  413. EXPECT_EQ(rdata_count, actual_count);
  414. actual_count = 0;
  415. renderer.writeName(dummyName2());
  416. while (reader.iterateSingleSig()) {
  417. ++actual_count;
  418. }
  419. EXPECT_EQ(sig_count, actual_count);
  420. }
  421. };
  422. // This one does not adhere to the usual way the reader is used, trying
  423. // to confuse it. It iterates part of the data manually and then reads
  424. // the rest through iterate. It also reads the signatures in the middle
  425. // of rendering.
  426. template<bool start_data, bool start_sig>
  427. class HybridDecoder {
  428. public:
  429. static void decode(const isc::dns::RRClass& rrclass,
  430. const isc::dns::RRType& rrtype,
  431. size_t rdata_count, size_t sig_count, size_t,
  432. const vector<uint8_t>& encoded_data,
  433. size_t encoded_data_len,
  434. MessageRenderer& renderer)
  435. {
  436. vector<uint8_t> data;
  437. MessageRenderer* current;
  438. RdataReader reader(rrclass, rrtype, &encoded_data[0],
  439. rdata_count, sig_count,
  440. boost::bind(renderNameField, &renderer,
  441. additionalRequired(rrtype), _1, _2),
  442. boost::bind(appendOrRenderData, &data, &current, _1,
  443. _2));
  444. // The size matches
  445. EXPECT_EQ(encoded_data_len, reader.getSize());
  446. if (start_sig) {
  447. current = NULL;
  448. reader.nextSig();
  449. }
  450. // Render first part of data. If there's none, return empty Result and
  451. // do nothing.
  452. if (start_data) {
  453. current = &renderer;
  454. reader.next();
  455. }
  456. // Now, we let all sigs to be copied to data. We disable the
  457. // renderer for this.
  458. current = NULL;
  459. reader.iterateAllSigs();
  460. // Now return the renderer and render the rest of the data
  461. // cppcheck-suppress redundantAssignment
  462. // cppcheck-suppress unreadVariable
  463. current = &renderer;
  464. reader.iterate();
  465. // Now, this should not break anything and should be valid, but should
  466. // return ends.
  467. EXPECT_EQ(RdataReader::RRSET_BOUNDARY, reader.next());
  468. EXPECT_EQ(RdataReader::RRSET_BOUNDARY, reader.nextSig());
  469. // Render the name and the sigs
  470. renderer.writeName(dummyName2());
  471. renderer.writeData(&data[0], data.size());
  472. // The size matches even after use
  473. EXPECT_EQ(encoded_data_len, reader.getSize());
  474. }
  475. };
  476. typedef ::testing::Types<ManualDecoderStyle,
  477. CallbackDecoder, IterateDecoder, SingleIterateDecoder,
  478. HybridDecoder<true, true>, HybridDecoder<true, false>,
  479. HybridDecoder<false, true>,
  480. HybridDecoder<false, false> >
  481. DecoderStyles;
  482. // Each decoder style must contain a decode() method. Such method is expected
  483. // to decode the passed data, first render the Rdata into the passed renderer,
  484. // then write the dummyName2() there and write the RRSig data after that.
  485. // It may do other checks too.
  486. //
  487. // There are some slight differences to how to do the decoding, that's why we
  488. // have the typed test.
  489. TYPED_TEST_CASE(RdataEncodeDecodeTest, DecoderStyles);
  490. void
  491. RdataSerializationTest::encodeWrapper(size_t data_len) {
  492. // make sure the data buffer is large enough for the canary
  493. encoded_data_.resize(data_len + 2);
  494. // set the canary data
  495. encoded_data_.at(data_len) = 0xde;
  496. encoded_data_.at(data_len + 1) = 0xad;
  497. // encode, then check the canary is intact
  498. encoder_.encode(&encoded_data_[0], data_len);
  499. EXPECT_EQ(0xde, encoded_data_.at(data_len));
  500. EXPECT_EQ(0xad, encoded_data_.at(data_len + 1));
  501. // shrink the data buffer to the originally expected size (some tests
  502. // expect that). the actual encoded data should be intact.
  503. encoded_data_.resize(data_len);
  504. }
  505. bool
  506. rdataMatch(ConstRdataPtr rdata1, ConstRdataPtr rdata2) {
  507. return (rdata1->compare(*rdata2) == 0);
  508. }
  509. template<class DecoderStyle>
  510. void
  511. RdataEncodeDecodeTest<DecoderStyle>::
  512. checkEncode(RRClass rrclass, RRType rrtype,
  513. const vector<ConstRdataPtr>& rdata_list,
  514. size_t expected_varlen_fields,
  515. const vector<ConstRdataPtr>& rrsig_list,
  516. const void* old_data, size_t old_rdata_count,
  517. size_t old_rrsig_count)
  518. {
  519. // These two names will be rendered before and after the test RDATA,
  520. // to check in case the RDATA contain a domain name whether it's
  521. // compressed or not correctly. The names in the RDATA should basically
  522. // a subdomain of example.com, so it can be compressed due to dummyName2().
  523. // Likewise, dummyName2() should be able to be fully compressed due to
  524. // the name in the RDATA.
  525. const Name dummy_name("com");
  526. expected_renderer_.clear();
  527. actual_renderer_.clear();
  528. encoded_data_.clear();
  529. // Build expected wire-format data, skipping duplicate Rdata.
  530. expected_renderer_.writeName(dummy_name);
  531. vector<ConstRdataPtr> rdata_uniq_list;
  532. BOOST_FOREACH(const ConstRdataPtr& rdata, rdata_list) {
  533. if (std::find_if(rdata_uniq_list.begin(), rdata_uniq_list.end(),
  534. boost::bind(rdataMatch, rdata, _1)) ==
  535. rdata_uniq_list.end()) {
  536. rdata_uniq_list.push_back(rdata);
  537. rdata->toWire(expected_renderer_);
  538. }
  539. }
  540. expected_renderer_.writeName(dummyName2());
  541. vector<ConstRdataPtr> rrsig_uniq_list;
  542. BOOST_FOREACH(const ConstRdataPtr& rdata, rrsig_list) {
  543. if (std::find_if(rrsig_uniq_list.begin(), rrsig_uniq_list.end(),
  544. boost::bind(rdataMatch, rdata, _1)) ==
  545. rrsig_uniq_list.end()) {
  546. rrsig_uniq_list.push_back(rdata);
  547. rdata->toWire(expected_renderer_);
  548. }
  549. }
  550. // Then build wire format data using the encoded data.
  551. // 1st dummy name
  552. actual_renderer_.writeName(dummy_name);
  553. // Create encoded data. If old_xxx_count > 0, that part should be in
  554. // old_data, so should be excluded from addRdata/addSIGRdata.
  555. if (old_data) {
  556. encoder_.start(rrclass, rrtype, old_data, old_rdata_count,
  557. old_rrsig_count);
  558. } else {
  559. encoder_.start(rrclass, rrtype);
  560. }
  561. size_t count = 0;
  562. BOOST_FOREACH(const ConstRdataPtr& rdata, rdata_list) {
  563. if (++count > old_rdata_count) {
  564. encoder_.addRdata(*rdata);
  565. }
  566. }
  567. count = 0;
  568. BOOST_FOREACH(const ConstRdataPtr& rdata, rrsig_list) {
  569. if (++count > old_rrsig_count) {
  570. encoder_.addSIGRdata(*rdata);
  571. }
  572. }
  573. const size_t storage_len = encoder_.getStorageLength();
  574. encodeWrapper(storage_len);
  575. DecoderStyle::decode(rrclass, rrtype, rdata_uniq_list.size(),
  576. rrsig_uniq_list.size(), expected_varlen_fields,
  577. encoded_data_, storage_len, actual_renderer_);
  578. // Two sets of wire-format data should be identical.
  579. matchWireData(expected_renderer_.getData(), expected_renderer_.getLength(),
  580. actual_renderer_.getData(), actual_renderer_.getLength());
  581. }
  582. template<class DecoderStyle>
  583. void
  584. RdataEncodeDecodeTest<DecoderStyle>::
  585. addRdataCommon(const vector<ConstRdataPtr>& rrsigs) {
  586. // Basic check on the encoded data for (most of) all supported RR types,
  587. // in a comprehensive manner.
  588. for (size_t i = 0; test_rdata_list[i].rrclass != NULL; ++i) {
  589. SCOPED_TRACE(string(test_rdata_list[i].rrclass) + "/" +
  590. test_rdata_list[i].rrtype);
  591. const RRClass rrclass(test_rdata_list[i].rrclass);
  592. const RRType rrtype(test_rdata_list[i].rrtype);
  593. const ConstRdataPtr rdata = createRdata(rrtype, rrclass,
  594. test_rdata_list[i].rdata);
  595. rdata_list_.clear();
  596. rdata_list_.push_back(rdata);
  597. checkEncode(rrclass, rrtype, rdata_list_,
  598. test_rdata_list[i].n_varlen_fields, rrsigs);
  599. }
  600. }
  601. TYPED_TEST(RdataEncodeDecodeTest, addRdata) {
  602. vector<ConstRdataPtr> rrsigs;
  603. this->addRdataCommon(rrsigs); // basic tests without RRSIGs (empty vector)
  604. // Test with RRSIGs (covered type doesn't always match, but the encoder
  605. // doesn't check that)
  606. rrsigs.push_back(this->rrsig_rdata_);
  607. this->addRdataCommon(rrsigs);
  608. }
  609. template<class DecoderStyle>
  610. void
  611. RdataEncodeDecodeTest<DecoderStyle>::
  612. addRdataMultiCommon(const vector<ConstRdataPtr>& rrsigs, bool duplicate) {
  613. // Similar to addRdata(), but test with multiple RDATAs.
  614. // Four different cases are tested: a single fixed-len RDATA (A),
  615. // fixed-len data + domain name (MX), variable-len data only (TXT),
  616. // variable-len data + domain name (NAPTR).
  617. ConstRdataPtr a_rdata2 = createRdata(RRType::A(), RRClass::IN(),
  618. "192.0.2.54");
  619. rdata_list_.clear();
  620. rdata_list_.push_back(a_rdata_);
  621. rdata_list_.push_back(a_rdata2);
  622. if (duplicate) { // if duplicate is true, add duplicate Rdata
  623. rdata_list_.push_back(a_rdata_);
  624. }
  625. checkEncode(RRClass::IN(), RRType::A(), rdata_list_, 0, rrsigs);
  626. ConstRdataPtr mx_rdata1 = createRdata(RRType::MX(), RRClass::IN(),
  627. "5 mx1.example.com.");
  628. ConstRdataPtr mx_rdata2 = createRdata(RRType::MX(), RRClass::IN(),
  629. "10 mx2.example.com.");
  630. if (duplicate) { // check duplicate detection is case insensitive for names
  631. rdata_list_.push_back(createRdata(RRType::MX(), RRClass::IN(),
  632. "5 MX1.example.COM."));
  633. }
  634. rdata_list_.clear();
  635. rdata_list_.push_back(mx_rdata1);
  636. rdata_list_.push_back(mx_rdata2);
  637. checkEncode(RRClass::IN(), RRType::MX(), rdata_list_, 0, rrsigs);
  638. ConstRdataPtr txt_rdata1 = createRdata(RRType::TXT(), RRClass::IN(),
  639. "foo bar baz");
  640. ConstRdataPtr txt_rdata2 = createRdata(RRType::TXT(), RRClass::IN(),
  641. "another text data");
  642. if (duplicate) {
  643. rdata_list_.push_back(txt_rdata1);
  644. }
  645. rdata_list_.clear();
  646. rdata_list_.push_back(txt_rdata1);
  647. rdata_list_.push_back(txt_rdata2);
  648. checkEncode(RRClass::IN(), RRType::TXT(), rdata_list_, 1, rrsigs);
  649. ConstRdataPtr naptr_rdata1 =
  650. createRdata(RRType::NAPTR(), RRClass::IN(),
  651. "100 50 \"s\" \"http\" \"\" _http._tcp.example.com");
  652. ConstRdataPtr naptr_rdata2 =
  653. createRdata(RRType::NAPTR(), RRClass::IN(),
  654. "200 100 \"s\" \"http\" \"\" _http._tcp.example.com");
  655. if (duplicate) {
  656. rdata_list_.push_back(naptr_rdata1);
  657. }
  658. rdata_list_.clear();
  659. rdata_list_.push_back(naptr_rdata1);
  660. rdata_list_.push_back(naptr_rdata2);
  661. checkEncode(RRClass::IN(), RRType::NAPTR(), rdata_list_, 1, rrsigs);
  662. }
  663. void ignoreName(const LabelSequence&, unsigned) {
  664. }
  665. void
  666. checkLargeData(const in::DHCID* decoded, bool* called, const void* encoded,
  667. size_t length)
  668. {
  669. EXPECT_FALSE(*called); // Called exactly once
  670. *called = true;
  671. // Reconstruct the Rdata and check it.
  672. isc::util::InputBuffer ib(encoded, length);
  673. const in::DHCID reconstructed(ib, ib.getLength());
  674. EXPECT_EQ(0, reconstructed.compare(*decoded));
  675. }
  676. TEST_F(RdataSerializationTest, encodeLargeRdata) {
  677. // There should be no reason for a large RDATA to fail in encoding,
  678. // but we check such a case explicitly.
  679. encoded_data_.resize(65535); // max unsigned 16-bit int
  680. isc::util::InputBuffer buffer(&encoded_data_[0], encoded_data_.size());
  681. const in::DHCID large_dhcid(buffer, encoded_data_.size());
  682. encoder_.start(RRClass::IN(), RRType::DHCID());
  683. encoder_.addRdata(large_dhcid);
  684. encodeWrapper(encoder_.getStorageLength());
  685. // The encoded data should be identical to the original one.
  686. bool called = false;
  687. RdataReader reader(RRClass::IN(), RRType::DHCID(), &encoded_data_[0], 1, 0,
  688. ignoreName, boost::bind(checkLargeData, &large_dhcid,
  689. &called, _1, _2));
  690. reader.iterate();
  691. EXPECT_TRUE(called);
  692. called = false;
  693. reader.iterateAllSigs();
  694. EXPECT_FALSE(called);
  695. }
  696. TYPED_TEST(RdataEncodeDecodeTest, addRdataMulti) {
  697. vector<ConstRdataPtr> rrsigs;
  698. this->addRdataMultiCommon(rrsigs); // test without RRSIGs (empty vector)
  699. this->addRdataMultiCommon(rrsigs, true); // ditto, but with duplicated data
  700. // Tests with two RRSIGs
  701. rrsigs.push_back(this->rrsig_rdata_);
  702. rrsigs.push_back(createRdata(RRType::RRSIG(), RRClass::IN(),
  703. "A 5 2 3600 20120814220826 "
  704. "20120715220826 54321 com. FAKE"));
  705. this->addRdataMultiCommon(rrsigs);
  706. // Similar to the previous, but with duplicate RRSIG.
  707. rrsigs.push_back(this->rrsig_rdata_);
  708. this->addRdataMultiCommon(rrsigs, true);
  709. }
  710. TEST_F(RdataSerializationTest, badAddRdata) {
  711. // Some operations must follow start().
  712. EXPECT_THROW(encoder_.addRdata(*a_rdata_), isc::InvalidOperation);
  713. EXPECT_THROW(encoder_.getStorageLength(), isc::InvalidOperation);
  714. // will allocate space of some arbitrary size (256 bytes)
  715. EXPECT_THROW(encodeWrapper(256), isc::InvalidOperation);
  716. // Bad buffer for encode
  717. encoder_.start(RRClass::IN(), RRType::A());
  718. encoder_.addRdata(*a_rdata_);
  719. const size_t buf_len = encoder_.getStorageLength();
  720. // NULL buffer for encode
  721. EXPECT_THROW(encoder_.encode(NULL, buf_len), isc::BadValue);
  722. // buffer length is too short (we don't use the wrraper because we don't
  723. // like to tweak the length arg to encode()).
  724. encoded_data_.resize(buf_len - 1);
  725. EXPECT_THROW(encoder_.encode(&encoded_data_[0], buf_len - 1),
  726. isc::BadValue);
  727. // Some of the following checks confirm that adding an Rdata of the
  728. // wrong RR type will be rejected. Several different cases are checked,
  729. // but there shouldn't be any essential difference among these cases in
  730. // the tested code; these cases were considered because in an older version
  731. // of implementation rejected them for possibly different reasons, and
  732. // we simply keep these cases as they are not so many (and may help detect
  733. // future possible regression).
  734. encoder_.start(RRClass::IN(), RRType::AAAA());
  735. EXPECT_THROW(encoder_.addRdata(*a_rdata_), std::bad_cast);
  736. encoder_.start(RRClass::IN(), RRType::A());
  737. EXPECT_THROW(encoder_.addRdata(*aaaa_rdata_), std::bad_cast);
  738. const ConstRdataPtr rp_rdata =
  739. createRdata(RRType::RP(), RRClass::IN(), "a.example. b.example");
  740. encoder_.start(RRClass::IN(), RRType::NS());
  741. EXPECT_THROW(encoder_.addRdata(*rp_rdata), std::bad_cast);
  742. encoder_.start(RRClass::IN(), RRType::DHCID());
  743. EXPECT_THROW(encoder_.addRdata(*rp_rdata), std::bad_cast);
  744. const ConstRdataPtr txt_rdata = createRdata(RRType::TXT(), RRClass::IN(),
  745. "a");
  746. encoder_.start(RRClass::IN(), RRType::MX());
  747. EXPECT_THROW(encoder_.addRdata(*txt_rdata), std::bad_cast);
  748. encoder_.start(RRClass::IN(), RRType::NS());
  749. EXPECT_THROW(encoder_.addRdata(*txt_rdata), std::bad_cast);
  750. const ConstRdataPtr ns_rdata =
  751. createRdata(RRType::NS(), RRClass::IN(), "ns.example.");
  752. encoder_.start(RRClass::IN(), RRType::DNAME());
  753. EXPECT_THROW(encoder_.addRdata(*ns_rdata), std::bad_cast);
  754. const ConstRdataPtr dname_rdata =
  755. createRdata(RRType::DNAME(), RRClass::IN(), "dname.example.");
  756. encoder_.start(RRClass::IN(), RRType::NS());
  757. EXPECT_THROW(encoder_.addRdata(*dname_rdata), std::bad_cast);
  758. // RDATA len exceeds the 16-bit range. Technically not invalid, but
  759. // we don't support that (and it's practically useless anyway).
  760. encoded_data_.resize(65536); // use encoded_data_ for placeholder
  761. isc::util::InputBuffer buffer(&encoded_data_[0], encoded_data_.size());
  762. encoder_.start(RRClass::IN(), RRType::DHCID());
  763. EXPECT_THROW(encoder_.addRdata(in::DHCID(buffer, encoded_data_.size())),
  764. RdataEncodingError);
  765. // RRSIG cannot be used as the main RDATA type (can only be added as
  766. // a signature for some other type of RDATAs).
  767. EXPECT_THROW(encoder_.start(RRClass::IN(), RRType::RRSIG()),
  768. isc::BadValue);
  769. }
  770. template<class DecoderStyle>
  771. void
  772. RdataEncodeDecodeTest<DecoderStyle>::
  773. mergeRdataCommon(const vector<ConstRdataPtr>& old_rrsigs,
  774. const vector<ConstRdataPtr>& rrsigs)
  775. {
  776. // Test with fixed-length old RDATA
  777. rdata_list_.clear();
  778. rdata_list_.push_back(a_rdata_);
  779. checkEncode(RRClass::IN(), RRType::A(), rdata_list_, 0, old_rrsigs);
  780. vector<uint8_t> old_encoded_data = encoded_data_;
  781. ConstRdataPtr a_rdata2 = createRdata(RRType::A(), RRClass::IN(),
  782. "192.0.2.54");
  783. rdata_list_.push_back(a_rdata2);
  784. vector<ConstRdataPtr> rrsigs_all = old_rrsigs;
  785. rrsigs_all.insert(rrsigs_all.end(), rrsigs.begin(), rrsigs.end());
  786. checkEncode(RRClass::IN(), RRType::A(), rdata_list_, 0, rrsigs_all,
  787. &old_encoded_data[0], 1, old_rrsigs.size());
  788. // Test with variable-length old RDATA
  789. rdata_list_.clear();
  790. rrsigs_all.clear();
  791. ConstRdataPtr txt_rdata1 = createRdata(RRType::TXT(), RRClass::IN(),
  792. "foo bar baz");
  793. rdata_list_.push_back(txt_rdata1);
  794. checkEncode(RRClass::IN(), RRType::TXT(), rdata_list_, 1, old_rrsigs);
  795. old_encoded_data = encoded_data_;
  796. ConstRdataPtr txt_rdata2 = createRdata(RRType::TXT(), RRClass::IN(),
  797. "another text data");
  798. rdata_list_.push_back(txt_rdata2);
  799. rrsigs_all = old_rrsigs;
  800. rrsigs_all.insert(rrsigs_all.end(), rrsigs.begin(), rrsigs.end());
  801. checkEncode(RRClass::IN(), RRType::TXT(), rdata_list_, 1, rrsigs_all,
  802. &old_encoded_data[0], 1, old_rrsigs.size());
  803. }
  804. TYPED_TEST(RdataEncodeDecodeTest, mergeRdata) {
  805. vector<ConstRdataPtr> old_rrsigs;
  806. vector<ConstRdataPtr> rrsigs;
  807. // Test without RRSIGs, either for old or new.
  808. this->mergeRdataCommon(old_rrsigs, rrsigs);
  809. // Test without RRSIG for old and with RRSIG for new.
  810. rrsigs.push_back(this->rrsig_rdata_);
  811. this->mergeRdataCommon(old_rrsigs, rrsigs);
  812. // Test with RRSIG for old and without RRSIG for new.
  813. rrsigs.clear();
  814. old_rrsigs.push_back(this->rrsig_rdata_);
  815. this->mergeRdataCommon(old_rrsigs, rrsigs);
  816. // Tests with RRSIGs for both old and new.
  817. old_rrsigs.clear();
  818. rrsigs.push_back(createRdata(RRType::RRSIG(), RRClass::IN(),
  819. "A 5 2 3600 20120814220826 "
  820. "20120715220826 54321 com. FAKE"));
  821. this->mergeRdataCommon(old_rrsigs, rrsigs);
  822. }
  823. void
  824. checkSigData(const ConstRdataPtr& decoded, bool* called, const void* encoded,
  825. size_t length)
  826. {
  827. EXPECT_FALSE(*called); // Called exactly once
  828. *called = true;
  829. // Reconstruct the RRSig and check it.
  830. isc::util::InputBuffer ib(encoded, length);
  831. const generic::RRSIG reconstructed(ib, ib.getLength());
  832. EXPECT_EQ(0, reconstructed.compare(*decoded));
  833. }
  834. TEST_F(RdataSerializationTest, addSIGRdataOnly) {
  835. // Encoded data that only contain RRSIGs. Mostly useless, but can happen
  836. // (in a partially broken zone) and it's accepted.
  837. encoder_.start(RRClass::IN(), RRType::A());
  838. encoder_.addSIGRdata(*rrsig_rdata_);
  839. encodeWrapper(encoder_.getStorageLength());
  840. ASSERT_LT(sizeof(uint16_t), encoder_.getStorageLength());
  841. bool called = false;
  842. RdataReader reader(RRClass::IN(), RRType::A(), &encoded_data_[0], 0, 1,
  843. ignoreName, boost::bind(checkSigData, rrsig_rdata_,
  844. &called, _1, _2));
  845. reader.iterate();
  846. EXPECT_FALSE(called);
  847. reader.iterateAllSigs();
  848. EXPECT_TRUE(called);
  849. }
  850. TEST_F(RdataSerializationTest, badAddSIGRdata) {
  851. // try adding SIG before start
  852. EXPECT_THROW(encoder_.addSIGRdata(*rrsig_rdata_), isc::InvalidOperation);
  853. // Very big RRSIG. This implementation rejects it.
  854. isc::util::OutputBuffer ob(0);
  855. rrsig_rdata_->toWire(ob);
  856. // append dummy trailing signature to make it too big
  857. vector<uint8_t> dummy_sig(65536 - ob.getLength());
  858. ob.writeData(&dummy_sig[0], dummy_sig.size());
  859. ASSERT_EQ(65536, ob.getLength());
  860. isc::util::InputBuffer ib(ob.getData(), ob.getLength());
  861. const generic::RRSIG big_sigrdata(ib, ob.getLength());
  862. encoder_.start(RRClass::IN(), RRType::A());
  863. EXPECT_THROW(encoder_.addSIGRdata(big_sigrdata), RdataEncodingError);
  864. }
  865. }