rdata_ns_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // $Id$
  15. #include <dns/buffer.h>
  16. #include <dns/exceptions.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/rdata.h>
  19. #include <dns/rdataclass.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. #include <gtest/gtest.h>
  23. #include "unittest_util.h"
  24. #include "rdata_unittest.h"
  25. using isc::UnitTestUtil;
  26. using namespace std;
  27. using namespace isc::dns;
  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. {
  47. EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com")));
  48. // explicitly add a trailing dot. should be the same RDATA.
  49. EXPECT_EQ(0, rdata_ns.compare(generic::NS("ns.example.com.")));
  50. // should be case sensitive.
  51. EXPECT_EQ(0, rdata_ns.compare(generic::NS("NS.EXAMPLE.COM")));
  52. // RDATA of a class-independent type should be recognized for any
  53. // "unknown" class.
  54. EXPECT_EQ(0, rdata_ns.compare(*createRdata(RRType("NS"), RRClass(65000),
  55. "ns.example.com")));
  56. }
  57. TEST_F(Rdata_NS_Test, createFromWire)
  58. {
  59. EXPECT_EQ(0, rdata_ns.compare(
  60. *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  61. "testdata/rdata_ns_fromWire")));
  62. // RDLENGTH is too short
  63. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  64. "testdata/rdata_ns_fromWire", 18),
  65. InvalidRdataLength);
  66. // RDLENGTH is too long
  67. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  68. "testdata/rdata_ns_fromWire", 36),
  69. InvalidRdataLength);
  70. // incomplete name. the error should be detected in the name constructor
  71. EXPECT_THROW(rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  72. "testdata/rdata_ns_fromWire", 71),
  73. DNSMessageFORMERR);
  74. EXPECT_EQ(0, generic::NS("ns2.example.com").compare(
  75. *rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  76. "testdata/rdata_ns_fromWire", 55)));
  77. EXPECT_THROW(*rdataFactoryFromFile(RRType("NS"), RRClass("IN"),
  78. "testdata/rdata_ns_fromWire", 63),
  79. InvalidRdataLength);
  80. }
  81. TEST_F(Rdata_NS_Test, toWireBuffer)
  82. {
  83. rdata_ns.toWire(obuffer);
  84. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  85. obuffer.getData(), obuffer.getLength(),
  86. wiredata_ns, sizeof(wiredata_ns));
  87. }
  88. TEST_F(Rdata_NS_Test, toWireRenderer)
  89. {
  90. rdata_ns.toWire(renderer);
  91. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  92. obuffer.getData(), obuffer.getLength(),
  93. wiredata_ns, sizeof(wiredata_ns));
  94. rdata_ns2.toWire(renderer);
  95. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  96. obuffer.getData(), obuffer.getLength(),
  97. wiredata_ns2, sizeof(wiredata_ns2));
  98. }
  99. TEST_F(Rdata_NS_Test, toText)
  100. {
  101. EXPECT_EQ("ns.example.com.", rdata_ns.toText());
  102. }
  103. TEST_F(Rdata_NS_Test, compare)
  104. {
  105. generic::NS small("a.example");
  106. generic::NS large("example");
  107. EXPECT_EQ(true, Name("a.example") > Name("example"));
  108. EXPECT_GT(0, small.compare(large));
  109. }
  110. TEST_F(Rdata_NS_Test, getNSName)
  111. {
  112. EXPECT_EQ(Name("ns.example.com"), rdata_ns.getNSName());
  113. }
  114. }