name_unittest.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // Copyright (C) 2009 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 <vector>
  15. #include <string>
  16. #include <sstream>
  17. #include <iomanip>
  18. #include <limits>
  19. #include <stdexcept>
  20. #include <util/buffer.h>
  21. #include <dns/exceptions.h>
  22. #include <dns/name.h>
  23. #include <dns/messagerenderer.h>
  24. #include <dns/tests/unittest_util.h>
  25. #include <gtest/gtest.h>
  26. using namespace std;
  27. using namespace isc;
  28. using namespace isc::dns;
  29. using namespace isc::util;
  30. //
  31. // XXX: these are defined as class static constants, but some compilers
  32. // seemingly cannot find the symbols when used in the EXPECT_xxx macros.
  33. //
  34. const size_t Name::MAX_WIRE;
  35. const size_t Name::MAX_LABELS;
  36. // This is a name of maximum allowed number of labels
  37. const char* max_labels_str = "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 40
  38. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 80
  39. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 120
  40. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 160
  41. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 200
  42. "0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9." // 240
  43. "0.1.2.3.4.5.6";
  44. // This is a name of maximum allowed length
  45. const char* max_len_str = "123456789.123456789.123456789.123456789.123456789."
  46. "123456789.123456789.123456789.123456789.123456789."
  47. "123456789.123456789.123456789.123456789.123456789."
  48. "123456789.123456789.123456789.123456789.123456789."
  49. "123456789.123456789.123456789.123456789.123456789."
  50. "123";
  51. namespace {
  52. class NameTest : public ::testing::Test {
  53. protected:
  54. NameTest() : example_name("www.example.com"),
  55. example_name_upper("WWW.EXAMPLE.COM"),
  56. small_name("aaa.example.com"),
  57. large_name("zzz.example.com"),
  58. origin_name("example.com."),
  59. origin_name_upper("EXAMPLE.COM"),
  60. buffer_actual(0), buffer_expected(0)
  61. {}
  62. const Name example_name;
  63. Name example_name_upper; // this will be modified and cannot be const
  64. const Name small_name;
  65. const Name large_name;
  66. const Name origin_name;
  67. const Name origin_name_upper;
  68. OutputBuffer buffer_actual, buffer_expected;
  69. //
  70. // helper methods
  71. //
  72. static Name nameFactoryFromWire(const char* datafile, size_t position,
  73. bool downcase = false);
  74. // construct a name including all non-upper-case-alphabet characters.
  75. static Name nameFactoryLowerCase();
  76. void compareInWireFormat(const Name& name_actual,
  77. const Name& name_expected);
  78. };
  79. const Name downcased_global("\\255.EXAMPLE.COM", true);
  80. Name
  81. NameTest::nameFactoryFromWire(const char* datafile, size_t position,
  82. bool downcase)
  83. {
  84. vector<unsigned char> data;
  85. UnitTestUtil::readWireData(datafile, data);
  86. InputBuffer buffer(&data[0], data.size());
  87. buffer.setPosition(position);
  88. return (Name(buffer, downcase));
  89. }
  90. Name
  91. NameTest::nameFactoryLowerCase() {
  92. string lowercase_namestr;
  93. lowercase_namestr.reserve(Name::MAX_WIRE);
  94. unsigned int ch = 0;
  95. unsigned int labelcount = 0;
  96. do {
  97. if (ch < 'A' || ch > 'Z') {
  98. ostringstream ss;
  99. ss.setf(ios_base::right, ios_base::adjustfield);
  100. ss.width(3);
  101. ss << setfill('0') << ch;
  102. lowercase_namestr += '\\' + ss.str();
  103. if (++labelcount == Name::MAX_LABELLEN) {
  104. lowercase_namestr.push_back('.');
  105. labelcount = 0;
  106. }
  107. }
  108. } while (++ch <= Name::MAX_WIRE);
  109. return (Name(lowercase_namestr));
  110. }
  111. void
  112. NameTest::compareInWireFormat(const Name& name_actual,
  113. const Name& name_expected)
  114. {
  115. buffer_actual.clear();
  116. buffer_expected.clear();
  117. name_actual.toWire(buffer_actual);
  118. name_expected.toWire(buffer_expected);
  119. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  120. buffer_actual.getData(), buffer_actual.getLength(),
  121. buffer_expected.getData(), buffer_expected.getLength());
  122. }
  123. TEST_F(NameTest, nonlocalObject) {
  124. // A previous version of code relied on a non local static object for
  125. // name construction, so a non local static Name object defined outside
  126. // the name module might not be initialized correctly. This test detects
  127. // that kind of bug.
  128. EXPECT_EQ("\\255.example.com.", downcased_global.toText());
  129. }
  130. template <typename ExceptionType>
  131. void
  132. checkBadTextName(const string& txt) {
  133. // Check it results in the specified type of exception as well as
  134. // NameParserException.
  135. EXPECT_THROW(Name(txt, false), ExceptionType);
  136. EXPECT_THROW(Name(txt, false), NameParserException);
  137. // The same is thrown when constructing by the master-file constructor
  138. EXPECT_THROW(Name(txt.c_str(), txt.length(), &Name::ROOT_NAME()),
  139. ExceptionType);
  140. EXPECT_THROW(Name(txt.c_str(), txt.length(), &Name::ROOT_NAME()),
  141. NameParserException);
  142. }
  143. TEST_F(NameTest, fromText) {
  144. vector<string> strnames;
  145. strnames.push_back("www.example.com");
  146. strnames.push_back("www.example.com."); // with a trailing dot
  147. strnames.push_back("wWw.exAmpLe.com"); // mixed cases
  148. strnames.push_back("\\wWw.exAmpLe.com"); // escape with a backslash
  149. // decimal representation for "WWW"
  150. strnames.push_back("\\087\\087\\087.example.com");
  151. vector<string>::const_iterator it;
  152. for (it = strnames.begin(); it != strnames.end(); ++it) {
  153. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name, Name(*it));
  154. }
  155. // root names
  156. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, Name("@"), Name("."));
  157. // downcase
  158. EXPECT_EQ(Name("Www.eXample.coM", true).toText(), example_name.toText());
  159. //
  160. // Tests for bogus names. These should trigger exceptions.
  161. //
  162. // empty label cannot be followed by another label
  163. checkBadTextName<EmptyLabel>(".a");
  164. // duplicate period
  165. checkBadTextName<EmptyLabel>("a..");
  166. // label length must be < 64
  167. checkBadTextName<TooLongLabel>("012345678901234567890123456789"
  168. "012345678901234567890123456789"
  169. "0123");
  170. // now-unsupported bitstring labels
  171. checkBadTextName<BadLabelType>("\\[b11010000011101]");
  172. // label length must be < 64
  173. checkBadTextName<TooLongLabel>("012345678901234567890123456789"
  174. "012345678901234567890123456789"
  175. "012\\x");
  176. // but okay as long as resulting len < 64 even if the original string is
  177. // "too long"
  178. EXPECT_NO_THROW(Name("012345678901234567890123456789"
  179. "012345678901234567890123456789"
  180. "01\\x"));
  181. // incomplete \DDD pattern (exactly 3 D's must appear)
  182. checkBadTextName<BadEscape>("\\12abc");
  183. // \DDD must not exceed 255
  184. checkBadTextName<BadEscape>("\\256");
  185. // Same tests for \111 as for \\x above
  186. checkBadTextName<TooLongLabel>("012345678901234567890123456789"
  187. "012345678901234567890123456789"
  188. "012\\111");
  189. EXPECT_NO_THROW(Name("012345678901234567890123456789"
  190. "012345678901234567890123456789"
  191. "01\\111"));
  192. // A domain name must be 255 octets or less
  193. checkBadTextName<TooLongName>("123456789.123456789.123456789.123456789."
  194. "123456789.123456789.123456789.123456789."
  195. "123456789.123456789.123456789.123456789."
  196. "123456789.123456789.123456789.123456789."
  197. "123456789.123456789.123456789.123456789."
  198. "123456789.123456789.123456789.123456789."
  199. "123456789.1234");
  200. // This is a possible longest name and should be accepted
  201. EXPECT_NO_THROW(Name(string(max_len_str)));
  202. // \DDD must consist of 3 digits.
  203. checkBadTextName<IncompleteName>("\\12");
  204. // a name with the max number of labels. should be constructed without
  205. // an error, and its length should be the max value.
  206. Name maxlabels = Name(string(max_labels_str));
  207. EXPECT_EQ(Name::MAX_LABELS, maxlabels.getLabelCount());
  208. }
  209. // on the rest while we prepare it.
  210. // Check the @ syntax is accepted and it just copies the origin.
  211. TEST_F(NameTest, copyOrigin) {
  212. EXPECT_EQ(origin_name, Name("@", 1, &origin_name));
  213. // The downcase works on the origin too. But only when we provide it.
  214. EXPECT_EQ(origin_name, Name("@", 1, &origin_name_upper, true));
  215. EXPECT_EQ(origin_name_upper, Name("@", 1, &origin_name_upper, true));
  216. // If we don't provide the origin, it throws
  217. EXPECT_THROW(Name("@", 1, NULL), MissingNameOrigin);
  218. }
  219. // Test the master-file constructor does not append the origin when the
  220. // provided name is absolute
  221. TEST_F(NameTest, dontAppendOrigin) {
  222. EXPECT_EQ(example_name, Name("www.example.com.", 16, &origin_name));
  223. // The downcase works (only if provided, though)
  224. EXPECT_EQ(example_name, Name("WWW.EXAMPLE.COM.", 16, &origin_name, true));
  225. EXPECT_EQ(example_name_upper, Name("WWW.EXAMPLE.COM.", 16, &origin_name));
  226. // And it does not require the origin to be provided
  227. EXPECT_NO_THROW(Name("www.example.com.", 16, NULL));
  228. }
  229. // Test the master-file constructor properly appends the origin when
  230. // the provided name is relative.
  231. TEST_F(NameTest, appendOrigin) {
  232. EXPECT_EQ(example_name, Name("www", 3, &origin_name));
  233. // Check the downcase works (if provided)
  234. EXPECT_EQ(example_name, Name("WWW", 3, &origin_name, true));
  235. EXPECT_EQ(example_name, Name("WWW", 3, &origin_name_upper, true));
  236. EXPECT_EQ(example_name_upper, Name("WWW", 3, &origin_name_upper));
  237. // Check we can prepend more than one label
  238. EXPECT_EQ(Name("a.b.c.d.example.com."), Name("a.b.c.d", 7, &origin_name));
  239. // When the name is relative, we throw.
  240. EXPECT_THROW(Name("www", 3, NULL), MissingNameOrigin);
  241. }
  242. // When we don't provide the data, it throws
  243. TEST_F(NameTest, noDataProvided) {
  244. EXPECT_THROW(Name(NULL, 10, NULL), isc::InvalidParameter);
  245. EXPECT_THROW(Name(NULL, 10, &origin_name), isc::InvalidParameter);
  246. EXPECT_THROW(Name("www", 0, NULL), isc::InvalidParameter);
  247. EXPECT_THROW(Name("www", 0, &origin_name), isc::InvalidParameter);
  248. }
  249. // When we combine the first part and the origin together, the resulting name
  250. // is too long. It should throw. Other test checks this is valid when alone
  251. // (without the origin appended).
  252. TEST_F(NameTest, combinedTooLong) {
  253. EXPECT_THROW(Name(max_len_str, strlen(max_len_str), &origin_name),
  254. TooLongName);
  255. EXPECT_THROW(Name(max_labels_str, strlen(max_labels_str), &origin_name),
  256. TooLongName);
  257. // Appending the root should be OK
  258. EXPECT_NO_THROW(Name(max_len_str, strlen(max_len_str),
  259. &Name::ROOT_NAME()));
  260. EXPECT_NO_THROW(Name(max_labels_str, strlen(max_labels_str),
  261. &Name::ROOT_NAME()));
  262. }
  263. // Test the handling of @ in the name. If it is alone, it is the origin (when
  264. // it exists) or the root. If it is somewhere else, it has no special meaning.
  265. TEST_F(NameTest, atSign) {
  266. // If it is alone, it is the origin
  267. EXPECT_EQ(origin_name, Name("@", 1, &origin_name));
  268. EXPECT_THROW(Name("@", 1, NULL), MissingNameOrigin);
  269. EXPECT_EQ(Name::ROOT_NAME(), Name("@"));
  270. // It is not alone. It is taken verbatim. We check the name converted
  271. // back to the textual form, since checking it agains other name object
  272. // may be wrong -- if we create it wrong the same way as the tested
  273. // object.
  274. EXPECT_EQ("\\@.", Name("@.").toText());
  275. EXPECT_EQ("\\@.", Name("@.", 2, NULL).toText());
  276. EXPECT_EQ("\\@something.", Name("@something").toText());
  277. EXPECT_EQ("something\\@.", Name("something@").toText());
  278. EXPECT_EQ("\\@x.example.com.", Name("@x", 2, &origin_name).toText());
  279. EXPECT_EQ("x\\@.example.com.", Name("x@", 2, &origin_name).toText());
  280. // An escaped at-sign isn't active
  281. EXPECT_EQ("\\@.", Name("\\@").toText());
  282. EXPECT_EQ("\\@.example.com.", Name("\\@", 2, &origin_name).toText());
  283. }
  284. TEST_F(NameTest, fromWire) {
  285. //
  286. // test cases derived from BIND9 tests.
  287. //
  288. // normal case with a compression pointer
  289. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
  290. nameFactoryFromWire("name_fromWire1", 25),
  291. Name("vix.com"));
  292. // bogus label character (looks like a local compression pointer)
  293. EXPECT_THROW(nameFactoryFromWire("name_fromWire2", 25), DNSMessageFORMERR);
  294. // a bad compression pointer (too big)
  295. EXPECT_THROW(nameFactoryFromWire("name_fromWire3_1", 25),
  296. DNSMessageFORMERR);
  297. // forward reference
  298. EXPECT_THROW(nameFactoryFromWire("name_fromWire3_2", 25),
  299. DNSMessageFORMERR);
  300. // invalid name length
  301. EXPECT_THROW(nameFactoryFromWire("name_fromWire4", 550), DNSMessageFORMERR);
  302. // skip test for from Wire5. It's for disabling decompression, but our
  303. // implementation always allows it.
  304. // bad pointer (too big)
  305. EXPECT_THROW(nameFactoryFromWire("name_fromWire6", 25), DNSMessageFORMERR);
  306. // input ends unexpectedly
  307. EXPECT_THROW(nameFactoryFromWire("name_fromWire7", 25), DNSMessageFORMERR);
  308. // many hops of compression but valid. should succeed.
  309. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
  310. nameFactoryFromWire("name_fromWire8", 383),
  311. Name("vix.com"));
  312. //
  313. // Additional test cases
  314. //
  315. // large names, a long but valid one, and invalid (too long) one.
  316. EXPECT_EQ(Name::MAX_WIRE,
  317. nameFactoryFromWire("name_fromWire9", 0).getLength());
  318. EXPECT_THROW(nameFactoryFromWire("name_fromWire10", 0).getLength(),
  319. DNSMessageFORMERR);
  320. // A name with possible maximum number of labels; awkward but valid
  321. EXPECT_EQ(nameFactoryFromWire("name_fromWire11", 0).getLabelCount(),
  322. Name::MAX_LABELS);
  323. // Wire format including an invalid label length
  324. EXPECT_THROW(nameFactoryFromWire("name_fromWire12", 0), DNSMessageFORMERR);
  325. // converting upper-case letters to down-case
  326. EXPECT_EQ("vix.com.",
  327. nameFactoryFromWire("name_fromWire1", 25, true).toText());
  328. EXPECT_EQ(3, nameFactoryFromWire("name_fromWire1", 25).getLabelCount());
  329. }
  330. TEST_F(NameTest, copyConstruct) {
  331. Name copy(example_name);
  332. EXPECT_EQ(copy, example_name);
  333. // Check the copied data is valid even after the original is deleted
  334. Name* copy2 = new Name(example_name);
  335. Name copy3(*copy2);
  336. delete copy2;
  337. EXPECT_EQ(copy3, example_name);
  338. }
  339. TEST_F(NameTest, assignment) {
  340. Name copy(".");
  341. copy = example_name;
  342. EXPECT_EQ(copy, example_name);
  343. // Check if the copied data is valid even after the original is deleted
  344. Name* copy2 = new Name(example_name);
  345. Name copy3(".");
  346. copy3 = *copy2;
  347. delete copy2;
  348. EXPECT_EQ(copy3, example_name);
  349. // Self assignment
  350. copy = copy;
  351. EXPECT_EQ(example_name, copy);
  352. }
  353. TEST_F(NameTest, toText) {
  354. // tests derived from BIND9
  355. EXPECT_EQ("a.b.c.d", Name("a.b.c.d").toText(true));
  356. EXPECT_EQ("a.\\\\[[.c.d", Name("a.\\\\[\\[.c.d").toText(true));
  357. EXPECT_EQ("a.b.C.d.", Name("a.b.C.d").toText(false));
  358. EXPECT_EQ("a.b.", Name("a.b.").toText(false));
  359. // test omit_final_dot. It's false by default.
  360. EXPECT_EQ("a.b.c.d", Name("a.b.c.d.").toText(true));
  361. EXPECT_EQ(Name("a.b.").toText(false), Name("a.b.").toText());
  362. // the root name is a special case: omit_final_dot will be ignored.
  363. EXPECT_EQ(".", Name(".").toText(true));
  364. // test all printable characters to see whether special characters are
  365. // escaped while the others are intact. note that the conversion is
  366. // implementation specific; for example, it's not invalid to escape a
  367. // "normal" character such as 'a' with regard to the standard.
  368. string all_printable("!\\\"#\\$%&'\\(\\)*+,-\\./0123456789:\\;<=>?\\@"
  369. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  370. "[\\\\]^_.`abcdefghijklmnopqrstuvwxyz{|}~.");
  371. EXPECT_EQ(all_printable,
  372. nameFactoryFromWire("name_fromWire13", 0).toText());
  373. string all_nonprintable(
  374. "\\000\\001\\002\\003\\004\\005\\006\\007\\008\\009"
  375. "\\010\\011\\012\\013\\014\\015\\016\\017\\018\\019"
  376. "\\020\\021\\022\\023\\024\\025\\026\\027\\028\\029"
  377. "\\030\\031\\032\\127\\128\\129"
  378. "\\130\\131\\132\\133\\134\\135\\136\\137\\138\\139"
  379. "\\140\\141\\142\\143\\144\\145\\146\\147\\148\\149"
  380. "\\150\\151\\152\\153\\154\\155\\156."
  381. "\\157\\158\\159"
  382. "\\160\\161\\162\\163\\164\\165\\166\\167\\168\\169"
  383. "\\170\\171\\172\\173\\174\\175\\176\\177\\178\\179"
  384. "\\180\\181\\182\\183\\184\\185\\186\\187\\188\\189"
  385. "\\190\\191\\192\\193\\194\\195\\196\\197\\198\\199"
  386. "\\200\\201\\202\\203\\204\\205\\206\\207\\208\\209"
  387. "\\210\\211\\212\\213\\214\\215\\216\\217\\218\\219."
  388. "\\220\\221\\222\\223\\224\\225\\226\\227\\228\\229"
  389. "\\230\\231\\232\\233\\234\\235\\236\\237\\238\\239"
  390. "\\240\\241\\242\\243\\244\\245\\246\\247\\248\\249"
  391. "\\250\\251\\252\\253\\254\\255.");
  392. EXPECT_EQ(all_nonprintable,
  393. nameFactoryFromWire("name_fromWire14", 0).toText());
  394. }
  395. TEST_F(NameTest, toWireBuffer) {
  396. vector<unsigned char> data;
  397. OutputBuffer buffer(0);
  398. UnitTestUtil::readWireData(string("01610376697803636f6d00"), data);
  399. Name("a.vix.com.").toWire(buffer);
  400. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &data[0], data.size(),
  401. buffer.getData(), buffer.getLength());
  402. }
  403. //
  404. // We test various corner cases in Renderer tests, but add this test case
  405. // to fill the code coverage gap.
  406. //
  407. TEST_F(NameTest, toWireRenderer) {
  408. vector<unsigned char> data;
  409. MessageRenderer renderer;
  410. UnitTestUtil::readWireData(string("01610376697803636f6d00"), data);
  411. Name("a.vix.com.").toWire(renderer);
  412. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &data[0], data.size(),
  413. renderer.getData(), renderer.getLength());
  414. }
  415. //
  416. // Helper class to hold comparison test parameters.
  417. //
  418. struct CompareParameters {
  419. CompareParameters(const Name& n1, const Name& n2,
  420. NameComparisonResult::NameRelation r, int o,
  421. unsigned int l) :
  422. name1(n1), name2(n2), reln(r), order(o), labels(l) {}
  423. static int normalizeOrder(int o)
  424. {
  425. if (o > 0) {
  426. return (1);
  427. } else if (o < 0) {
  428. return (-1);
  429. }
  430. return (0);
  431. }
  432. Name name1;
  433. Name name2;
  434. NameComparisonResult::NameRelation reln;
  435. int order;
  436. unsigned int labels;
  437. };
  438. TEST_F(NameTest, compare) {
  439. vector<CompareParameters> params;
  440. params.push_back(CompareParameters(Name("c.d"), Name("a.b.c.d"),
  441. NameComparisonResult::SUPERDOMAIN,
  442. -1, 3));
  443. params.push_back(CompareParameters(Name("a.b.c.d"), Name("c.d"),
  444. NameComparisonResult::SUBDOMAIN, 1, 3));
  445. params.push_back(CompareParameters(Name("a.b.c.d"), Name("c.d.e.f"),
  446. NameComparisonResult::COMMONANCESTOR,
  447. -1, 1));
  448. params.push_back(CompareParameters(Name("a.b.c.d"), Name("f.g.c.d"),
  449. NameComparisonResult::COMMONANCESTOR,
  450. -1, 3));
  451. params.push_back(CompareParameters(Name("a.b.c.d"), Name("A.b.C.d."),
  452. NameComparisonResult::EQUAL,
  453. 0, 5));
  454. vector<CompareParameters>::const_iterator it;
  455. for (it = params.begin(); it != params.end(); ++it) {
  456. NameComparisonResult result = (*it).name1.compare((*it).name2);
  457. EXPECT_EQ((*it).reln, result.getRelation());
  458. EXPECT_EQ((*it).order,
  459. CompareParameters::normalizeOrder(result.getOrder()));
  460. EXPECT_EQ((*it).labels, result.getCommonLabels());
  461. }
  462. }
  463. TEST_F(NameTest, equal) {
  464. EXPECT_TRUE(example_name == Name("WWW.EXAMPLE.COM."));
  465. EXPECT_TRUE(example_name.equals(Name("WWW.EXAMPLE.COM.")));
  466. EXPECT_TRUE(example_name != Name("www.example.org."));
  467. EXPECT_TRUE(example_name.nequals(Name("www.example.org.")));
  468. // lengths don't match
  469. EXPECT_TRUE(example_name != Name("www2.example.com."));
  470. EXPECT_TRUE(example_name.nequals(Name("www2.example.com.")));
  471. // lengths are equal, but # of labels don't match (first test checks the
  472. // prerequisite).
  473. EXPECT_EQ(example_name.getLength(), Name("www\\.example.com.").getLength());
  474. EXPECT_TRUE(example_name != Name("www\\.example.com."));
  475. EXPECT_TRUE(example_name.nequals(Name("www\\.example.com.")));
  476. }
  477. TEST_F(NameTest, isWildcard) {
  478. EXPECT_FALSE(example_name.isWildcard());
  479. EXPECT_TRUE(Name("*.a.example.com").isWildcard());
  480. EXPECT_FALSE(Name("a.*.example.com").isWildcard());
  481. }
  482. TEST_F(NameTest, concatenate) {
  483. NameComparisonResult result =
  484. Name("aaa.www.example.com.").compare(Name("aaa").concatenate(example_name));
  485. EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());
  486. result = example_name.compare(Name(".").concatenate(example_name));
  487. EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());
  488. result = example_name.compare(example_name.concatenate(Name(".")));
  489. EXPECT_EQ(NameComparisonResult::EQUAL, result.getRelation());
  490. // concatenating two valid names would result in too long a name.
  491. Name n1("123456789.123456789.123456789.123456789.123456789."
  492. "123456789.123456789.123456789.123456789.123456789."
  493. "123456789.123456789.123456789.123456789.123456789.");
  494. Name n2("123456789.123456789.123456789.123456789.123456789."
  495. "123456789.123456789.123456789.123456789.123456789."
  496. "1234.");
  497. EXPECT_THROW(n1.concatenate(n2), TooLongName);
  498. }
  499. TEST_F(NameTest, reverse) {
  500. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.reverse(),
  501. Name("com.example.www."));
  502. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, Name(".").reverse(),
  503. Name("."));
  504. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName,
  505. Name("a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s").reverse(),
  506. Name("s.r.q.p.o.n.m.l.k.j.i.h.g.f.e.d.c.b.a"));
  507. }
  508. TEST_F(NameTest, split) {
  509. // normal cases with or without explicitly specifying the trailing dot.
  510. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1, 2),
  511. Name("example.com."));
  512. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1, 3),
  513. Name("example.com."));
  514. // edge cases: only the first or last label.
  515. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(0, 1),
  516. Name("www."));
  517. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(3, 1),
  518. Name("."));
  519. // invalid range: an exception should be thrown.
  520. EXPECT_THROW(example_name.split(1, 0), OutOfRange);
  521. EXPECT_THROW(example_name.split(2, 3), OutOfRange);
  522. // invalid range: the following parameters would cause overflow,
  523. // bypassing naive validation.
  524. EXPECT_THROW(example_name.split(1, numeric_limits<unsigned int>::max()),
  525. OutOfRange);
  526. }
  527. TEST_F(NameTest, split_for_suffix) {
  528. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(1),
  529. Name("example.com"));
  530. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(0),
  531. example_name);
  532. EXPECT_PRED_FORMAT2(UnitTestUtil::matchName, example_name.split(3),
  533. Name("."));
  534. // Invalid case: the level must be less than the original label count.
  535. EXPECT_THROW(example_name.split(4), OutOfRange);
  536. }
  537. TEST_F(NameTest, downcase) {
  538. // usual case: all-upper case name to all-lower case
  539. compareInWireFormat(example_name_upper.downcase(), example_name);
  540. // confirm that non upper-case characters are intact
  541. compareInWireFormat(nameFactoryLowerCase().downcase(),
  542. nameFactoryLowerCase());
  543. // confirm the calling object is actually modified
  544. example_name_upper.downcase();
  545. compareInWireFormat(example_name_upper, example_name);
  546. }
  547. TEST_F(NameTest, at) {
  548. // Confirm at() produces the exact sequence of wire-format name data
  549. vector<uint8_t> data;
  550. for (size_t i = 0; i < example_name.getLength(); i++) {
  551. data.push_back(example_name.at(i));
  552. }
  553. example_name.toWire(buffer_expected);
  554. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  555. &data[0], data.size(), buffer_expected.getData(),
  556. buffer_expected.getLength());
  557. // Out-of-range access: should trigger an exception.
  558. EXPECT_THROW(example_name.at(example_name.getLength()), OutOfRange);
  559. }
  560. //
  561. // The following set of tests confirm the result of <=, <, >=, >
  562. // The test logic is simple, and all tests are just straightforward variations
  563. // of the first one.
  564. //
  565. TEST_F(NameTest, leq) {
  566. // small <= large is true
  567. EXPECT_TRUE(small_name.leq(large_name));
  568. EXPECT_TRUE(small_name <= large_name);
  569. // small <= small is true
  570. EXPECT_TRUE(small_name.leq(small_name));
  571. EXPECT_LE(small_name, small_name);
  572. // large <= small is false
  573. EXPECT_FALSE(large_name.leq(small_name));
  574. EXPECT_FALSE(large_name <= small_name);
  575. }
  576. TEST_F(NameTest, geq) {
  577. EXPECT_TRUE(large_name.geq(small_name));
  578. EXPECT_TRUE(large_name >= small_name);
  579. EXPECT_TRUE(large_name.geq(large_name));
  580. EXPECT_GE(large_name, large_name);
  581. EXPECT_FALSE(small_name.geq(large_name));
  582. EXPECT_FALSE(small_name >= large_name);
  583. }
  584. TEST_F(NameTest, lthan) {
  585. EXPECT_TRUE(small_name.lthan(large_name));
  586. EXPECT_TRUE(small_name < large_name);
  587. EXPECT_FALSE(small_name.lthan(small_name));
  588. // cppcheck-suppress duplicateExpression
  589. EXPECT_FALSE(small_name < small_name);
  590. EXPECT_FALSE(large_name.lthan(small_name));
  591. EXPECT_FALSE(large_name < small_name);
  592. }
  593. TEST_F(NameTest, gthan) {
  594. EXPECT_TRUE(large_name.gthan(small_name));
  595. EXPECT_TRUE(large_name > small_name);
  596. EXPECT_FALSE(large_name.gthan(large_name));
  597. // cppcheck-suppress duplicateExpression
  598. EXPECT_FALSE(large_name > large_name);
  599. EXPECT_FALSE(small_name.gthan(large_name));
  600. EXPECT_FALSE(small_name > large_name);
  601. }
  602. TEST_F(NameTest, constants) {
  603. EXPECT_EQ(Name("."), Name::ROOT_NAME());
  604. }
  605. // test operator<<. We simply confirm it appends the result of toText().
  606. TEST_F(NameTest, LeftShiftOperator) {
  607. ostringstream oss;
  608. oss << example_name;
  609. EXPECT_EQ(example_name.toText(), oss.str());
  610. }
  611. }