Browse Source

[2572] adjusted getTotalSourceSize() to handle unknown source size

JINMEI Tatuya 12 years ago
parent
commit
54cbb54f35
2 changed files with 24 additions and 0 deletions
  1. 6 0
      src/lib/dns/master_lexer.cc
  2. 18 0
      src/lib/dns/tests/master_lexer_unittest.cc

+ 6 - 0
src/lib/dns/master_lexer.cc

@@ -187,6 +187,12 @@ size_t
 MasterLexer::getTotalSourceSize() const {
     size_t total_size = 0;
     BOOST_FOREACH(InputSourcePtr& src, impl_->sources_) {
+        // If the size of any pushed source is unknown, the total is also
+        // considered unknown.
+        if (src->getSize() == SOURCE_SIZE_UNKNOWN) {
+            return (SOURCE_SIZE_UNKNOWN);
+        }
+
         total_size += src->getSize();
     }
     return (total_size);

+ 18 - 0
src/lib/dns/tests/master_lexer_unittest.cc

@@ -142,6 +142,24 @@ TEST_F(MasterLexerTest, nestedPush) {
     EXPECT_TRUE(lexer.getSourceName().empty());
 }
 
+TEST_F(MasterLexerTest, unknownSourceSize) {
+    // Similar to the previous case, but the size of the second source
+    // will be considered "unknown" (by emulating an error).
+    ss << "test";
+    lexer.pushSource(ss);
+    EXPECT_EQ(4, lexer.getTotalSourceSize());
+
+    stringstream ss2;
+    ss2.setstate(std::ios_base::failbit); // this will make the size unknown
+    lexer.pushSource(ss2);
+    // Then the total size is also unknown.
+    EXPECT_EQ(MasterLexer::SOURCE_SIZE_UNKNOWN, lexer.getTotalSourceSize());
+
+    // If we pop that source, the size becomes known again.
+    lexer.popSource();
+    EXPECT_EQ(4, lexer.getTotalSourceSize());
+}
+
 TEST_F(MasterLexerTest, invalidPop) {
     // popSource() cannot be called if the sources stack is empty.
     EXPECT_THROW(lexer.popSource(), isc::InvalidOperation);