rrparamregistry_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <string>
  15. #include <sstream>
  16. #include <stdint.h>
  17. #include <gtest/gtest.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <dns/rrparamregistry.h>
  22. #include <dns/rrtype.h>
  23. using namespace std;
  24. using namespace isc::dns;
  25. using namespace isc::util;
  26. using namespace isc::dns::rdata;
  27. namespace {
  28. class RRParamRegistryTest : public ::testing::Test {
  29. protected:
  30. RRParamRegistryTest()
  31. {
  32. ostringstream oss1;
  33. oss1 << test_class_code;
  34. // cppcheck-suppress useInitializationList
  35. test_class_unknown_str = "CLASS" + oss1.str();
  36. ostringstream oss2;
  37. oss2 << test_type_code;
  38. test_type_unknown_str = "TYPE" + oss2.str();
  39. }
  40. ~RRParamRegistryTest()
  41. {
  42. // cleanup any non well-known parameters that possibly remain
  43. // as a side effect.
  44. RRParamRegistry::getRegistry().removeType(test_type_code);
  45. RRParamRegistry::getRegistry().removeClass(test_class_code);
  46. RRParamRegistry::getRegistry().removeRdataFactory(
  47. RRType(test_type_code), RRClass(test_class_code));
  48. RRParamRegistry::getRegistry().removeRdataFactory(
  49. RRType(test_type_code));
  50. }
  51. string test_class_unknown_str;
  52. string test_type_unknown_str;
  53. // we assume class/type numbers are officially unassigned. If not we'll
  54. // need to update the test cases.
  55. static const uint16_t test_class_code = 65533;
  56. static const uint16_t test_type_code = 65534;
  57. static const string test_class_str;
  58. static const string test_type_str;
  59. };
  60. const string RRParamRegistryTest::test_class_str("TESTCLASS");
  61. const string RRParamRegistryTest::test_type_str("TESTTYPE");
  62. TEST_F(RRParamRegistryTest, addRemove) {
  63. RRParamRegistry::getRegistry().addType(test_type_str, test_type_code);
  64. RRParamRegistry::getRegistry().addClass(test_class_str, test_class_code);
  65. EXPECT_EQ(65533, RRClass("TESTCLASS").getCode());
  66. EXPECT_EQ(65534, RRType("TESTTYPE").getCode());
  67. // the first removal attempt should succeed
  68. EXPECT_TRUE(RRParamRegistry::getRegistry().removeType(test_type_code));
  69. // then toText() should treat it as an "unknown"
  70. EXPECT_EQ(test_type_unknown_str, RRType(test_type_code).toText());
  71. // attempt of removing non-existent mapping should result in 'false'
  72. EXPECT_FALSE(RRParamRegistry::getRegistry().removeType(test_type_code));
  73. // same set of tests for RR class.
  74. EXPECT_TRUE(RRParamRegistry::getRegistry().removeClass(test_class_code));
  75. EXPECT_EQ(test_class_unknown_str, RRClass(test_class_code).toText());
  76. EXPECT_FALSE(RRParamRegistry::getRegistry().removeClass(test_class_code));
  77. }
  78. TEST_F(RRParamRegistryTest, addError) {
  79. // An attempt to override a pre-registered class should fail with an
  80. // exception, and the pre-registered one should remain in the registry.
  81. EXPECT_THROW(RRParamRegistry::getRegistry().addClass(test_class_str, 1),
  82. RRClassExists);
  83. EXPECT_EQ("IN", RRClass(1).toText());
  84. // Same for RRType
  85. EXPECT_THROW(RRParamRegistry::getRegistry().addType(test_type_str, 1),
  86. RRTypeExists);
  87. EXPECT_EQ("A", RRType(1).toText());
  88. }
  89. class TestRdataFactory : public AbstractRdataFactory {
  90. public:
  91. virtual RdataPtr create(const string& rdata_str) const
  92. { return (RdataPtr(new in::A(rdata_str))); }
  93. virtual RdataPtr create(InputBuffer& buffer, size_t rdata_len) const
  94. { return (RdataPtr(new in::A(buffer, rdata_len))); }
  95. virtual RdataPtr create(const Rdata& source) const
  96. { return (RdataPtr(new in::A(dynamic_cast<const in::A&>(source)))); }
  97. };
  98. TEST_F(RRParamRegistryTest, addRemoveFactory) {
  99. // By default, the test type/code pair should be considered "unknown",
  100. // so the following should trigger an exception.
  101. EXPECT_THROW(createRdata(RRType(test_type_code), RRClass(test_class_code),
  102. "192.0.2.1"),
  103. InvalidRdataText);
  104. // Add factories so that we can treat this pair just like in::A.
  105. RRParamRegistry::getRegistry().add(test_type_str, test_type_code,
  106. test_class_str, test_class_code,
  107. RdataFactoryPtr(new TestRdataFactory));
  108. // Now it should be accepted, and should be identical to the same data of
  109. // in::A.
  110. EXPECT_EQ(0, in::A("192.0.2.1").compare(
  111. *createRdata(RRType(test_type_code), RRClass(test_class_code),
  112. "192.0.2.1")));
  113. // It should still fail with other classes as we specified the factories
  114. // as class-specific.
  115. EXPECT_THROW(createRdata(RRType(test_type_code), RRClass("IN"),
  116. "192.0.2.1"),
  117. InvalidRdataText);
  118. // Add the factories also as a class independent RRtype
  119. RRParamRegistry::getRegistry().add(test_type_str, test_type_code,
  120. RdataFactoryPtr(new TestRdataFactory));
  121. // Now it should be okay for other classes than the test class.
  122. EXPECT_EQ(0, in::A("192.0.2.1").compare(
  123. *createRdata(RRType(test_type_code), RRClass("IN"),
  124. "192.0.2.1")));
  125. // Remove the added factories: first attempt should succeed; the second
  126. // should return false as there's no match
  127. EXPECT_TRUE(RRParamRegistry::getRegistry().removeRdataFactory(
  128. RRType(test_type_code), RRClass(test_class_code)));
  129. EXPECT_FALSE(RRParamRegistry::getRegistry().removeRdataFactory(
  130. RRType(test_type_code), RRClass(test_class_code)));
  131. EXPECT_TRUE(RRParamRegistry::getRegistry().removeRdataFactory(
  132. RRType(test_type_code)));
  133. EXPECT_FALSE(RRParamRegistry::getRegistry().removeRdataFactory(
  134. RRType(test_type_code)));
  135. }
  136. }