Browse Source

[2442] unified from std::string and with lexer ctro for text-like rdata.

some tests needed to be adjusted:
- ' ' * 256 (in python) now doesn't work as intended; they are considered
  space
- in auth tests, make sure txt RDATA containing a space is explicitly double
  quoted
- in Rdata_TXT_LIKE_Test::createFromText, removed the last test case because
  it's actually a valid representation as commented.  now the implementation
  correctly accepts it.
JINMEI Tatuya 12 years ago
parent
commit
e4036de3cb

+ 1 - 1
src/bin/auth/tests/auth_srv_unittest.cc

@@ -225,7 +225,7 @@ createBuiltinVersionResponse(const qid_t qid, vector<uint8_t>& data) {
     message.setHeaderFlag(Message::HEADERFLAG_AA);
     RRsetPtr rrset_version = RRsetPtr(new RRset(version_name, RRClass::CH(),
                                                 RRType::TXT(), RRTTL(0)));
-    rrset_version->addRdata(generic::TXT(PACKAGE_STRING));
+    rrset_version->addRdata(generic::TXT("\"" PACKAGE_STRING "\""));
     message.addRRset(Message::SECTION_ANSWER, rrset_version);
 
     RRsetPtr rrset_version_ns = RRsetPtr(new RRset(apex_name, RRClass::CH(),

+ 1 - 1
src/lib/dns/python/tests/rdata_python_test.py

@@ -38,7 +38,7 @@ class RdataTest(unittest.TestCase):
         self.assertRaises(InvalidRdataText, Rdata, RRType("A"), RRClass("IN"),
                           "Invalid Rdata Text")
         self.assertRaises(CharStringTooLong, Rdata, RRType("TXT"),
-                          RRClass("IN"), ' ' * 256)
+                          RRClass("IN"), 'x' * 256)
         self.assertRaises(InvalidRdataLength, Rdata, RRType("TXT"),
                           RRClass("IN"), bytes(65536))
         self.assertRaises(DNSMessageFORMERR, Rdata, RRType("TXT"),

+ 12 - 33
src/lib/dns/rdata/generic/detail/txt_like.h

@@ -23,6 +23,7 @@
 #include <stdint.h>
 
 #include <string>
+#include <sstream>
 #include <vector>
 
 namespace isc {
@@ -85,43 +86,20 @@ public:
     /// \c InvalidRdataText is thrown if the method cannot process the
     /// parameter data.
     explicit TXTLikeImpl(const std::string& txtstr) {
-        // TBD: this is a simple, incomplete implementation that only supports
-        // a single character-string.
-
-        size_t length = txtstr.size();
-        size_t pos_begin = 0;
-
-        if (length > 1 && txtstr[0] == '"' && txtstr[length - 1] == '"') {
-            pos_begin = 1;
-            length -= 2;
-        }
-
-        if (length > MAX_CHARSTRING_LEN) {
-            isc_throw(CharStringTooLong, RRType(typeCode) <<
-                      " RDATA construction from text:"
-                      " string length is too long: " << length);
-        }
-
-        // TBD: right now, we don't support escaped characters
-        if (txtstr.find('\\') != string::npos) {
-            isc_throw(InvalidRdataText, RRType(typeCode) <<
-                      " RDATA from text:"
-                      " escaped character is currently not supported: " <<
-                      txtstr);
-        }
-
-        std::vector<uint8_t> data;
-        data.reserve(length + 1);
-        data.push_back(length);
-        data.insert(data.end(), txtstr.begin() + pos_begin,
-                    txtstr.begin() + pos_begin + length);
-        string_list_.push_back(data);
+        std::istringstream ss(txtstr);
+        MasterLexer lexer;
+        lexer.pushSource(ss);
+        buildFromTextHelper(lexer);
     }
 
-    TXTLikeImpl(MasterLexer& lexer, const Name*,
-                MasterLoader::Options,
+    TXTLikeImpl(MasterLexer& lexer, const Name*, MasterLoader::Options,
                 MasterLoaderCallbacks&)
     {
+        buildFromTextHelper(lexer);
+    }
+
+private:
+    void buildFromTextHelper(MasterLexer& lexer) {
         while (true) {
             const MasterToken& token = lexer.getNextToken(
                 MasterToken::QSTRING, true);
@@ -135,6 +113,7 @@ public:
         lexer.ungetToken();
     }
 
+public:
     /// \brief The copy constructor.
     ///
     /// Trivial for now, we could've used the default one.

+ 1 - 7
src/lib/dns/tests/rdata_txt_like_unittest.cc

@@ -154,16 +154,10 @@ TYPED_TEST(Rdata_TXT_LIKE_Test, createFromText) {
 
     // The escape character makes the double quote a part of character-string,
     // so this is invalid input and should be rejected.
-    EXPECT_THROW(TypeParam("\"Test-String\\\""), InvalidRdataText);
+    EXPECT_THROW(TypeParam("\"Test-String\\\""), MasterLexer::LexerError);
     EXPECT_THROW(TypeParam(this->lexer, NULL, MasterLoader::MANY_ERRORS,
                            this->loader_cb), MasterLexer::LexerError);
     EXPECT_EQ(MasterToken::END_OF_LINE, this->lexer.getNextToken().getType());
-
-    // Terminating double-quote is provided, so this is valid, but in this
-    // version of implementation we reject escaped characters.
-    EXPECT_THROW(TypeParam("\"Test-String\\\"\""), InvalidRdataText);
-    TypeParam(this->lexer, NULL, MasterLoader::MANY_ERRORS,
-              this->loader_cb);
 }
 
 void