From: Mukund Sivaraman Date: Thu, 28 Mar 2013 01:48:06 +0000 (+0530) Subject: [2387] Rewrite unified NSEC3 tests using helper methods X-Git-Tag: bind10-1.2.0beta1-release~481^2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b5d7336d2353cd552c2b253a358f612b54b56e54;p=thirdparty%2Fkea.git [2387] Rewrite unified NSEC3 tests using helper methods --- diff --git a/src/lib/dns/tests/rdata_nsec3_unittest.cc b/src/lib/dns/tests/rdata_nsec3_unittest.cc index 6aceaa8edb..959ae73622 100644 --- a/src/lib/dns/tests/rdata_nsec3_unittest.cc +++ b/src/lib/dns/tests/rdata_nsec3_unittest.cc @@ -43,66 +43,91 @@ namespace { // Note: some tests can be shared with NSEC3PARAM. They are unified as // typed tests defined in nsec3param_like_unittest. class Rdata_NSEC3_Test : public RdataTest { - // there's nothing to specialize -public: +protected: Rdata_NSEC3_Test() : nsec3_txt("1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6 " "NS SOA RRSIG DNSKEY NSEC3PARAM"), - nsec3_nosalt_txt("1 1 1 - H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A" ) + nsec3_nosalt_txt("1 1 1 - H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A"), + rdata_nsec3(nsec3_txt) {} + + void checkFromText_None(const string& rdata_str) { + checkFromText( + rdata_str, rdata_nsec3, false, false); + } + + void checkFromText_InvalidText(const string& rdata_str) { + checkFromText( + rdata_str, rdata_nsec3, true, true); + } + + void checkFromText_BadValue(const string& rdata_str) { + checkFromText( + rdata_str, rdata_nsec3, true, true); + } + + void checkFromText_LexerError(const string& rdata_str) { + checkFromText + ( + rdata_str, rdata_nsec3, true, true); + } + const string nsec3_txt; const string nsec3_nosalt_txt; + const generic::NSEC3 rdata_nsec3; }; TEST_F(Rdata_NSEC3_Test, fromText) { - // A normal case: the test constructor should successfully parse the - // text and construct nsec3_txt. It will be tested against the wire format - // representation in the createFromWire test. - - // hash that has the possible max length (see badText about the magic - // numbers) + // Hash that has the possible max length EXPECT_EQ(255, generic::NSEC3("1 1 1 D399EAAB " + string((255 * 8) / 5, '0') + " NS").getNext().size()); - // type bitmap is empty. it's possible and allowed for NSEC3. + // Hash is too long. Max = 255 bytes, base32-hex converts each 5 bytes + // of the original to 8 characters, so 260 * 8 / 5 is the smallest length + // of the encoded string that exceeds the max and doesn't require padding. + checkFromText_InvalidText("1 1 1 D399EAAB " + string((260 * 8) / 5, '0') + + " NS"); + + // Type bitmap is empty. it's possible and allowed for NSEC3. EXPECT_NO_THROW(generic::NSEC3( "1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6")); - // empty salt is also okay. + // Empty salt is also okay. EXPECT_NO_THROW(const generic::NSEC3 rdata_nosalt_nsec3(nsec3_nosalt_txt)); -} -TEST_F(Rdata_NSEC3_Test, badText) { - EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE " - "0123456789ABCDEFGHIJKLMNOPQRSTUV " - "BIFF POW SPOON"), - InvalidRdataText); - EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE " - "WXYZWXYZWXYZ=WXYZWXYZ==WXYZWXYZW A NS SOA"), - BadValue); // bad base32hex - EXPECT_THROW(generic::NSEC3("1 1 1000000 ADDAFEEE " - "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"), - InvalidRdataText); + // Bad type mnemonics + checkFromText_InvalidText("1 1 1 ADDAFEEE 0123456789ABCDEFGHIJKLMNOPQRSTUV " + "BIFF POW SPOON"); + + // Bad base32hex + checkFromText_BadValue("1 1 1 ADDAFEEE " + "WXYZWXYZWXYZ=WXYZWXYZ==WXYZWXYZW A NS SOA"); + + // Hash algorithm out of range + checkFromText_InvalidText("256 1 1 ADDAFEEE " + "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"); + + // Flags out of range + checkFromText_InvalidText("1 256 1 ADDAFEEE " + "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"); + + // Iterations out of range + checkFromText_InvalidText("1 1 65536 ADDAFEEE " + "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"); + + // Space is not allowed in salt (this actually causes the Base64 + // decoder to throw) + checkFromText_BadValue("1 1 1 D399 EAAB H9RSFB7FPF2L8" + "HG35CMPC765TDK23RP6"); // Next hash shouldn't be padded - EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE CPNMU=== A NS SOA"), - InvalidRdataText); + checkFromText_InvalidText("1 1 1 ADDAFEEE CPNMU=== A NS SOA"); // String instead of number - EXPECT_THROW(generic::NSEC3("foo 1 1 ADDAFEEECPNMU=== A NS SOA"), - InvalidRdataText); - EXPECT_THROW(generic::NSEC3("1 foo 1 ADDAFEEECPNMU=== A NS SOA"), - InvalidRdataText); - EXPECT_THROW(generic::NSEC3("1 1 foo ADDAFEEECPNMU=== A NS SOA"), - InvalidRdataText); - - // Hash is too long. Max = 255 bytes, base32-hex converts each 5 bytes - // of the original to 8 characters, so 260 * 8 / 5 is the smallest length - // of the encoded string that exceeds the max and doesn't require padding. - EXPECT_THROW(generic::NSEC3("1 1 1 D399EAAB " + string((260 * 8) / 5, '0') + - " NS"), - InvalidRdataText); + checkFromText_LexerError("foo 1 1 ADDAFEEECPNMU=== A NS SOA"); + checkFromText_LexerError("1 foo 1 ADDAFEEECPNMU=== A NS SOA"); + checkFromText_LexerError("1 1 foo ADDAFEEECPNMU=== A NS SOA"); } TEST_F(Rdata_NSEC3_Test, createFromWire) { @@ -152,29 +177,6 @@ TEST_F(Rdata_NSEC3_Test, createFromLexer) { EXPECT_EQ(0, rdata_nosalt_nsec3.compare( *test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), nsec3_nosalt_txt))); - - // Exceptions cause NULL to be returned. - - // hash algorithm out of range - EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), - "256 1 1 D399EAAB H9RSFB7FPF2L8" - "HG35CMPC765TDK23RP6")); - // flags out of range - EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), - "1 256 1 D399EAAB H9RSFB7FPF2L8" - "HG35CMPC765TDK23RP6")); - // iterations out of range - EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), - "1 1 65536 D399EAAB H9RSFB7FPF2L8" - "HG35CMPC765TDK23RP6")); - // space is not allowed in salt - EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), - "1 1 1 D399 EAAB H9RSFB7FPF2L8" - "HG35CMPC765TDK23RP6")); - // Next hash is padded. - EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(), - "1 1 1 ADDAFEEE CPNMU=== " - "A NS SOA")); } TEST_F(Rdata_NSEC3_Test, assign) {