blob: 9801c9211d41e492838e6ae94ebbff1878a50ef0 [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 Pesavento3fdb02f2023-04-12 02:32:38 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040035namespace ndn::tests {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070036
37/**
38 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
39 *
40 * Note that the specified PATH will be removed after fixture is destroyed.
41 * **Do not specify non-temporary paths.**
42 */
43template<class Path>
44class PibDirFixture
45{
46public:
47 PibDirFixture()
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070048 {
Davide Pesavento7e780642018-11-24 15:51:34 -050049 if (std::getenv("NDN_CLIENT_PIB") != nullptr) {
50 m_oldPib = std::getenv("NDN_CLIENT_PIB");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070051 }
Davide Pesavento7e780642018-11-24 15:51:34 -050052 if (std::getenv("NDN_CLIENT_TPM") != nullptr) {
53 m_oldTpm = std::getenv("NDN_CLIENT_TPM");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070054 }
55
56 /// @todo Consider change to an in-memory PIB/TPM
57 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
58 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
59 }
60
61 ~PibDirFixture()
62 {
63 if (!m_oldPib.empty()) {
Davide Pesavento7e780642018-11-24 15:51:34 -050064 setenv("NDN_CLIENT_PIB", m_oldPib.data(), true);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070065 }
66 else {
67 unsetenv("NDN_CLIENT_PIB");
68 }
69
70 if (!m_oldTpm.empty()) {
Davide Pesavento7e780642018-11-24 15:51:34 -050071 setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070072 }
73 else {
74 unsetenv("NDN_CLIENT_TPM");
75 }
76
77 boost::filesystem::remove_all(m_pibDir);
Davide Pesavento0e768ef2022-05-09 20:03:44 -040078 KeyChain::resetDefaultLocators();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070079 }
80
81protected:
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040082 const std::string m_pibDir{Path::PATH};
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070083
84private:
85 std::string m_oldPib;
86 std::string m_oldTpm;
87};
88
Alexander Afanasyevcf490552016-06-27 22:51:36 -070089/**
90 * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation
91 */
92template<class Path>
93class TestHomeFixture : public PibDirFixture<Path>
94{
95public:
96 TestHomeFixture()
97 {
98 setenv("TEST_HOME", this->m_pibDir.c_str(), true);
99 }
100
101 ~TestHomeFixture()
102 {
103 unsetenv("TEST_HOME");
104 }
105
106 void
Davide Pesavento7e780642018-11-24 15:51:34 -0500107 createClientConf(std::initializer_list<std::string> lines) const
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700108 {
109 boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn");
110 std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str());
111 for (auto line : lines) {
112 boost::replace_all(line, "%PATH%", this->m_pibDir);
113 of << line << std::endl;
114 }
115 }
116};
117
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700118struct DefaultPibDir
119{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400120 static constexpr std::string_view PATH{"build/keys"};
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700121};
122
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400123} // namespace ndn::tests
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700124
Davide Pesavento09904412021-03-24 16:40:53 -0400125#endif // NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP