Browse Source

[1576] made DefaultNSEC3HashCreator so an experimental app can use it internally.

JINMEI Tatuya 13 years ago
parent
commit
540d2a6678
3 changed files with 58 additions and 17 deletions
  1. 12 12
      src/lib/dns/nsec3hash.cc
  2. 21 3
      src/lib/dns/nsec3hash.h
  3. 25 2
      src/lib/dns/tests/nsec3hash_unittest.cc

+ 12 - 12
src/lib/dns/nsec3hash.cc

@@ -143,18 +143,6 @@ NSEC3HashRFC5155::match(const generic::NSEC3PARAM& nsec3param) const {
                   nsec3param.getSalt()));
 }
 
-class DefaultNSEC3HashCreator : public NSEC3HashCreator {
-public:
-    virtual NSEC3Hash* create(const generic::NSEC3PARAM& param) const {
-        return (new NSEC3HashRFC5155(param.getHashalg(), param.getIterations(),
-                                     param.getSalt()));
-    }
-    virtual NSEC3Hash* create(const generic::NSEC3& nsec3) const {
-        return (new NSEC3HashRFC5155(nsec3.getHashalg(), nsec3.getIterations(),
-                                 nsec3.getSalt()));
-    }
-};
-
 // A static pointer that refers to the currently usable creator.
 // Only get/setNSEC3HashCreator are expected to get access to this variable
 // directly.
@@ -186,6 +174,18 @@ NSEC3Hash::create(const generic::NSEC3& nsec3) {
     return (getNSEC3HashCreator()->create(nsec3));
 }
 
+NSEC3Hash*
+DefaultNSEC3HashCreator::create(const generic::NSEC3PARAM& param) const {
+    return (new NSEC3HashRFC5155(param.getHashalg(), param.getIterations(),
+                                 param.getSalt()));
+}
+
+NSEC3Hash*
+DefaultNSEC3HashCreator::create(const generic::NSEC3& nsec3) const {
+    return (new NSEC3HashRFC5155(nsec3.getHashalg(), nsec3.getIterations(),
+                                 nsec3.getSalt()));
+}
+
 void
 setNSEC3HashCreator(const NSEC3HashCreator* new_creator) {
     creator = new_creator;

+ 21 - 3
src/lib/dns/nsec3hash.h

@@ -170,9 +170,12 @@ public:
 /// The two main methods named \c create() correspond to the static factory
 /// methods of \c NSEC3Hash of the same name.
 ///
-/// By default, the library uses a builtin creator implementation.  The
-/// \c setNSEC3HashCreator() function can be used to replace it with a user
-/// defined version.
+/// By default, the library uses the \c DefaultNSEC3HashCreator creator.
+/// The \c setNSEC3HashCreator() function can be used to replace it with a
+/// user defined version.  For such customization purposes as implementing
+/// experimental new hash algorithms, the application may internally want to
+/// use the \c DefaultNSEC3HashCreator in general cases while creating a
+/// customized type of \c NSEC3Hash object for that particular hash algorithm.
 ///
 /// The creator objects are generally expected to be stateless; they will
 /// only encapsulate the factory logic.  The \c create() methods are declared
@@ -209,6 +212,21 @@ public:
         const = 0;
 };
 
+/// \brief The default NSEC3Hash creator.
+///
+/// This derived class implements the \c NSEC3HashCreator interfaces for
+/// the standard NSEC3 hash calculator as defined in RFC5155.  The library
+/// will use this creator by default, so normal applications don't have to
+/// be aware of this class at all.  This class is publicly visible for the
+/// convenience of special applications that want to customize the creator
+/// behavior for a particular type of parameters while preserving the default
+/// behavior for others.
+class DefaultNSEC3HashCreator : public NSEC3HashCreator {
+public:
+    virtual NSEC3Hash* create(const rdata::generic::NSEC3PARAM& param) const;
+    virtual NSEC3Hash* create(const rdata::generic::NSEC3& nsec3) const;
+};
+
 /// \brief The registrar of \c NSEC3HashCreator.
 ///
 /// This function sets or resets the system-wide \c NSEC3HashCreator that

+ 25 - 2
src/lib/dns/tests/nsec3hash_unittest.cc

@@ -160,14 +160,25 @@ class TestNSEC3Hash : public NSEC3Hash {
     }
 };
 
+// This faked creator basically creates the faked calculator regardless of
+// the passed NSEC3PARAM or NSEC3.  But if the most significant bit of flags
+// is set, it will behave like the default creator.
 class TestNSEC3HashCreator : public NSEC3HashCreator {
 public:
-    virtual NSEC3Hash* create(const generic::NSEC3PARAM&) const {
+    virtual NSEC3Hash* create(const generic::NSEC3PARAM& param) const {
+        if ((param.getFlags() & 0x80) != 0) {
+            return (default_creator_.create(param));
+        }
         return (new TestNSEC3Hash);
     }
-    virtual NSEC3Hash* create(const generic::NSEC3&) const {
+    virtual NSEC3Hash* create(const generic::NSEC3& nsec3) const {
+        if ((nsec3.getFlags() & 0x80) != 0) {
+            return (default_creator_.create(nsec3));
+        }
         return (new TestNSEC3Hash);
     }
+private:
+    DefaultNSEC3HashCreator default_creator_;
 };
 
 TEST_F(NSEC3HashTest, setCreator) {
@@ -189,6 +200,18 @@ TEST_F(NSEC3HashTest, setCreator) {
     EXPECT_EQ("00000000000000000000000000000000",
               test_hash->calculate(Name("example")));
 
+    // If we set a special flag big (0x80) on creation, it will act like the
+    // default creator.
+    test_hash.reset(NSEC3Hash::create(generic::NSEC3PARAM(
+                                          "1 128 12 aabbccdd")));
+    EXPECT_EQ("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
+              test_hash->calculate(Name("example")));
+    test_hash.reset(NSEC3Hash::create(generic::NSEC3
+                                      ("1 128 12 aabbccdd " +
+                                       string(nsec3_common))));
+    EXPECT_EQ("0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
+              test_hash->calculate(Name("example")));
+
     // Reset the creator to default, and confirm that
     setNSEC3HashCreator(NULL);
     test_hash.reset(NSEC3Hash::create(generic::NSEC3PARAM("1 0 12 aabbccdd")));