From: JINMEI Tatuya Date: Mon, 13 Feb 2012 17:59:39 +0000 (-0800) Subject: [1576] made DefaultNSEC3HashCreator so an experimental app can use it internally. X-Git-Tag: trac2351_base~97^2~35^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=540d2a66787b2ff57e20a2c99a84d2875dfbcc54;p=thirdparty%2Fkea.git [1576] made DefaultNSEC3HashCreator so an experimental app can use it internally. --- diff --git a/src/lib/dns/nsec3hash.cc b/src/lib/dns/nsec3hash.cc index cee504fd2e..159dff3fb7 100644 --- a/src/lib/dns/nsec3hash.cc +++ b/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; diff --git a/src/lib/dns/nsec3hash.h b/src/lib/dns/nsec3hash.h index b743948b53..20567086c9 100644 --- a/src/lib/dns/nsec3hash.h +++ b/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 diff --git a/src/lib/dns/tests/nsec3hash_unittest.cc b/src/lib/dns/tests/nsec3hash_unittest.cc index 3f2dad2c8f..e607c74088 100644 --- a/src/lib/dns/tests/nsec3hash_unittest.cc +++ b/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")));