rdata_serialization_unittest.cc 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. std::vector<ConstRdataPtr> encoded; // for duplicate check include old
  563. BOOST_FOREACH(const ConstRdataPtr& rdata, rdata_list) {
  564. if (++count > old_rdata_count) {
  565. const bool uniq =
  566. (std::find_if(encoded.begin(), encoded.end(),
  567. boost::bind(rdataMatch, rdata, _1)) ==
  568. encoded.end());
  569. EXPECT_EQ(uniq, encoder_.addRdata(*rdata));
  570. }
  571. encoded.push_back(rdata); // we need to remember old rdata too
  572. }
  573. count = 0;
  574. encoded.clear();
  575. BOOST_FOREACH(const ConstRdataPtr& rdata, rrsig_list) {
  576. if (++count > old_rrsig_count) {
  577. const bool uniq =
  578. (std::find_if(encoded.begin(), encoded.end(),
  579. boost::bind(rdataMatch, rdata, _1)) ==
  580. encoded.end());
  581. EXPECT_EQ(uniq, encoder_.addSIGRdata(*rdata));
  582. }
  583. encoded.push_back(rdata);
  584. }
  585. const size_t storage_len = encoder_.getStorageLength();
  586. encodeWrapper(storage_len);
  587. DecoderStyle::decode(rrclass, rrtype, rdata_uniq_list.size(),
  588. rrsig_uniq_list.size(), expected_varlen_fields,
  589. encoded_data_, storage_len, actual_renderer_);
  590. // Two sets of wire-format data should be identical.
  591. matchWireData(expected_renderer_.getData(), expected_renderer_.getLength(),
  592. actual_renderer_.getData(), actual_renderer_.getLength());
  593. }
  594. template<class DecoderStyle>
  595. void
  596. RdataEncodeDecodeTest<DecoderStyle>::
  597. addRdataCommon(const vector<ConstRdataPtr>& rrsigs) {
  598. // Basic check on the encoded data for (most of) all supported RR types,
  599. // in a comprehensive manner.
  600. for (size_t i = 0; test_rdata_list[i].rrclass != NULL; ++i) {
  601. SCOPED_TRACE(string(test_rdata_list[i].rrclass) + "/" +
  602. test_rdata_list[i].rrtype);
  603. const RRClass rrclass(test_rdata_list[i].rrclass);
  604. const RRType rrtype(test_rdata_list[i].rrtype);
  605. const ConstRdataPtr rdata = createRdata(rrtype, rrclass,
  606. test_rdata_list[i].rdata);
  607. rdata_list_.clear();
  608. rdata_list_.push_back(rdata);
  609. checkEncode(rrclass, rrtype, rdata_list_,
  610. test_rdata_list[i].n_varlen_fields, rrsigs);
  611. }
  612. }
  613. TYPED_TEST(RdataEncodeDecodeTest, addRdata) {
  614. vector<ConstRdataPtr> rrsigs;
  615. this->addRdataCommon(rrsigs); // basic tests without RRSIGs (empty vector)
  616. // Test with RRSIGs (covered type doesn't always match, but the encoder
  617. // doesn't check that)
  618. rrsigs.push_back(this->rrsig_rdata_);
  619. this->addRdataCommon(rrsigs);
  620. }
  621. template<class DecoderStyle>
  622. void
  623. RdataEncodeDecodeTest<DecoderStyle>::
  624. addRdataMultiCommon(const vector<ConstRdataPtr>& rrsigs, bool duplicate) {
  625. // Similar to addRdata(), but test with multiple RDATAs.
  626. // Four different cases are tested: a single fixed-len RDATA (A),
  627. // fixed-len data + domain name (MX), variable-len data only (TXT),
  628. // variable-len data + domain name (NAPTR).
  629. ConstRdataPtr a_rdata2 = createRdata(RRType::A(), RRClass::IN(),
  630. "192.0.2.54");
  631. rdata_list_.clear();
  632. rdata_list_.push_back(a_rdata_);
  633. rdata_list_.push_back(a_rdata2);
  634. if (duplicate) { // if duplicate is true, add duplicate Rdata
  635. rdata_list_.push_back(a_rdata_);
  636. }
  637. checkEncode(RRClass::IN(), RRType::A(), rdata_list_, 0, rrsigs);
  638. ConstRdataPtr mx_rdata1 = createRdata(RRType::MX(), RRClass::IN(),
  639. "5 mx1.example.com.");
  640. ConstRdataPtr mx_rdata2 = createRdata(RRType::MX(), RRClass::IN(),
  641. "10 mx2.example.com.");
  642. if (duplicate) { // check duplicate detection is case insensitive for names
  643. rdata_list_.push_back(createRdata(RRType::MX(), RRClass::IN(),
  644. "5 MX1.example.COM."));
  645. }
  646. rdata_list_.clear();
  647. rdata_list_.push_back(mx_rdata1);
  648. rdata_list_.push_back(mx_rdata2);
  649. checkEncode(RRClass::IN(), RRType::MX(), rdata_list_, 0, rrsigs);
  650. ConstRdataPtr txt_rdata1 = createRdata(RRType::TXT(), RRClass::IN(),
  651. "foo bar baz");
  652. ConstRdataPtr txt_rdata2 = createRdata(RRType::TXT(), RRClass::IN(),
  653. "another text data");
  654. if (duplicate) {
  655. rdata_list_.push_back(txt_rdata1);
  656. }
  657. rdata_list_.clear();
  658. rdata_list_.push_back(txt_rdata1);
  659. rdata_list_.push_back(txt_rdata2);
  660. checkEncode(RRClass::IN(), RRType::TXT(), rdata_list_, 1, rrsigs);
  661. ConstRdataPtr naptr_rdata1 =
  662. createRdata(RRType::NAPTR(), RRClass::IN(),
  663. "100 50 \"s\" \"http\" \"\" _http._tcp.example.com");
  664. ConstRdataPtr naptr_rdata2 =
  665. createRdata(RRType::NAPTR(), RRClass::IN(),
  666. "200 100 \"s\" \"http\" \"\" _http._tcp.example.com");
  667. if (duplicate) {
  668. rdata_list_.push_back(naptr_rdata1);
  669. }
  670. rdata_list_.clear();
  671. rdata_list_.push_back(naptr_rdata1);
  672. rdata_list_.push_back(naptr_rdata2);
  673. checkEncode(RRClass::IN(), RRType::NAPTR(), rdata_list_, 1, rrsigs);
  674. }
  675. void ignoreName(const LabelSequence&, unsigned) {
  676. }
  677. void
  678. checkLargeData(const in::DHCID* decoded, bool* called, const void* encoded,
  679. size_t length)
  680. {
  681. EXPECT_FALSE(*called); // Called exactly once
  682. *called = true;
  683. // Reconstruct the Rdata and check it.
  684. isc::util::InputBuffer ib(encoded, length);
  685. const in::DHCID reconstructed(ib, ib.getLength());
  686. EXPECT_EQ(0, reconstructed.compare(*decoded));
  687. }
  688. TEST_F(RdataSerializationTest, encodeLargeRdata) {
  689. // There should be no reason for a large RDATA to fail in encoding,
  690. // but we check such a case explicitly.
  691. encoded_data_.resize(65535); // max unsigned 16-bit int
  692. isc::util::InputBuffer buffer(&encoded_data_[0], encoded_data_.size());
  693. const in::DHCID large_dhcid(buffer, encoded_data_.size());
  694. encoder_.start(RRClass::IN(), RRType::DHCID());
  695. encoder_.addRdata(large_dhcid);
  696. encodeWrapper(encoder_.getStorageLength());
  697. // The encoded data should be identical to the original one.
  698. bool called = false;
  699. RdataReader reader(RRClass::IN(), RRType::DHCID(), &encoded_data_[0], 1, 0,
  700. ignoreName, boost::bind(checkLargeData, &large_dhcid,
  701. &called, _1, _2));
  702. reader.iterate();
  703. EXPECT_TRUE(called);
  704. called = false;
  705. reader.iterateAllSigs();
  706. EXPECT_FALSE(called);
  707. }
  708. TYPED_TEST(RdataEncodeDecodeTest, addRdataMulti) {
  709. vector<ConstRdataPtr> rrsigs;
  710. this->addRdataMultiCommon(rrsigs); // test without RRSIGs (empty vector)
  711. this->addRdataMultiCommon(rrsigs, true); // ditto, but with duplicated data
  712. // Tests with two RRSIGs
  713. rrsigs.push_back(this->rrsig_rdata_);
  714. rrsigs.push_back(createRdata(RRType::RRSIG(), RRClass::IN(),
  715. "A 5 2 3600 20120814220826 "
  716. "20120715220826 54321 com. FAKE"));
  717. this->addRdataMultiCommon(rrsigs);
  718. // Similar to the previous, but with duplicate RRSIG.
  719. rrsigs.push_back(this->rrsig_rdata_);
  720. this->addRdataMultiCommon(rrsigs, true);
  721. }
  722. TEST_F(RdataSerializationTest, badAddRdata) {
  723. // Some operations must follow start().
  724. EXPECT_THROW(encoder_.addRdata(*a_rdata_), isc::InvalidOperation);
  725. EXPECT_THROW(encoder_.getStorageLength(), isc::InvalidOperation);
  726. // will allocate space of some arbitrary size (256 bytes)
  727. EXPECT_THROW(encodeWrapper(256), isc::InvalidOperation);
  728. // Bad buffer for encode
  729. encoder_.start(RRClass::IN(), RRType::A());
  730. encoder_.addRdata(*a_rdata_);
  731. const size_t buf_len = encoder_.getStorageLength();
  732. // NULL buffer for encode
  733. EXPECT_THROW(encoder_.encode(NULL, buf_len), isc::BadValue);
  734. // buffer length is too short (we don't use the wrraper because we don't
  735. // like to tweak the length arg to encode()).
  736. encoded_data_.resize(buf_len - 1);
  737. EXPECT_THROW(encoder_.encode(&encoded_data_[0], buf_len - 1),
  738. isc::BadValue);
  739. // Some of the following checks confirm that adding an Rdata of the
  740. // wrong RR type will be rejected. Several different cases are checked,
  741. // but there shouldn't be any essential difference among these cases in
  742. // the tested code; these cases were considered because in an older version
  743. // of implementation rejected them for possibly different reasons, and
  744. // we simply keep these cases as they are not so many (and may help detect
  745. // future possible regression).
  746. encoder_.start(RRClass::IN(), RRType::AAAA());
  747. EXPECT_THROW(encoder_.addRdata(*a_rdata_), std::bad_cast);
  748. encoder_.start(RRClass::IN(), RRType::A());
  749. EXPECT_THROW(encoder_.addRdata(*aaaa_rdata_), std::bad_cast);
  750. const ConstRdataPtr rp_rdata =
  751. createRdata(RRType::RP(), RRClass::IN(), "a.example. b.example");
  752. encoder_.start(RRClass::IN(), RRType::NS());
  753. EXPECT_THROW(encoder_.addRdata(*rp_rdata), std::bad_cast);
  754. encoder_.start(RRClass::IN(), RRType::DHCID());
  755. EXPECT_THROW(encoder_.addRdata(*rp_rdata), std::bad_cast);
  756. const ConstRdataPtr txt_rdata = createRdata(RRType::TXT(), RRClass::IN(),
  757. "a");
  758. encoder_.start(RRClass::IN(), RRType::MX());
  759. EXPECT_THROW(encoder_.addRdata(*txt_rdata), std::bad_cast);
  760. encoder_.start(RRClass::IN(), RRType::NS());
  761. EXPECT_THROW(encoder_.addRdata(*txt_rdata), std::bad_cast);
  762. const ConstRdataPtr ns_rdata =
  763. createRdata(RRType::NS(), RRClass::IN(), "ns.example.");
  764. encoder_.start(RRClass::IN(), RRType::DNAME());
  765. EXPECT_THROW(encoder_.addRdata(*ns_rdata), std::bad_cast);
  766. const ConstRdataPtr dname_rdata =
  767. createRdata(RRType::DNAME(), RRClass::IN(), "dname.example.");
  768. encoder_.start(RRClass::IN(), RRType::NS());
  769. EXPECT_THROW(encoder_.addRdata(*dname_rdata), std::bad_cast);
  770. // RDATA len exceeds the 16-bit range. Technically not invalid, but
  771. // we don't support that (and it's practically useless anyway).
  772. encoded_data_.resize(65536); // use encoded_data_ for placeholder
  773. isc::util::InputBuffer buffer(&encoded_data_[0], encoded_data_.size());
  774. encoder_.start(RRClass::IN(), RRType::DHCID());
  775. EXPECT_THROW(encoder_.addRdata(in::DHCID(buffer, encoded_data_.size())),
  776. RdataEncodingError);
  777. // RRSIG cannot be used as the main RDATA type (can only be added as
  778. // a signature for some other type of RDATAs).
  779. EXPECT_THROW(encoder_.start(RRClass::IN(), RRType::RRSIG()),
  780. isc::BadValue);
  781. }
  782. struct MergeTestData {
  783. const char* const type_txt; // "AAAA", "NS", etc
  784. const char* const rdata_txt1;
  785. const char* const rdata_txt2;
  786. const char* const rdata_txt3;
  787. const size_t varlen_fields; // number of variable-len fields in RDATA
  788. } merge_test_data[] = {
  789. // For test with fixed-length RDATA
  790. {"A", "192.0.2.53", "192.0.2.54", "192.0.2.55", 0},
  791. // For test with variable-length RDATA
  792. {"TXT", "foo bar baz", "another text data", "yet another", 1},
  793. // For test with RDATA containing domain name
  794. {"MX", "5 mx1.example.com.", "10 mx2.example.com.", "20 mx.example.", 0},
  795. {NULL, NULL, NULL, NULL, 0}
  796. };
  797. // Identifier for slightly difference modes of "merge data" test below.
  798. // We test various combinations on # of old (before merge) and new (being
  799. // merged) RDATAs.
  800. enum MergeTestMode {
  801. ONE_OLD_ONE_NEW = 0,
  802. MULTI_OLD_NO_NEW,
  803. ONE_OLD_MULTI_NEW,
  804. DUPLICATE_NEW, // The new RDATA is a duplicate of the old one
  805. NO_OLD_ONE_NEW, // no old RDATA; this can also cover the case where
  806. // the resulting RdataSet is RRSIG-only.
  807. ONE_OLD_NO_NEW
  808. };
  809. // A helper to build vectors of Rata's for the given test mode.
  810. void
  811. createMergeData(int mode, const MergeTestData& data,
  812. const RRClass& rrclass, const RRType& rrtype,
  813. vector<ConstRdataPtr>& old_list,
  814. vector<ConstRdataPtr>& new_list)
  815. {
  816. old_list.clear();
  817. new_list.clear();
  818. old_list.push_back(createRdata(rrtype, rrclass, data.rdata_txt1));
  819. new_list.push_back(createRdata(rrtype, rrclass, data.rdata_txt2));
  820. switch (static_cast<MergeTestMode>(mode)) {
  821. case ONE_OLD_ONE_NEW:
  822. break;
  823. case MULTI_OLD_NO_NEW:
  824. old_list.push_back(createRdata(rrtype, rrclass, data.rdata_txt3));
  825. break;
  826. case ONE_OLD_MULTI_NEW:
  827. new_list.push_back(createRdata(rrtype, rrclass, data.rdata_txt3));
  828. break;
  829. case DUPLICATE_NEW:
  830. new_list.push_back(createRdata(rrtype, rrclass, data.rdata_txt1));
  831. break;
  832. case NO_OLD_ONE_NEW:
  833. old_list.clear();
  834. break;
  835. case ONE_OLD_NO_NEW:
  836. new_list.clear();
  837. break;
  838. }
  839. }
  840. template<class DecoderStyle>
  841. void
  842. RdataEncodeDecodeTest<DecoderStyle>::
  843. mergeRdataCommon(const vector<ConstRdataPtr>& old_rrsigs,
  844. const vector<ConstRdataPtr>& rrsigs)
  845. {
  846. const RRClass rrclass(RRClass::IN()); // class is fixed in the test
  847. vector<uint8_t> old_encoded_data;
  848. vector<ConstRdataPtr> rrsigs_all;
  849. vector<ConstRdataPtr> old_list;
  850. vector<ConstRdataPtr> new_list;
  851. // For each type of test Rdata, we check all modes of test scenarios.
  852. for (const MergeTestData* data = merge_test_data;
  853. data->type_txt;
  854. ++data) {
  855. const RRType rrtype(data->type_txt);
  856. for (int mode = 0; mode <= ONE_OLD_NO_NEW; ++mode) {
  857. createMergeData(mode, *data, rrclass, rrtype, old_list, new_list);
  858. // Encode the old data
  859. rdata_list_ = old_list;
  860. checkEncode(rrclass, RRType(data->type_txt), rdata_list_,
  861. data->varlen_fields, old_rrsigs);
  862. old_encoded_data = encoded_data_; // make a copy of the data
  863. // Prepare new data. rrsigs_all is set to "old_rrsigs + rrsigs".
  864. // Then check the behavior in the "merge" mode.
  865. const size_t old_rdata_count = rdata_list_.size();
  866. rdata_list_.insert(rdata_list_.end(), new_list.begin(),
  867. new_list.end());
  868. rrsigs_all = old_rrsigs;
  869. rrsigs_all.insert(rrsigs_all.end(), rrsigs.begin(), rrsigs.end());
  870. checkEncode(rrclass, rrtype, rdata_list_, data->varlen_fields,
  871. rrsigs_all, &old_encoded_data[0], old_rdata_count,
  872. old_rrsigs.size());
  873. }
  874. }
  875. }
  876. TYPED_TEST(RdataEncodeDecodeTest, mergeRdata) {
  877. vector<ConstRdataPtr> old_rrsigs;
  878. vector<ConstRdataPtr> rrsigs;
  879. // Test without RRSIGs, either for old or new.
  880. this->mergeRdataCommon(old_rrsigs, rrsigs);
  881. // Test without RRSIG for old and with RRSIG for new.
  882. rrsigs.push_back(this->rrsig_rdata_);
  883. this->mergeRdataCommon(old_rrsigs, rrsigs);
  884. // Test with RRSIG for old and without RRSIG for new.
  885. rrsigs.clear();
  886. old_rrsigs.push_back(this->rrsig_rdata_);
  887. this->mergeRdataCommon(old_rrsigs, rrsigs);
  888. // Tests with RRSIGs for both old and new.
  889. old_rrsigs.clear();
  890. rrsigs.push_back(createRdata(RRType::RRSIG(), RRClass::IN(),
  891. "A 5 2 3600 20120814220826 "
  892. "20120715220826 54321 com. FAKE"));
  893. this->mergeRdataCommon(old_rrsigs, rrsigs);
  894. // Tests with multiple old RRSIGs.
  895. rrsigs.clear();
  896. old_rrsigs.clear();
  897. old_rrsigs.push_back(this->rrsig_rdata_);
  898. old_rrsigs.push_back(createRdata(RRType::RRSIG(), RRClass::IN(),
  899. "A 5 2 3600 20120814220826 "
  900. "20120715220826 54321 com. FAKE"));
  901. this->mergeRdataCommon(old_rrsigs, rrsigs);
  902. // Tests with duplicate RRSIG in new one (keeping the old_rrsigs)
  903. rrsigs.push_back(this->rrsig_rdata_);
  904. this->mergeRdataCommon(old_rrsigs, rrsigs);
  905. }
  906. void
  907. checkSigData(const ConstRdataPtr& decoded, bool* called, const void* encoded,
  908. size_t length)
  909. {
  910. EXPECT_FALSE(*called); // Called exactly once
  911. *called = true;
  912. // Reconstruct the RRSig and check it.
  913. isc::util::InputBuffer ib(encoded, length);
  914. const generic::RRSIG reconstructed(ib, ib.getLength());
  915. EXPECT_EQ(0, reconstructed.compare(*decoded));
  916. }
  917. TEST_F(RdataSerializationTest, addSIGRdataOnly) {
  918. // Encoded data that only contain RRSIGs. Mostly useless, but can happen
  919. // (in a partially broken zone) and it's accepted.
  920. encoder_.start(RRClass::IN(), RRType::A());
  921. encoder_.addSIGRdata(*rrsig_rdata_);
  922. encodeWrapper(encoder_.getStorageLength());
  923. ASSERT_LT(sizeof(uint16_t), encoder_.getStorageLength());
  924. bool called = false;
  925. RdataReader reader(RRClass::IN(), RRType::A(), &encoded_data_[0], 0, 1,
  926. ignoreName, boost::bind(checkSigData, rrsig_rdata_,
  927. &called, _1, _2));
  928. reader.iterate();
  929. EXPECT_FALSE(called);
  930. reader.iterateAllSigs();
  931. EXPECT_TRUE(called);
  932. }
  933. TEST_F(RdataSerializationTest, badAddSIGRdata) {
  934. // try adding SIG before start
  935. EXPECT_THROW(encoder_.addSIGRdata(*rrsig_rdata_), isc::InvalidOperation);
  936. // Very big RRSIG. This implementation rejects it.
  937. isc::util::OutputBuffer ob(0);
  938. rrsig_rdata_->toWire(ob);
  939. // append dummy trailing signature to make it too big
  940. vector<uint8_t> dummy_sig(65536 - ob.getLength());
  941. ob.writeData(&dummy_sig[0], dummy_sig.size());
  942. ASSERT_EQ(65536, ob.getLength());
  943. isc::util::InputBuffer ib(ob.getData(), ob.getLength());
  944. const generic::RRSIG big_sigrdata(ib, ob.getLength());
  945. encoder_.start(RRClass::IN(), RRType::A());
  946. EXPECT_THROW(encoder_.addSIGRdata(big_sigrdata), RdataEncodingError);
  947. }
  948. }