Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 22 | #include "util/config-file.hpp" |
| 23 | |
| 24 | #include <cstdlib> |
| 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
| 27 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 28 | namespace ndn { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 29 | namespace tests { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 30 | |
| 31 | class ConfigFileFixture |
| 32 | { |
| 33 | public: |
| 34 | ConfigFileFixture() |
| 35 | { |
| 36 | m_HOME = std::getenv("HOME"); |
| 37 | } |
| 38 | |
| 39 | ~ConfigFileFixture() |
| 40 | { |
| 41 | setenv("HOME", m_HOME.c_str(), 1); |
| 42 | // std::cerr << "restoring home = " << m_HOME << std::endl; |
| 43 | } |
| 44 | |
| 45 | protected: |
| 46 | std::string m_HOME; |
| 47 | }; |
| 48 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 49 | BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 50 | |
| 51 | BOOST_AUTO_TEST_CASE(TestParse) |
| 52 | { |
| 53 | using namespace boost::filesystem; |
| 54 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 55 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 56 | setenv("HOME", "tests/unit-tests/util/config-file-home", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 57 | |
| 58 | path homePath(absolute(std::getenv("HOME"))); |
| 59 | homePath /= ".ndn/client.conf"; |
| 60 | |
| 61 | try |
| 62 | { |
| 63 | ConfigFile config; |
| 64 | |
| 65 | BOOST_REQUIRE_EQUAL(config.getPath(), homePath); |
| 66 | |
| 67 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 68 | BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere"); |
| 69 | BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01"); |
| 70 | } |
| 71 | catch(const std::runtime_error& error) |
| 72 | { |
| 73 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(EmptyPathParse) |
| 78 | { |
| 79 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 80 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 81 | setenv("HOME", "tests/unit-tests/util/does/not/exist", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 82 | try |
| 83 | { |
| 84 | ConfigFile config; |
| 85 | } |
| 86 | catch(const std::runtime_error& error) |
| 87 | { |
| 88 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(MalformedParse) |
| 93 | { |
| 94 | using namespace boost::filesystem; |
| 95 | // std::cerr << "current home = " << std::getenv("HOME") << std::endl; |
| 96 | |
Alexander Afanasyev | 8b1674a | 2014-05-15 00:58:43 -0700 | [diff] [blame] | 97 | setenv("HOME", "tests/unit-tests/util/config-file-malformed-home", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 98 | |
| 99 | bool fileWasMalformed = false; |
| 100 | try |
| 101 | { |
| 102 | ConfigFile config; |
| 103 | } |
| 104 | catch(const ConfigFile::Error& error) |
| 105 | { |
| 106 | fileWasMalformed = true; |
| 107 | } |
| 108 | |
| 109 | BOOST_REQUIRE(fileWasMalformed); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_SUITE_END() |
| 113 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 114 | } // namespace tests |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 115 | } // namespace ndn |