From: Mukund Sivaraman Date: Fri, 2 Nov 2012 04:27:22 +0000 (+0530) Subject: [2369] Throw OpenError when opening the input file fails X-Git-Tag: trac2487_base~18^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2090f1d5702cbc00da7b52a2c1247bd37334ebc;p=thirdparty%2Fkea.git [2369] Throw OpenError when opening the input file fails --- diff --git a/src/lib/dns/master_lexer_inputsource.cc b/src/lib/dns/master_lexer_inputsource.cc index a747dfbfc4..f38d6c3a53 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 9c8533eb46..bb507c9de3 100644 --- a/src/lib/dns/master_lexer_inputsource.h +++ b/src/lib/dns/master_lexer_inputsource.h @@ -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; diff --git a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc index 09c83d52a6..8e0cf62278 100644 --- a/src/lib/dns/tests/master_lexer_inputsource_unittest.cc +++ b/src/lib/dns/tests/master_lexer_inputsource_unittest.cc @@ -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