rdata_minfo_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (C) 2011-2013 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 <string>
  15. #include <util/buffer.h>
  16. #include <dns/exceptions.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/rdata.h>
  19. #include <dns/rdataclass.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. #include <gtest/gtest.h>
  23. #include <dns/tests/unittest_util.h>
  24. #include <dns/tests/rdata_unittest.h>
  25. #include <util/unittests/wiredata.h>
  26. using namespace std;
  27. using namespace isc::util;
  28. using namespace isc::dns;
  29. using namespace isc::dns::rdata;
  30. using isc::UnitTestUtil;
  31. using isc::util::unittests::matchWireData;
  32. namespace {
  33. class Rdata_MINFO_Test : public RdataTest {
  34. protected:
  35. Rdata_MINFO_Test():
  36. minfo_txt("rmailbox.example.com. emailbox.example.com."),
  37. minfo_txt2("root.example.com. emailbox.example.com."),
  38. too_long_label("01234567890123456789012345678901234567"
  39. "89012345678901234567890123."),
  40. rdata_minfo(minfo_txt),
  41. rdata_minfo2(minfo_txt2)
  42. {}
  43. void checkFromText_None(const string& rdata_str) {
  44. checkFromText<generic::MINFO, isc::Exception, isc::Exception>(
  45. rdata_str, rdata_minfo, false, false);
  46. }
  47. void checkFromText_LexerError(const string& rdata_str) {
  48. checkFromText
  49. <generic::MINFO, InvalidRdataText, MasterLexer::LexerError>(
  50. rdata_str, rdata_minfo, true, true);
  51. }
  52. void checkFromText_TooLongLabel(const string& rdata_str) {
  53. checkFromText<generic::MINFO, TooLongLabel, TooLongLabel>(
  54. rdata_str, rdata_minfo, true, true);
  55. }
  56. void checkFromText_BadString(const string& rdata_str) {
  57. checkFromText<generic::MINFO, InvalidRdataText, isc::Exception>(
  58. rdata_str, rdata_minfo, true, false);
  59. }
  60. void checkFromText_EmptyLabel(const string& rdata_str) {
  61. checkFromText<generic::MINFO, EmptyLabel, EmptyLabel>(
  62. rdata_str, rdata_minfo, true, true);
  63. }
  64. void checkFromText_MissingOrigin(const string& rdata_str) {
  65. checkFromText
  66. <generic::MINFO, MissingNameOrigin, MissingNameOrigin>(
  67. rdata_str, rdata_minfo, true, true);
  68. }
  69. void checkFromText_Origin(const string& rdata_str, const Name* origin) {
  70. checkFromText<generic::MINFO, MissingNameOrigin, isc::Exception>(
  71. rdata_str, rdata_minfo, true, false, origin);
  72. }
  73. const string minfo_txt;
  74. const string minfo_txt2;
  75. const string too_long_label;
  76. const generic::MINFO rdata_minfo;
  77. const generic::MINFO rdata_minfo2;
  78. };
  79. TEST_F(Rdata_MINFO_Test, createFromText) {
  80. EXPECT_EQ(Name("rmailbox.example.com."), rdata_minfo.getRmailbox());
  81. EXPECT_EQ(Name("emailbox.example.com."), rdata_minfo.getEmailbox());
  82. EXPECT_EQ(Name("root.example.com."), rdata_minfo2.getRmailbox());
  83. EXPECT_EQ(Name("emailbox.example.com."), rdata_minfo2.getEmailbox());
  84. checkFromText_None(minfo_txt);
  85. // origin defined for lexer constructor, but not string constructor
  86. const Name origin("example.com");
  87. checkFromText_Origin("rmailbox emailbox", &origin);
  88. // lexer constructor accepts extra text, but string constructor doesn't
  89. checkFromText_BadString("rmailbox.example.com. emailbox.example.com. "
  90. "extra.example.com.");
  91. }
  92. TEST_F(Rdata_MINFO_Test, badText) {
  93. // too long names
  94. checkFromText_TooLongLabel("root.example.com." + too_long_label +
  95. " emailbox.example.com.");
  96. checkFromText_TooLongLabel("root.example.com. emailbox.example.com." +
  97. too_long_label);
  98. // invalid names
  99. checkFromText_EmptyLabel("root..example.com. emailbox.example.com.");
  100. checkFromText_EmptyLabel("root.example.com. emailbox..example.com.");
  101. // missing name
  102. checkFromText_LexerError("root.example.com.");
  103. // missing origin
  104. checkFromText_MissingOrigin("root.example.com emailbox.example.com.");
  105. checkFromText_MissingOrigin("root.example.com. emailbox.example.com");
  106. }
  107. TEST_F(Rdata_MINFO_Test, createFromWire) {
  108. // uncompressed names
  109. EXPECT_EQ(0, rdata_minfo.compare(
  110. *rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  111. "rdata_minfo_fromWire1.wire")));
  112. // compressed names
  113. EXPECT_EQ(0, rdata_minfo.compare(
  114. *rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  115. "rdata_minfo_fromWire2.wire", 15)));
  116. // RDLENGTH is too short
  117. EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  118. "rdata_minfo_fromWire3.wire"),
  119. InvalidRdataLength);
  120. // RDLENGTH is too long
  121. EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  122. "rdata_minfo_fromWire4.wire"),
  123. InvalidRdataLength);
  124. // bogus rmailbox name, the error should be detected in the name
  125. // constructor
  126. EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  127. "rdata_minfo_fromWire5.wire"),
  128. DNSMessageFORMERR);
  129. // bogus emailbox name, the error should be detected in the name
  130. // constructor
  131. EXPECT_THROW(rdataFactoryFromFile(RRType::MINFO(), RRClass::IN(),
  132. "rdata_minfo_fromWire6.wire"),
  133. DNSMessageFORMERR);
  134. }
  135. TEST_F(Rdata_MINFO_Test, assignment) {
  136. generic::MINFO copy((string(minfo_txt2)));
  137. copy = rdata_minfo;
  138. EXPECT_EQ(0, copy.compare(rdata_minfo));
  139. // Check if the copied data is valid even after the original is deleted
  140. generic::MINFO* copy2 = new generic::MINFO(rdata_minfo);
  141. generic::MINFO copy3((string(minfo_txt2)));
  142. copy3 = *copy2;
  143. delete copy2;
  144. EXPECT_EQ(0, copy3.compare(rdata_minfo));
  145. // Self assignment
  146. copy = copy;
  147. EXPECT_EQ(0, copy.compare(rdata_minfo));
  148. }
  149. TEST_F(Rdata_MINFO_Test, toWireBuffer) {
  150. rdata_minfo.toWire(obuffer);
  151. vector<unsigned char> data;
  152. UnitTestUtil::readWireData("rdata_minfo_toWireUncompressed1.wire", data);
  153. matchWireData(&data[0], data.size(),
  154. obuffer.getData(), obuffer.getLength());
  155. obuffer.clear();
  156. rdata_minfo2.toWire(obuffer);
  157. vector<unsigned char> data2;
  158. UnitTestUtil::readWireData("rdata_minfo_toWireUncompressed2.wire", data2);
  159. matchWireData(&data2[0], data2.size(),
  160. obuffer.getData(), obuffer.getLength());
  161. }
  162. TEST_F(Rdata_MINFO_Test, toWireRenderer) {
  163. rdata_minfo.toWire(renderer);
  164. vector<unsigned char> data;
  165. UnitTestUtil::readWireData("rdata_minfo_toWire1.wire", data);
  166. matchWireData(&data[0], data.size(),
  167. renderer.getData(), renderer.getLength());
  168. renderer.clear();
  169. rdata_minfo2.toWire(renderer);
  170. vector<unsigned char> data2;
  171. UnitTestUtil::readWireData("rdata_minfo_toWire2.wire", data2);
  172. matchWireData(&data2[0], data2.size(),
  173. renderer.getData(), renderer.getLength());
  174. }
  175. TEST_F(Rdata_MINFO_Test, toText) {
  176. EXPECT_EQ(minfo_txt, rdata_minfo.toText());
  177. EXPECT_EQ(minfo_txt2, rdata_minfo2.toText());
  178. }
  179. TEST_F(Rdata_MINFO_Test, compare) {
  180. // check reflexivity
  181. EXPECT_EQ(0, rdata_minfo.compare(rdata_minfo));
  182. // names must be compared in case-insensitive manner
  183. EXPECT_EQ(0, rdata_minfo.compare(generic::MINFO("RMAILBOX.example.com. "
  184. "emailbox.EXAMPLE.com.")));
  185. // another MINFO whose rmailbox name is larger than that of rdata_minfo.
  186. const generic::MINFO large1_minfo("zzzzzzzz.example.com. "
  187. "emailbox.example.com.");
  188. EXPECT_GT(0, rdata_minfo.compare(large1_minfo));
  189. EXPECT_LT(0, large1_minfo.compare(rdata_minfo));
  190. // another MINFO whose emailbox name is larger than that of rdata_minfo.
  191. const generic::MINFO large2_minfo("rmailbox.example.com. "
  192. "zzzzzzzzzzz.example.com.");
  193. EXPECT_GT(0, rdata_minfo.compare(large2_minfo));
  194. EXPECT_LT(0, large2_minfo.compare(rdata_minfo));
  195. // comparison attempt between incompatible RR types should be rejected
  196. EXPECT_THROW(rdata_minfo.compare(*RdataTest::rdata_nomatch), bad_cast);
  197. }
  198. }