BIND 10 trac2369, updated. 80b227af0bf08ac9e7bdf645b11e8593b717add3 [2369] Move exceptions to the top of class

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Nov 2 04:29:47 UTC 2012


The branch, trac2369 has been updated
       via  80b227af0bf08ac9e7bdf645b11e8593b717add3 (commit)
       via  a2090f1d5702cbc00da7b52a2c1247bd37334ebc (commit)
       via  f142087f73411145565baf4a3280607413162e06 (commit)
      from  e32617d592d204e4c5c27ff3ecab77ea4e258cb7 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 80b227af0bf08ac9e7bdf645b11e8593b717add3
Author: Mukund Sivaraman <muks at isc.org>
Date:   Fri Nov 2 09:59:16 2012 +0530

    [2369] Move exceptions to the top of class

commit a2090f1d5702cbc00da7b52a2c1247bd37334ebc
Author: Mukund Sivaraman <muks at isc.org>
Date:   Fri Nov 2 09:57:22 2012 +0530

    [2369] Throw OpenError when opening the input file fails

commit f142087f73411145565baf4a3280607413162e06
Author: Mukund Sivaraman <muks at isc.org>
Date:   Fri Nov 2 09:45:27 2012 +0530

    [2369] Add a file test as well

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dns/master_lexer_inputsource.cc            |    4 ++
 src/lib/dns/master_lexer_inputsource.h             |   47 +++++++++------
 .../dns/tests/master_lexer_inputsource_unittest.cc |   61 ++++++++++++++------
 3 files changed, 75 insertions(+), 37 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dns/master_lexer_inputsource.cc b/src/lib/dns/master_lexer_inputsource.cc
index a747dfb..f38d6c3 100644
--- a/src/lib/dns/master_lexer_inputsource.cc
+++ b/src/lib/dns/master_lexer_inputsource.cc
@@ -47,6 +47,10 @@ InputSource::InputSource(const char* filename) :
     input_(file_stream_)
 {
     file_stream_.open(filename, std::fstream::in);
+    if (file_stream_.fail()) {
+        isc_throw(OpenError,
+                  "Error opening the input source file: " << filename);
+    }
 }
 
 InputSource::~InputSource()
