]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
bug #1627: Add common base class for name parser exceptions
authorMukund Sivaraman <muks@isc.org>
Mon, 26 Mar 2012 11:59:47 +0000 (17:29 +0530)
committerMukund Sivaraman <muks@isc.org>
Fri, 30 Mar 2012 09:13:28 +0000 (14:43 +0530)
src/lib/dns/name.h
src/lib/dns/tests/name_unittest.cc

index ca64d69a33233119a391436758f2bcdbb249b2c1..ef32f90d7bbf11cd880447ed12af6b1a11e6a714 100644 (file)
@@ -31,34 +31,43 @@ class OutputBuffer;
 namespace dns {
 class AbstractMessageRenderer;
 
+///
+/// \brief Base class for name parser exceptions.
+///
+class NameParserException : public Exception {
+public:
+    NameParserException(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
 ///
 /// \brief A standard DNS module exception that is thrown if the name parser
 /// encounters an empty label in the middle of a name.
 ///
-class EmptyLabel : public Exception {
+class EmptyLabel : public NameParserException {
 public:
     EmptyLabel(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
 /// \brief A standard DNS module exception that is thrown if the name parser
 /// encounters too long a name.
 ///
-class TooLongName : public Exception {
+class TooLongName : public NameParserException {
 public:
     TooLongName(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
 /// \brief A standard DNS module exception that is thrown if the name parser
 /// encounters too long a label.
 ///
-class TooLongLabel : public Exception {
+class TooLongLabel : public NameParserException {
 public:
     TooLongLabel(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
@@ -67,20 +76,20 @@ public:
 /// applies to bitstring labels, which would begin with "\[".  Incomplete cases
 /// include an incomplete escaped sequence such as "\12".
 ///
-class BadLabelType : public Exception {
+class BadLabelType : public NameParserException {
 public:
     BadLabelType(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
 /// \brief A standard DNS module exception that is thrown if the name parser
 /// fails to decode a "\"-escaped sequence.
 ///
-class BadEscape : public Exception {
+class BadEscape : public NameParserException {
 public:
     BadEscape(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
@@ -90,10 +99,10 @@ public:
 /// An attempt of constructing a name from an empty string will trigger this
 /// exception.
 ///
-class IncompleteName : public Exception {
+class IncompleteName : public NameParserException {
 public:
     IncompleteName(const char* file, size_t line, const char* what) :
-        isc::Exception(file, line, what) {}
+        NameParserException(file, line, what) {}
 };
 
 ///
index c5f3b7f73e0db566377979591b6ca6d106c4ad37..31d8396e421c1c2d1554b553c87cdee025bb49db 100644 (file)
@@ -151,7 +151,7 @@ TEST_F(NameTest, fromText) {
     EXPECT_EQ(Name("Www.eXample.coM", true).toText(), example_name.toText());
 
     //
-    // Tests for bogus names.  These should trigger an exception.
+    // Tests for bogus names.  These should trigger exceptions.
     //
     // empty label cannot be followed by another label
     EXPECT_THROW(Name(".a"), EmptyLabel);
@@ -212,6 +212,43 @@ 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.