message_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // $Id$
  15. #include <dns/buffer.h>
  16. #include <dns/rrset.h>
  17. #include <dns/message.h>
  18. #include <gtest/gtest.h>
  19. namespace {
  20. using isc::dns::Name;
  21. using isc::dns::RRClass;
  22. using isc::dns::RRType;
  23. using isc::dns::TTL;
  24. using isc::dns::Rdata::IN::A;
  25. using isc::dns::RRset;
  26. using isc::dns::RRsetPtr;
  27. using isc::dns::RR;
  28. using isc::dns::Message;
  29. using isc::dns::SECTION_QUESTION;
  30. using isc::dns::SECTION_ANSWER;
  31. using isc::dns::SECTION_AUTHORITY;
  32. using isc::dns::SECTION_ADDITIONAL;
  33. // The fixture for testing class RRClass.
  34. class MessageTest : public ::testing::Test {
  35. protected:
  36. MessageTest()
  37. {
  38. query.add_question(Name("www.example.com"), RRClass::IN, RRType::A);
  39. }
  40. Message query;
  41. Message response;
  42. };
  43. TEST_F(MessageTest, check_flags)
  44. {
  45. EXPECT_EQ(false, query.get_qr());
  46. query.set_qr(true);
  47. EXPECT_EQ(true, query.get_qr());
  48. EXPECT_EQ(false, query.get_aa());
  49. query.set_aa(true);
  50. EXPECT_EQ(true, query.get_aa());
  51. EXPECT_EQ(false, query.get_tc());
  52. query.set_tc(true);
  53. EXPECT_EQ(true, query.get_tc());
  54. EXPECT_EQ(false, query.get_rd());
  55. query.set_rd(true);
  56. EXPECT_EQ(true, query.get_rd());
  57. EXPECT_EQ(false, query.get_ra());
  58. query.set_ra(true);
  59. EXPECT_EQ(true, query.get_ra());
  60. EXPECT_EQ(false, query.get_ad());
  61. query.set_ad(true);
  62. EXPECT_EQ(true, query.get_ad());
  63. EXPECT_EQ(false, query.get_cd());
  64. query.set_cd(true);
  65. EXPECT_EQ(true, query.get_cd());
  66. }
  67. TEST_F(MessageTest, get_question)
  68. {
  69. const std::vector<RRsetPtr>& question = query.get_section(SECTION_QUESTION);
  70. EXPECT_EQ(1, question.size());
  71. EXPECT_EQ("www.example.com. IN A", (**question.begin()).to_text());
  72. }
  73. TEST_F(MessageTest, make_response)
  74. {
  75. query.make_response();
  76. EXPECT_EQ(true, query.get_qr());
  77. EXPECT_EQ("www.example.com. IN A",
  78. (**query.get_section(SECTION_QUESTION).begin()).to_text());
  79. EXPECT_EQ(0, query.get_section(SECTION_ANSWER).size());
  80. EXPECT_EQ(0, query.get_section(SECTION_AUTHORITY).size());
  81. EXPECT_EQ(0, query.get_section(SECTION_ADDITIONAL).size());
  82. }
  83. TEST_F(MessageTest, add_rr)
  84. {
  85. // Add one RR to the answer section.
  86. response.add_rr(SECTION_ANSWER, RR(Name("www.example.com"), RRClass::IN,
  87. RRType::A, TTL(3600), A("192.0.2.1")));
  88. EXPECT_EQ("www.example.com. 3600 IN A 192.0.2.1",
  89. (**response.get_section(SECTION_ANSWER).begin()).to_text());
  90. // Add another RR of the same RRset with a larger TTL. The original
  91. // (smaller) TTL should remain.
  92. response.add_rr(SECTION_ANSWER, RR(Name("www.example.com"), RRClass::IN,
  93. RRType::A, TTL(7200), A("192.0.2.2")));
  94. EXPECT_EQ("www.example.com. 3600 IN A 192.0.2.1\n"
  95. "www.example.com. 3600 IN A 192.0.2.2",
  96. (**response.get_section(SECTION_ANSWER).begin()).to_text());
  97. // Add one more RR of the same RRset with a smaller TTL. The new (smaller)
  98. // TTL should replace with the old one.
  99. response.add_rr(SECTION_ANSWER, RR(Name("www.example.com"), RRClass::IN,
  100. RRType::A, TTL(1800), A("192.0.2.3")));
  101. EXPECT_EQ("www.example.com. 1800 IN A 192.0.2.1\n"
  102. "www.example.com. 1800 IN A 192.0.2.2\n"
  103. "www.example.com. 1800 IN A 192.0.2.3",
  104. (**response.get_section(SECTION_ANSWER).begin()).to_text());
  105. }
  106. }