Browse Source

[2390] Update std::string PTR constructor to use the MasterLexer

Also adjust tests.
Mukund Sivaraman 12 years ago
parent
commit
3d35bcb14c
2 changed files with 27 additions and 10 deletions
  1. 19 2
      src/lib/dns/rdata/generic/ptr_12.cc
  2. 8 8
      src/lib/dns/tests/rdata_ptr_unittest.cc

+ 19 - 2
src/lib/dns/rdata/generic/ptr_12.cc

@@ -29,8 +29,25 @@ using namespace isc::util;
 // BEGIN_RDATA_NAMESPACE
 
 PTR::PTR(const std::string& type_str) :
-    ptr_name_(type_str)
-{}
+    // Fill in dummy name and replace them soon below.
+    ptr_name_(Name::ROOT_NAME())
+{
+    try {
+        std::istringstream ss(type_str);
+        MasterLexer lexer;
+        lexer.pushSource(ss);
+
+        ptr_name_ = createNameFromLexer(lexer, NULL);
+
+        if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
+            isc_throw(InvalidRdataText, "extra input text for PTR: "
+                      << type_str);
+        }
+    } catch (const MasterLexer::LexerError& ex) {
+        isc_throw(InvalidRdataText, "Failed to construct PTR from '" <<
+                  type_str << "': " << ex.what());
+    }
+}
 
 PTR::PTR(InputBuffer& buffer, size_t) :
     ptr_name_(buffer)

+ 8 - 8
src/lib/dns/tests/rdata_ptr_unittest.cc

@@ -40,8 +40,8 @@ class Rdata_PTR_Test : public RdataTest {
     // there's nothing to specialize
 };
 
-const generic::PTR rdata_ptr("ns.example.com");
-const generic::PTR rdata_ptr2("ns2.example.com");
+const generic::PTR rdata_ptr("ns.example.com.");
+const generic::PTR rdata_ptr2("ns2.example.com.");
 const uint8_t wiredata_ptr[] = {
     0x02, 0x6e, 0x73, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
     0x63, 0x6f, 0x6d, 0x00 };
@@ -54,15 +54,15 @@ const uint8_t wiredata_ptr2[] = {
     0x03, 0x6e, 0x73, 0x32, 0xc0, 0x03 };
 
 TEST_F(Rdata_PTR_Test, createFromText) {
-    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("ns.example.com")));
+    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("ns.example.com.")));
     // explicitly add a trailing dot.  should be the same RDATA.
     EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("ns.example.com.")));
     // should be case sensitive.
-    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("NS.EXAMPLE.COM")));
+    EXPECT_EQ(0, rdata_ptr.compare(generic::PTR("NS.EXAMPLE.COM.")));
     // RDATA of a class-independent type should be recognized for any
     // "unknown" class.
     EXPECT_EQ(0, rdata_ptr.compare(*createRdata(RRType("PTR"), RRClass(65000),
-                                               "ns.example.com")));
+                                               "ns.example.com.")));
 }
 
 TEST_F(Rdata_PTR_Test, createFromWire) {
@@ -82,7 +82,7 @@ TEST_F(Rdata_PTR_Test, createFromWire) {
                                       "rdata_ns_fromWire", 71),
                  DNSMessageFORMERR);
 
-    EXPECT_EQ(0, generic::PTR("ns2.example.com").compare(
+    EXPECT_EQ(0, generic::PTR("ns2.example.com.").compare(
                   *rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
                                         "rdata_ns_fromWire", 55)));
     EXPECT_THROW(*rdataFactoryFromFile(RRType("PTR"), RRClass("IN"),
@@ -119,8 +119,8 @@ TEST_F(Rdata_PTR_Test, toText) {
 }
 
 TEST_F(Rdata_PTR_Test, compare) {
-    generic::PTR small("a.example");
-    generic::PTR large("example");
+    generic::PTR small("a.example.");
+    generic::PTR large("example.");
     EXPECT_TRUE(Name("a.example") > Name("example"));
     EXPECT_GT(0, small.compare(large));
 }