rdata_ns_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. using isc::UnitTestUtil;
  25. using namespace std;
  26. using namespace isc::dns;
  27. using namespace isc::util;
  28. using namespace isc::dns::rdata;
  29. namespace {
  30. class Rdata_NS_Test : public RdataTest {
  31. // there's nothing to specialize
  32. };
  33. const generic::NS rdata_ns("ns.example.com.");
  34. const generic::NS rdata_ns2("ns2.example.com.");
  35. const uint8_t wiredata_ns[] = {
  36. 0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
  37. 0x63, 0x6f, 0x6d, 0x00 };
  38. const uint8_t wiredata_ns2[] = {
  39. // first name: ns.example.com.
  40. 0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
  41. 0x63, 0x6f, 0x6d, 0x00,
  42. // second name: ns2.example.com. all labels except the first should be
  43. // compressed.
  44. 0x03, 0x6e, 0x73, 0x32, 0xc0, 0x03 };
  45. TEST_F(Rdata_NS_Test, createFromText) {
  46. EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com.")));
  47. // explicitly add a trailing dot. should be the same RDATA.
  48. EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com.")));
  49. // should be case sensitive.
  50. EXPECT_EQ(0, rdata_ns.compare(generic::NS("NS.EXAMPLE.COM.")));
  51. // RDATA of a class-independent type should be recognized for any
  52. // "unknown" class.
  53. EXPECT_EQ(0, rdata_ns.compare(*createRdata(RRType("NS"), RRClass(65000),
  54. "ns.example.com.")));
  55. }
  56. TEST_F(Rdata_NS_Test, badText) {
  57. // Extra input at end of line
  58. EXPECT_THROW(generic::NS("ns.example.com. extra."), InvalidRdataText);
  59. }
  60. TEST_F(Rdata_NS_Test, createFromWire) {
  61. EXPECT_EQ(0, rdata_ns.compare(
  62. *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  63. "rdata_ns_fromWire")));
  64. // RDLENGTH is too short
  65. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  66. "rdata_ns_fromWire", 18),
  67. InvalidRdataLength);
  68. // RDLENGTH is too long
  69. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  70. "rdata_ns_fromWire", 36),
  71. InvalidRdataLength);
  72. // incomplete name. the error should be detected in the name constructor
  73. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  74. "rdata_ns_fromWire", 71),
  75. DNSMessageFORMERR);
  76. EXPECT_EQ(0, generic::NS("ns2.example.com.").compare(
  77. *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  78. "rdata_ns_fromWire", 55)));
  79. EXPECT_THROW(*rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  80. "rdata_ns_fromWire", 63),
  81. InvalidRdataLength);
  82. }
  83. TEST_F(Rdata_NS_Test, createFromLexer) {
  84. EXPECT_EQ(0, rdata_ns.compare(
  85. *test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
  86. "ns.example.com.")));
  87. // test::createRdataUsingLexer() constructs relative to
  88. // "example.org." origin.
  89. EXPECT_EQ(0, generic::NS("ns8.example.org.").compare(
  90. *test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
  91. "ns8")));
  92. // Exceptions cause NULL to be returned.
  93. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
  94. ""));
  95. // Extra input at end of line
  96. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NS(), RRClass::IN(),
  97. "ns.example.com. extra."));
  98. }
  99. TEST_F(Rdata_NS_Test, toWireBuffer) {
  100. rdata_ns.toWire(obuffer);
  101. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  102. obuffer.getData(), obuffer.getLength(),
  103. wiredata_ns, sizeof(wiredata_ns));
  104. }
  105. TEST_F(Rdata_NS_Test, toWireRenderer) {
  106. rdata_ns.toWire(renderer);
  107. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  108. renderer.getData(), renderer.getLength(),
  109. wiredata_ns, sizeof(wiredata_ns));
  110. rdata_ns2.toWire(renderer);
  111. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  112. renderer.getData(), renderer.getLength(),
  113. wiredata_ns2, sizeof(wiredata_ns2));
  114. }
  115. TEST_F(Rdata_NS_Test, toText) {
  116. EXPECT_EQ("ns.example.com.", rdata_ns.toText());
  117. }
  118. TEST_F(Rdata_NS_Test, compare) {
  119. generic::NS small("a.example.");
  120. generic::NS large("example.");
  121. EXPECT_TRUE(Name("a.example") > Name("example"));
  122. EXPECT_GT(0, small.compare(large));
  123. }
  124. TEST_F(Rdata_NS_Test, getNSName) {
  125. EXPECT_EQ(Name("ns.example.com."), rdata_ns.getNSName());
  126. }
  127. }