blob: f4d6f66c26461df10c553f04947cb72b0fd4b5f1 [file] [log] [blame]
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -06007#include "util/config-file.hpp"
8
9#include <cstdlib>
10
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070011#include "boost-test.hpp"
12
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060013namespace ndn {
14
15class ConfigFileFixture
16{
17public:
18 ConfigFileFixture()
19 {
20 m_HOME = std::getenv("HOME");
21 }
22
23 ~ConfigFileFixture()
24 {
25 setenv("HOME", m_HOME.c_str(), 1);
26 // std::cerr << "restoring home = " << m_HOME << std::endl;
27 }
28
29protected:
30 std::string m_HOME;
31};
32
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070033BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
Steve DiBenedettoc07b3a22014-03-19 12:32:52 -060034
35BOOST_AUTO_TEST_CASE(TestParse)
36{
37 using namespace boost::filesystem;
38 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
39
40 setenv("HOME", "tests/util/config-file-home", 1);
41
42 path homePath(absolute(std::getenv("HOME")));
43 homePath /= ".ndn/client.conf";
44
45 try
46 {
47 ConfigFile config;
48
49 BOOST_REQUIRE_EQUAL(config.getPath(), homePath);
50
51 const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
52 BOOST_CHECK_EQUAL(parsed.get<std::string>("a"), "/path/to/nowhere");
53 BOOST_CHECK_EQUAL(parsed.get<std::string>("b"), "some-othervalue.01");
54 }
55 catch(const std::runtime_error& error)
56 {
57 BOOST_FAIL("Unexpected exception: " << error.what());
58 }
59}
60
61BOOST_AUTO_TEST_CASE(EmptyPathParse)
62{
63 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
64
65 setenv("HOME", "tests/util/does/not/exist", 1);
66 try
67 {
68 ConfigFile config;
69 }
70 catch(const std::runtime_error& error)
71 {
72 BOOST_FAIL("Unexpected exception: " << error.what());
73 }
74}
75
76BOOST_AUTO_TEST_CASE(MalformedParse)
77{
78 using namespace boost::filesystem;
79 // std::cerr << "current home = " << std::getenv("HOME") << std::endl;
80
81 setenv("HOME", "tests/util/config-file-malformed-home", 1);
82
83 bool fileWasMalformed = false;
84 try
85 {
86 ConfigFile config;
87 }
88 catch(const ConfigFile::Error& error)
89 {
90 fileWasMalformed = true;
91 }
92
93 BOOST_REQUIRE(fileWasMalformed);
94}
95
96BOOST_AUTO_TEST_SUITE_END()
97
98} // namespace ndn