blob: b472bb203a50da52e3174d057d8cc989d7af6659 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060011 */
12
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060013#include "util/config-file.hpp"
14
15#include <cstdlib>
16
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070017#include "boost-test.hpp"
18
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060019namespace ndn {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070020namespace tests {
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060021
22class ConfigFileFixture
23{
24public:
25 ConfigFileFixture()
26 {
27 m_HOME = std::getenv("HOME");
28 }
29
30 ~ConfigFileFixture()
31 {
32 setenv("HOME", m_HOME.c_str(), 1);
33 // std::cerr << "restoring home = " << m_HOME << std::endl;
34 }
35
36protected:
37 std::string m_HOME;
38};
39
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070040BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060041
42BOOST_AUTO_TEST_CASE(TestParse)
43{
44 using namespace boost::filesystem;
45 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
46
47 setenv("HOME", "tests/util/config-file-home", 1);
48
49 path homePath(absolute(std::getenv("HOME")));
50 homePath /= ".ndn/client.conf";
51
52 try
53 {
54 ConfigFile config;
55
56 BOOST_REQUIRE_EQUAL(config.getPath(), homePath);
57
58 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
59 BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
60 BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
61 }
62 catch(const std::runtime_error& error)
63 {
64 BOOST_FAIL("Unexpected exception: " << error.what());
65 }
66}
67
68BOOST_AUTO_TEST_CASE(EmptyPathParse)
69{
70 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
71
72 setenv("HOME", "tests/util/does/not/exist", 1);
73 try
74 {
75 ConfigFile config;
76 }
77 catch(const std::runtime_error& error)
78 {
79 BOOST_FAIL("Unexpected exception: " << error.what());
80 }
81}
82
83BOOST_AUTO_TEST_CASE(MalformedParse)
84{
85 using namespace boost::filesystem;
86 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
87
88 setenv("HOME", "tests/util/config-file-malformed-home", 1);
89
90 bool fileWasMalformed = false;
91 try
92 {
93 ConfigFile config;
94 }
95 catch(const ConfigFile::Error& error)
96 {
97 fileWasMalformed = true;
98 }
99
100 BOOST_REQUIRE(fileWasMalformed);
101}
102
103BOOST_AUTO_TEST_SUITE_END()
104
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700105} // namespace tests
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -0600106} // namespace ndn