labelsequence_unittest.cc 42 KB

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