dnsmessage_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (C) 2011 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 <dns/message.h>
  15. #include <dns/opcode.h>
  16. #include <dns/rdata.h>
  17. #include <dns/rcode.h>
  18. #include <dns/rrset.h>
  19. #include <dns/rrttl.h>
  20. #include <gtest/gtest.h>
  21. #include <testutils/dnsmessage_test.h>
  22. #include <boost/bind.hpp>
  23. #include <string>
  24. #include <sstream>
  25. using namespace std;
  26. using namespace isc::dns;
  27. namespace isc {
  28. namespace testutils {
  29. const unsigned int QR_FLAG = 0x1;
  30. const unsigned int AA_FLAG = 0x2;
  31. const unsigned int TC_FLAG = 0x4;
  32. const unsigned int RD_FLAG = 0x8;
  33. const unsigned int RA_FLAG = 0x10;
  34. const unsigned int AD_FLAG = 0x20;
  35. const unsigned int CD_FLAG = 0x40;
  36. void
  37. headerCheck(const Message& message, const qid_t qid, const Rcode& rcode,
  38. const uint16_t opcodeval, const unsigned int flags,
  39. const unsigned int qdcount,
  40. const unsigned int ancount, const unsigned int nscount,
  41. const unsigned int arcount)
  42. {
  43. EXPECT_EQ(qid, message.getQid());
  44. EXPECT_EQ(rcode, message.getRcode());
  45. EXPECT_EQ(opcodeval, message.getOpcode().getCode());
  46. EXPECT_EQ((flags & QR_FLAG) != 0,
  47. message.getHeaderFlag(Message::HEADERFLAG_QR));
  48. EXPECT_EQ((flags & AA_FLAG) != 0,
  49. message.getHeaderFlag(Message::HEADERFLAG_AA));
  50. EXPECT_EQ((flags & TC_FLAG) != 0,
  51. message.getHeaderFlag(Message::HEADERFLAG_TC));
  52. EXPECT_EQ((flags & RA_FLAG) != 0,
  53. message.getHeaderFlag(Message::HEADERFLAG_RA));
  54. EXPECT_EQ((flags & RD_FLAG) != 0,
  55. message.getHeaderFlag(Message::HEADERFLAG_RD));
  56. EXPECT_EQ((flags & AD_FLAG) != 0,
  57. message.getHeaderFlag(Message::HEADERFLAG_AD));
  58. EXPECT_EQ((flags & CD_FLAG) != 0,
  59. message.getHeaderFlag(Message::HEADERFLAG_CD));
  60. EXPECT_EQ(qdcount, message.getRRCount(Message::SECTION_QUESTION));
  61. EXPECT_EQ(ancount, message.getRRCount(Message::SECTION_ANSWER));
  62. EXPECT_EQ(nscount, message.getRRCount(Message::SECTION_AUTHORITY));
  63. EXPECT_EQ(arcount, message.getRRCount(Message::SECTION_ADDITIONAL));
  64. }
  65. namespace {
  66. ::testing::AssertionResult
  67. matchRdata(const char*, const char*,
  68. const rdata::Rdata& expected, const rdata::Rdata& actual)
  69. {
  70. if (expected.compare(actual) != 0) {
  71. ::testing::Message msg;
  72. msg << "Two RDATAs are expected to be equal but not:\n"
  73. << " Actual: " << actual.toText() << "\n"
  74. << "Expected: " << expected.toText();
  75. return (::testing::AssertionFailure(msg));
  76. }
  77. return (::testing::AssertionSuccess());
  78. }
  79. // A helper callback of masterLoad() used by textToRRset() below.
  80. void
  81. setRRset(RRsetPtr rrset, RRsetPtr* rrsetp) {
  82. if (*rrsetp) {
  83. // may be a sig
  84. if (rrset->getType() == RRType::RRSIG()) {
  85. (*rrsetp)->addRRsig(rrset);
  86. } else {
  87. isc_throw(isc::Unexpected,
  88. "multiple RRsets are given to textToRRset");
  89. }
  90. } else {
  91. *rrsetp = rrset;
  92. }
  93. }
  94. }
  95. RRsetPtr
  96. textToRRset(const string& text_rrset, const RRClass& rrclass,
  97. const Name& origin)
  98. {
  99. stringstream ss(text_rrset);
  100. RRsetPtr rrset;
  101. masterLoad(ss, origin, rrclass, boost::bind(setRRset, _1, &rrset));
  102. return (rrset);
  103. }
  104. void
  105. rrsetCheck(isc::dns::ConstRRsetPtr expected_rrset,
  106. isc::dns::ConstRRsetPtr actual_rrset)
  107. {
  108. SCOPED_TRACE("Comparing RRsets\n"
  109. " Actual: " + actual_rrset->toText() +
  110. " Expected: " + expected_rrset->toText());
  111. EXPECT_EQ(expected_rrset->getName(), actual_rrset->getName());
  112. EXPECT_EQ(expected_rrset->getClass(), actual_rrset->getClass());
  113. EXPECT_EQ(expected_rrset->getType(), actual_rrset->getType());
  114. EXPECT_EQ(expected_rrset->getTTL(), actual_rrset->getTTL());
  115. isc::dns::RdataIteratorPtr rdata_it = actual_rrset->getRdataIterator();
  116. isc::dns::RdataIteratorPtr expected_rdata_it =
  117. expected_rrset->getRdataIterator();
  118. while (!expected_rdata_it->isLast()) {
  119. EXPECT_FALSE(rdata_it->isLast());
  120. if (rdata_it->isLast()) {
  121. // buggy case, should stop here
  122. break;
  123. }
  124. EXPECT_PRED_FORMAT2(matchRdata, expected_rdata_it->getCurrent(),
  125. rdata_it->getCurrent());
  126. expected_rdata_it->next();
  127. rdata_it->next();
  128. }
  129. // Make sure we have examined all sets of rrset RDATA
  130. EXPECT_TRUE(rdata_it->isLast());
  131. }
  132. } // end of namespace testutils
  133. } // end of namespace isc