Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #ifndef NDN_TESTS_TEST_HOME_FIXTURE_HPP |
| 23 | #define NDN_TESTS_TEST_HOME_FIXTURE_HPP |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/v2/key-chain.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include <cstdlib> |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 28 | #include <fstream> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include <initializer_list> |
| 30 | |
| 31 | #include <boost/algorithm/string.hpp> |
| 32 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace tests { |
| 36 | |
| 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 | */ |
| 43 | template<class Path> |
| 44 | class PibDirFixture |
| 45 | { |
| 46 | public: |
| 47 | PibDirFixture() |
| 48 | : m_pibDir(Path().PATH) |
| 49 | { |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 50 | if (std::getenv("NDN_CLIENT_PIB") != nullptr) { |
| 51 | m_oldPib = std::getenv("NDN_CLIENT_PIB"); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 52 | } |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 53 | if (std::getenv("NDN_CLIENT_TPM") != nullptr) { |
| 54 | m_oldTpm = std::getenv("NDN_CLIENT_TPM"); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// @todo Consider change to an in-memory PIB/TPM |
| 58 | setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
| 59 | setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
| 60 | } |
| 61 | |
| 62 | ~PibDirFixture() |
| 63 | { |
| 64 | if (!m_oldPib.empty()) { |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 65 | setenv("NDN_CLIENT_PIB", m_oldPib.data(), true); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 66 | } |
| 67 | else { |
| 68 | unsetenv("NDN_CLIENT_PIB"); |
| 69 | } |
| 70 | |
| 71 | if (!m_oldTpm.empty()) { |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 72 | setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 73 | } |
| 74 | else { |
| 75 | unsetenv("NDN_CLIENT_TPM"); |
| 76 | } |
| 77 | |
| 78 | boost::filesystem::remove_all(m_pibDir); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 79 | const_cast<std::string&>(security::v2::KeyChain::getDefaultPibLocator()).clear(); |
| 80 | const_cast<std::string&>(security::v2::KeyChain::getDefaultTpmLocator()).clear(); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [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 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 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 |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 109 | createClientConf(std::initializer_list<std::string> lines) const |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 120 | struct DefaultPibDir |
| 121 | { |
| 122 | const std::string PATH = "build/keys"; |
| 123 | }; |
| 124 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 125 | } // namespace tests |
| 126 | } // namespace ndn |
| 127 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 128 | #endif // NDN_TESTS_TEST_HOME_FIXTURE_HPP |