dnsmessage_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. using namespace isc::dns;
  23. namespace isc {
  24. namespace testutils {
  25. const unsigned int QR_FLAG = 0x1;
  26. const unsigned int AA_FLAG = 0x2;
  27. const unsigned int TC_FLAG = 0x4;
  28. const unsigned int RD_FLAG = 0x8;
  29. const unsigned int RA_FLAG = 0x10;
  30. const unsigned int AD_FLAG = 0x20;
  31. const unsigned int CD_FLAG = 0x40;
  32. void
  33. headerCheck(const Message& message, const qid_t qid, const Rcode& rcode,
  34. const uint16_t opcodeval, const unsigned int flags,
  35. const unsigned int qdcount,
  36. const unsigned int ancount, const unsigned int nscount,
  37. const unsigned int arcount)
  38. {
  39. EXPECT_EQ(qid, message.getQid());
  40. EXPECT_EQ(rcode, message.getRcode());
  41. EXPECT_EQ(opcodeval, message.getOpcode().getCode());
  42. EXPECT_EQ((flags & QR_FLAG) != 0,
  43. message.getHeaderFlag(Message::HEADERFLAG_QR));
  44. EXPECT_EQ((flags & AA_FLAG) != 0,
  45. message.getHeaderFlag(Message::HEADERFLAG_AA));
  46. EXPECT_EQ((flags & TC_FLAG) != 0,
  47. message.getHeaderFlag(Message::HEADERFLAG_TC));
  48. EXPECT_EQ((flags & RA_FLAG) != 0,
  49. message.getHeaderFlag(Message::HEADERFLAG_RA));
  50. EXPECT_EQ((flags & RD_FLAG) != 0,
  51. message.getHeaderFlag(Message::HEADERFLAG_RD));
  52. EXPECT_EQ((flags & AD_FLAG) != 0,
  53. message.getHeaderFlag(Message::HEADERFLAG_AD));
  54. EXPECT_EQ((flags & CD_FLAG) != 0,
  55. message.getHeaderFlag(Message::HEADERFLAG_CD));
  56. EXPECT_EQ(qdcount, message.getRRCount(Message::SECTION_QUESTION));
  57. EXPECT_EQ(ancount, message.getRRCount(Message::SECTION_ANSWER));
  58. EXPECT_EQ(nscount, message.getRRCount(Message::SECTION_AUTHORITY));
  59. EXPECT_EQ(arcount, message.getRRCount(Message::SECTION_ADDITIONAL));
  60. }
  61. namespace {
  62. ::testing::AssertionResult
  63. matchRdata(const char*, const char*,
  64. const rdata::Rdata& expected, const rdata::Rdata& actual)
  65. {
  66. if (expected.compare(actual) != 0) {
  67. ::testing::Message msg;
  68. msg << "Two RDATAs are expected to be equal but not:\n"
  69. << " Actual: " << actual.toText() << "\n"
  70. << "Expected: " << expected.toText();
  71. return (::testing::AssertionFailure(msg));
  72. }
  73. return (::testing::AssertionSuccess());
  74. }
  75. }
  76. void
  77. rrsetCheck(isc::dns::ConstRRsetPtr expected_rrset,
  78. isc::dns::ConstRRsetPtr actual_rrset)
  79. {
  80. EXPECT_EQ(expected_rrset->getName(), actual_rrset->getName());
  81. EXPECT_EQ(expected_rrset->getClass(), actual_rrset->getClass());
  82. EXPECT_EQ(expected_rrset->getType(), actual_rrset->getType());
  83. EXPECT_EQ(expected_rrset->getTTL(), actual_rrset->getTTL());
  84. isc::dns::RdataIteratorPtr rdata_it = actual_rrset->getRdataIterator();
  85. isc::dns::RdataIteratorPtr expected_rdata_it =
  86. expected_rrset->getRdataIterator();
  87. while (!expected_rdata_it->isLast()) {
  88. EXPECT_FALSE(rdata_it->isLast());
  89. if (rdata_it->isLast()) {
  90. // buggy case, should stop here
  91. break;
  92. }
  93. EXPECT_PRED_FORMAT2(matchRdata, expected_rdata_it->getCurrent(),
  94. rdata_it->getCurrent());
  95. expected_rdata_it->next();
  96. rdata_it->next();
  97. }
  98. // Make sure we have examined all sets of rrset RDATA
  99. EXPECT_TRUE(rdata_it->isLast());
  100. }
  101. } // end of namespace testutils
  102. } // end of namespace isc