diff --git a/src/lib/dns/master_lexer_inputsource.h b/src/lib/dns/master_lexer_inputsource.h
index 9c8533e..b7eee5f 100644
--- a/src/lib/dns/master_lexer_inputsource.h
+++ b/src/lib/dns/master_lexer_inputsource.h
@@ -37,12 +37,40 @@ namespace master_lexer_internal {
 /// This class is not meant for public use.
 class InputSource {
 public:
+    /// \brief Returned by getChar() when end of stream is reached.
+    static const int END_OF_STREAM;
+
+    /// \brief Exception thrown when ungetChar() is made to go before
+    /// the start of buffer.
+    struct UngetBeforeBeginning : public OutOfRange {
+        UngetBeforeBeginning(const char* file, size_t line, const char* what) :
+            OutOfRange(file, line, what)
+        {}
+    };
+
+    /// \brief Exception thrown when we fail to read from the input
+    /// stream or file.
+    struct ReadError : public Unexpected {
+        ReadError(const char* file, size_t line, const char* what) :
+            Unexpected(file, line, what)
+        {}
+    };
+
+    /// \brief Exception thrown when we fail to open the input file.
+    struct OpenError : public Unexpected {
+        OpenError(const char* file, size_t line, const char* what) :
+            Unexpected(file, line, what)
+        {}
+    };
+
     /// \brief Constructor which takes an input stream. The stream is
     /// read-from, but it is not closed.
     InputSource(std::istream& input_stream);
 
     /// \brief Constructor which takes a filename to read from. The
     /// associated file stream is managed internally.
+    ///
+    /// \throws OpenError when opening the input file fails.
     InputSource(const char* filename);
 
     /// \brief Destructor
@@ -71,25 +99,6 @@ public:
         saved_line_ = line_;
     }
 
-    /// \brief Exception thrown when ungetChar() is made to go before
-    /// the start of buffer.
-    struct UngetBeforeBeginning : public OutOfRange {
-        UngetBeforeBeginning(const char* file, size_t line, const char* what) :
-            OutOfRange(file, line, what)
-        {}
-    };
-
-    /// \brief Exception thrown when we fail to read from the input
-    /// stream or file.
-    struct ReadError : public Unexpected {
-        ReadError(const char* file, size_t line, const char* what) :
-            Unexpected(file, line, what)
-        {}
-    };
-
-    /// \brief Returned by getChar() when end of stream is reached.
-    static const int END_OF_STREAM;
-
     /// \brief Returns a single character from the input source. If end
     /// of file is reached, \c END_OF_STREAM is returned.
     ///
diff --git a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc
index a6876d5..8e0cf62 100644
--- a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc
+++ b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc
@@ -59,50 +59,75 @@ TEST_F(InputSourceTest, getName) {
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", source2.getName());
 }
 
+TEST_F(InputSourceTest, nonExistentFile) {
+    EXPECT_THROW({
+        InputSource source(TEST_DATA_SRCDIR "/videokilledtheradiostar");
+    }, InputSource::OpenError);
+}
+
 // getChar() should return characters from the input stream in
 // sequence. ungetChar() should skip backwards.
-TEST_F(InputSourceTest, getAndUngetChar) {
-    for (size_t i = 0; i < str_length_; i++) {
-        EXPECT_EQ(str_[i], source_.getChar());
-        EXPECT_FALSE(source_.atEOF());
+void
+checkGetAndUngetChar(InputSource& source, const char* str, size_t str_length)
+{
+    for (size_t i = 0; i < str_length; i++) {
+        EXPECT_EQ(str[i], source.getChar());
+        EXPECT_FALSE(source.atEOF());
     }
 
     // At this point, we still have not reached EOF.
-    EXPECT_FALSE(source_.atEOF());
+    EXPECT_FALSE(source.atEOF());
 
     // This should cause EOF to be set.
-    EXPECT_EQ(InputSource::END_OF_STREAM, source_.getChar());
+    EXPECT_EQ(InputSource::END_OF_STREAM, source.getChar());
 
     // Now, EOF should be set.
-    EXPECT_TRUE(source_.atEOF());
+    EXPECT_TRUE(source.atEOF());
 
     // Now, let's go backwards. This should cause the EOF to be set to
     // false.
-    source_.ungetChar();
+    source.ungetChar();
 
     // Now, EOF should be false.
-    EXPECT_FALSE(source_.atEOF());
+    EXPECT_FALSE(source.atEOF());
 
     // This should cause EOF to be set again.
-    EXPECT_EQ(InputSource::END_OF_STREAM, source_.getChar());
+    EXPECT_EQ(InputSource::END_OF_STREAM, source.getChar());
 
     // Now, EOF should be set.
-    EXPECT_TRUE(source_.atEOF());
+    EXPECT_TRUE(source.atEOF());
 
     // Now, let's go backwards in a loop. Start by skipping the EOF.
-    source_.ungetChar();
+    source.ungetChar();
 
-    for (size_t i = 0; i < str_length_; i++) {
-        size_t index = str_length_ - 1 - i;
+    for (size_t i = 0; i < str_length; i++) {
+        size_t index = str_length - 1 - i;
         // Skip one character.
-        source_.ungetChar();
-        EXPECT_EQ(str_[index], source_.getChar());
+        source.ungetChar();
+        EXPECT_EQ(str[index], source.getChar());
         // Skip the character we received again.
-        source_.ungetChar();
+        source.ungetChar();
     }
 
     // Skipping past the start of buffer should throw.
-    EXPECT_THROW(source_.ungetChar(), InputSource::UngetBeforeBeginning);
+    EXPECT_THROW(source.ungetChar(), InputSource::UngetBeforeBeginning);
+}
+
+TEST_F(InputSourceTest, stream) {
+    checkGetAndUngetChar(source_, str_, str_length_);
+}
+
+TEST_F(InputSourceTest, file) {
+    const char* str =
+        ";; a simple (incomplete) zone file\n"
+        "\n"
+        "example.com. 3600 IN TXT \"test data\"\n"
+        "www.example.com. 60 IN A 192.0.2.1\n"
+        "www.example.com. 60 IN A 192.0.2.2\n";
+    size_t str_length = strlen(str);
+
+    InputSource source(TEST_DATA_SRCDIR "/masterload.txt");
+    checkGetAndUngetChar(source, str, str_length);
 }
 
 // ungetAll() should skip back to the place where the InputSource



More information about the bind10-changes mailing list