labelsequence_unittest.cc 43 KB

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