blob: 3d4f05650d6a3328d4bf7f53f3c0a732ac3efdc5 [file] [log] [blame]
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento7e780642018-11-24 15:51:34 -05002/*
Davide Pesavento09904412021-03-24 16:40:53 -04003 * Copyright (c) 2013-2021 Regents of the University of California.
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP
23#define NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070024
Alexander Afanasyev09236c22020-06-03 13:42:38 -040025#include "ndn-cxx/security/key-chain.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include <cstdlib>
Alexander Afanasyevcf490552016-06-27 22:51:36 -070028#include <fstream>
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include <initializer_list>
30
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040031#include <boost/algorithm/string/replace.hpp>
Davide Pesaventod8e0cad2021-05-26 21:43:47 -040032#include <boost/filesystem/operations.hpp>
33#include <boost/filesystem/path.hpp>
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070034
35namespace ndn {
36namespace tests {
37
38/**
39 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
40 *
41 * Note that the specified PATH will be removed after fixture is destroyed.
42 * **Do not specify non-temporary paths.**
43 */
44template<class Path>
45class PibDirFixture
46{
47public:
48 PibDirFixture()
49 : m_pibDir(Path().PATH)
50 {
Davide Pesavento7e780642018-11-24 15:51:34 -050051 if (std::getenv("NDN_CLIENT_PIB") != nullptr) {
52 m_oldPib = std::getenv("NDN_CLIENT_PIB");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070053 }
Davide Pesavento7e780642018-11-24 15:51:34 -050054 if (std::getenv("NDN_CLIENT_TPM") != nullptr) {
55 m_oldTpm = std::getenv("NDN_CLIENT_TPM");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070056 }
57
58 /// @todo Consider change to an in-memory PIB/TPM
59 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
60 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
61 }
62
63 ~PibDirFixture()
64 {
65 if (!m_oldPib.empty()) {
Davide Pesavento7e780642018-11-24 15:51:34 -050066 setenv("NDN_CLIENT_PIB", m_oldPib.data(), true);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070067 }
68 else {
69 unsetenv("NDN_CLIENT_PIB");
70 }
71
72 if (!m_oldTpm.empty()) {
Davide Pesavento7e780642018-11-24 15:51:34 -050073 setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070074 }
75 else {
76 unsetenv("NDN_CLIENT_TPM");
77 }
78
79 boost::filesystem::remove_all(m_pibDir);
Alexander Afanasyev09236c22020-06-03 13:42:38 -040080 const_cast<std::string&>(KeyChain::getDefaultPibLocator()).clear();
81 const_cast<std::string&>(KeyChain::getDefaultTpmLocator()).clear();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070082 }
83
84protected:
85 const std::string m_pibDir;
86
87private:
88 std::string m_oldPib;
89 std::string m_oldTpm;
90};
91
Alexander Afanasyevcf490552016-06-27 22:51:36 -070092/**
93 * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
94 */
95template<class Path>
96class TestHomeFixture : public PibDirFixture<Path>
97{
98public:
99 TestHomeFixture()
100 {
101 setenv("TEST_HOME", this->m_pibDir.c_str(), true);
102 }
103
104 ~TestHomeFixture()
105 {
106 unsetenv("TEST_HOME");
107 }
108
109 void
Davide Pesavento7e780642018-11-24 15:51:34 -0500110 createClientConf(std::initializer_list<std::string> lines) const
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700111 {
112 boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
113 std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
114 for (auto line : lines) {
115 boost::replace_all(line, "%PATH%", this->m_pibDir);
116 of << line << std::endl;
117 }
118 }
119};
120
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700121struct DefaultPibDir
122{
123 const std::string PATH = "build/keys";
124};
125
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700126} // namespace tests
127} // namespace ndn
128
Davide Pesavento09904412021-03-24 16:40:53 -0400129#endif // NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP