}
+SENF_AUTO_UNIT_TEST(configBundle_empty)
+{
+ TempFile cfg ("test.cfg");
+ cfg << TempFile::close;
+
+ senf::console::ScopedDirectory<> root;
+ root.add("fun2", senf::console::factory::Command(&fun2));
+
+ senf::console::ConfigBundle bundle(root);
+ bundle.add( senf::console::FileConfig("/tmp/test.cfg"));
+
+ SENF_CHECK_NO_THROW( bundle.parse() );
+}
+
SENF_AUTO_UNIT_TEST(configBundle)
{
namespace fty = senf::console::factory;
}
prefix_ senf::console::detail::ConfigFileSource::ConfigFileSource(std::string const & filename)
- : filename_ (filename), ignoreMissing_ (true)
+ : filename_ (filename), ignoreMissing_ (false)
{}
#endif
}
+#define SENF_CHECK_THROW_SYSTEMEXCEPTION( expr, errorNumber) \
+ try { \
+ BOOST_TEST_PASSPOINT(); \
+ expr; \
+ BOOST_ERROR( "senf::SystemException is expected"); \
+ } catch( senf::SystemException const & ex ) { \
+ BOOST_CHECK( ex.anyOf( errorNumber)); \
+ } \
+
+
SENF_AUTO_UNIT_TEST(configFile)
{
namespace fty = senf::console::factory;
{
senf::console::ConfigFile cfg ("i don't exist");
+ SENF_CHECK_THROW_SYSTEMEXCEPTION(
+ cfg.parse(), ENOENT);
cfg.ignoreMissing();
SENF_CHECK_NO_THROW( cfg.parse() );
}
+ {
+ if (getuid() != 0 && boost::filesystem::exists("/etc/shadow")) {
+ senf::console::ConfigFile cfg ("/etc/shadow");
+ SENF_CHECK_THROW_SYSTEMEXCEPTION(
+ cfg.parse(), EACCES);
+ }
+ }
}
SENF_AUTO_UNIT_TEST(configFileRestrict)
// Custom includes
#include <cerrno>
+#include <sys/stat.h>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/spirit/iterator/file_iterator.hpp>
#include <boost/spirit/iterator/position_iterator.hpp>
prefix_ void senf::console::CommandParser::parseFile(std::string const & filename, Callback cb)
{
+ // file_iterator sets errno to EINVAL and returns error when file size is 0
+ // so we check the file size before
+ struct stat statBuf;
+ if (stat( filename.c_str(), &statBuf) != 0)
+ throw SystemException(errno SENF_EXC_DEBUGINFO);
+ if (statBuf.st_size == 0) return;
boost::spirit::file_iterator<> i (filename);
- if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO);
+ if (!i) {
+ if (errno == 0)
+ // hmm.. errno==0 but the file_iterator is false; something is wrong but we
+ // do not know what exactly, so we throw a SystemeException with EINVAL
+ throw SystemException(EINVAL SENF_EXC_DEBUGINFO);
+ else
+ throw SystemException(errno SENF_EXC_DEBUGINFO);
+ }
boost::spirit::file_iterator<> const i_end (i.make_end());
parseLoop(i, i_end, filename, cb);
}
prefix_ bool senf::SystemException::anyOf(int c0, int c1, int c2, int c3, int c4, int c5,
int c6, int c7, int c8, int c9)
+ const
{
return
(c0 && code_ == c0) ||
char const * errorString() const; ///< Error string (\c strerror() value)
bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0,
- int c6=0, int c7=0, int c8=0, int c9=0);
+ int c6=0, int c7=0, int c8=0, int c9=0) const;
///< \c true, if errorNumber() is one of \a c0 ... \a c9
-
-
private:
void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND);