blob: b9b7dcfe317d561f7aa1464b4447c8e518a7a4f2 [file] [log] [blame]
Yumin Xia2c509c22017-02-09 14:37:36 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef NDNS_TESTS_TEST_HOME_FIXTURE_HPP
21#define NDNS_TESTS_TEST_HOME_FIXTURE_HPP
22
23#include "boost-test.hpp"
24
25#include <ndn-cxx/security/v2/key-chain.hpp>
26
27#include <boost/filesystem.hpp>
28#include <boost/algorithm/string.hpp>
29#include <fstream>
30
31namespace ndn {
32namespace ndns {
33namespace tests {
34
35/**
36 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
37 *
38 * Note that the specified PATH will be removed after fixture is destroyed.
39 * **Do not specify non-temporary paths.**
40 */
41template<class Path>
42class PibDirFixture
43{
44public:
45 PibDirFixture()
46 : m_pibDir(Path().PATH)
47 {
48 if (getenv("NDN_CLIENT_PIB") != nullptr) {
49 m_oldPib = getenv("NDN_CLIENT_PIB");
50 }
51 if (getenv("NDN_CLIENT_TPM") != nullptr) {
52 m_oldTpm = getenv("NDN_CLIENT_TPM");
53 }
54
55 /// @todo Consider change to an in-memory PIB/TPM
56 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
57 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
58 }
59
60 ~PibDirFixture()
61 {
62 if (!m_oldPib.empty()) {
63 setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true);
64 }
65 else {
66 unsetenv("NDN_CLIENT_PIB");
67 }
68
69 if (!m_oldTpm.empty()) {
70 setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true);
71 }
72 else {
73 unsetenv("NDN_CLIENT_TPM");
74 }
75
76 boost::filesystem::remove_all(m_pibDir);
77 }
78
79protected:
80 const std::string m_pibDir;
81
82private:
83 std::string m_oldPib;
84 std::string m_oldTpm;
85};
86
87/**
88 * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
89 */
90template<class Path>
91class TestHomeFixture : public PibDirFixture<Path>
92{
93public:
94 TestHomeFixture()
95 {
96 setenv("TEST_HOME", this->m_pibDir.c_str(), true);
97 }
98
99 ~TestHomeFixture()
100 {
101 unsetenv("TEST_HOME");
102 }
103
104 void
105 createClientConf(std::initializer_list<std::string> lines)
106 {
107 boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
108 std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
109 for (auto line : lines) {
110 boost::replace_all(line, "%PATH%", this->m_pibDir);
111 of << line << std::endl;
112 }
113 }
114};
115
116struct DefaultPibDir
117{
118 const std::string PATH = "build/keys";
119};
120
121} // namespace tests
122} // namespace ndns
123} // namespace ndn
124
125#endif // NDNS_TESTS_TEST_HOME_FIXTURE_HPP