From: Mukund Sivaraman Date: Wed, 26 Dec 2012 05:47:52 +0000 (+0530) Subject: [2432] Don't throw when comparing iterators from different RRsetCollectionBase implem... X-Git-Tag: bind10-1.0.0-rc-release~153 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=12b24545cb9aeb86229694cda68c71577587f125;p=thirdparty%2Fkea.git [2432] Don't throw when comparing iterators from different RRsetCollectionBase implementations --- diff --git a/src/lib/dns/rrset_collection.h b/src/lib/dns/rrset_collection.h index fa8592768e..9cbf98c425 100644 --- a/src/lib/dns/rrset_collection.h +++ b/src/lib/dns/rrset_collection.h @@ -117,8 +117,11 @@ protected: } virtual bool equals(Iter& other) { - const DnsIter& other_real = dynamic_cast(other); - return (iter_ == other_real.iter_); + const DnsIter* other_real = dynamic_cast(&other); + if (other_real == NULL) { + return (false); + } + return (iter_ == other_real->iter_); } private: diff --git a/src/lib/dns/tests/rrset_collection_unittest.cc b/src/lib/dns/tests/rrset_collection_unittest.cc index 29853e12bc..c7b66366c5 100644 --- a/src/lib/dns/tests/rrset_collection_unittest.cc +++ b/src/lib/dns/tests/rrset_collection_unittest.cc @@ -18,6 +18,8 @@ #include +#include + using namespace isc::dns; using namespace isc::dns::rdata; using namespace std; @@ -178,4 +180,77 @@ TEST_F(RRsetCollectionTest, iteratorTest) { EXPECT_EQ(4, count); } +// This is a dummy class which is used in iteratorCompareDifferent test +// to compare iterators from different RRsetCollectionBase +// implementations. +class MyRRsetCollection : public RRsetCollectionBase { +public: + MyRRsetCollection() + {} + + virtual const isc::dns::AbstractRRset* find + (const isc::dns::Name&, const isc::dns::RRType&, + const isc::dns::RRClass&) + const + { + return (NULL); + } + + typedef std::list MyCollection; + +protected: + class MyIter : public RRsetCollectionBase::Iter { + public: + MyIter(MyCollection::iterator& iter) : + iter_(iter) + {} + + virtual const isc::dns::AbstractRRset& getValue() { + return (*iter_); + } + + virtual IterPtr getNext() { + MyCollection::iterator it = iter_; + it++; + return (RRsetCollectionBase::IterPtr(new MyIter(it))); + } + + virtual bool equals(Iter& other) { + const MyIter* other_real = dynamic_cast(&other); + if (other_real == NULL) { + return (false); + } + return (iter_ == other_real->iter_); + } + + private: + MyCollection::iterator iter_; + }; + + virtual RRsetCollectionBase::IterPtr getBeginning() { + MyCollection::iterator it = dummy_list_.begin(); + return (RRsetCollectionBase::IterPtr(new MyIter(it))); + } + + virtual RRsetCollectionBase::IterPtr getEnd() { + MyCollection::iterator it = dummy_list_.end(); + return (RRsetCollectionBase::IterPtr(new MyIter(it))); + } + +private: + MyCollection dummy_list_; +}; + +TEST_F(RRsetCollectionTest, iteratorCompareDifferent) { + // Create objects of two different RRsetCollectionBase + // implementations. + RRsetCollection cln1; + MyRRsetCollection cln2; + + // Comparing two iterators from different RRsetCollectionBase + // implementations must not throw. + EXPECT_NE(cln2.begin(), cln1.begin()); + EXPECT_NE(cln1.end(), cln2.end()); +} + } // namespace