blob: 06316bb15b28a2b645c4ada93ebad9323e413e67 [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 {
Yingdi Yud9715e32014-06-27 08:48:47 -070036 m_HOME = std::getenv("TEST_HOME");
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060037 }
38
39 ~ConfigFileFixture()
40 {
Yingdi Yud9715e32014-06-27 08:48:47 -070041 setenv("TEST_HOME", m_HOME.c_str(), 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060042 // 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;
Yingdi Yud9715e32014-06-27 08:48:47 -070054 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060055
Yingdi Yud9715e32014-06-27 08:48:47 -070056 setenv("TEST_HOME", "tests/unit-tests/util/config-file-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060057
Yingdi Yud9715e32014-06-27 08:48:47 -070058 path homePath(absolute(std::getenv("TEST_HOME")));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060059 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 }
Yingdi Yud9715e32014-06-27 08:48:47 -070071 catch (const std::runtime_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060072 {
73 BOOST_FAIL("Unexpected exception: " << error.what());
74 }
75}
76
77BOOST_AUTO_TEST_CASE(EmptyPathParse)
78{
Yingdi Yud9715e32014-06-27 08:48:47 -070079 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060080
Yingdi Yud9715e32014-06-27 08:48:47 -070081 setenv("TEST_HOME", "tests/unit-tests/util/does/not/exist", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060082 try
83 {
84 ConfigFile config;
85 }
Yingdi Yud9715e32014-06-27 08:48:47 -070086 catch (const std::runtime_error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060087 {
88 BOOST_FAIL("Unexpected exception: " << error.what());
89 }
90}
91
92BOOST_AUTO_TEST_CASE(MalformedParse)
93{
94 using namespace boost::filesystem;
Yingdi Yud9715e32014-06-27 08:48:47 -070095 // std::cerr << "current home = " << std::getenv("TEST_HOME") << std::endl;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060096
Yingdi Yud9715e32014-06-27 08:48:47 -070097 setenv("TEST_HOME", "tests/unit-tests/util/config-file-malformed-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060098
99 bool fileWasMalformed = false;
100 try
101 {
102 ConfigFile config;
103 }
Yingdi Yud9715e32014-06-27 08:48:47 -0700104 catch (const ConfigFile::Error& error)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600105 {
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