blob: 17e9abfa0f668421f0fa45507f0aa4746cfd3ca1 [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
Davide Pesaventobdd88c12020-11-26 00:35:08 -050023#include <cstdlib>
Yumin Xia2c509c22017-02-09 14:37:36 -080024#include <fstream>
Davide Pesaventobdd88c12020-11-26 00:35:08 -050025#include <initializer_list>
26
27#include <boost/algorithm/string/replace.hpp>
28#include <boost/filesystem.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080029
30namespace ndn {
31namespace ndns {
32namespace tests {
33
34/**
35 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
36 *
37 * Note that the specified PATH will be removed after fixture is destroyed.
38 * **Do not specify non-temporary paths.**
39 */
40template<class Path>
41class PibDirFixture
42{
43public:
44 PibDirFixture()
45 : m_pibDir(Path().PATH)
46 {
Davide Pesaventobdd88c12020-11-26 00:35:08 -050047 if (std::getenv("NDN_CLIENT_PIB") != nullptr) {
48 m_oldPib = std::getenv("NDN_CLIENT_PIB");
Yumin Xia2c509c22017-02-09 14:37:36 -080049 }
Davide Pesaventobdd88c12020-11-26 00:35:08 -050050 if (std::getenv("NDN_CLIENT_TPM") != nullptr) {
51 m_oldTpm = std::getenv("NDN_CLIENT_TPM");
Yumin Xia2c509c22017-02-09 14:37:36 -080052 }
53
54 /// @todo Consider change to an in-memory PIB/TPM
55 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
56 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
57 }
58
59 ~PibDirFixture()
60 {
61 if (!m_oldPib.empty()) {
Davide Pesaventobdd88c12020-11-26 00:35:08 -050062 setenv("NDN_CLIENT_PIB", m_oldPib.data(), true);
Yumin Xia2c509c22017-02-09 14:37:36 -080063 }
64 else {
65 unsetenv("NDN_CLIENT_PIB");
66 }
67
68 if (!m_oldTpm.empty()) {
Davide Pesaventobdd88c12020-11-26 00:35:08 -050069 setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true);
Yumin Xia2c509c22017-02-09 14:37:36 -080070 }
71 else {
72 unsetenv("NDN_CLIENT_TPM");
73 }
74
75 boost::filesystem::remove_all(m_pibDir);
76 }
77
78protected:
79 const std::string m_pibDir;
80
81private:
82 std::string m_oldPib;
83 std::string m_oldTpm;
84};
85
86/**
87 * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
88 */
89template<class Path>
90class TestHomeFixture : public PibDirFixture<Path>
91{
92public:
93 TestHomeFixture()
94 {
Davide Pesaventobdd88c12020-11-26 00:35:08 -050095 setenv("TEST_HOME", this->m_pibDir.data(), true);
Yumin Xia2c509c22017-02-09 14:37:36 -080096 }
97
98 ~TestHomeFixture()
99 {
100 unsetenv("TEST_HOME");
101 }
102
103 void
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500104 createClientConf(std::initializer_list<std::string> lines) const
Yumin Xia2c509c22017-02-09 14:37:36 -0800105 {
106 boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
107 std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
108 for (auto line : lines) {
109 boost::replace_all(line, "%PATH%", this->m_pibDir);
110 of << line << std::endl;
111 }
112 }
113};
114
115struct DefaultPibDir
116{
117 const std::string PATH = "build/keys";
118};
119
120} // namespace tests
121} // namespace ndns
122} // namespace ndn
123
124#endif // NDNS_TESTS_TEST_HOME_FIXTURE_HPP