question_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <vector>
  15. #include <sstream>
  16. #include <exceptions/exceptions.h>
  17. #include <util/buffer.h>
  18. #include <dns/exceptions.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/name.h>
  21. #include <dns/question.h>
  22. #include <dns/rrclass.h>
  23. #include <dns/rrtype.h>
  24. #include <gtest/gtest.h>
  25. #include <dns/tests/unittest_util.h>
  26. using isc::UnitTestUtil;
  27. using namespace std;
  28. using namespace isc::dns;
  29. using namespace isc::util;
  30. namespace {
  31. class QuestionTest : public ::testing::Test {
  32. protected:
  33. QuestionTest() : obuffer(0), renderer(obuffer),
  34. example_name1(Name("foo.example.com")),
  35. example_name2(Name("bar.example.com")),
  36. test_question1(example_name1, RRClass::IN(),
  37. RRType::NS()),
  38. test_question2(example_name2, RRClass::CH(),
  39. RRType::A())
  40. {}
  41. OutputBuffer obuffer;
  42. MessageRenderer renderer;
  43. Name example_name1;
  44. Name example_name2;
  45. Question test_question1;
  46. Question test_question2;
  47. vector<unsigned char> wiredata;
  48. };
  49. Question
  50. questionFromWire(const char* datafile, size_t position = 0) {
  51. vector<unsigned char> data;
  52. UnitTestUtil::readWireData(datafile, data);
  53. InputBuffer buffer(&data[0], data.size());
  54. buffer.setPosition(position);
  55. return (Question(buffer));
  56. }
  57. TEST_F(QuestionTest, fromWire) {
  58. Question q = questionFromWire("question_fromWire");
  59. EXPECT_EQ(example_name1, q.getName());
  60. EXPECT_EQ(RRClass::IN(), q.getClass());
  61. EXPECT_EQ(RRType::NS(), q.getType());
  62. // owner name of the second Question is compressed. It's uncommon
  63. // (to have multiple questions), but isn't prohibited by the protocol.
  64. q = questionFromWire("question_fromWire", 21);
  65. EXPECT_EQ(example_name2, q.getName());
  66. EXPECT_EQ(RRClass::CH(), q.getClass());
  67. EXPECT_EQ(RRType::A(), q.getType());
  68. // Pathological cases: Corresponding exceptions will be thrown from
  69. // the underlying parser.
  70. EXPECT_THROW(questionFromWire("question_fromWire", 31), DNSMessageFORMERR);
  71. EXPECT_THROW(questionFromWire("question_fromWire", 36), IncompleteRRClass);
  72. }
  73. TEST_F(QuestionTest, toText) {
  74. EXPECT_EQ("foo.example.com. IN NS\n", test_question1.toText());
  75. EXPECT_EQ("bar.example.com. CH A\n", test_question2.toText());
  76. }
  77. TEST_F(QuestionTest, toWireBuffer) {
  78. test_question1.toWire(obuffer);
  79. test_question2.toWire(obuffer);
  80. UnitTestUtil::readWireData("question_toWire1", wiredata);
  81. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, obuffer.getData(),
  82. obuffer.getLength(), &wiredata[0], wiredata.size());
  83. }
  84. TEST_F(QuestionTest, toWireRenderer) {
  85. test_question1.toWire(renderer);
  86. test_question2.toWire(renderer);
  87. UnitTestUtil::readWireData("question_toWire2", wiredata);
  88. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, obuffer.getData(),
  89. obuffer.getLength(), &wiredata[0], wiredata.size());
  90. }
  91. TEST_F(QuestionTest, toWireTruncated) {
  92. // If the available length in the renderer is too small, it would require
  93. // truncation. This won't happen in normal cases, but protocol wise it
  94. // could still happen if and when we support some (possibly future) opcode
  95. // that allows multiple questions.
  96. // Set the length limit to the qname length so that the whole question
  97. // would request truncated
  98. renderer.setLengthLimit(example_name1.getLength());
  99. EXPECT_FALSE(renderer.isTruncated()); // check pre-render condition
  100. EXPECT_EQ(0, test_question1.toWire(renderer));
  101. EXPECT_TRUE(renderer.isTruncated());
  102. EXPECT_EQ(0, renderer.getLength()); // renderer shouldn't have any data
  103. }
  104. // test operator<<. We simply confirm it appends the result of toText().
  105. TEST_F(QuestionTest, LeftShiftOperator) {
  106. ostringstream oss;
  107. oss << test_question1;
  108. EXPECT_EQ(test_question1.toText(), oss.str());
  109. }
  110. TEST_F(QuestionTest, comparison) {
  111. const Name a("a"), b("b");
  112. const RRClass in(RRClass::IN()), ch(RRClass::CH());
  113. const RRType ns(RRType::NS()), aaaa(RRType::AAAA());
  114. EXPECT_TRUE(Question(a, in, ns) < Question(a, in, aaaa));
  115. EXPECT_FALSE(Question(a, in, aaaa) < Question(a, in, ns));
  116. EXPECT_TRUE(Question(a, in, ns) < Question(a, ch, ns));
  117. EXPECT_FALSE(Question(a, ch, ns) < Question(a, in, ns));
  118. EXPECT_TRUE(Question(a, in, ns) < Question(a, ch, aaaa));
  119. EXPECT_FALSE(Question(a, ch, aaaa) < Question(a, in, ns));
  120. EXPECT_TRUE(Question(a, in, ns) < Question(b, in, ns));
  121. EXPECT_FALSE(Question(a, in, ns) < Question(a, in, ns));
  122. EXPECT_TRUE(Question(a, in, ns) < Question(b, ch, ns));
  123. EXPECT_FALSE(Question(b, ch, ns) < Question(a, in, ns));
  124. EXPECT_TRUE(Question(a, in, ns) < Question(b, ch, aaaa));
  125. EXPECT_FALSE(Question(b, ch, aaaa) < Question(a, in, ns));
  126. EXPECT_FALSE(Question(a, in, ns) < Question(a, in, ns));
  127. EXPECT_FALSE(Question(a, ch, ns) < Question(a, ch, ns));
  128. EXPECT_FALSE(Question(b, in, ns) < Question(b, in, ns));
  129. EXPECT_FALSE(Question(b, in, aaaa) < Question(b, in, aaaa));
  130. // Identical questions are equal
  131. EXPECT_TRUE(Question(a, in, ns) == Question(a, in, ns));
  132. EXPECT_FALSE(Question(a, in, ns) != Question(a, in, ns));
  133. // Components differing by one component are unequal...
  134. EXPECT_FALSE(Question(b, in, ns) == Question(a, in, ns));
  135. EXPECT_TRUE(Question(b, in, ns) != Question(a, in, ns));
  136. EXPECT_FALSE(Question(a, ch, ns) == Question(a, in, ns));
  137. EXPECT_TRUE(Question(a, ch, ns) != Question(a, in, ns));
  138. EXPECT_FALSE(Question(a, in, aaaa) == Question(a, in, ns));
  139. EXPECT_TRUE(Question(a, in, aaaa) != Question(a, in, ns));
  140. // ... as are those differing by two components
  141. EXPECT_FALSE(Question(b, ch, ns) == Question(a, in, ns));
  142. EXPECT_TRUE(Question(b, ch, ns) != Question(a, in, ns));
  143. EXPECT_FALSE(Question(b, in, aaaa) == Question(a, in, ns));
  144. EXPECT_TRUE(Question(b, in, aaaa) != Question(a, in, ns));
  145. EXPECT_FALSE(Question(a, ch, aaaa) == Question(a, in, ns));
  146. EXPECT_TRUE(Question(a, ch, aaaa) != Question(a, in, ns));
  147. // ... and question differing by all three
  148. EXPECT_FALSE(Question(b, ch, aaaa) == Question(a, in, ns));
  149. EXPECT_TRUE(Question(b, ch, aaaa) != Question(a, in, ns));
  150. }
  151. }