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