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 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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" |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 23 | #include "../util/test-home-environment-fixture.hpp" |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 24 | |
| 25 | #include <cstdlib> |
| 26 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
| 28 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 29 | namespace ndn { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 30 | namespace tests { |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 31 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 32 | BOOST_FIXTURE_TEST_SUITE(UtilConfigFile, util::TestHomeEnvironmentFixture) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_CASE(TestParse) |
| 35 | { |
| 36 | using namespace boost::filesystem; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 37 | // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 38 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 39 | setenv("TEST_HOME", "tests/unit-tests/util/config-file-home", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 40 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 41 | path homePath(absolute(std::getenv("TEST_HOME"))); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 42 | homePath /= ".ndn/client.conf"; |
| 43 | |
| 44 | try |
| 45 | { |
| 46 | ConfigFile config; |
| 47 | |
| 48 | BOOST_REQUIRE_EQUAL(config.getPath(), homePath); |
| 49 | |
| 50 | const ConfigFile::Parsed& parsed = config.getParsedConfiguration(); |
| 51 | BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere"); |
| 52 | BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01"); |
| 53 | } |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 54 | catch (const std::runtime_error& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 55 | { |
| 56 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(EmptyPathParse) |
| 61 | { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 62 | // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 63 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 64 | setenv("TEST_HOME", "tests/unit-tests/util/does/not/exist", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 65 | try |
| 66 | { |
| 67 | ConfigFile config; |
| 68 | } |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 69 | catch (const std::runtime_error& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 70 | { |
| 71 | BOOST_FAIL("Unexpected exception: " << error.what()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(MalformedParse) |
| 76 | { |
| 77 | using namespace boost::filesystem; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 78 | // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 79 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 80 | setenv("TEST_HOME", "tests/unit-tests/util/config-file-malformed-home", 1); |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 81 | |
| 82 | bool fileWasMalformed = false; |
| 83 | try |
| 84 | { |
| 85 | ConfigFile config; |
| 86 | } |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 87 | catch (const ConfigFile::Error& error) |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 88 | { |
| 89 | fileWasMalformed = true; |
| 90 | } |
| 91 | |
| 92 | BOOST_REQUIRE(fileWasMalformed); |
| 93 | } |
| 94 | |
| 95 | BOOST_AUTO_TEST_SUITE_END() |
| 96 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 97 | } // namespace tests |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 98 | } // namespace ndn |