rdata_dname_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/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 "unittest_util.h"
  23. #include "rdata_unittest.h"
  24. using isc::UnitTestUtil;
  25. using namespace std;
  26. using namespace isc::dns;
  27. using namespace isc::dns::rdata;
  28. namespace {
  29. class Rdata_DNAME_Test : public RdataTest {
  30. // there's nothing to specialize
  31. };
  32. const generic::DNAME rdata_dname("dn.example.com");
  33. const generic::DNAME rdata_dname2("dn2.example.com");
  34. const uint8_t wiredata_dname[] = {
  35. 0x02, 0x64, 0x6e, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
  36. 0x63, 0x6f, 0x6d, 0x00 };
  37. const uint8_t wiredata_dname2[] = {
  38. // first name: dn.example.com.
  39. 0x02, 0x64, 0x6e, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
  40. 0x63, 0x6f, 0x6d, 0x00,
  41. // second name: dn2.example.com. all labels except the first should be
  42. // compressed.
  43. 0x03, 0x64, 0x6e, 0x32, 0xc0, 0x03 };
  44. TEST_F(Rdata_DNAME_Test, createFromText)
  45. {
  46. EXPECT_EQ(0, rdata_dname.compare(generic::DNAME("dn.example.com")));
  47. // explicitly add a trailing dot. should be the same RDATA.
  48. EXPECT_EQ(0, rdata_dname.compare(generic::DNAME("dn.example.com.")));
  49. // should be case sensitive.
  50. EXPECT_EQ(0, rdata_dname.compare(generic::DNAME("DN.EXAMPLE.COM")));
  51. // RDATA of a class-independent type should be recognized for any
  52. // "unknown" class.
  53. EXPECT_EQ(0, rdata_dname.compare(*createRdata(RRType("DNAME"),
  54. RRClass(65000),
  55. "dn.example.com")));
  56. }
  57. TEST_F(Rdata_DNAME_Test, createFromWire)
  58. {
  59. EXPECT_EQ(0, rdata_dname.compare(
  60. *rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  61. "testdata/rdata_dname_fromWire")));
  62. // RDLENGTH is too short
  63. EXPECT_THROW(rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  64. "testdata/rdata_dname_fromWire", 18),
  65. InvalidRdataLength);
  66. // RDLENGTH is too long
  67. EXPECT_THROW(rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  68. "testdata/rdata_dname_fromWire", 36),
  69. InvalidRdataLength);
  70. // incomplete name. the error should be detected in the name constructor
  71. EXPECT_THROW(rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  72. "testdata/rdata_dname_fromWire", 71),
  73. IncompleteName);
  74. EXPECT_EQ(0, generic::DNAME("dn2.example.com").compare(
  75. *rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  76. "testdata/rdata_dname_fromWire", 55)));
  77. EXPECT_THROW(*rdataFactoryFromFile(RRType("DNAME"), RRClass("IN"),
  78. "testdata/rdata_dname_fromWire", 63),
  79. InvalidRdataLength);
  80. }
  81. TEST_F(Rdata_DNAME_Test, toWireBuffer)
  82. {
  83. rdata_dname.toWire(obuffer);
  84. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  85. obuffer.getData(), obuffer.getLength(),
  86. wiredata_dname, sizeof(wiredata_dname));
  87. }
  88. TEST_F(Rdata_DNAME_Test, toWireRenderer)
  89. {
  90. rdata_dname.toWire(renderer);
  91. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  92. obuffer.getData(), obuffer.getLength(),
  93. wiredata_dname, sizeof(wiredata_dname));
  94. rdata_dname2.toWire(renderer);
  95. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  96. obuffer.getData(), obuffer.getLength(),
  97. wiredata_dname2, sizeof(wiredata_dname2));
  98. }
  99. TEST_F(Rdata_DNAME_Test, toText)
  100. {
  101. EXPECT_EQ("dn.example.com.", rdata_dname.toText());
  102. }
  103. TEST_F(Rdata_DNAME_Test, getDname)
  104. {
  105. EXPECT_EQ(Name("dn.example.com."), rdata_dname.getDname());
  106. }
  107. }