rdata_in_aaaa_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2010 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/exceptions.h>
  16. #include <dns/messagerenderer.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rdataclass.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <gtest/gtest.h>
  22. #include <dns/tests/unittest_util.h>
  23. #include <dns/tests/rdata_unittest.h>
  24. #include <util/unittests/wiredata.h>
  25. using namespace std;
  26. using namespace isc::dns;
  27. using namespace isc::util;
  28. using namespace isc::dns::rdata;
  29. using isc::UnitTestUtil;
  30. using isc::util::unittests::matchWireData;
  31. namespace {
  32. class Rdata_IN_AAAA_Test : public RdataTest {
  33. protected:
  34. Rdata_IN_AAAA_Test() : rdata_in_aaaa("2001:db8::1234") {}
  35. // Common check to see the result of in::A Rdata construction either from
  36. // std::string or with MasterLexer object. If it's expected to succeed
  37. // the result should be identical to the commonly used test data
  38. // (rdata_in_a); otherwise it should result in the exception specified as
  39. // the template parameter.
  40. void checkFromTextIN_AAAA(const string& in_aaaa_txt,
  41. bool throw_str_version = true,
  42. bool throw_lexer_version = true)
  43. {
  44. checkFromText<in::AAAA, InvalidRdataText, InvalidRdataText>(
  45. in_aaaa_txt, rdata_in_aaaa, throw_str_version,
  46. throw_lexer_version);
  47. }
  48. const in::AAAA rdata_in_aaaa;
  49. };
  50. const uint8_t wiredata_in_aaaa[] = {
  51. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x12, 0x34 };
  53. TEST_F(Rdata_IN_AAAA_Test, createFromText) {
  54. // Normal case: no exception for either case, so the exception type
  55. // doesn't matter.
  56. checkFromText<in::AAAA, isc::Exception, isc::Exception>(
  57. "2001:db8::1234", rdata_in_aaaa, false, false);
  58. // should reject an IP4 address.
  59. checkFromTextIN_AAAA("192.0.2.1");
  60. // or any meaningless text as an IPv6 address
  61. checkFromTextIN_AAAA("xxx");
  62. // trailing white space: only string version throws
  63. checkFromTextIN_AAAA("2001:db8::1234 ", true, false);
  64. // same for beginning white space.
  65. checkFromTextIN_AAAA(" 2001:db8::1234", true, false);
  66. // same for trailing non-space garbage (note that lexer version still
  67. // ignore it; it's expected to be detected at a higher layer).
  68. checkFromTextIN_AAAA("2001:db8::1234 xxx", true, false);
  69. // nul character after a valid textual representation.
  70. string nul_after_addr = "2001:db8::1234";
  71. nul_after_addr.push_back(0);
  72. checkFromTextIN_AAAA(nul_after_addr, true, true);
  73. // a valid address surrounded by parentheses; only okay with lexer
  74. checkFromTextIN_AAAA("(2001:db8::1234)", true, false);
  75. // input that would cause lexer-specific error; it's bad text as an
  76. // address so should result in the string version, too.
  77. checkFromText<in::AAAA, InvalidRdataText, MasterLexer::LexerError>(
  78. ")2001:db8::1234", rdata_in_aaaa);
  79. }
  80. TEST_F(Rdata_IN_AAAA_Test, createFromWire) {
  81. // Valid data
  82. EXPECT_EQ(0, rdata_in_aaaa.compare(
  83. *rdataFactoryFromFile(RRType::AAAA(), RRClass::IN(),
  84. "rdata_in_aaaa_fromWire")));
  85. // RDLENGTH is too short
  86. EXPECT_THROW(rdataFactoryFromFile(RRType::AAAA(), RRClass::IN(),
  87. "rdata_in_aaaa_fromWire", 18),
  88. DNSMessageFORMERR);
  89. // RDLENGTH is too long
  90. EXPECT_THROW(rdataFactoryFromFile(RRType::AAAA(), RRClass::IN(),
  91. "rdata_in_aaaa_fromWire", 36),
  92. DNSMessageFORMERR);
  93. // buffer too short.
  94. EXPECT_THROW(rdataFactoryFromFile(RRType::AAAA(), RRClass::IN(),
  95. "rdata_in_aaaa_fromWire", 55),
  96. DNSMessageFORMERR);
  97. }
  98. TEST_F(Rdata_IN_AAAA_Test, createFromLexer) {
  99. EXPECT_EQ(0, rdata_in_aaaa.compare(
  100. *test::createRdataUsingLexer(RRType::AAAA(), RRClass::IN(),
  101. "2001:db8::1234")));
  102. }
  103. TEST_F(Rdata_IN_AAAA_Test, toWireBuffer) {
  104. rdata_in_aaaa.toWire(obuffer);
  105. matchWireData(wiredata_in_aaaa, sizeof (wiredata_in_aaaa),
  106. obuffer.getData(), obuffer.getLength());
  107. }
  108. TEST_F(Rdata_IN_AAAA_Test, toWireRenderer) {
  109. rdata_in_aaaa.toWire(renderer);
  110. matchWireData(wiredata_in_aaaa, sizeof (wiredata_in_aaaa),
  111. renderer.getData(), renderer.getLength());
  112. }
  113. TEST_F(Rdata_IN_AAAA_Test, toText) {
  114. EXPECT_EQ("2001:db8::1234", rdata_in_aaaa.toText());
  115. }
  116. TEST_F(Rdata_IN_AAAA_Test, compare) {
  117. in::AAAA small1("::1");
  118. in::AAAA small2("1:2:3:4:5:6:7:8");
  119. in::AAAA large1("ffff::");
  120. in::AAAA large2("8:7:6:5:4:3:2:1");
  121. // trivial case: self equivalence
  122. // cppcheck-suppress uselessCallsCompare
  123. EXPECT_EQ(0, small1.compare(small1));
  124. // confirm these are compared as unsigned values
  125. EXPECT_GT(0, small1.compare(large1));
  126. EXPECT_LT(0, large1.compare(small1));
  127. // confirm these are compared in network byte order
  128. EXPECT_GT(0, small2.compare(large2));
  129. EXPECT_LT(0, large2.compare(small2));
  130. // comparison attempt between incompatible RR types should be rejected
  131. EXPECT_THROW(rdata_in_aaaa.compare(*RdataTest::rdata_nomatch), bad_cast);
  132. }
  133. }