labelsequence_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <set>
  21. using namespace isc::dns;
  22. using namespace std;
  23. namespace {
  24. class LabelSequenceTest : public ::testing::Test {
  25. public:
  26. LabelSequenceTest() : n1("example.org"), n2("example.com"),
  27. n3("example.org"), n4("foo.bar.test.example"),
  28. n5("example.ORG"), n6("ExAmPlE.org"),
  29. n7("."), n8("foo.example.org.bar"),
  30. n9("\\000xample.org"),
  31. n10("\\000xample.org"),
  32. n11("\\000xample.com"),
  33. n12("\\000xamplE.com"),
  34. ls1(n1), ls2(n2), ls3(n3), ls4(n4), ls5(n5),
  35. ls6(n6), ls7(n7), ls8(n8),
  36. ls9(n9), ls10(n10), ls11(n11), ls12(n12)
  37. {};
  38. // Need to keep names in scope for at least the lifetime of
  39. // the labelsequences
  40. Name n1, n2, n3, n4, n5, n6, n7, n8;
  41. Name n9, n10, n11, n12;
  42. LabelSequence ls1, ls2, ls3, ls4, ls5, ls6, ls7, ls8;
  43. LabelSequence ls9, ls10, ls11, ls12;
  44. };
  45. // Basic equality tests
  46. TEST_F(LabelSequenceTest, equals_sensitive) {
  47. EXPECT_TRUE(ls1.equals(ls1, true));
  48. EXPECT_FALSE(ls1.equals(ls2, true));
  49. EXPECT_TRUE(ls1.equals(ls3, true));
  50. EXPECT_FALSE(ls1.equals(ls4, true));
  51. EXPECT_FALSE(ls1.equals(ls5, true));
  52. EXPECT_FALSE(ls1.equals(ls6, true));
  53. EXPECT_FALSE(ls1.equals(ls7, true));
  54. EXPECT_FALSE(ls1.equals(ls8, true));
  55. EXPECT_FALSE(ls2.equals(ls1, true));
  56. EXPECT_TRUE(ls2.equals(ls2, true));
  57. EXPECT_FALSE(ls2.equals(ls3, true));
  58. EXPECT_FALSE(ls2.equals(ls4, true));
  59. EXPECT_FALSE(ls2.equals(ls5, true));
  60. EXPECT_FALSE(ls2.equals(ls6, true));
  61. EXPECT_FALSE(ls2.equals(ls7, true));
  62. EXPECT_FALSE(ls2.equals(ls8, true));
  63. EXPECT_FALSE(ls4.equals(ls1, true));
  64. EXPECT_FALSE(ls4.equals(ls2, true));
  65. EXPECT_FALSE(ls4.equals(ls3, true));
  66. EXPECT_TRUE(ls4.equals(ls4, true));
  67. EXPECT_FALSE(ls4.equals(ls5, true));
  68. EXPECT_FALSE(ls4.equals(ls6, true));
  69. EXPECT_FALSE(ls4.equals(ls7, true));
  70. EXPECT_FALSE(ls4.equals(ls8, true));
  71. EXPECT_FALSE(ls5.equals(ls1, true));
  72. EXPECT_FALSE(ls5.equals(ls2, true));
  73. EXPECT_FALSE(ls5.equals(ls3, true));
  74. EXPECT_FALSE(ls5.equals(ls4, true));
  75. EXPECT_TRUE(ls5.equals(ls5, true));
  76. EXPECT_FALSE(ls5.equals(ls6, true));
  77. EXPECT_FALSE(ls5.equals(ls7, true));
  78. EXPECT_FALSE(ls5.equals(ls8, true));
  79. EXPECT_TRUE(ls9.equals(ls10, true));
  80. EXPECT_FALSE(ls9.equals(ls11, true));
  81. EXPECT_FALSE(ls9.equals(ls12, true));
  82. EXPECT_FALSE(ls11.equals(ls12, true));
  83. }
  84. TEST_F(LabelSequenceTest, equals_insensitive) {
  85. EXPECT_TRUE(ls1.equals(ls1));
  86. EXPECT_FALSE(ls1.equals(ls2));
  87. EXPECT_TRUE(ls1.equals(ls3));
  88. EXPECT_FALSE(ls1.equals(ls4));
  89. EXPECT_TRUE(ls1.equals(ls5));
  90. EXPECT_TRUE(ls1.equals(ls6));
  91. EXPECT_FALSE(ls1.equals(ls7));
  92. EXPECT_FALSE(ls2.equals(ls1));
  93. EXPECT_TRUE(ls2.equals(ls2));
  94. EXPECT_FALSE(ls2.equals(ls3));
  95. EXPECT_FALSE(ls2.equals(ls4));
  96. EXPECT_FALSE(ls2.equals(ls5));
  97. EXPECT_FALSE(ls2.equals(ls6));
  98. EXPECT_FALSE(ls2.equals(ls7));
  99. EXPECT_TRUE(ls3.equals(ls1));
  100. EXPECT_FALSE(ls3.equals(ls2));
  101. EXPECT_TRUE(ls3.equals(ls3));
  102. EXPECT_FALSE(ls3.equals(ls4));
  103. EXPECT_TRUE(ls3.equals(ls5));
  104. EXPECT_TRUE(ls3.equals(ls6));
  105. EXPECT_FALSE(ls3.equals(ls7));
  106. EXPECT_FALSE(ls4.equals(ls1));
  107. EXPECT_FALSE(ls4.equals(ls2));
  108. EXPECT_FALSE(ls4.equals(ls3));
  109. EXPECT_TRUE(ls4.equals(ls4));
  110. EXPECT_FALSE(ls4.equals(ls5));
  111. EXPECT_FALSE(ls4.equals(ls6));
  112. EXPECT_FALSE(ls4.equals(ls7));
  113. EXPECT_TRUE(ls5.equals(ls1));
  114. EXPECT_FALSE(ls5.equals(ls2));
  115. EXPECT_TRUE(ls5.equals(ls3));
  116. EXPECT_FALSE(ls5.equals(ls4));
  117. EXPECT_TRUE(ls5.equals(ls5));
  118. EXPECT_TRUE(ls5.equals(ls6));
  119. EXPECT_FALSE(ls5.equals(ls7));
  120. EXPECT_TRUE(ls9.equals(ls10));
  121. EXPECT_FALSE(ls9.equals(ls11));
  122. EXPECT_FALSE(ls9.equals(ls12));
  123. EXPECT_TRUE(ls11.equals(ls12));
  124. }
  125. // Compare tests
  126. TEST_F(LabelSequenceTest, compare) {
  127. // "example.org." and "example.org.", case sensitive
  128. NameComparisonResult result = ls1.compare(ls3, true);
  129. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  130. result.getRelation());
  131. EXPECT_EQ(0, result.getOrder());
  132. EXPECT_EQ(3, result.getCommonLabels());
  133. // "example.org." and "example.ORG.", case sensitive
  134. result = ls3.compare(ls5, true);
  135. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  136. result.getRelation());
  137. EXPECT_LT(0, result.getOrder());
  138. EXPECT_EQ(1, result.getCommonLabels());
  139. // "example.org." and "example.ORG.", case in-sensitive
  140. result = ls3.compare(ls5);
  141. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  142. result.getRelation());
  143. EXPECT_EQ(0, result.getOrder());
  144. EXPECT_EQ(3, result.getCommonLabels());
  145. Name na("a.example.org");
  146. Name nb("b.example.org");
  147. LabelSequence lsa(na);
  148. LabelSequence lsb(nb);
  149. // "a.example.org." and "b.example.org.", case in-sensitive
  150. result = lsa.compare(lsb);
  151. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  152. result.getRelation());
  153. EXPECT_GT(0, result.getOrder());
  154. EXPECT_EQ(3, result.getCommonLabels());
  155. // "example.org." and "b.example.org.", case in-sensitive
  156. lsa.stripLeft(1);
  157. result = lsa.compare(lsb);
  158. EXPECT_EQ(isc::dns::NameComparisonResult::SUPERDOMAIN,
  159. result.getRelation());
  160. EXPECT_GT(0, result.getOrder());
  161. EXPECT_EQ(3, result.getCommonLabels());
  162. Name nc("g.f.e.d.c.example.org");
  163. LabelSequence lsc(nc);
  164. // "g.f.e.d.c.example.org." and "b.example.org" (not absolute), case
  165. // in-sensitive
  166. lsb.stripRight(1);
  167. result = lsc.compare(lsb);
  168. EXPECT_EQ(isc::dns::NameComparisonResult::NONE,
  169. result.getRelation());
  170. EXPECT_EQ(0, result.getOrder());
  171. EXPECT_EQ(0, result.getCommonLabels());
  172. // "g.f.e.d.c.example.org." and "example.org.", case in-sensitive
  173. result = lsc.compare(ls1);
  174. EXPECT_EQ(isc::dns::NameComparisonResult::SUBDOMAIN,
  175. result.getRelation());
  176. EXPECT_LT(0, result.getOrder());
  177. EXPECT_EQ(3, result.getCommonLabels());
  178. // "e.d.c.example.org." and "example.org.", case in-sensitive
  179. lsc.stripLeft(2);
  180. result = lsc.compare(ls1);
  181. EXPECT_EQ(isc::dns::NameComparisonResult::SUBDOMAIN,
  182. result.getRelation());
  183. EXPECT_LT(0, result.getOrder());
  184. EXPECT_EQ(3, result.getCommonLabels());
  185. // "example.org." and "example.org.", case in-sensitive
  186. lsc.stripLeft(3);
  187. result = lsc.compare(ls1);
  188. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  189. result.getRelation());
  190. EXPECT_EQ(0, result.getOrder());
  191. EXPECT_EQ(3, result.getCommonLabels());
  192. // "." and "example.org.", case in-sensitive
  193. lsc.stripLeft(2);
  194. result = lsc.compare(ls1);
  195. EXPECT_EQ(isc::dns::NameComparisonResult::SUPERDOMAIN,
  196. result.getRelation());
  197. EXPECT_GT(0, result.getOrder());
  198. EXPECT_EQ(1, result.getCommonLabels());
  199. Name nd("a.b.c.isc.example.org");
  200. LabelSequence lsd(nd);
  201. Name ne("w.x.y.isc.EXAMPLE.org");
  202. LabelSequence lse(ne);
  203. // "a.b.c.isc.example.org." and "w.x.y.isc.EXAMPLE.org.",
  204. // case sensitive
  205. result = lsd.compare(lse, true);
  206. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  207. result.getRelation());
  208. EXPECT_LT(0, result.getOrder());
  209. EXPECT_EQ(2, result.getCommonLabels());
  210. // "a.b.c.isc.example.org." and "w.x.y.isc.EXAMPLE.org.",
  211. // case in-sensitive
  212. result = lsd.compare(lse);
  213. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  214. result.getRelation());
  215. EXPECT_GT(0, result.getOrder());
  216. EXPECT_EQ(4, result.getCommonLabels());
  217. // "isc.example.org." and "isc.EXAMPLE.org.", case sensitive
  218. lsd.stripLeft(3);
  219. lse.stripLeft(3);
  220. result = lsd.compare(lse, true);
  221. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  222. result.getRelation());
  223. EXPECT_LT(0, result.getOrder());
  224. EXPECT_EQ(2, result.getCommonLabels());
  225. // "isc.example.org." and "isc.EXAMPLE.org.", case in-sensitive
  226. result = lsd.compare(lse);
  227. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  228. result.getRelation());
  229. EXPECT_EQ(0, result.getOrder());
  230. EXPECT_EQ(4, result.getCommonLabels());
  231. Name nf("a.b.c.isc.example.org");
  232. LabelSequence lsf(nf);
  233. Name ng("w.x.y.isc.EXAMPLE.org");
  234. LabelSequence lsg(ng);
  235. // "a.b.c.isc.example.org." and "w.x.y.isc.EXAMPLE.org" (not
  236. // absolute), case in-sensitive
  237. lsg.stripRight(1);
  238. result = lsg.compare(lsf);
  239. EXPECT_EQ(isc::dns::NameComparisonResult::NONE,
  240. result.getRelation());
  241. EXPECT_EQ(0, result.getOrder());
  242. EXPECT_EQ(0, result.getCommonLabels());
  243. // "a.b.c.isc.example.org" (not absolute) and
  244. // "w.x.y.isc.EXAMPLE.org" (not absolute), case in-sensitive
  245. lsf.stripRight(1);
  246. result = lsg.compare(lsf);
  247. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  248. result.getRelation());
  249. EXPECT_LT(0, result.getOrder());
  250. EXPECT_EQ(3, result.getCommonLabels());
  251. // "a.b.c.isc.example" (not absolute) and
  252. // "w.x.y.isc.EXAMPLE" (not absolute), case in-sensitive
  253. lsf.stripRight(1);
  254. lsg.stripRight(1);
  255. result = lsg.compare(lsf);
  256. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  257. result.getRelation());
  258. EXPECT_LT(0, result.getOrder());
  259. EXPECT_EQ(2, result.getCommonLabels());
  260. // "a.b.c" (not absolute) and
  261. // "w.x.y" (not absolute), case in-sensitive
  262. lsf.stripRight(2);
  263. lsg.stripRight(2);
  264. result = lsg.compare(lsf);
  265. EXPECT_EQ(isc::dns::NameComparisonResult::NONE,
  266. result.getRelation());
  267. EXPECT_EQ(0, result.getOrder());
  268. EXPECT_EQ(0, result.getCommonLabels());
  269. Name nh("aexample.org");
  270. LabelSequence lsh(nh);
  271. Name ni("bexample.org");
  272. LabelSequence lsi(ni);
  273. // "aexample.org" (not absolute) and
  274. // "bexample.org" (not absolute), case in-sensitive
  275. lsh.stripRight(1);
  276. lsi.stripRight(1);
  277. result = lsh.compare(lsi);
  278. EXPECT_EQ(isc::dns::NameComparisonResult::COMMONANCESTOR,
  279. result.getRelation());
  280. EXPECT_GT(0, result.getOrder());
  281. EXPECT_EQ(1, result.getCommonLabels());
  282. // "aexample" (not absolute) and
  283. // "bexample" (not absolute), case in-sensitive
  284. lsh.stripRight(1);
  285. lsi.stripRight(1);
  286. result = lsh.compare(lsi);
  287. EXPECT_EQ(isc::dns::NameComparisonResult::NONE,
  288. result.getRelation());
  289. EXPECT_EQ(0, result.getOrder());
  290. EXPECT_EQ(0, result.getCommonLabels());
  291. Name nj("example.org");
  292. LabelSequence lsj(nj);
  293. Name nk("example.org");
  294. LabelSequence lsk(nk);
  295. // "example.org" (not absolute) and
  296. // "example.org" (not absolute), case in-sensitive
  297. lsj.stripRight(1);
  298. lsk.stripRight(1);
  299. result = lsj.compare(lsk);
  300. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  301. result.getRelation());
  302. EXPECT_EQ(0, result.getOrder());
  303. EXPECT_EQ(2, result.getCommonLabels());
  304. // "example" (not absolute) and
  305. // "example" (not absolute), case in-sensitive
  306. lsj.stripRight(1);
  307. lsk.stripRight(1);
  308. result = lsj.compare(lsk);
  309. EXPECT_EQ(isc::dns::NameComparisonResult::EQUAL,
  310. result.getRelation());
  311. EXPECT_EQ(0, result.getOrder());
  312. EXPECT_EQ(1, result.getCommonLabels());
  313. }
  314. void
  315. getDataCheck(const uint8_t* expected_data, size_t expected_len,
  316. const LabelSequence& ls)
  317. {
  318. size_t len;
  319. const uint8_t* data = ls.getData(&len);
  320. ASSERT_EQ(expected_len, len) << "Expected data: " << expected_data <<
  321. " name: " << ls.getName().toText();
  322. EXPECT_EQ(expected_len, ls.getDataLength()) <<
  323. "Expected data: " << expected_data <<
  324. " name: " << ls.getName().toText();
  325. for (size_t i = 0; i < len; ++i) {
  326. EXPECT_EQ(expected_data[i], data[i]) <<
  327. "Difference at pos " << i << ": Expected data: " << expected_data <<
  328. " name: " << ls.getName().toText();;
  329. }
  330. }
  331. // Convenient data converter for expected data. Label data must be of
  332. // uint8_t*, while it's convenient if we can specify some test data in
  333. // plain string (which is of char*). This wrapper converts the latter to
  334. // the former in a safer way.
  335. void
  336. getDataCheck(const char* expected_char_data, size_t expected_len,
  337. const LabelSequence& ls)
  338. {
  339. const vector<uint8_t> expected_data(expected_char_data,
  340. expected_char_data + expected_len);
  341. getDataCheck(&expected_data[0], expected_len, ls);
  342. }
  343. TEST_F(LabelSequenceTest, getData) {
  344. getDataCheck("\007example\003org\000", 13, ls1);
  345. getDataCheck("\007example\003com\000", 13, ls2);
  346. getDataCheck("\007example\003org\000", 13, ls3);
  347. getDataCheck("\003foo\003bar\004test\007example\000", 22, ls4);
  348. getDataCheck("\007example\003ORG\000", 13, ls5);
  349. getDataCheck("\007ExAmPlE\003org\000", 13, ls6);
  350. getDataCheck("\000", 1, ls7);
  351. };
  352. TEST_F(LabelSequenceTest, stripLeft) {
  353. EXPECT_TRUE(ls1.equals(ls3));
  354. ls1.stripLeft(0);
  355. getDataCheck("\007example\003org\000", 13, ls1);
  356. EXPECT_TRUE(ls1.equals(ls3));
  357. ls1.stripLeft(1);
  358. getDataCheck("\003org\000", 5, ls1);
  359. EXPECT_FALSE(ls1.equals(ls3));
  360. ls1.stripLeft(1);
  361. getDataCheck("\000", 1, ls1);
  362. EXPECT_TRUE(ls1.equals(ls7));
  363. ls2.stripLeft(2);
  364. getDataCheck("\000", 1, ls2);
  365. EXPECT_TRUE(ls2.equals(ls7));
  366. }
  367. TEST_F(LabelSequenceTest, stripRight) {
  368. EXPECT_TRUE(ls1.equals(ls3));
  369. ls1.stripRight(1);
  370. getDataCheck("\007example\003org", 12, ls1);
  371. EXPECT_FALSE(ls1.equals(ls3));
  372. ls1.stripRight(1);
  373. getDataCheck("\007example", 8, ls1);
  374. EXPECT_FALSE(ls1.equals(ls3));
  375. ASSERT_FALSE(ls1.equals(ls2));
  376. ls2.stripRight(2);
  377. getDataCheck("\007example", 8, ls2);
  378. EXPECT_TRUE(ls1.equals(ls2));
  379. }
  380. TEST_F(LabelSequenceTest, stripOutOfRange) {
  381. EXPECT_THROW(ls1.stripLeft(100), isc::OutOfRange);
  382. EXPECT_THROW(ls1.stripLeft(5), isc::OutOfRange);
  383. EXPECT_THROW(ls1.stripLeft(4), isc::OutOfRange);
  384. EXPECT_THROW(ls1.stripLeft(3), isc::OutOfRange);
  385. getDataCheck("\007example\003org\000", 13, ls1);
  386. EXPECT_THROW(ls1.stripRight(100), isc::OutOfRange);
  387. EXPECT_THROW(ls1.stripRight(5), isc::OutOfRange);
  388. EXPECT_THROW(ls1.stripRight(4), isc::OutOfRange);
  389. EXPECT_THROW(ls1.stripRight(3), isc::OutOfRange);
  390. getDataCheck("\007example\003org\000", 13, ls1);
  391. }
  392. TEST_F(LabelSequenceTest, getLabelCount) {
  393. EXPECT_EQ(3, ls1.getLabelCount());
  394. ls1.stripLeft(0);
  395. EXPECT_EQ(3, ls1.getLabelCount());
  396. ls1.stripLeft(1);
  397. EXPECT_EQ(2, ls1.getLabelCount());
  398. ls1.stripLeft(1);
  399. EXPECT_EQ(1, ls1.getLabelCount());
  400. EXPECT_EQ(3, ls2.getLabelCount());
  401. ls2.stripRight(1);
  402. EXPECT_EQ(2, ls2.getLabelCount());
  403. ls2.stripRight(1);
  404. EXPECT_EQ(1, ls2.getLabelCount());
  405. EXPECT_EQ(3, ls3.getLabelCount());
  406. ls3.stripRight(2);
  407. EXPECT_EQ(1, ls3.getLabelCount());
  408. EXPECT_EQ(5, ls4.getLabelCount());
  409. ls4.stripRight(3);
  410. EXPECT_EQ(2, ls4.getLabelCount());
  411. EXPECT_EQ(3, ls5.getLabelCount());
  412. ls5.stripLeft(2);
  413. EXPECT_EQ(1, ls5.getLabelCount());
  414. }
  415. TEST_F(LabelSequenceTest, comparePart) {
  416. EXPECT_FALSE(ls1.equals(ls8));
  417. // strip root label from example.org.
  418. ls1.stripRight(1);
  419. // strip foo from foo.example.org.bar.
  420. ls8.stripLeft(1);
  421. // strip bar. (i.e. bar and root) too
  422. ls8.stripRight(2);
  423. EXPECT_TRUE(ls1.equals(ls8));
  424. // Data comparison
  425. size_t len;
  426. const uint8_t* data = ls1.getData(&len);
  427. getDataCheck(data, len, ls8);
  428. }
  429. TEST_F(LabelSequenceTest, isAbsolute) {
  430. ASSERT_TRUE(ls1.isAbsolute());
  431. ls1.stripLeft(1);
  432. ASSERT_TRUE(ls1.isAbsolute());
  433. ls1.stripRight(1);
  434. ASSERT_FALSE(ls1.isAbsolute());
  435. ASSERT_TRUE(ls2.isAbsolute());
  436. ls2.stripRight(1);
  437. ASSERT_FALSE(ls2.isAbsolute());
  438. ASSERT_TRUE(ls3.isAbsolute());
  439. ls3.stripLeft(2);
  440. ASSERT_TRUE(ls3.isAbsolute());
  441. }
  442. // The following are test data used in the getHash test below. Normally
  443. // we use example/documentation domain names for testing, but in this case
  444. // we'd specifically like to use more realistic data, and are intentionally
  445. // using real-world samples: They are the NS names of root and some top level
  446. // domains as of this test.
  447. const char* const root_servers[] = {
  448. "a.root-servers.net", "b.root-servers.net", "c.root-servers.net",
  449. "d.root-servers.net", "e.root-servers.net", "f.root-servers.net",
  450. "g.root-servers.net", "h.root-servers.net", "i.root-servers.net",
  451. "j.root-servers.net", "k.root-servers.net", "l.root-servers.net",
  452. "m.root-servers.net", NULL
  453. };
  454. const char* const gtld_servers[] = {
  455. "a.gtld-servers.net", "b.gtld-servers.net", "c.gtld-servers.net",
  456. "d.gtld-servers.net", "e.gtld-servers.net", "f.gtld-servers.net",
  457. "g.gtld-servers.net", "h.gtld-servers.net", "i.gtld-servers.net",
  458. "j.gtld-servers.net", "k.gtld-servers.net", "l.gtld-servers.net",
  459. "m.gtld-servers.net", NULL
  460. };
  461. const char* const jp_servers[] = {
  462. "a.dns.jp", "b.dns.jp", "c.dns.jp", "d.dns.jp", "e.dns.jp",
  463. "f.dns.jp", "g.dns.jp", NULL
  464. };
  465. const char* const cn_servers[] = {
  466. "a.dns.cn", "b.dns.cn", "c.dns.cn", "d.dns.cn", "e.dns.cn",
  467. "ns.cernet.net", NULL
  468. };
  469. const char* const ca_servers[] = {
  470. "k.ca-servers.ca", "e.ca-servers.ca", "a.ca-servers.ca", "z.ca-servers.ca",
  471. "tld.isc-sns.net", "c.ca-servers.ca", "j.ca-servers.ca", "l.ca-servers.ca",
  472. "sns-pb.isc.org", "f.ca-servers.ca", NULL
  473. };
  474. // A helper function used in the getHash test below.
  475. void
  476. hashDistributionCheck(const char* const* servers) {
  477. const size_t BUCKETS = 64; // constant used in the MessageRenderer
  478. set<Name> names;
  479. vector<size_t> hash_counts(BUCKETS);
  480. // Store all test names and their super domain names (excluding the
  481. // "root" label) in the set, calculates their hash values, and increments
  482. // the counter for the corresponding hash "bucket".
  483. for (size_t i = 0; servers[i] != NULL; ++i) {
  484. const Name name(servers[i]);
  485. for (size_t l = 0; l < name.getLabelCount() - 1; ++l) {
  486. pair<set<Name>::const_iterator, bool> ret =
  487. names.insert(name.split(l));
  488. if (ret.second) {
  489. hash_counts[LabelSequence((*ret.first)).getHash(false) %
  490. BUCKETS]++;
  491. }
  492. }
  493. }
  494. // See how many conflicts we have in the buckets. For the testing purpose
  495. // we expect there's at most 2 conflicts in each set, which is an
  496. // arbitrary choice (it should happen to succeed with the hash function
  497. // and data we are using; if it's not the case, maybe with an update to
  498. // the hash implementation, we should revise the test).
  499. for (size_t i = 0; i < BUCKETS; ++i) {
  500. EXPECT_GE(3, hash_counts[i]);
  501. }
  502. }
  503. TEST_F(LabelSequenceTest, getHash) {
  504. // Trivial case. The same sequence should have the same hash.
  505. EXPECT_EQ(ls1.getHash(true), ls1.getHash(true));
  506. // Check the case-insensitive mode behavior.
  507. EXPECT_EQ(ls1.getHash(false), ls5.getHash(false));
  508. // Check that the distribution of hash values is "not too bad" (such as
  509. // everything has the same hash value due to a stupid bug). It's
  510. // difficult to check such things reliably. We do some ad hoc tests here.
  511. hashDistributionCheck(root_servers);
  512. hashDistributionCheck(jp_servers);
  513. hashDistributionCheck(cn_servers);
  514. hashDistributionCheck(ca_servers);
  515. }
  516. }