blob: 08d215863e66c1d95784301e389ebc89f0a21320 [file] [log] [blame]
Yumin Xia2c509c22017-02-09 14:37:36 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Afanasyev60514ec2020-06-03 14:18:53 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Yumin Xia2c509c22017-02-09 14:37:36 -08004 *
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
Yumin Xia2c509c22017-02-09 14:37:36 -080025#include <boost/filesystem.hpp>
26#include <boost/algorithm/string.hpp>
27#include <fstream>
28
29namespace ndn {
30namespace ndns {
31namespace tests {
32
33/**
34 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
35 *
36 * Note that the specified PATH will be removed after fixture is destroyed.
37 * **Do not specify non-temporary paths.**
38 */
39template<class Path>
40class PibDirFixture
41{
42public:
43 PibDirFixture()
44 : m_pibDir(Path().PATH)
45 {
46 if (getenv("NDN_CLIENT_PIB") != nullptr) {
47 m_oldPib = getenv("NDN_CLIENT_PIB");
48 }
49 if (getenv("NDN_CLIENT_TPM") != nullptr) {
50 m_oldTpm = getenv("NDN_CLIENT_TPM");
51 }
52
53 /// @todo Consider change to an in-memory PIB/TPM
54 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
55 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
56 }
57
58 ~PibDirFixture()
59 {
60 if (!m_oldPib.empty()) {
61 setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true);
62 }
63 else {
64 unsetenv("NDN_CLIENT_PIB");
65 }
66
67 if (!m_oldTpm.empty()) {
68 setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true);
69 }
70 else {
71 unsetenv("NDN_CLIENT_TPM");
72 }
73
74 boost::filesystem::remove_all(m_pibDir);
75 }
76
77protected:
78 const std::string m_pibDir;
79
80private:
81 std::string m_oldPib;
82 std::string m_oldTpm;
83};
84
85/**
86 * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
87 */
88template<class Path>
89class TestHomeFixture : public PibDirFixture<Path>
90{
91public:
92 TestHomeFixture()
93 {
94 setenv("TEST_HOME", this->m_pibDir.c_str(), true);
95 }
96
97 ~TestHomeFixture()
98 {
99 unsetenv("TEST_HOME");
100 }
101
102 void
103 createClientConf(std::initializer_list<std::string> lines)
104 {
105 boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
106 std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
107 for (auto line : lines) {
108 boost::replace_all(line, "%PATH%", this->m_pibDir);
109 of << line << std::endl;
110 }
111 }
112};
113
114struct DefaultPibDir
115{
116 const std::string PATH = "build/keys";
117};
118
119} // namespace tests
120} // namespace ndns
121} // namespace ndn
122
123#endif // NDNS_TESTS_TEST_HOME_FIXTURE_HPP