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