blob: 2f6650eef7a370d649a7bb768e819d00f1b82896 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 DiBenedettoc07b3a22014-03-19 12:32:52 -060020 */
21
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060022#include "util/config-file.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070023#include "../util/test-home-environment-fixture.hpp"
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060024
25#include <cstdlib>
26
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
28
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060029namespace ndn {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070030namespace tests {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060031
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070032BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, util::TestHomeEnvironmentFixture)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060033
34BOOST_AUTO_TEST_CASE(TestParse)
35{
36 using namespace boost::filesystem;
Yingdi Yud9715e32014-06-27 08:48:47 -070037 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060038
Yingdi Yud9715e32014-06-27 08:48:47 -070039 setenv("TEST_HOME", "tests/unit-tests/util/config-file-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060040
Yingdi Yud9715e32014-06-27 08:48:47 -070041 path homePath(absolute(std::getenv("TEST_HOME")));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060042 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 Yud9715e32014-06-27 08:48:47 -070054 catch (const std::runtime_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060055 {
56 BOOST_FAIL("Unexpected exception: " << error.what());
57 }
58}
59
60BOOST_AUTO_TEST_CASE(EmptyPathParse)
61{
Yingdi Yud9715e32014-06-27 08:48:47 -070062 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060063
Yingdi Yud9715e32014-06-27 08:48:47 -070064 setenv("TEST_HOME", "tests/unit-tests/util/does/not/exist", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060065 try
66 {
67 ConfigFile config;
68 }
Yingdi Yud9715e32014-06-27 08:48:47 -070069 catch (const std::runtime_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060070 {
71 BOOST_FAIL("Unexpected exception: " << error.what());
72 }
73}
74
75BOOST_AUTO_TEST_CASE(MalformedParse)
76{
77 using namespace boost::filesystem;
Yingdi Yud9715e32014-06-27 08:48:47 -070078 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060079
Yingdi Yud9715e32014-06-27 08:48:47 -070080 setenv("TEST_HOME", "tests/unit-tests/util/config-file-malformed-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060081
82 bool fileWasMalformed = false;
83 try
84 {
85 ConfigFile config;
86 }
Yingdi Yud9715e32014-06-27 08:48:47 -070087 catch (const ConfigFile::Error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060088 {
89 fileWasMalformed = true;
90 }
91
92 BOOST_REQUIRE(fileWasMalformed);
93}
94
95BOOST_AUTO_TEST_SUITE_END()
96
Yingdi Yuf56c68f2014-04-24 21:50:13 -070097} // namespace tests
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060098} // namespace ndn