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 { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 20 | namespace tests { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 21 | |
| 22 | class ConfigFileFixture |
| 23 | { |
| 24 | public: |
| 25 | ConfigFileFixture() |
| 26 | { |
| 27 | m_HOME = std::getenv("HOME"); |
| 28 | } |
| 29 | |
| 30 | ~ConfigFileFixture() |
| 31 | { |
| 32 | setenv("HOME", m_HOME.c_str(), 1); |
| 33 | // std::cerr << "restoring home = " << m_HOME << std::endl; |
| 34 | } |
| 35 | |
| 36 | protected: |
| 37 | std::string m_HOME; |
| 38 | }; |
| 39 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 41 | |
| 42 | BOOST_AUTO_TEST_CASE(TestParse) |
| 43 | { |
| 44 | using namespace boost::filesystem; |
| 45 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 46 | |
| 47 | setenv("HOME", "tests/util/config-file-home", 1); |
| 48 | |
| 49 | path homePath(absolute(std::getenv("HOME"))); |
| 50 | homePath /= ".ndn/client.conf"; |
| 51 | |
| 52 | try |
| 53 | { |
| 54 | ConfigFile config; |
| 55 | |
| 56 | BOOST_REQUIRE_EQUAL(config.getPath(), homePath); |
| 57 | |
| 58 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 59 | BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere"); |
| 60 | BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01"); |
| 61 | } |
| 62 | catch(const std::runtime_error& error) |
| 63 | { |
| 64 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(EmptyPathParse) |
| 69 | { |
| 70 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 71 | |
| 72 | setenv("HOME", "tests/util/does/not/exist", 1); |
| 73 | try |
| 74 | { |
| 75 | ConfigFile config; |
| 76 | } |
| 77 | catch(const std::runtime_error& error) |
| 78 | { |
| 79 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(MalformedParse) |
| 84 | { |
| 85 | using namespace boost::filesystem; |
| 86 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 87 | |
| 88 | setenv("HOME", "tests/util/config-file-malformed-home", 1); |
| 89 | |
| 90 | bool fileWasMalformed = false; |
| 91 | try |
| 92 | { |
| 93 | ConfigFile config; |
| 94 | } |
| 95 | catch(const ConfigFile::Error& error) |
| 96 | { |
| 97 | fileWasMalformed = true; |
| 98 | } |
| 99 | |
| 100 | BOOST_REQUIRE(fileWasMalformed); |
| 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_SUITE_END() |
| 104 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame^] | 105 | } // namespace tests |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 106 | } // namespace ndn |