blob: eac5f21e03ff6bb7980ceb3d97feb1e4da8c33e3 [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 Pesavento51974f62024-12-21 20:42:45 -05003 * Copyright (c) 2013-2024 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 Pesaventoeee3e822016-11-26 19:19:34 +010027#include <cstdlib>
28
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060030
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Util)
32BOOST_FIXTURE_TEST_SUITE(TestConfigFile, TestHomeEnvSaver)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_CASE(Parse)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060035{
Davide Pesavento74daf742018-11-23 18:14:13 -050036 setenv("TEST_HOME", "tests/unit/util/config-file-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060037
Davide Pesavento51974f62024-12-21 20:42:45 -050038 auto homePath = std::filesystem::absolute(std::getenv("TEST_HOME"));
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060039 homePath /= ".ndn/client.conf";
40
Davide Pesaventoeee3e822016-11-26 19:19:34 +010041 ConfigFile config;
Davide Pesavento51974f62024-12-21 20:42:45 -050042 BOOST_CHECK_EQUAL(config.getPath(), homePath);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060043
Davide Pesaventoeee3e822016-11-26 19:19:34 +010044 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
45 BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
46 BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060047}
48
Davide Pesaventoeee3e822016-11-26 19:19:34 +010049BOOST_AUTO_TEST_CASE(ParseEmptyPath)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060050{
Davide Pesavento74daf742018-11-23 18:14:13 -050051 setenv("TEST_HOME", "tests/unit/util/does/not/exist", 1);
Davide Pesaventoeee3e822016-11-26 19:19:34 +010052
Davide Pesavento51974f62024-12-21 20:42:45 -050053 BOOST_CHECK_NO_THROW(ConfigFile{});
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060054}
55
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056BOOST_AUTO_TEST_CASE(ParseMalformed)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060057{
Davide Pesavento74daf742018-11-23 18:14:13 -050058 setenv("TEST_HOME", "tests/unit/util/config-file-malformed-home", 1);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060059
Davide Pesavento51974f62024-12-21 20:42:45 -050060 BOOST_CHECK_THROW(ConfigFile{}, ConfigFile::Error);
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060061}
62
Davide Pesaventoeee3e822016-11-26 19:19:34 +010063BOOST_AUTO_TEST_SUITE_END() // TestConfigFile
64BOOST_AUTO_TEST_SUITE_END() // Util
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060065
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040066} // namespace ndn::tests