rdata_opt_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/messagerenderer.h>
  16. #include <dns/rdata.h>
  17. #include <dns/rdataclass.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrtype.h>
  20. #include <gtest/gtest.h>
  21. #include <dns/tests/unittest_util.h>
  22. #include <dns/tests/rdata_unittest.h>
  23. using isc::UnitTestUtil;
  24. using namespace std;
  25. using namespace isc::dns;
  26. using namespace isc::util;
  27. using namespace isc::dns::rdata;
  28. namespace {
  29. class Rdata_OPT_Test : public RdataTest {
  30. // there's nothing to specialize
  31. };
  32. const generic::OPT rdata_opt;
  33. TEST_F(Rdata_OPT_Test, createFromText) {
  34. // OPT RR cannot be created from text.
  35. EXPECT_THROW(generic::OPT("this does not matter"), InvalidRdataText);
  36. }
  37. TEST_F(Rdata_OPT_Test, createFromWire) {
  38. // Valid cases: in the simple implementation with no supported options,
  39. // we can only check these don't throw.
  40. EXPECT_NO_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass("CLASS4096"),
  41. "rdata_opt_fromWire"));
  42. EXPECT_NO_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::CH(),
  43. "rdata_opt_fromWire", 2));
  44. // short buffer case.
  45. EXPECT_THROW(rdataFactoryFromFile(RRType::OPT(), RRClass::IN(),
  46. "rdata_opt_fromWire", 11),
  47. InvalidBufferPosition);
  48. }
  49. TEST_F(Rdata_OPT_Test, createFromLexer) {
  50. // OPT RR cannot be created from text. Exceptions cause NULL to be
  51. // returned.
  52. EXPECT_FALSE(test::createRdataUsingLexer(RRType::OPT(), RRClass::IN(),
  53. "this does not matter"));
  54. }
  55. TEST_F(Rdata_OPT_Test, toWireBuffer) {
  56. rdata_opt.toWire(obuffer);
  57. EXPECT_EQ(0, obuffer.getLength());
  58. }
  59. TEST_F(Rdata_OPT_Test, toWireRenderer) {
  60. rdata_opt.toWire(renderer);
  61. EXPECT_EQ(0, obuffer.getLength());
  62. }
  63. TEST_F(Rdata_OPT_Test, toText) {
  64. EXPECT_THROW(rdata_opt.toText(),
  65. isc::InvalidOperation);
  66. }
  67. TEST_F(Rdata_OPT_Test, compare) {
  68. EXPECT_THROW(rdata_opt.compare(
  69. *rdataFactoryFromFile(RRType::OPT(), RRClass::CH(),
  70. "rdata_opt_fromWire", 2)),
  71. isc::InvalidOperation);
  72. // comparison attempt between incompatible RR types also results in
  73. // isc::InvalidOperation.
  74. EXPECT_THROW(rdata_opt.compare(*RdataTest::rdata_nomatch),
  75. isc::InvalidOperation);
  76. }
  77. TEST_F(Rdata_OPT_Test, append) {
  78. EXPECT_THROW(rdata_opt.toText(),
  79. isc::InvalidOperation);
  80. }
  81. TEST_F(Rdata_OPT_Test, getPseudoRRs) {
  82. const generic::OPT rdf =
  83. dynamic_cast<const generic::OPT&>
  84. (*rdataFactoryFromFile(RRType("OPT"), RRClass("IN"),
  85. "rdata_opt_fromWire", 2));
  86. const std::vector<generic::OPT::PseudoRR>& rrs = rdf.getPseudoRRs();
  87. ASSERT_FALSE(rrs.empty());
  88. EXPECT_EQ(1, rrs.size());
  89. EXPECT_EQ(3, rrs.at(0).getCode());
  90. EXPECT_EQ(3, rrs.at(0).getLength());
  91. const uint8_t expected_data[] = {0x00, 0x01, 0x02};
  92. const uint8_t* actual_data = rrs.at(0).getData();
  93. EXPECT_EQ(0, std::memcmp(expected_data, actual_data,
  94. sizeof(expected_data)));
  95. }
  96. }