Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 4 | * |
| 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 Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 23 | #include <cstdlib> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 24 | #include <fstream> |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 25 | #include <initializer_list> |
| 26 | |
| 27 | #include <boost/algorithm/string/replace.hpp> |
| 28 | #include <boost/filesystem.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | namespace 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 | */ |
| 40 | template<class Path> |
| 41 | class PibDirFixture |
| 42 | { |
| 43 | public: |
| 44 | PibDirFixture() |
| 45 | : m_pibDir(Path().PATH) |
| 46 | { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 47 | if (std::getenv("NDN_CLIENT_PIB") != nullptr) { |
| 48 | m_oldPib = std::getenv("NDN_CLIENT_PIB"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 49 | } |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 50 | if (std::getenv("NDN_CLIENT_TPM") != nullptr) { |
| 51 | m_oldTpm = std::getenv("NDN_CLIENT_TPM"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 52 | } |
| 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 Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 62 | setenv("NDN_CLIENT_PIB", m_oldPib.data(), true); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 63 | } |
| 64 | else { |
| 65 | unsetenv("NDN_CLIENT_PIB"); |
| 66 | } |
| 67 | |
| 68 | if (!m_oldTpm.empty()) { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 69 | setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 70 | } |
| 71 | else { |
| 72 | unsetenv("NDN_CLIENT_TPM"); |
| 73 | } |
| 74 | |
| 75 | boost::filesystem::remove_all(m_pibDir); |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | const std::string m_pibDir; |
| 80 | |
| 81 | private: |
| 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 | */ |
| 89 | template<class Path> |
| 90 | class TestHomeFixture : public PibDirFixture<Path> |
| 91 | { |
| 92 | public: |
| 93 | TestHomeFixture() |
| 94 | { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 95 | setenv("TEST_HOME", this->m_pibDir.data(), true); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | ~TestHomeFixture() |
| 99 | { |
| 100 | unsetenv("TEST_HOME"); |
| 101 | } |
| 102 | |
| 103 | void |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame^] | 104 | createClientConf(std::initializer_list<std::string> lines) const |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 105 | { |
| 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 | |
| 115 | struct 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 |