Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 4 | * 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 Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 33 | #include <boost/filesystem.hpp> |
| 34 | #include <boost/algorithm/string.hpp> |
| 35 | |
| 36 | namespace nlsr { |
| 37 | namespace 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 | */ |
| 45 | template<class Path> |
| 46 | class PibDirFixture |
| 47 | { |
| 48 | public: |
| 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 Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | protected: |
| 84 | const std::string m_pibDir; |
| 85 | |
| 86 | private: |
| 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 | */ |
| 94 | template<class Path> |
| 95 | class TestHomeFixture : public PibDirFixture<Path> |
| 96 | { |
| 97 | public: |
| 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 | |
| 120 | struct 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 |