Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 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 | |
| 33 | #include <ndn-cxx/security/v2/key-chain.hpp> |
| 34 | |
| 35 | #include <boost/filesystem.hpp> |
| 36 | #include <boost/algorithm/string.hpp> |
| 37 | |
| 38 | namespace nlsr { |
| 39 | namespace tests { |
| 40 | |
| 41 | /** |
| 42 | * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths |
| 43 | * |
| 44 | * Note that the specified PATH will be removed after fixture is destroyed. |
| 45 | * **Do not specify non-temporary paths.** |
| 46 | */ |
| 47 | template<class Path> |
| 48 | class PibDirFixture |
| 49 | { |
| 50 | public: |
| 51 | PibDirFixture() |
| 52 | : m_pibDir(Path().PATH) |
| 53 | { |
| 54 | if (getenv("NDN_CLIENT_PIB") != nullptr) { |
| 55 | m_oldPib = getenv("NDN_CLIENT_PIB"); |
| 56 | } |
| 57 | if (getenv("NDN_CLIENT_TPM") != nullptr) { |
| 58 | m_oldTpm = getenv("NDN_CLIENT_TPM"); |
| 59 | } |
| 60 | |
| 61 | /// @todo Consider change to an in-memory PIB/TPM |
| 62 | setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
| 63 | setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
| 64 | } |
| 65 | |
| 66 | ~PibDirFixture() |
| 67 | { |
| 68 | if (!m_oldPib.empty()) { |
| 69 | setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true); |
| 70 | } |
| 71 | else { |
| 72 | unsetenv("NDN_CLIENT_PIB"); |
| 73 | } |
| 74 | |
| 75 | if (!m_oldTpm.empty()) { |
| 76 | setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true); |
| 77 | } |
| 78 | else { |
| 79 | unsetenv("NDN_CLIENT_TPM"); |
| 80 | } |
| 81 | |
| 82 | boost::filesystem::remove_all(m_pibDir); |
| 83 | //const_cast<std::string&>(ndn::security::v2::KeyChain::getDefaultPibLocator()).clear(); |
| 84 | //const_cast<std::string&>(ndn::security::v2::KeyChain::getDefaultTpmLocator()).clear(); |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | const std::string m_pibDir; |
| 89 | |
| 90 | private: |
| 91 | std::string m_oldPib; |
| 92 | std::string m_oldTpm; |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation |
| 97 | */ |
| 98 | template<class Path> |
| 99 | class TestHomeFixture : public PibDirFixture<Path> |
| 100 | { |
| 101 | public: |
| 102 | TestHomeFixture() |
| 103 | { |
| 104 | setenv("TEST_HOME", this->m_pibDir.c_str(), true); |
| 105 | } |
| 106 | |
| 107 | ~TestHomeFixture() |
| 108 | { |
| 109 | unsetenv("TEST_HOME"); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | createClientConf(std::initializer_list<std::string> lines) |
| 114 | { |
| 115 | boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn"); |
| 116 | std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str()); |
| 117 | for (auto line : lines) { |
| 118 | boost::replace_all(line, "%PATH%", this->m_pibDir); |
| 119 | of << line << std::endl; |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | struct DefaultPibDir |
| 125 | { |
| 126 | const std::string PATH = "build/keys"; |
| 127 | }; |
| 128 | |
| 129 | } // namespace tests |
| 130 | } // namespace nlsr |
| 131 | |
| 132 | #endif // NLSR_TEST_HOME_FIXTURE_HPP |