]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2369] Throw OpenError when opening the input file fails
authorMukund Sivaraman <muks@isc.org>
Fri, 2 Nov 2012 04:27:22 +0000 (09:57 +0530)
committerMukund Sivaraman <muks@isc.org>
Fri, 2 Nov 2012 04:27:30 +0000 (09:57 +0530)
src/lib/dns/master_lexer_inputsource.cc
src/lib/dns/master_lexer_inputsource.h
src/lib/dns/tests/master_lexer_inputsource_unittest.cc

index a747dfbfc4aa35149ba743b898211582ec5099d8..f38d6c3a53886b6f5518c73bfdba9d9ac11bfd49 100644 (file)
@@ -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()
index 9c8533eb461f8eabbe439953d7bd0a89e50e10ab..bb507c9de31e9769e2f9a4597a884ee40b0e3d17 100644 (file)
@@ -43,6 +43,8 @@ public:
 
     /// \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
@@ -87,6 +89,13 @@ public:
         {}
     };
 
+    /// \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 Returned by getChar() when end of stream is reached.
     static const int END_OF_STREAM;
 
index 09c83d52a603e9f267c0313c5d3ca5a9e037e096..8e0cf622783cf6a88af7866b9e15a13a4616f076 100644 (file)
@@ -59,6 +59,12 @@ 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.
 void