rrparamregistry_unittest.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <string>
  16. #include <sstream>
  17. #include <stdint.h>
  18. #include <gtest/gtest.h>
  19. #include "rrclass.h"
  20. #include "rrparamregistry.h"
  21. #include "rrtype.h"
  22. using namespace std;
  23. using namespace isc::dns;
  24. namespace {
  25. class RRParamRegistryTest : public ::testing::Test {
  26. protected:
  27. RRParamRegistryTest()
  28. {
  29. ostringstream oss1;
  30. oss1 << test_class_code;
  31. test_class_unknown_str = "CLASS" + oss1.str();
  32. ostringstream oss2;
  33. oss2 << test_type_code;
  34. test_type_unknown_str = "TYPE" + oss2.str();
  35. }
  36. string test_class_unknown_str;
  37. string test_type_unknown_str;
  38. // we assume class/type numbers are officially unassigned. If not we'll
  39. // need to update the test cases.
  40. static const uint16_t test_class_code = 65533;
  41. static const uint16_t test_type_code = 65534;
  42. static const string test_class_str;
  43. static const string test_type_str;
  44. };
  45. const string RRParamRegistryTest::test_class_str("TESTCLASS");
  46. const string RRParamRegistryTest::test_type_str("TESTTYPE");
  47. TEST_F(RRParamRegistryTest, addRemove)
  48. {
  49. RRParamRegistry::getRegistry().addType(test_type_str, test_type_code);
  50. RRParamRegistry::getRegistry().addClass(test_class_str, test_class_code);
  51. EXPECT_EQ(65533, RRClass("TESTCLASS").getCode());
  52. EXPECT_EQ(65534, RRType("TESTTYPE").getCode());
  53. // the first removal attempt should succeed
  54. EXPECT_TRUE(RRParamRegistry::getRegistry().removeType(65534));
  55. // then toText() should treat it as an "unknown"
  56. EXPECT_EQ(test_type_unknown_str, RRType(test_type_code).toText());
  57. // attempt of removing non-existent mapping should result in 'false'
  58. EXPECT_FALSE(RRParamRegistry::getRegistry().removeType(65534));
  59. // same set of tests for RR class.
  60. EXPECT_TRUE(RRParamRegistry::getRegistry().removeClass(65533));
  61. EXPECT_EQ(test_class_unknown_str, RRClass(test_class_code).toText());
  62. EXPECT_FALSE(RRParamRegistry::getRegistry().removeClass(65533));
  63. }
  64. TEST_F(RRParamRegistryTest, addError)
  65. {
  66. // An attempt to override a pre-registered class should fail with an
  67. // exception, and the pre-registered one should remain in the registry.
  68. EXPECT_THROW(RRParamRegistry::getRegistry().addClass(test_class_str, 1),
  69. RRClassExists);
  70. EXPECT_EQ("IN", RRClass(1).toText());
  71. // Same for RRType
  72. EXPECT_THROW(RRParamRegistry::getRegistry().addType(test_type_str, 1),
  73. RRTypeExists);
  74. EXPECT_EQ("A", RRType(1).toText());
  75. }
  76. }