blob: ef05416544ae07d3752863f8237bb3e6b6a90896 [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 {
20
21class ConfigFileFixture
22{
23public:
24 ConfigFileFixture()
25 {
26 m_HOME = std::getenv("HOME");
27 }
28
29 ~ConfigFileFixture()
30 {
31 setenv("HOME", m_HOME.c_str(), 1);
32 // std::cerr << "restoring home = " << m_HOME << std::endl;
33 }
34
35protected:
36 std::string m_HOME;
37};
38
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070039BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060040
41BOOST_AUTO_TEST_CASE(TestParse)
42{
43 using namespace boost::filesystem;
44 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
45
46 setenv("HOME", "tests/util/config-file-home", 1);
47
48 path homePath(absolute(std::getenv("HOME")));
49 homePath /= ".ndn/client.conf";
50
51 try
52 {
53 ConfigFile config;
54
55 BOOST_REQUIRE_EQUAL(config.getPath(), homePath);
56
57 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
58 BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
59 BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
60 }
61 catch(const std::runtime_error& error)
62 {
63 BOOST_FAIL("Unexpected exception: " << error.what());
64 }
65}
66
67BOOST_AUTO_TEST_CASE(EmptyPathParse)
68{
69 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
70
71 setenv("HOME", "tests/util/does/not/exist", 1);
72 try
73 {
74 ConfigFile config;
75 }
76 catch(const std::runtime_error& error)
77 {
78 BOOST_FAIL("Unexpected exception: " << error.what());
79 }
80}
81
82BOOST_AUTO_TEST_CASE(MalformedParse)
83{
84 using namespace boost::filesystem;
85 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
86
87 setenv("HOME", "tests/util/config-file-malformed-home", 1);
88
89 bool fileWasMalformed = false;
90 try
91 {
92 ConfigFile config;
93 }
94 catch(const ConfigFile::Error& error)
95 {
96 fileWasMalformed = true;
97 }
98
99 BOOST_REQUIRE(fileWasMalformed);
100}
101
102BOOST_AUTO_TEST_SUITE_END()
103
104} // namespace ndn