blob: 9e098c1f4a6ce013f06e72897ce0c0df2dd2fbf5 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesaventod8e0cad2021-05-26 21:43:47 -04003 * Copyright (c) 2013-2021 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/config-file.hpp"
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/unit/test-home-env-saver.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026
Davide Pesaventod8e0cad2021-05-26 21:43:47 -040027#include <boost/filesystem/operations.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include <cstdlib>
29
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060030namespace ndn {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070031namespace tests {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060032
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Util)
34BOOST_FIXTURE_TEST_SUITE(TestConfigFile, TestHomeEnvSaver)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060035
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_CASE(Parse)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060037{
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038 namespace fs = boost::filesystem;
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060039
Davide Pesavento74daf742018-11-23 18:14:13 -050040 setenv("TEST_HOME", "tests/unit/util/config-file-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060041
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042 fs::path homePath(fs::absolute(std::getenv("TEST_HOME")));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060043 homePath /= ".ndn/client.conf";
44
Davide Pesaventoeee3e822016-11-26 19:19:34 +010045 ConfigFile config;
46 BOOST_REQUIRE_EQUAL(config.getPath(), homePath);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060047
Davide Pesaventoeee3e822016-11-26 19:19:34 +010048 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
49 BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
50 BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060051}
52
Davide Pesaventoeee3e822016-11-26 19:19:34 +010053BOOST_AUTO_TEST_CASE(ParseEmptyPath)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060054{
Davide Pesavento74daf742018-11-23 18:14:13 -050055 setenv("TEST_HOME", "tests/unit/util/does/not/exist", 1);
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056
57 BOOST_CHECK_NO_THROW(ConfigFile config);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060058}
59
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060BOOST_AUTO_TEST_CASE(ParseMalformed)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060061{
Davide Pesavento74daf742018-11-23 18:14:13 -050062 setenv("TEST_HOME", "tests/unit/util/config-file-malformed-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060063
Davide Pesaventoeee3e822016-11-26 19:19:34 +010064 BOOST_CHECK_THROW(ConfigFile config, ConfigFile::Error);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060065}
66
Davide Pesaventoeee3e822016-11-26 19:19:34 +010067BOOST_AUTO_TEST_SUITE_END() // TestConfigFile
68BOOST_AUTO_TEST_SUITE_END() // Util
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060069
Yingdi Yuf56c68f2014-04-24 21:50:13 -070070} // namespace tests
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060071} // namespace ndn