Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 7 | #include "util/config-file.hpp" |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 11 | #include "boost-test.hpp" |
| 12 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 13 | namespace ndn { |
| 14 | |
| 15 | class ConfigFileFixture |
| 16 | { |
| 17 | public: |
| 18 | ConfigFileFixture() |
| 19 | { |
| 20 | m_HOME = std::getenv("HOME"); |
| 21 | } |
| 22 | |
| 23 | ~ConfigFileFixture() |
| 24 | { |
| 25 | setenv("HOME", m_HOME.c_str(), 1); |
| 26 | // std::cerr << "restoring home = " << m_HOME << std::endl; |
| 27 | } |
| 28 | |
| 29 | protected: |
| 30 | std::string m_HOME; |
| 31 | }; |
| 32 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_CASE(TestParse) |
| 36 | { |
| 37 | using namespace boost::filesystem; |
| 38 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 39 | |
| 40 | setenv("HOME", "tests/util/config-file-home", 1); |
| 41 | |
| 42 | path homePath(absolute(std::getenv("HOME"))); |
| 43 | homePath /= ".ndn/client.conf"; |
| 44 | |
| 45 | try |
| 46 | { |
| 47 | ConfigFile config; |
| 48 | |
| 49 | BOOST_REQUIRE_EQUAL(config.getPath(), homePath); |
| 50 | |
| 51 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 52 | BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere"); |
| 53 | BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01"); |
| 54 | } |
| 55 | catch(const std::runtime_error& error) |
| 56 | { |
| 57 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(EmptyPathParse) |
| 62 | { |
| 63 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 64 | |
| 65 | setenv("HOME", "tests/util/does/not/exist", 1); |
| 66 | try |
| 67 | { |
| 68 | ConfigFile config; |
| 69 | } |
| 70 | catch(const std::runtime_error& error) |
| 71 | { |
| 72 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(MalformedParse) |
| 77 | { |
| 78 | using namespace boost::filesystem; |
| 79 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 80 | |
| 81 | setenv("HOME", "tests/util/config-file-malformed-home", 1); |
| 82 | |
| 83 | bool fileWasMalformed = false; |
| 84 | try |
| 85 | { |
| 86 | ConfigFile config; |
| 87 | } |
| 88 | catch(const ConfigFile::Error& error) |
| 89 | { |
| 90 | fileWasMalformed = true; |
| 91 | } |
| 92 | |
| 93 | BOOST_REQUIRE(fileWasMalformed); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_SUITE_END() |
| 97 | |
| 98 | } // namespace ndn |