labelsequence_unittest.cc 43 KB

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