labelsequence_unittest.cc 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 <dns/labelsequence.h>
  15. #include <dns/name.h>
  16. #include <exceptions/exceptions.h>
  17. #include <gtest/gtest.h>
  18. #include <boost/functional/hash.hpp>
  19. #include <string>
  20. #include <vector>
  21. #include <utility>
  22. #include <set>
  23. using namespace isc::dns;
  24. using namespace std;
  25. // XXX: this is defined as class static constants, but some compilers
  26. // seemingly cannot find the symbols when used in the EXPECT_xxx macros.
  27. const size_t LabelSequence::MAX_SERIALIZED_LENGTH;
  28. namespace {
  29. // Common check that two labelsequences are equal
  30. void check_equal(const LabelSequence& ls1, const LabelSequence& ls2) {
  31. NameComparisonResult result = ls1.compare(ls2);
  32. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  33. result.getRelation()) << ls1.toText() << " != " << ls2.toText();
  34. EXPECT_EQ(0, result.getOrder()) << ls1.toText() << " != " << ls2.toText();
  35. EXPECT_EQ(ls1.getLabelCount(), result.getCommonLabels());
  36. }
  37. // Common check for general comparison of two labelsequences
  38. void check_compare(const LabelSequence& ls1, const LabelSequence& ls2,
  39. isc::dns::NameComparisonResult::NameRelation relation,
  40. size_t common_labels, bool check_order, int order=0) {
  41. NameComparisonResult result = ls1.compare(ls2);
  42. EXPECT_EQ(relation, result.getRelation());
  43. EXPECT_EQ(common_labels, result.getCommonLabels());
  44. if (check_order) {
  45. EXPECT_EQ(order, result.getOrder());
  46. }
  47. }
  48. class LabelSequenceTest : public ::testing::Test {
  49. public:
  50. LabelSequenceTest() : n1("example.org"), n2("example.com"),
  51. n3("example.org"), n4("foo.bar.test.example"),
  52. n5("example.ORG"), n6("ExAmPlE.org"),
  53. n7("."), n8("foo.example.org.bar"),
  54. n9("\\000xample.org"),
  55. n10("\\000xample.org"),
  56. n11("\\000xample.com"),
  57. n12("\\000xamplE.com"),
  58. n_maxlabel("0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  59. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  60. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  61. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  62. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  63. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  64. "0.1.2.3.4.5.6"),
  65. ls1(n1), ls2(n2), ls3(n3), ls4(n4), ls5(n5),
  66. ls6(n6), ls7(n7), ls8(n8),
  67. ls9(n9), ls10(n10), ls11(n11), ls12(n12)
  68. {};
  69. // Need to keep names in scope for at least the lifetime of
  70. // the labelsequences
  71. Name n1, n2, n3, n4, n5, n6, n7, n8;
  72. Name n9, n10, n11, n12;
  73. const Name n_maxlabel;
  74. LabelSequence ls1, ls2, ls3, ls4, ls5, ls6, ls7, ls8;
  75. LabelSequence ls9, ls10, ls11, ls12;
  76. };
  77. // Basic equality tests
  78. TEST_F(LabelSequenceTest, equals_sensitive) {
  79. EXPECT_TRUE(ls1.equals(ls1, true));
  80. EXPECT_FALSE(ls1.equals(ls2, true));
  81. EXPECT_TRUE(ls1.equals(ls3, true));
  82. EXPECT_FALSE(ls1.equals(ls4, true));
  83. EXPECT_FALSE(ls1.equals(ls5, true));
  84. EXPECT_FALSE(ls1.equals(ls6, true));
  85. EXPECT_FALSE(ls1.equals(ls7, true));
  86. EXPECT_FALSE(ls1.equals(ls8, true));
  87. EXPECT_FALSE(ls2.equals(ls1, true));
  88. EXPECT_TRUE(ls2.equals(ls2, true));
  89. EXPECT_FALSE(ls2.equals(ls3, true));
  90. EXPECT_FALSE(ls2.equals(ls4, true));
  91. EXPECT_FALSE(ls2.equals(ls5, true));
  92. EXPECT_FALSE(ls2.equals(ls6, true));
  93. EXPECT_FALSE(ls2.equals(ls7, true));
  94. EXPECT_FALSE(ls2.equals(ls8, true));
  95. EXPECT_FALSE(ls4.equals(ls1, true));
  96. EXPECT_FALSE(ls4.equals(ls2, true));
  97. EXPECT_FALSE(ls4.equals(ls3, true));
  98. EXPECT_TRUE(ls4.equals(ls4, true));
  99. EXPECT_FALSE(ls4.equals(ls5, true));
  100. EXPECT_FALSE(ls4.equals(ls6, true));
  101. EXPECT_FALSE(ls4.equals(ls7, true));
  102. EXPECT_FALSE(ls4.equals(ls8, true));
  103. EXPECT_FALSE(ls5.equals(ls1, true));
  104. EXPECT_FALSE(ls5.equals(ls2, true));
  105. EXPECT_FALSE(ls5.equals(ls3, true));
  106. EXPECT_FALSE(ls5.equals(ls4, true));
  107. EXPECT_TRUE(ls5.equals(ls5, true));
  108. EXPECT_FALSE(ls5.equals(ls6, true));
  109. EXPECT_FALSE(ls5.equals(ls7, true));
  110. EXPECT_FALSE(ls5.equals(ls8, true));
  111. EXPECT_TRUE(ls9.equals(ls10, true));
  112. EXPECT_FALSE(ls9.equals(ls11, true));
  113. EXPECT_FALSE(ls9.equals(ls12, true));
  114. EXPECT_FALSE(ls11.equals(ls12, true));
  115. }
  116. TEST_F(LabelSequenceTest, equals_insensitive) {
  117. EXPECT_TRUE(ls1.equals(ls1));
  118. EXPECT_FALSE(ls1.equals(ls2));
  119. EXPECT_TRUE(ls1.equals(ls3));
  120. EXPECT_FALSE(ls1.equals(ls4));
  121. EXPECT_TRUE(ls1.equals(ls5));
  122. EXPECT_TRUE(ls1.equals(ls6));
  123. EXPECT_FALSE(ls1.equals(ls7));
  124. EXPECT_FALSE(ls2.equals(ls1));
  125. EXPECT_TRUE(ls2.equals(ls2));
  126. EXPECT_FALSE(ls2.equals(ls3));
  127. EXPECT_FALSE(ls2.equals(ls4));
  128. EXPECT_FALSE(ls2.equals(ls5));
  129. EXPECT_FALSE(ls2.equals(ls6));
  130. EXPECT_FALSE(ls2.equals(ls7));
  131. EXPECT_TRUE(ls3.equals(ls1));
  132. EXPECT_FALSE(ls3.equals(ls2));
  133. EXPECT_TRUE(ls3.equals(ls3));
  134. EXPECT_FALSE(ls3.equals(ls4));
  135. EXPECT_TRUE(ls3.equals(ls5));
  136. EXPECT_TRUE(ls3.equals(ls6));
  137. EXPECT_FALSE(ls3.equals(ls7));
  138. EXPECT_FALSE(ls4.equals(ls1));
  139. EXPECT_FALSE(ls4.equals(ls2));
  140. EXPECT_FALSE(ls4.equals(ls3));
  141. EXPECT_TRUE(ls4.equals(ls4));
  142. EXPECT_FALSE(ls4.equals(ls5));
  143. EXPECT_FALSE(ls4.equals(ls6));
  144. EXPECT_FALSE(ls4.equals(ls7));
  145. EXPECT_TRUE(ls5.equals(ls1));
  146. EXPECT_FALSE(ls5.equals(ls2));
  147. EXPECT_TRUE(ls5.equals(ls3));
  148. EXPECT_FALSE(ls5.equals(ls4));
  149. EXPECT_TRUE(ls5.equals(ls5));
  150. EXPECT_TRUE(ls5.equals(ls6));
  151. EXPECT_FALSE(ls5.equals(ls7));
  152. EXPECT_TRUE(ls9.equals(ls10));
  153. EXPECT_FALSE(ls9.equals(ls11));
  154. EXPECT_FALSE(ls9.equals(ls12));
  155. EXPECT_TRUE(ls11.equals(ls12));
  156. }
  157. // operator==(). This is mostly trivial wrapper, so it should suffice to
  158. // check some basic cases.
  159. TEST_F(LabelSequenceTest, operatorEqual) {
  160. EXPECT_TRUE(ls1 == ls1); // self equivalence
  161. EXPECT_TRUE(ls1 == LabelSequence(n1)); // equivalent two different objects
  162. EXPECT_FALSE(ls1 == ls2); // non equivalent objects
  163. EXPECT_TRUE(ls1 == ls5); // it's always case insensitive
  164. }
  165. // Compare tests
  166. TEST_F(LabelSequenceTest, compare) {
  167. // "example.org." and "example.org.", case sensitive
  168. NameComparisonResult result = ls1.compare(ls3, true);
  169. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  170. result.getRelation());
  171. EXPECT_EQ(0, result.getOrder());
  172. EXPECT_EQ(3, result.getCommonLabels());
  173. // "example.org." and "example.ORG.", case sensitive
  174. result = ls3.compare(ls5, true);
  175. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  176. result.getRelation());
  177. EXPECT_LT(0, result.getOrder());
  178. EXPECT_EQ(1, result.getCommonLabels());
  179. // "example.org." and "example.ORG.", case in-sensitive
  180. result = ls3.compare(ls5);
  181. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  182. result.getRelation());
  183. EXPECT_EQ(0, result.getOrder());
  184. EXPECT_EQ(3, result.getCommonLabels());
  185. Name na("a.example.org");
  186. Name nb("b.example.org");
  187. LabelSequence lsa(na);
  188. LabelSequence lsb(nb);
  189. // "a.example.org." and "b.example.org.", case in-sensitive
  190. result = lsa.compare(lsb);
  191. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  192. result.getRelation());
  193. EXPECT_GT(0, result.getOrder());
  194. EXPECT_EQ(3, result.getCommonLabels());
  195. // "example.org." and "b.example.org.", case in-sensitive
  196. lsa.stripLeft(1);
  197. result = lsa.compare(lsb);
  198. EXPECT_EQ(isc::dns::NameComparisonResult::SUPERDOMAIN,
  199. result.getRelation());
  200. EXPECT_GT(0, result.getOrder());
  201. EXPECT_EQ(3, result.getCommonLabels());
  202. Name nc("g.f.e.d.c.example.org");
  203. LabelSequence lsc(nc);
  204. // "g.f.e.d.c.example.org." and "b.example.org" (not absolute), case
  205. // in-sensitive; the absolute one is always smaller.
  206. lsb.stripRight(1);
  207. result = lsc.compare(lsb);
  208. EXPECT_EQ(isc::dns::NameComparisonResult::NONE, result.getRelation());
  209. EXPECT_GT(0, result.getOrder());
  210. EXPECT_EQ(0, result.getCommonLabels());
  211. // "g.f.e.d.c.example.org." and "example.org.", case in-sensitive
  212. result = lsc.compare(ls1);
  213. EXPECT_EQ(isc::dns::NameComparisonResult::SUBDOMAIN,
  214. result.getRelation());
  215. EXPECT_LT(0, result.getOrder());
  216. EXPECT_EQ(3, result.getCommonLabels());
  217. // "e.d.c.example.org." and "example.org.", case in-sensitive
  218. lsc.stripLeft(2);
  219. result = lsc.compare(ls1);
  220. EXPECT_EQ(isc::dns::NameComparisonResult::SUBDOMAIN,
  221. result.getRelation());
  222. EXPECT_LT(0, result.getOrder());
  223. EXPECT_EQ(3, result.getCommonLabels());
  224. // "example.org." and "example.org.", case in-sensitive
  225. lsc.stripLeft(3);
  226. result = lsc.compare(ls1);
  227. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  228. result.getRelation());
  229. EXPECT_EQ(0, result.getOrder());
  230. EXPECT_EQ(3, result.getCommonLabels());
  231. // "." and "example.org.", case in-sensitive
  232. lsc.stripLeft(2);
  233. result = lsc.compare(ls1);
  234. EXPECT_EQ(isc::dns::NameComparisonResult::SUPERDOMAIN,
  235. result.getRelation());
  236. EXPECT_GT(0, result.getOrder());
  237. EXPECT_EQ(1, result.getCommonLabels());
  238. Name nd("a.b.c.isc.example.org");
  239. LabelSequence lsd(nd);
  240. Name ne("w.x.y.isc.EXAMPLE.org");
  241. LabelSequence lse(ne);
  242. // "a.b.c.isc.example.org." and "w.x.y.isc.EXAMPLE.org.",
  243. // case sensitive
  244. result = lsd.compare(lse, true);
  245. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  246. result.getRelation());
  247. EXPECT_LT(0, result.getOrder());
  248. EXPECT_EQ(2, result.getCommonLabels());
  249. // "a.b.c.isc.example.org." and "w.x.y.isc.EXAMPLE.org.",
  250. // case in-sensitive
  251. result = lsd.compare(lse);
  252. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  253. result.getRelation());
  254. EXPECT_GT(0, result.getOrder());
  255. EXPECT_EQ(4, result.getCommonLabels());
  256. // "isc.example.org." and "isc.EXAMPLE.org.", case sensitive
  257. lsd.stripLeft(3);
  258. lse.stripLeft(3);
  259. result = lsd.compare(lse, true);
  260. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  261. result.getRelation());
  262. EXPECT_LT(0, result.getOrder());
  263. EXPECT_EQ(2, result.getCommonLabels());
  264. // "isc.example.org." and "isc.EXAMPLE.org.", case in-sensitive
  265. result = lsd.compare(lse);
  266. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  267. result.getRelation());
  268. EXPECT_EQ(0, result.getOrder());
  269. EXPECT_EQ(4, result.getCommonLabels());
  270. Name nf("a.b.c.isc.example.org");
  271. LabelSequence lsf(nf);
  272. Name ng("w.x.y.isc.EXAMPLE.org");
  273. LabelSequence lsg(ng);
  274. // lsf: "a.b.c.isc.example.org."
  275. // lsg: "w.x.y.isc.EXAMPLE.org" (not absolute), case in-sensitive.
  276. // the absolute one is always smaller.
  277. lsg.stripRight(1);
  278. result = lsg.compare(lsf); // lsg > lsf
  279. EXPECT_EQ(isc::dns::NameComparisonResult::NONE, result.getRelation());
  280. EXPECT_LT(0, result.getOrder());
  281. EXPECT_EQ(0, result.getCommonLabels());
  282. // "a.b.c.isc.example.org" (not absolute) and
  283. // "w.x.y.isc.EXAMPLE.org" (not absolute), case in-sensitive
  284. lsf.stripRight(1);
  285. result = lsg.compare(lsf);
  286. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  287. result.getRelation());
  288. EXPECT_LT(0, result.getOrder());
  289. EXPECT_EQ(3, result.getCommonLabels());
  290. // "a.b.c.isc.example" (not absolute) and
  291. // "w.x.y.isc.EXAMPLE" (not absolute), case in-sensitive
  292. lsf.stripRight(1);
  293. lsg.stripRight(1);
  294. result = lsg.compare(lsf);
  295. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  296. result.getRelation());
  297. EXPECT_LT(0, result.getOrder());
  298. EXPECT_EQ(2, result.getCommonLabels());
  299. // lsf: "a.b.c" (not absolute) and
  300. // lsg: "w.x.y" (not absolute), case in-sensitive; a.b.c < w.x.y;
  301. // no common labels.
  302. lsf.stripRight(2);
  303. lsg.stripRight(2);
  304. result = lsf.compare(lsg);
  305. EXPECT_EQ(isc::dns::NameComparisonResult::NONE, result.getRelation());
  306. EXPECT_GT(0, result.getOrder());
  307. EXPECT_EQ(0, result.getCommonLabels());
  308. // lsf2: a.b.cc (not absolute); a.b.c < a.b.cc, no common labels.
  309. const Name nf2("a.b.cc");
  310. LabelSequence lsf2(nf2);
  311. lsf2.stripRight(1);
  312. result = lsf.compare(lsf2);
  313. EXPECT_EQ(isc::dns::NameComparisonResult::NONE, result.getRelation());
  314. EXPECT_GT(0, result.getOrder());
  315. EXPECT_EQ(0, result.getCommonLabels());
  316. Name nh("aexample.org");
  317. LabelSequence lsh(nh);
  318. Name ni("bexample.org");
  319. LabelSequence lsi(ni);
  320. // "aexample.org" (not absolute) and
  321. // "bexample.org" (not absolute), case in-sensitive
  322. lsh.stripRight(1);
  323. lsi.stripRight(1);
  324. result = lsh.compare(lsi);
  325. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  326. result.getRelation());
  327. EXPECT_GT(0, result.getOrder());
  328. EXPECT_EQ(1, result.getCommonLabels());
  329. // "aexample" (not absolute) and
  330. // "bexample" (not absolute), case in-sensitive;
  331. // aexample < bexample; no common labels.
  332. lsh.stripRight(1);
  333. lsi.stripRight(1);
  334. result = lsh.compare(lsi);
  335. EXPECT_EQ(isc::dns::NameComparisonResult::NONE, result.getRelation());
  336. EXPECT_GT(0, result.getOrder());
  337. EXPECT_EQ(0, result.getCommonLabels());
  338. Name nj("example.org");
  339. LabelSequence lsj(nj);
  340. Name nk("example.org");
  341. LabelSequence lsk(nk);
  342. // "example.org" (not absolute) and
  343. // "example.org" (not absolute), case in-sensitive
  344. lsj.stripRight(1);
  345. lsk.stripRight(1);
  346. result = lsj.compare(lsk);
  347. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  348. result.getRelation());
  349. EXPECT_EQ(0, result.getOrder());
  350. EXPECT_EQ(2, result.getCommonLabels());
  351. // "example" (not absolute) and
  352. // "example" (not absolute), case in-sensitive
  353. lsj.stripRight(1);
  354. lsk.stripRight(1);
  355. result = lsj.compare(lsk);
  356. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  357. result.getRelation());
  358. EXPECT_EQ(0, result.getOrder());
  359. EXPECT_EQ(1, result.getCommonLabels());
  360. }
  361. void
  362. getDataCheck(const uint8_t* expected_data, size_t expected_len,
  363. const LabelSequence& ls)
  364. {
  365. size_t len;
  366. const uint8_t* data = ls.getData(&len);
  367. ASSERT_EQ(expected_len, len) << "Expected data: " << expected_data <<
  368. ", label sequence: " << ls;
  369. EXPECT_EQ(expected_len, ls.getDataLength()) <<
  370. "Expected data: " << expected_data <<
  371. ", label sequence: " << ls;
  372. for (size_t i = 0; i < len; ++i) {
  373. EXPECT_EQ(expected_data[i], data[i]) <<
  374. "Difference at pos " << i << ": Expected data: " << expected_data <<
  375. ", label sequence: " << ls;
  376. }
  377. }
  378. // Convenient data converter for expected data. Label data must be of
  379. // uint8_t*, while it's convenient if we can specify some test data in
  380. // plain string (which is of char*). This wrapper converts the latter to
  381. // the former in a safer way.
  382. void
  383. getDataCheck(const char* expected_char_data, size_t expected_len,
  384. const LabelSequence& ls)
  385. {
  386. const vector<uint8_t> expected_data(expected_char_data,
  387. expected_char_data + expected_len);
  388. getDataCheck(&expected_data[0], expected_len, ls);
  389. }
  390. TEST_F(LabelSequenceTest, getData) {
  391. getDataCheck("\007example\003org\000", 13, ls1);
  392. getDataCheck("\007example\003com\000", 13, ls2);
  393. getDataCheck("\007example\003org\000", 13, ls3);
  394. getDataCheck("\003foo\003bar\004test\007example\000", 22, ls4);
  395. getDataCheck("\007example\003ORG\000", 13, ls5);
  396. getDataCheck("\007ExAmPlE\003org\000", 13, ls6);
  397. getDataCheck("\000", 1, ls7);
  398. };
  399. TEST_F(LabelSequenceTest, stripLeft) {
  400. EXPECT_TRUE(ls1.equals(ls3));
  401. ls1.stripLeft(0);
  402. getDataCheck("\007example\003org\000", 13, ls1);
  403. EXPECT_TRUE(ls1.equals(ls3));
  404. ls1.stripLeft(1);
  405. getDataCheck("\003org\000", 5, ls1);
  406. EXPECT_FALSE(ls1.equals(ls3));
  407. ls1.stripLeft(1);
  408. getDataCheck("\000", 1, ls1);
  409. EXPECT_TRUE(ls1.equals(ls7));
  410. ls2.stripLeft(2);
  411. getDataCheck("\000", 1, ls2);
  412. EXPECT_TRUE(ls2.equals(ls7));
  413. }
  414. TEST_F(LabelSequenceTest, stripRight) {
  415. EXPECT_TRUE(ls1.equals(ls3));
  416. ls1.stripRight(1);
  417. getDataCheck("\007example\003org", 12, ls1);
  418. EXPECT_FALSE(ls1.equals(ls3));
  419. ls1.stripRight(1);
  420. getDataCheck("\007example", 8, ls1);
  421. EXPECT_FALSE(ls1.equals(ls3));
  422. ASSERT_FALSE(ls1.equals(ls2));
  423. ls2.stripRight(2);
  424. getDataCheck("\007example", 8, ls2);
  425. EXPECT_TRUE(ls1.equals(ls2));
  426. }
  427. TEST_F(LabelSequenceTest, stripOutOfRange) {
  428. EXPECT_THROW(ls1.stripLeft(100), isc::OutOfRange);
  429. EXPECT_THROW(ls1.stripLeft(5), isc::OutOfRange);
  430. EXPECT_THROW(ls1.stripLeft(4), isc::OutOfRange);
  431. EXPECT_THROW(ls1.stripLeft(3), isc::OutOfRange);
  432. getDataCheck("\007example\003org\000", 13, ls1);
  433. EXPECT_THROW(ls1.stripRight(100), isc::OutOfRange);
  434. EXPECT_THROW(ls1.stripRight(5), isc::OutOfRange);
  435. EXPECT_THROW(ls1.stripRight(4), isc::OutOfRange);
  436. EXPECT_THROW(ls1.stripRight(3), isc::OutOfRange);
  437. getDataCheck("\007example\003org\000", 13, ls1);
  438. }
  439. TEST_F(LabelSequenceTest, getLabelCount) {
  440. EXPECT_EQ(3, ls1.getLabelCount());
  441. ls1.stripLeft(0);
  442. EXPECT_EQ(3, ls1.getLabelCount());
  443. ls1.stripLeft(1);
  444. EXPECT_EQ(2, ls1.getLabelCount());
  445. ls1.stripLeft(1);
  446. EXPECT_EQ(1, ls1.getLabelCount());
  447. EXPECT_EQ(3, ls2.getLabelCount());
  448. ls2.stripRight(1);
  449. EXPECT_EQ(2, ls2.getLabelCount());
  450. ls2.stripRight(1);
  451. EXPECT_EQ(1, ls2.getLabelCount());
  452. EXPECT_EQ(3, ls3.getLabelCount());
  453. ls3.stripRight(2);
  454. EXPECT_EQ(1, ls3.getLabelCount());
  455. EXPECT_EQ(5, ls4.getLabelCount());
  456. ls4.stripRight(3);
  457. EXPECT_EQ(2, ls4.getLabelCount());
  458. EXPECT_EQ(3, ls5.getLabelCount());
  459. ls5.stripLeft(2);
  460. EXPECT_EQ(1, ls5.getLabelCount());
  461. }
  462. TEST_F(LabelSequenceTest, comparePart) {
  463. EXPECT_FALSE(ls1.equals(ls8));
  464. // strip root label from example.org.
  465. ls1.stripRight(1);
  466. // strip foo from foo.example.org.bar.
  467. ls8.stripLeft(1);
  468. // strip bar. (i.e. bar and root) too
  469. ls8.stripRight(2);
  470. EXPECT_TRUE(ls1.equals(ls8));
  471. // Data comparison
  472. size_t len;
  473. const uint8_t* data = ls1.getData(&len);
  474. getDataCheck(data, len, ls8);
  475. }
  476. TEST_F(LabelSequenceTest, isAbsolute) {
  477. ASSERT_TRUE(ls1.isAbsolute());
  478. ls1.stripLeft(1);
  479. ASSERT_TRUE(ls1.isAbsolute());
  480. ls1.stripRight(1);
  481. ASSERT_FALSE(ls1.isAbsolute());
  482. ASSERT_TRUE(ls2.isAbsolute());
  483. ls2.stripRight(1);
  484. ASSERT_FALSE(ls2.isAbsolute());
  485. ASSERT_TRUE(ls3.isAbsolute());
  486. ls3.stripLeft(2);
  487. ASSERT_TRUE(ls3.isAbsolute());
  488. }
  489. TEST_F(LabelSequenceTest, toText) {
  490. EXPECT_EQ(".", ls7.toText());
  491. EXPECT_EQ("example.org.", ls1.toText());
  492. ls1.stripLeft(1);
  493. EXPECT_EQ("org.", ls1.toText());
  494. ls1.stripLeft(1);
  495. EXPECT_EQ(".", ls1.toText());
  496. EXPECT_EQ("example.com.", ls2.toText());
  497. ls2.stripRight(1);
  498. EXPECT_EQ("example.com", ls2.toText());
  499. ls2.stripRight(1);
  500. EXPECT_EQ("example", ls2.toText());
  501. EXPECT_EQ("foo.example.org.bar.", ls8.toText());
  502. ls8.stripRight(2);
  503. EXPECT_EQ("foo.example.org", ls8.toText());
  504. EXPECT_EQ(".", ls7.toText());
  505. EXPECT_THROW(ls7.stripLeft(1), isc::OutOfRange);
  506. Name n_long1("012345678901234567890123456789"
  507. "012345678901234567890123456789012."
  508. "012345678901234567890123456789"
  509. "012345678901234567890123456789012."
  510. "012345678901234567890123456789"
  511. "012345678901234567890123456789012."
  512. "012345678901234567890123456789"
  513. "0123456789012345678901234567890");
  514. LabelSequence ls_long1(n_long1);
  515. EXPECT_EQ("012345678901234567890123456789"
  516. "012345678901234567890123456789012."
  517. "012345678901234567890123456789"
  518. "012345678901234567890123456789012."
  519. "012345678901234567890123456789"
  520. "012345678901234567890123456789012."
  521. "012345678901234567890123456789"
  522. "0123456789012345678901234567890.", ls_long1.toText());
  523. ls_long1.stripRight(1);
  524. EXPECT_EQ("012345678901234567890123456789"
  525. "012345678901234567890123456789012."
  526. "012345678901234567890123456789"
  527. "012345678901234567890123456789012."
  528. "012345678901234567890123456789"
  529. "012345678901234567890123456789012."
  530. "012345678901234567890123456789"
  531. "0123456789012345678901234567890", ls_long1.toText());
  532. LabelSequence ls_long2(n_maxlabel);
  533. EXPECT_EQ("0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  534. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  535. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  536. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  537. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  538. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  539. "0.1.2.3.4.5.6.", ls_long2.toText());
  540. ls_long2.stripRight(1);
  541. EXPECT_EQ("0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  542. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  543. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  544. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  545. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  546. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9."
  547. "0.1.2.3.4.5.6", ls_long2.toText());
  548. ls_long2.stripRight(125);
  549. EXPECT_EQ("0.1", ls_long2.toText());
  550. }
  551. // The following are test data used in the getHash test below. Normally
  552. // we use example/documentation domain names for testing, but in this case
  553. // we'd specifically like to use more realistic data, and are intentionally
  554. // using real-world samples: They are the NS names of root and some top level
  555. // domains as of this test.
  556. const char* const root_servers[] = {
  557. "a.root-servers.net", "b.root-servers.net", "c.root-servers.net",
  558. "d.root-servers.net", "e.root-servers.net", "f.root-servers.net",
  559. "g.root-servers.net", "h.root-servers.net", "i.root-servers.net",
  560. "j.root-servers.net", "k.root-servers.net", "l.root-servers.net",
  561. "m.root-servers.net", NULL
  562. };
  563. const char* const gtld_servers[] = {
  564. "a.gtld-servers.net", "b.gtld-servers.net", "c.gtld-servers.net",
  565. "d.gtld-servers.net", "e.gtld-servers.net", "f.gtld-servers.net",
  566. "g.gtld-servers.net", "h.gtld-servers.net", "i.gtld-servers.net",
  567. "j.gtld-servers.net", "k.gtld-servers.net", "l.gtld-servers.net",
  568. "m.gtld-servers.net", NULL
  569. };
  570. const char* const jp_servers[] = {
  571. "a.dns.jp", "b.dns.jp", "c.dns.jp", "d.dns.jp", "e.dns.jp",
  572. "f.dns.jp", "g.dns.jp", NULL
  573. };
  574. const char* const cn_servers[] = {
  575. "a.dns.cn", "b.dns.cn", "c.dns.cn", "d.dns.cn", "e.dns.cn",
  576. "ns.cernet.net", NULL
  577. };
  578. const char* const ca_servers[] = {
  579. "k.ca-servers.ca", "e.ca-servers.ca", "a.ca-servers.ca", "z.ca-servers.ca",
  580. "tld.isc-sns.net", "c.ca-servers.ca", "j.ca-servers.ca", "l.ca-servers.ca",
  581. "sns-pb.isc.org", "f.ca-servers.ca", NULL
  582. };
  583. // A helper function used in the getHash test below.
  584. void
  585. hashDistributionCheck(const char* const* servers) {
  586. const size_t BUCKETS = 64; // constant used in the MessageRenderer
  587. set<Name> names;
  588. vector<size_t> hash_counts(BUCKETS);
  589. // Store all test names and their super domain names (excluding the
  590. // "root" label) in the set, calculates their hash values, and increments
  591. // the counter for the corresponding hash "bucket".
  592. for (size_t i = 0; servers[i] != NULL; ++i) {
  593. const Name name(servers[i]);
  594. for (size_t l = 0; l < name.getLabelCount() - 1; ++l) {
  595. pair<set<Name>::const_iterator, bool> ret =
  596. names.insert(name.split(l));
  597. if (ret.second) {
  598. hash_counts[LabelSequence((*ret.first)).getHash(false) %
  599. BUCKETS]++;
  600. }
  601. }
  602. }
  603. // See how many conflicts we have in the buckets. For the testing purpose
  604. // we expect there's at most 2 conflicts in each set, which is an
  605. // arbitrary choice (it should happen to succeed with the hash function
  606. // and data we are using; if it's not the case, maybe with an update to
  607. // the hash implementation, we should revise the test).
  608. for (size_t i = 0; i < BUCKETS; ++i) {
  609. EXPECT_GE(3, hash_counts[i]);
  610. }
  611. }
  612. TEST_F(LabelSequenceTest, getHash) {
  613. // Trivial case. The same sequence should have the same hash.
  614. EXPECT_EQ(ls1.getHash(true), ls1.getHash(true));
  615. // Check the case-insensitive mode behavior.
  616. EXPECT_EQ(ls1.getHash(false), ls5.getHash(false));
  617. // Check that the distribution of hash values is "not too bad" (such as
  618. // everything has the same hash value due to a stupid bug). It's
  619. // difficult to check such things reliably. We do some ad hoc tests here.
  620. hashDistributionCheck(root_servers);
  621. hashDistributionCheck(jp_servers);
  622. hashDistributionCheck(cn_servers);
  623. hashDistributionCheck(ca_servers);
  624. }
  625. // test operator<<. We simply confirm it appends the result of toText().
  626. TEST_F(LabelSequenceTest, LeftShiftOperator) {
  627. ostringstream oss;
  628. oss << ls1;
  629. EXPECT_EQ(ls1.toText(), oss.str());
  630. }
  631. TEST_F(LabelSequenceTest, serialize) {
  632. // placeholder for serialized data. We use a sufficiently large space
  633. // for testing the overwrapping cases below.
  634. uint8_t labels_buf[LabelSequence::MAX_SERIALIZED_LENGTH * 3];
  635. // vector to store expected and actual data
  636. vector<LabelSequence> actual_labelseqs;
  637. typedef pair<size_t, const uint8_t*> DataPair;
  638. vector<DataPair> expected;
  639. // An absolute sequence directly constructed from a valid name.
  640. // labels = 3, offset sequence = 0, 8, 12, data = "example.com."
  641. actual_labelseqs.push_back(ls1);
  642. const uint8_t expected_data1[] = {
  643. 3, 0, 8, 12, 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  644. 3, 'o', 'r', 'g', 0 };
  645. expected.push_back(DataPair(sizeof(expected_data1), expected_data1));
  646. // Strip the original one from right.
  647. // labels = 2, offset sequence = 0, 8, data = "example.com" (non absolute)
  648. LabelSequence ls_rstripped = ls1;
  649. ls_rstripped.stripRight(1);
  650. actual_labelseqs.push_back(ls_rstripped);
  651. const uint8_t expected_data2[] = {
  652. 2, 0, 8, 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
  653. 3, 'o', 'r', 'g'};
  654. expected.push_back(DataPair(sizeof(expected_data2), expected_data2));
  655. // Strip the original one from left.
  656. // labels = 2, offset sequence = 0, 4, data = "com."
  657. // Note that offsets are adjusted so that they begin with 0.
  658. LabelSequence ls_lstripped = ls1;
  659. ls_lstripped.stripLeft(1);
  660. actual_labelseqs.push_back(ls_lstripped);
  661. const uint8_t expected_data3[] = { 2, 0, 4, 3, 'o', 'r', 'g', 0 };
  662. expected.push_back(DataPair(sizeof(expected_data3), expected_data3));
  663. // Root label.
  664. LabelSequence ls_root(Name::ROOT_NAME());
  665. actual_labelseqs.push_back(ls_root);
  666. const uint8_t expected_data4[] = { 1, 0, 0 };
  667. expected.push_back(DataPair(sizeof(expected_data4), expected_data4));
  668. // Non absolute single-label.
  669. LabelSequence ls_single = ls_rstripped;
  670. ls_single.stripRight(1);
  671. actual_labelseqs.push_back(ls_single);
  672. const uint8_t expected_data5[] = {
  673. 1, 0, 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e' };
  674. expected.push_back(DataPair(sizeof(expected_data5), expected_data5));
  675. // For each data set, serialize the labels and compare the data to the
  676. // expected one.
  677. vector<DataPair>::const_iterator it = expected.begin();
  678. vector<LabelSequence>::const_iterator itl = actual_labelseqs.begin();
  679. for (; it != expected.end(); ++it, ++itl) {
  680. SCOPED_TRACE(itl->toText());
  681. const size_t serialized_len = itl->getSerializedLength();
  682. ASSERT_GE(LabelSequence::MAX_SERIALIZED_LENGTH, serialized_len);
  683. itl->serialize(labels_buf, serialized_len);
  684. EXPECT_EQ(it->first, serialized_len);
  685. EXPECT_EQ(0, memcmp(it->second, labels_buf, serialized_len));
  686. EXPECT_EQ(NameComparisonResult::EQUAL,
  687. LabelSequence(labels_buf).compare(*itl).getRelation());
  688. // Shift the data to the middle of the buffer for overwrap check
  689. uint8_t* const bp = labels_buf;
  690. std::memcpy(bp + serialized_len, bp, serialized_len);
  691. // Memory layout is now as follows:
  692. // <- ser_len -> <- ser_len ------>
  693. // bp bp+ser_len bp+(ser_len*2)
  694. // olen,odata,ndata
  695. // end of buffer would be the first byte of offsets: invalid.
  696. EXPECT_THROW(LabelSequence(bp + serialized_len).
  697. serialize(bp + 2, serialized_len),
  698. isc::BadValue);
  699. // begin of buffer would be the last byte of ndata: invalid.
  700. EXPECT_THROW(LabelSequence(bp + serialized_len).
  701. serialize(bp + (2 * serialized_len) - 1, serialized_len),
  702. isc::BadValue);
  703. // A boundary safe case: buffer is placed after the sequence data.
  704. // should cause no disruption.
  705. LabelSequence(bp + serialized_len).
  706. serialize(bp + 2 * serialized_len, serialized_len);
  707. // A boundary safe case: buffer is placed before the sequence data
  708. // should cause no disruption. (but the original serialized data will
  709. // be overridden, so it can't be used any more)
  710. LabelSequence(bp + serialized_len).
  711. serialize(bp + 1, serialized_len);
  712. }
  713. EXPECT_THROW(ls1.serialize(labels_buf, ls1.getSerializedLength() - 1),
  714. isc::BadValue);
  715. }
  716. TEST_F(LabelSequenceTest, badDeserialize) {
  717. EXPECT_THROW(LabelSequence(NULL), isc::BadValue);
  718. const uint8_t zero_offsets[] = { 0 };
  719. EXPECT_THROW(LabelSequence ls(zero_offsets), isc::BadValue);
  720. const uint8_t toomany_offsets[] = { Name::MAX_LABELS + 1 };
  721. EXPECT_THROW(LabelSequence ls(toomany_offsets), isc::BadValue);
  722. // exceed MAX_LABEL_LEN
  723. const uint8_t offsets_toolonglabel[] = { 2, 0, 64 };
  724. EXPECT_THROW(LabelSequence ls(offsets_toolonglabel), isc::BadValue);
  725. // Inconsistent data: an offset is lower than the previous offset
  726. const uint8_t offsets_lower[] = { 3, // # of offsets
  727. 0, 2, 1, // offsets
  728. 1, 'a', 1, 'b', 0};
  729. EXPECT_THROW(LabelSequence ls(offsets_lower), isc::BadValue);
  730. // Inconsistent data: an offset is equal to the previous offset
  731. const uint8_t offsets_noincrease[] = { 2, 0, 0, 0, 0 };
  732. EXPECT_THROW(LabelSequence ls(offsets_noincrease), isc::BadValue);
  733. }
  734. namespace {
  735. // Helper function; repeatedly calls
  736. // - Initially, all three labelsequences should be the same
  737. // - repeatedly performs:
  738. // - checks all three are equal
  739. // - stripLeft on ls1
  740. // - checks ls1 and ls2 are different, and ls2 and ls3 are equal
  741. // - stripLeft on ls2
  742. // - checks ls1 and ls2 are equal, and ls2 and ls3 are different
  743. // - stripLeft on ls3
  744. //
  745. // (this test makes sure the stripLeft of one has no effect on the other
  746. // two, and that the strip properties hold regardless of how they were
  747. // constructed)
  748. //
  749. void stripLeftCheck(LabelSequence ls1, LabelSequence ls2, LabelSequence ls3) {
  750. ASSERT_LT(1, ls1.getLabelCount());
  751. while (ls1.getLabelCount() > 1) {
  752. check_equal(ls1, ls2);
  753. check_equal(ls2, ls3);
  754. ls1.stripLeft(1);
  755. check_compare(ls1, ls2, isc::dns::NameComparisonResult::SUPERDOMAIN,
  756. ls1.getLabelCount(), true, -1);
  757. check_equal(ls2, ls3);
  758. ls2.stripLeft(1);
  759. check_equal(ls1, ls2);
  760. check_compare(ls2, ls3, isc::dns::NameComparisonResult::SUPERDOMAIN,
  761. ls1.getLabelCount(), true, -1);
  762. ls3.stripLeft(1);
  763. }
  764. }
  765. // Similar to stripLeftCheck, but using stripRight()
  766. void stripRightCheck(LabelSequence ls1, LabelSequence ls2, LabelSequence ls3) {
  767. ASSERT_LT(1, ls1.getLabelCount());
  768. while (ls1.getLabelCount() > 1) {
  769. check_equal(ls1, ls2);
  770. check_equal(ls2, ls3);
  771. ls1.stripRight(1);
  772. check_compare(ls1, ls2, isc::dns::NameComparisonResult::NONE, 0,
  773. false);
  774. check_equal(ls2, ls3);
  775. ls2.stripRight(1);
  776. check_equal(ls1, ls2);
  777. check_compare(ls2, ls3, isc::dns::NameComparisonResult::NONE, 0,
  778. false);
  779. ls3.stripRight(1);
  780. }
  781. }
  782. } // end anonymous namespace
  783. class ExtendableLabelSequenceTest : public ::testing::Test {
  784. public:
  785. ExtendableLabelSequenceTest() : bar("bar."),
  786. example_org("example.org"),
  787. foo("foo."),
  788. foo_bar("foo.bar."),
  789. foo_bar_example_org("foo.bar.example.org."),
  790. foo_bar_foo_bar("foo.bar.foo.bar."),
  791. foo_example("foo.example."),
  792. org("org")
  793. {
  794. // explicitely set to non-zero data, to make sure
  795. // we don't try to use data we don't set
  796. memset(buf, 0xff, LabelSequence::MAX_SERIALIZED_LENGTH);
  797. }
  798. Name bar;
  799. Name example_org;
  800. Name foo;
  801. Name foo_bar;
  802. Name foo_bar_example_org;
  803. Name foo_bar_foo_bar;
  804. Name foo_example;
  805. Name org;
  806. uint8_t buf[LabelSequence::MAX_SERIALIZED_LENGTH];
  807. };
  808. // Test that 'extendable' labelsequences behave correctly when using
  809. // stripLeft() and stripRight()
  810. TEST_F(ExtendableLabelSequenceTest, extendableLabelSequence) {
  811. LabelSequence ls1(example_org);
  812. LabelSequence ls2(example_org);
  813. LabelSequence els(ls1, buf);
  814. // ls1 is absolute, so els should be too
  815. EXPECT_TRUE(els.isAbsolute());
  816. check_equal(ls1, els);
  817. ASSERT_EQ(ls1.getDataLength(), els.getDataLength());
  818. stripLeftCheck(ls1, els, ls2);
  819. stripRightCheck(ls1, els, ls2);
  820. // Creating an extendable labelsequence from a non-absolute
  821. // label sequence should result in a non-absolute label sequence
  822. ls1.stripRight(1);
  823. els = LabelSequence(ls1, buf);
  824. EXPECT_FALSE(els.isAbsolute());
  825. check_equal(ls1, els);
  826. // and extending with the root label should make it absolute again
  827. els.extend(LabelSequence(Name(".")), buf);
  828. EXPECT_TRUE(els.isAbsolute());
  829. check_equal(ls2, els);
  830. }
  831. // Test that 'extendable' LabelSequences behave correctly when initialized
  832. // with a stripped source LabelSequence
  833. TEST_F(ExtendableLabelSequenceTest, extendableLabelSequenceLeftStrippedSource) {
  834. LabelSequence ls1(foo_bar_example_org);
  835. LabelSequence ls2(foo_bar_example_org);
  836. while (ls1.getLabelCount() > 2) {
  837. ls1.stripLeft(1);
  838. ls2.stripLeft(1);
  839. LabelSequence els(ls1, buf);
  840. ASSERT_EQ(ls1.getDataLength(), els.getDataLength());
  841. stripLeftCheck(ls1, els, ls2);
  842. stripRightCheck(ls1, els, ls2);
  843. }
  844. }
  845. TEST_F(ExtendableLabelSequenceTest, extendableLabelSequenceRightStrippedSource) {
  846. LabelSequence ls1(foo_bar_example_org);
  847. LabelSequence ls2(foo_bar_example_org);
  848. while (ls1.getLabelCount() > 2) {
  849. ls1.stripRight(1);
  850. ls2.stripRight(1);
  851. LabelSequence els(ls1, buf);
  852. ASSERT_EQ(ls1.getDataLength(), els.getDataLength());
  853. stripLeftCheck(ls1, els, ls2);
  854. stripRightCheck(ls1, els, ls2);
  855. }
  856. }
  857. // Check some basic 'extend' functionality
  858. TEST_F(ExtendableLabelSequenceTest, extend) {
  859. LabelSequence ls1(foo_bar);
  860. LabelSequence ls2(foo);
  861. LabelSequence ls3(bar);
  862. LabelSequence ls4(foo_bar);
  863. LabelSequence els(ls2, buf);
  864. check_compare(ls1, els, isc::dns::NameComparisonResult::COMMONANCESTOR, 1,
  865. true, -4);
  866. els.extend(ls3, buf);
  867. EXPECT_TRUE(els.isAbsolute());
  868. check_equal(ls1, els);
  869. stripLeftCheck(ls1, els, ls4);
  870. stripRightCheck(ls1, els, ls4);
  871. // strip, then extend again
  872. els.stripRight(2); // (2, 1 for root label, 1 for last label)
  873. els.extend(ls3, buf);
  874. EXPECT_TRUE(els.isAbsolute());
  875. check_equal(ls1, els);
  876. // Extending again should make it different
  877. els.extend(ls3, buf);
  878. EXPECT_TRUE(els.isAbsolute());
  879. check_compare(ls1, els, isc::dns::NameComparisonResult::COMMONANCESTOR, 2,
  880. true, 4);
  881. // Extending with a non-absolute name should make it non-absolute as well
  882. ls3.stripRight(1);
  883. els.extend(ls3, buf);
  884. EXPECT_FALSE(els.isAbsolute());
  885. Name check_name("foo.bar.bar.bar");
  886. LabelSequence check_ls(check_name);
  887. check_ls.stripRight(1);
  888. check_equal(check_ls, els);
  889. // And try extending when both are not absolute
  890. els.stripRight(3);
  891. ls1.stripRight(1);
  892. EXPECT_FALSE(els.isAbsolute());
  893. els.extend(ls3, buf);
  894. EXPECT_FALSE(els.isAbsolute());
  895. check_equal(ls1, els);
  896. // Extending non-absolute with absolute should make it absolute again
  897. EXPECT_FALSE(els.isAbsolute());
  898. els.extend(LabelSequence(Name("absolute.")), buf);
  899. EXPECT_TRUE(els.isAbsolute());
  900. check_equal(LabelSequence(Name("foo.bar.absolute")), els);
  901. }
  902. TEST_F(ExtendableLabelSequenceTest, extendLeftStripped) {
  903. LabelSequence ls1(foo_example);
  904. LabelSequence ls2(example_org);
  905. LabelSequence ls3(org);
  906. LabelSequence els(ls1, buf);
  907. els.stripLeft(1);
  908. els.extend(ls3, buf);
  909. EXPECT_TRUE(els.isAbsolute());
  910. check_equal(ls2, els);
  911. }
  912. // Check that when extending with itself, it does not cause horrible failures
  913. TEST_F(ExtendableLabelSequenceTest, extendWithItself) {
  914. LabelSequence ls1(foo_bar);
  915. LabelSequence ls2(foo_bar_foo_bar);
  916. LabelSequence els(ls1, buf);
  917. els.extend(els, buf);
  918. EXPECT_TRUE(els.isAbsolute());
  919. check_equal(ls2, els);
  920. // Also try for non-absolute names
  921. ls2.stripRight(1);
  922. els = LabelSequence(ls1, buf);
  923. els.stripRight(1);
  924. els.extend(els, buf);
  925. EXPECT_FALSE(els.isAbsolute());
  926. check_equal(ls2, els);
  927. // Once more, now start out with non-absolute labelsequence
  928. ls1.stripRight(1);
  929. els = LabelSequence(ls1, buf);
  930. els.extend(els, buf);
  931. EXPECT_FALSE(els.isAbsolute());
  932. check_equal(ls2, els);
  933. }
  934. // Test that 'extending' with just a root label is a no-op, iff the original
  935. // was already absolute
  936. TEST_F(ExtendableLabelSequenceTest, extendWithRoot) {
  937. LabelSequence ls1(example_org);
  938. LabelSequence els(LabelSequence(ls1, buf));
  939. check_equal(ls1, els);
  940. els.extend(LabelSequence(Name(".")), buf);
  941. EXPECT_TRUE(els.isAbsolute());
  942. check_equal(ls1, els);
  943. // but not if the original was not absolute (it will be equal to
  944. // the original labelsequence used above, but not the one it was based
  945. // on).
  946. LabelSequence ls2(example_org);
  947. ls2.stripRight(1);
  948. els = LabelSequence(ls2, buf);
  949. EXPECT_FALSE(els.isAbsolute());
  950. els.extend(LabelSequence(Name(".")), buf);
  951. EXPECT_TRUE(els.isAbsolute());
  952. check_equal(ls1, els);
  953. check_compare(ls2, els, isc::dns::NameComparisonResult::NONE, 0, true, 3);
  954. }
  955. // Check possible failure modes of extend()
  956. TEST_F(ExtendableLabelSequenceTest, extendBadData) {
  957. LabelSequence ls1(example_org);
  958. LabelSequence els(ls1, buf);
  959. // try use with unrelated labelsequence
  960. EXPECT_THROW(ls1.extend(ls1, buf), isc::BadValue);
  961. // Create a long name, but so that we can still extend once
  962. Name longlabel("1234567890123456789012345678901234567890"
  963. "12345678901234567890");
  964. LabelSequence long_ls(longlabel);
  965. els = LabelSequence(long_ls, buf);
  966. els.extend(els, buf);
  967. els.extend(long_ls, buf);
  968. els.extend(long_ls, buf);
  969. ASSERT_EQ(245, els.getDataLength());
  970. // Extending once more with 10 bytes should still work
  971. els.extend(LabelSequence(Name("123456789")), buf);
  972. EXPECT_TRUE(els.isAbsolute());
  973. // Extended label sequence should now look like
  974. const Name full_name(
  975. "123456789012345678901234567890123456789012345678901234567890."
  976. "123456789012345678901234567890123456789012345678901234567890."
  977. "123456789012345678901234567890123456789012345678901234567890."
  978. "123456789012345678901234567890123456789012345678901234567890."
  979. "123456789.");
  980. const LabelSequence full_ls(full_name);
  981. check_equal(full_ls, els);
  982. // But now, even the shortest extension should fail
  983. EXPECT_THROW(els.extend(LabelSequence(Name("1")), buf), isc::BadValue);
  984. // Check it hasn't been changed
  985. EXPECT_TRUE(els.isAbsolute());
  986. check_equal(full_ls, els);
  987. // Also check that extending past MAX_LABELS is not possible
  988. Name shortname("1.");
  989. LabelSequence short_ls(shortname);
  990. els = LabelSequence(short_ls, buf);
  991. for (size_t i=0; i < 126; ++i) {
  992. els.extend(short_ls, buf);
  993. }
  994. // Should now look like this
  995. const Name full_name2(
  996. "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1."
  997. "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1."
  998. "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1."
  999. "1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1."
  1000. "1.1.1.1.1.1.1.");
  1001. const LabelSequence full_ls2(full_name2);
  1002. EXPECT_TRUE(els.isAbsolute());
  1003. check_equal(full_ls2, els);
  1004. EXPECT_THROW(els.extend(short_ls, buf), isc::BadValue);
  1005. EXPECT_TRUE(els.isAbsolute());
  1006. check_equal(full_ls2, els);
  1007. }
  1008. }