From 1a46994e855e3a2a0b5b407df94e52ada3efee7e Mon Sep 17 00:00:00 2001 From: Mukund Sivaraman Date: Tue, 27 Mar 2012 13:06:18 +0530 Subject: [PATCH] [1627] Unify testcases for NameParserException checks --- src/lib/dns/tests/name_unittest.cc | 88 ++++++++++-------------------- 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/src/lib/dns/tests/name_unittest.cc b/src/lib/dns/tests/name_unittest.cc index 31d8396e42..7e4c7b3cdc 100644 --- a/src/lib/dns/tests/name_unittest.cc +++ b/src/lib/dns/tests/name_unittest.cc @@ -130,6 +130,15 @@ TEST_F(NameTest, nonlocalObject) { EXPECT_EQ("\\255.example.com.", downcased_global.toText()); } +template +void +checkBadTextName(const string& txt) { + // Check it results in the specified type of exception as well as + // NameParserException. + EXPECT_THROW(Name(txt, false), ExceptionType); + EXPECT_THROW(Name(txt, false), NameParserException); +} + TEST_F(NameTest, fromText) { vector strnames; strnames.push_back("www.example.com"); @@ -154,42 +163,42 @@ TEST_F(NameTest, fromText) { // Tests for bogus names. These should trigger exceptions. // // empty label cannot be followed by another label - EXPECT_THROW(Name(".a"), EmptyLabel); + checkBadTextName(".a"); // duplicate period - EXPECT_THROW(Name("a.."), EmptyLabel); + checkBadTextName("a.."); // label length must be < 64 - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "0123"), TooLongLabel); + checkBadTextName("012345678901234567890123456789" + "012345678901234567890123456789" + "0123"); // now-unsupported bitstring labels - EXPECT_THROW(Name("\\[b11010000011101]"), BadLabelType); + checkBadTextName("\\[b11010000011101]"); // label length must be < 64 - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "012\\x"), TooLongLabel); + checkBadTextName("012345678901234567890123456789" + "012345678901234567890123456789" + "012\\x"); // but okay as long as resulting len < 64 even if the original string is // "too long" EXPECT_NO_THROW(Name("012345678901234567890123456789" "012345678901234567890123456789" "01\\x")); // incomplete \DDD pattern (exactly 3 D's must appear) - EXPECT_THROW(Name("\\12abc"), BadEscape); + checkBadTextName("\\12abc"); // \DDD must not exceed 255 - EXPECT_THROW(Name("\\256"), BadEscape); + checkBadTextName("\\256"); // Same tests for \111 as for \\x above - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "012\\111"), TooLongLabel); + checkBadTextName("012345678901234567890123456789" + "012345678901234567890123456789" + "012\\111"); EXPECT_NO_THROW(Name("012345678901234567890123456789" "012345678901234567890123456789" "01\\111")); // A domain name must be 255 octets or less - EXPECT_THROW(Name("123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "1234"), TooLongName); + checkBadTextName("123456789.123456789.123456789.123456789.123456789." + "123456789.123456789.123456789.123456789.123456789." + "123456789.123456789.123456789.123456789.123456789." + "123456789.123456789.123456789.123456789.123456789." + "123456789.123456789.123456789.123456789.123456789." + "1234"); // This is a possible longest name and should be accepted EXPECT_NO_THROW(Name("123456789.123456789.123456789.123456789.123456789." "123456789.123456789.123456789.123456789.123456789." @@ -198,7 +207,7 @@ TEST_F(NameTest, fromText) { "123456789.123456789.123456789.123456789.123456789." "123")); // \DDD must consist of 3 digits. - EXPECT_THROW(Name("\\12"), IncompleteName); + checkBadTextName("\\12"); // a name with the max number of labels. should be constructed without // an error, and its length should be the max value. @@ -212,43 +221,6 @@ TEST_F(NameTest, fromText) { EXPECT_EQ(Name::MAX_LABELS, maxlabels.getLabelCount()); } -TEST_F(NameTest, testNameParserExceptions) { - // - // Tests for bogus names. These should trigger exceptions. - // - // empty label cannot be followed by another label - EXPECT_THROW(Name(".a"), NameParserException); - // duplicate period - EXPECT_THROW(Name("a.."), NameParserException); - // label length must be < 64 - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "0123"), NameParserException); - // now-unsupported bitstring labels - EXPECT_THROW(Name("\\[b11010000011101]"), NameParserException); - // label length must be < 64 - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "012\\x"), NameParserException); - // incomplete \DDD pattern (exactly 3 D's must appear) - EXPECT_THROW(Name("\\12abc"), NameParserException); - // \DDD must not exceed 255 - EXPECT_THROW(Name("\\256"), NameParserException); - // Same tests for \111 as for \\x above - EXPECT_THROW(Name("012345678901234567890123456789" - "012345678901234567890123456789" - "012\\111"), NameParserException); - // A domain name must be 255 octets or less - EXPECT_THROW(Name("123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "123456789.123456789.123456789.123456789.123456789." - "1234"), NameParserException); - // \DDD must consist of 3 digits. - EXPECT_THROW(Name("\\12"), NameParserException); -} - TEST_F(NameTest, fromWire) { // // test cases derived from BIND9 tests.