Browse Source

[master] applied the proposed fix of #906 to fix failure in MessageTest::toText

JINMEI Tatuya 14 years ago
parent
commit
c16b3ab523
2 changed files with 15 additions and 6 deletions
  1. 7 6
      src/lib/dns/tests/message_unittest.cc
  2. 8 0
      src/lib/util/unittests/testdata.h

+ 7 - 6
src/lib/dns/tests/message_unittest.cc

@@ -683,11 +683,12 @@ TEST_F(MessageTest, toWireWithoutRcode) {
 TEST_F(MessageTest, toText) {
     // Check toText() output for a typical DNS response with records in
     // all sections
-    ifstream ifs;
-    unittests::openTestData("message_toText1.txt", ifs);
+    
     factoryFromFile(message_parse, "message_toText1.wire");
     {
         SCOPED_TRACE("Message toText test (basic case)");
+        ifstream ifs;
+        unittests::openTestData("message_toText1.txt", ifs);
         unittests::matchTextData(ifs, message_parse.toText());
     }
 
@@ -695,12 +696,12 @@ TEST_F(MessageTest, toText) {
     // from the dig output (other than replacing tabs with a space): adding
     // a newline after the "OPT PSEUDOSECTION".  This is an intentional change
     // in our version for better readability.
-    ifs.close();
     message_parse.clear(Message::PARSE);
-    unittests::openTestData("message_toText2.txt", ifs);
     factoryFromFile(message_parse, "message_toText2.wire");
     {
         SCOPED_TRACE("Message toText test with EDNS");
+        ifstream ifs;
+        unittests::openTestData("message_toText2.txt", ifs);
         unittests::matchTextData(ifs, message_parse.toText());
     }
 
@@ -708,12 +709,12 @@ TEST_F(MessageTest, toText) {
     // from the dig output (other than replacing tabs with a space): removing
     // a redundant white space at the end of TSIG RDATA.  We'd rather consider
     // it a dig's defect than a feature.
-    ifs.close();
     message_parse.clear(Message::PARSE);
-    unittests::openTestData("message_toText3.txt", ifs);
     factoryFromFile(message_parse, "message_toText3.wire");
     {
         SCOPED_TRACE("Message toText test with TSIG");
+        ifstream ifs;
+        unittests::openTestData("message_toText3.txt", ifs);
         unittests::matchTextData(ifs, message_parse.toText());
     }
 }

+ 8 - 0
src/lib/util/unittests/testdata.h

@@ -34,6 +34,14 @@ void addTestDataPath(const std::string& path);
 /// addTestDataPath().  On success, ifs will be ready for reading the data
 /// stored in 'datafile'.  If the data file cannot be open with any of the
 /// registered paths, a runtime_error exception will be thrown.
+///
+/// \note Care should be taken if you want to reuse the same single \c ifs
+/// for multiple input data.  Some standard C++ library implementations retain
+/// the failure bit if the first stream reaches the end of the first file,
+/// and make the second call to \c ifstream::open() fail.  The safest way
+/// is to use a different \c ifstream object for a new call to this function;
+/// alternatively make sure you explicitly clear the error bit by calling
+/// \c ifstream::clear() on \c ifs.
 void openTestData(const char* const datafile, std::ifstream& ifs);
 }
 }