name_unittest.cc 29 KB

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