labelsequence_unittest.cc 37 KB

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