Browse Source

[2389] tepmlated common "from text" test pattern and used it for A/AAAA/SOA.

JINMEI Tatuya 12 years ago
parent
commit
ec7c54008a

+ 15 - 39
src/lib/dns/tests/rdata_in_a_unittest.cc

@@ -41,32 +41,11 @@ class Rdata_IN_A_Test : public RdataTest {
 protected:
     Rdata_IN_A_Test() : rdata_in_a("192.0.2.1") {}
 
-    // Common check to see the result of in::A Rdata construction either from
-    // std::string or with MasterLexer object.  If it's expected to succeed
-    // the result should be identical to the commonly used test data
-    // (rdata_in_a); otherwise it should result in the exception specified as
-    // the template parameter.
-    template <typename ExForString, typename ExForLexer>
-    void checkFromText(const string& in_a_txt,
+    void checkFromTextIN_A(const std::string& rdata_txt,
                        bool throw_str_version = true,
-                       bool throw_lexer_version = true)
-    {
-        if (throw_str_version) {
-            EXPECT_THROW(in::A in_a(in_a_txt), ExForString);
-        } else {
-            EXPECT_EQ(0, in::A(in_a_txt).compare(rdata_in_a));
-        }
-
-        std::stringstream ss(in_a_txt);
-        MasterLexer lexer;
-        lexer.pushSource(ss);
-        if (throw_lexer_version) {
-            EXPECT_THROW(in::A soa(lexer, NULL, MasterLoader::DEFAULT,
-                                   loader_cb), ExForLexer);
-        } else {
-                EXPECT_EQ(0, in::A(lexer, NULL, MasterLoader::DEFAULT,
-                           loader_cb).compare(rdata_in_a));
-        }
+                       bool throw_lexer_version = true) {
+        checkFromText<in::A, InvalidRdataText, InvalidRdataText>(
+            rdata_txt, rdata_in_a, throw_str_version, throw_lexer_version);
     }
 
     const in::A rdata_in_a;
@@ -77,35 +56,32 @@ const uint8_t wiredata_in_a[] = { 192, 0, 2, 1 };
 TEST_F(Rdata_IN_A_Test, createFromText) {
     // Normal case: no exception for either case, so the exception type
     // doesn't matter.
-    checkFromText<isc::Exception, isc::Exception>("192.0.2.1", false, false);
+    checkFromText<in::A, isc::Exception, isc::Exception>("192.0.2.1",
+                                                         rdata_in_a, false,
+                                                         false);
 
     // should reject an abbreviated form of IPv4 address
-    checkFromText<InvalidRdataText, InvalidRdataText>("10.1");
+    checkFromTextIN_A("10.1");
     // or an IPv6 address
-    checkFromText<InvalidRdataText, InvalidRdataText>("2001:db8::1234");
+    checkFromTextIN_A("2001:db8::1234");
     // or any meaningless text as an IP address
-    checkFromText<InvalidRdataText, InvalidRdataText>("xxx");
+    checkFromTextIN_A("xxx");
 
     // trailing white space: only string version throws
-    checkFromText<InvalidRdataText, InvalidRdataText>("192.0.2.1  ",
-                                                      true, false);
+    checkFromTextIN_A("192.0.2.1  ", true, false);
     // same for beginning white space.
-    checkFromText<InvalidRdataText, InvalidRdataText>("  192.0.2.1",
-                                                      true, false);
+    checkFromTextIN_A("  192.0.2.1", true, false);
     // same for trailing non-space garbage (note that lexer version still
     // ignore it; it's expected to be detected at a higher layer).
-    checkFromText<InvalidRdataText, InvalidRdataText>("192.0.2.1 xxx",
-                                                      true, false);
+    checkFromTextIN_A("192.0.2.1 xxx", true, false);
 
     // nul character after a valid textual representation.
     string nul_after_addr = "192.0.2.1";
     nul_after_addr.push_back(0);
-    checkFromText<InvalidRdataText, InvalidRdataText>(nul_after_addr, true,
-                                                      true);
+    checkFromTextIN_A(nul_after_addr, true, true);
 
     // a valid address surrounded by parentheses; only okay with lexer
-    checkFromText<InvalidRdataText, InvalidRdataText>("(192.0.2.1)", true,
-                                                      false);
+    checkFromTextIN_A("(192.0.2.1)", true, false);
 }
 
 TEST_F(Rdata_IN_A_Test, createFromWire) {

+ 15 - 34
src/lib/dns/tests/rdata_in_aaaa_unittest.cc

@@ -41,27 +41,13 @@ protected:
     // the result should be identical to the commonly used test data
     // (rdata_in_a); otherwise it should result in the exception specified as
     // the template parameter.
-    template <typename ExForString, typename ExForLexer>
-    void checkFromText(const string& in_aaaa_txt,
-                       bool throw_str_version = true,
-                       bool throw_lexer_version = true)
+    void checkFromTextIN_AAAA(const string& in_aaaa_txt,
+                              bool throw_str_version = true,
+                              bool throw_lexer_version = true)
     {
-        if (throw_str_version) {
-            EXPECT_THROW(in::AAAA in_aaaa(in_aaaa_txt), ExForString);
-        } else {
-            EXPECT_EQ(0, in::AAAA(in_aaaa_txt).compare(rdata_in_aaaa));
-        }
-
-        std::stringstream ss(in_aaaa_txt);
-        MasterLexer lexer;
-        lexer.pushSource(ss);
-        if (throw_lexer_version) {
-            EXPECT_THROW(in::AAAA soa(lexer, NULL, MasterLoader::DEFAULT,
-                                   loader_cb), ExForLexer);
-        } else {
-                EXPECT_EQ(0, in::AAAA(lexer, NULL, MasterLoader::DEFAULT,
-                           loader_cb).compare(rdata_in_aaaa));
-        }
+        checkFromText<in::AAAA, InvalidRdataText, InvalidRdataText>(
+            in_aaaa_txt, rdata_in_aaaa, throw_str_version,
+            throw_lexer_version);
     }
 
     const in::AAAA rdata_in_aaaa;
@@ -74,34 +60,29 @@ const uint8_t wiredata_in_aaaa[] = {
 TEST_F(Rdata_IN_AAAA_Test, createFromText) {
     // Normal case: no exception for either case, so the exception type
     // doesn't matter.
-    checkFromText<isc::Exception, isc::Exception>("2001:db8::1234", false,
-                                                  false);
+    checkFromText<in::AAAA, isc::Exception, isc::Exception>(
+        "2001:db8::1234", rdata_in_aaaa, false, false);
 
     // should reject an IP4 address.
-    checkFromText<InvalidRdataText, InvalidRdataText>("192.0.2.1");
+    checkFromTextIN_AAAA("192.0.2.1");
     // or any meaningless text as an IPv6 address
-    checkFromText<InvalidRdataText, InvalidRdataText>("xxx");
+    checkFromTextIN_AAAA("xxx");
 
     // trailing white space: only string version throws
-    checkFromText<InvalidRdataText, InvalidRdataText>("2001:db8::1234  ",
-                                                      true, false);
+    checkFromTextIN_AAAA("2001:db8::1234  ", true, false);
     // same for beginning white space.
-    checkFromText<InvalidRdataText, InvalidRdataText>("  2001:db8::1234",
-                                                      true, false);
+    checkFromTextIN_AAAA("  2001:db8::1234", true, false);
     // same for trailing non-space garbage (note that lexer version still
     // ignore it; it's expected to be detected at a higher layer).
-    checkFromText<InvalidRdataText, InvalidRdataText>("2001:db8::1234 xxx",
-                                                      true, false);
+    checkFromTextIN_AAAA("2001:db8::1234 xxx", true, false);
 
     // nul character after a valid textual representation.
     string nul_after_addr = "2001:db8::1234";
     nul_after_addr.push_back(0);
-    checkFromText<InvalidRdataText, InvalidRdataText>(nul_after_addr, true,
-                                                      true);
+    checkFromTextIN_AAAA(nul_after_addr, true, true);
 
     // a valid address surrounded by parentheses; only okay with lexer
-    checkFromText<InvalidRdataText, InvalidRdataText>("(2001:db8::1234)", true,
-                                                      false);
+    checkFromTextIN_AAAA("(2001:db8::1234)", true, false);
 }
 
 TEST_F(Rdata_IN_AAAA_Test, createFromWire) {

+ 59 - 61
src/lib/dns/tests/rdata_soa_unittest.cc

@@ -39,35 +39,14 @@ protected:
                   2010012601, 3600, 300, 3600000, 1200)
     {}
 
-    // Common check to see if the given text can be used to construct SOA
-    // Rdata that is identical rdata_soa.
-    void checkFromText(const char* soa_txt, const Name* origin = NULL) {
-        std::stringstream ss(soa_txt);
-        MasterLexer lexer;
-        lexer.pushSource(ss);
-
-        if (origin == NULL) {
-            // from-string constructor works correctly only when origin
-            // is NULL (by its nature).
-            EXPECT_EQ(0, generic::SOA(soa_txt).compare(rdata_soa));
-        }
-        EXPECT_EQ(0, generic::SOA(lexer, origin, MasterLoader::DEFAULT,
-                                  loader_cb).compare(rdata_soa));
-    }
-
-    // Common check if given text (which is invalid as SOA RDATA) is rejected
-    // with the specified type of exception: ExForString is the expected
-    // exception for the "from string" constructor; ExForLexer is for the
-    // constructor with master lexer.
     template <typename ExForString, typename ExForLexer>
-    void checkFromBadTexxt(const char* soa_txt, const Name* origin = NULL) {
-        EXPECT_THROW(generic::SOA soa(soa_txt), ExForString);
-
-        std::stringstream ss(soa_txt);
-        MasterLexer lexer;
-        lexer.pushSource(ss);
-        EXPECT_THROW(generic::SOA soa(lexer, origin, MasterLoader::DEFAULT,
-                                      loader_cb), ExForLexer);
+    void checkFromTextSOA(const string& soa_txt, const Name* origin = NULL,
+                          bool throw_str_version = true,
+                          bool throw_lexer_version = true)
+    {
+        checkFromText<generic::SOA, ExForString, ExForLexer>(
+            soa_txt, rdata_soa, throw_str_version, throw_lexer_version,
+            origin);
     }
 
     const generic::SOA rdata_soa;
@@ -75,87 +54,106 @@ protected:
 
 TEST_F(Rdata_SOA_Test, createFromText) {
     // A simple case.
-    checkFromText("ns.example.com. root.example.com. "
-                  "2010012601 3600 300 3600000 1200");
+    checkFromTextSOA<isc::Exception, isc::Exception>(
+        "ns.example.com. root.example.com. 2010012601 3600 300 3600000 1200",
+        NULL, false, false);
 
     // Beginning and trailing space are ignored.
-    checkFromText("  ns.example.com. root.example.com. "
-                  "2010012601 3600 300 3600000 1200  ");
+    checkFromTextSOA<isc::Exception, isc::Exception>(
+        "  ns.example.com. root.example.com. "
+        "2010012601 3600 300 3600000 1200  ", NULL, false, false);
 
     // using extended TTL-like form for some parameters.
-    checkFromText("ns.example.com. root.example.com. "
-                  "2010012601 1H 5M 1000H 20M");
+    checkFromTextSOA<isc::Exception, isc::Exception>(
+        "ns.example.com. root.example.com. 2010012601 1H 5M 1000H 20M",
+        NULL, false, false);
 
     // multi-line.
-    checkFromText("ns.example.com. (root.example.com.\n"
-                  "2010012601 1H 5M 1000H) 20M");
+    checkFromTextSOA<isc::Exception, isc::Exception>(
+        "ns.example.com. (root.example.com.\n"
+        "2010012601 1H 5M 1000H) 20M", NULL, false, false);
 
     // relative names for MNAME and RNAME with a separate origin (lexer
     // version only)
     const Name origin("example.com");
-    checkFromText("ns root 2010012601 1H 5M 1000H 20M", &origin);
+    checkFromTextSOA<MissingNameOrigin, isc::Exception>(
+        "ns root 2010012601 1H 5M 1000H 20M", &origin, true, false);
 
-    // with the '@' notation with a separate origin (lexer version only)
+    // with the '@' notation with a separate origin (lexer version only;
+    // string version would throw)
     const Name full_mname("ns.example.com");
-    checkFromText("@ root.example.com. 2010012601 1H 5M 1000H 20M",
-                  &full_mname);
+    checkFromTextSOA<MissingNameOrigin, isc::Exception>(
+        "@ root.example.com. 2010012601 1H 5M 1000H 20M", &full_mname, true,
+        false);
 
     // bad MNAME/RNAMEs
-    checkFromBadTexxt<EmptyLabel, EmptyLabel>(
+    checkFromTextSOA<EmptyLabel, EmptyLabel>(
         "bad..example. . 2010012601 1H 5M 1000H 20M");
-    checkFromBadTexxt<EmptyLabel, EmptyLabel>(
+    checkFromTextSOA<EmptyLabel, EmptyLabel>(
         ". bad..example. 2010012601 1H 5M 1000H 20M");
 
     // Names shouldn't be quoted. (Note: on completion of #2534, the resulting
     // exception will be different).
-    checkFromBadTexxt<MissingNameOrigin, MissingNameOrigin>(
+    checkFromTextSOA<MissingNameOrigin, MissingNameOrigin>(
         "\".\" . 0 0 0 0 0");
-    checkFromBadTexxt<MissingNameOrigin, MissingNameOrigin>(
+    checkFromTextSOA<MissingNameOrigin, MissingNameOrigin>(
         ". \".\" 0 0 0 0 0");
 
     // Missing MAME or RNAME: for the string version, the serial would be
     // tried as RNAME and result in "not absolute".  For the lexer version,
     // it reaches the end-of-line, missing min TTL.
-    checkFromBadTexxt<MissingNameOrigin, MasterLexer::LexerError>(
+    checkFromTextSOA<MissingNameOrigin, MasterLexer::LexerError>(
         ". 2010012601 0 0 0 0", &Name::ROOT_NAME());
 
     // bad serial.  the string version converts lexer error to
     // InvalidRdataText.
-    checkFromBadTexxt<InvalidRdataText, MasterLexer::LexerError>(
+    checkFromTextSOA<InvalidRdataText, MasterLexer::LexerError>(
         ". . bad 0 0 0 0");
 
     // bad serial; exceeding the uint32_t range (4294967296 = 2^32)
-    checkFromBadTexxt<InvalidRdataText, MasterLexer::LexerError>(
+    checkFromTextSOA<InvalidRdataText, MasterLexer::LexerError>(
         ". . 4294967296 0 0 0 0");
 
     // Bad format for other numeric parameters.  These will be tried as a TTL,
     // and result in an exception there.
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(". . 2010012601 bad 0 0 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
+        ". . 2010012601 bad 0 0 0");
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
         ". . 2010012601 4294967296 0 0 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(". . 2010012601 0 bad 0 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
+        ". . 2010012601 0 bad 0 0");
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
         ". . 2010012601 0 4294967296 0 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(". . 2010012601 0 0 bad 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
+        ". . 2010012601 0 0 bad 0");
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
         ". . 2010012601 0 0 4294967296 0");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(". . 2010012601 0 0 0 bad");
-    checkFromBadTexxt<InvalidRRTTL, InvalidRRTTL>(
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
+        ". . 2010012601 0 0 0 bad");
+    checkFromTextSOA<InvalidRRTTL, InvalidRRTTL>(
         ". . 2010012601 0 0 0 4294967296");
 
     // No space between RNAME and serial.  This case is the same as missing
     // M/RNAME.
-    checkFromBadTexxt<MissingNameOrigin, MasterLexer::LexerError>(
+    checkFromTextSOA<MissingNameOrigin, MasterLexer::LexerError>(
         ". example.0 0 0 0 0", &Name::ROOT_NAME());
 
     // Extra parameter.  string version immediately detects the error.
-    EXPECT_THROW(generic::SOA soa(". . 0 0 0 0 0 extra"), InvalidRdataText);
-    // Likewise.  Redundant newline is also considered an error.
-    EXPECT_THROW(generic::SOA soa(". . 0 0 0 0 0\n"), InvalidRdataText);
-    EXPECT_THROW(generic::SOA soa("\n. . 0 0 0 0 0"), InvalidRdataText);
     // lexer version defers the check to the upper layer (we pass origin
     // to skip the check with the string version).
-    checkFromText("ns root 2010012601 1H 5M 1000H 20M extra", &origin);
+    checkFromTextSOA<InvalidRdataText, isc::Exception>(
+        "ns.example.com. root.example.com. 2010012601 1H 5M 1000H 20M "
+        "extra", &origin, true, false);
+
+    // Likewise.  Redundant newline is also considered an error.  The lexer
+    // version accepts trailing newline, but not the beginning one (where
+    // the lexer expects a string excluding newline and EOF).
+    checkFromTextSOA<InvalidRdataText, isc::Exception>(
+        "ns.example.com. root.example.com. 2010012601 1H 5M 1000H 20M\n",
+        NULL, true, false);
+    checkFromTextSOA<InvalidRdataText, MasterLexer::LexerError>(
+        "\nns.example.com. root.example.com. 2010012601 1H 5M 1000H 20M",
+        NULL, true, true);
 }
 
 TEST_F(Rdata_SOA_Test, createFromWire) {

+ 41 - 0
src/lib/dns/tests/rdata_unittest.h

@@ -24,6 +24,9 @@
 
 #include <gtest/gtest.h>
 
+#include <string>
+#include <sstream>
+
 using namespace isc::util;
 
 namespace isc {
@@ -36,6 +39,44 @@ protected:
                                          const RRClass& rrclass,
                                          const char* datafile,
                                          size_t position = 0);
+
+    // Common check to see the result of Rdata construction of given type
+    // (template parameter RdataType) either from std::string or with
+    // MasterLexer object.  If it's expected to succeed the result should be
+    // identical to the commonly used test data (rdata_expected); otherwise it
+    // should result in the exception specified as the template parameter:
+    // ExForString for the string version, and ExForLexer for the lexer
+    // version.  throw_str_version and throw_lexer_version are set to true
+    // iff the string/lexer version is expected to throw, respectively.
+    // Parameter origin can be set to non NULL for the origin parameter of
+    // the lexer version of Rdata constructor.
+    template <typename RdataType, typename ExForString, typename ExForLexer>
+    void checkFromText(const std::string& rdata_txt,
+                       const RdataType& rdata_expected,
+                       bool throw_str_version = true,
+                       bool throw_lexer_version = true,
+                       const Name* origin = NULL)
+    {
+        SCOPED_TRACE(rdata_txt);
+
+        if (throw_str_version) {
+            EXPECT_THROW(RdataType rdata(rdata_txt), ExForString);
+        } else {
+            EXPECT_EQ(0, RdataType(rdata_txt).compare(rdata_expected));
+        }
+
+        std::stringstream ss(rdata_txt);
+        MasterLexer lexer;
+        lexer.pushSource(ss);
+        if (throw_lexer_version) {
+            EXPECT_THROW(RdataType rdata(lexer, origin, MasterLoader::DEFAULT,
+                                         loader_cb), ExForLexer);
+        } else {
+            EXPECT_EQ(0, RdataType(lexer, origin, MasterLoader::DEFAULT,
+                                   loader_cb).compare(rdata_expected));
+        }
+    }
+
     OutputBuffer obuffer;
     MessageRenderer renderer;
     /// This is an RDATA object of some "unknown" RR type so that it can be