blob: 720d81e973a3b28b80973050d049bceb55ed0d58 [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"
23
24#include <cstdlib>
25
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
27
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060028namespace ndn {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070029namespace tests {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060030
31class ConfigFileFixture
32{
33public:
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
45protected:
46 std::string m_HOME;
47};
48
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070049BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060050
51BOOST_AUTO_TEST_CASE(TestParse)
52{
53 using namespace boost::filesystem;
54 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
55
56 setenv("HOME", "tests/util/config-file-home", 1);
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
77BOOST_AUTO_TEST_CASE(EmptyPathParse)
78{
79 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
80
81 setenv("HOME", "tests/util/does/not/exist", 1);
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
92BOOST_AUTO_TEST_CASE(MalformedParse)
93{
94 using namespace boost::filesystem;
95 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
96
97 setenv("HOME", "tests/util/config-file-malformed-home", 1);
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
112BOOST_AUTO_TEST_SUITE_END()
113
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700114} // namespace tests
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600115} // namespace ndn