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