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 | /* |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2024 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 | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 22 | #ifndef NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP |
| 23 | #define NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/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> |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 28 | #include <filesystem> |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 29 | #include <fstream> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 30 | #include <initializer_list> |
| 31 | |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 32 | #include <boost/algorithm/string/replace.hpp> |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | namespace ndn::tests { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths |
| 38 | * |
| 39 | * Note that the specified PATH will be removed after fixture is destroyed. |
| 40 | * **Do not specify non-temporary paths.** |
| 41 | */ |
| 42 | template<class Path> |
| 43 | class PibDirFixture |
| 44 | { |
| 45 | public: |
| 46 | PibDirFixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 47 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 48 | if (const char* envPib = std::getenv("NDN_CLIENT_PIB"); envPib != nullptr) { |
| 49 | m_oldPib = envPib; |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 50 | } |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 51 | if (const char* envTpm = std::getenv("NDN_CLIENT_TPM"); envTpm != nullptr) { |
| 52 | m_oldTpm = envTpm; |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// @todo Consider change to an in-memory PIB/TPM |
| 56 | setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
| 57 | setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
| 58 | } |
| 59 | |
| 60 | ~PibDirFixture() |
| 61 | { |
| 62 | if (!m_oldPib.empty()) { |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 63 | setenv("NDN_CLIENT_PIB", m_oldPib.data(), true); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 64 | } |
| 65 | else { |
| 66 | unsetenv("NDN_CLIENT_PIB"); |
| 67 | } |
| 68 | |
| 69 | if (!m_oldTpm.empty()) { |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 70 | setenv("NDN_CLIENT_TPM", m_oldTpm.data(), true); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 71 | } |
| 72 | else { |
| 73 | unsetenv("NDN_CLIENT_TPM"); |
| 74 | } |
| 75 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 76 | std::filesystem::remove_all(m_pibDir); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 77 | KeyChain::resetDefaultLocators(); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | protected: |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 81 | const std::string m_pibDir{Path::PATH}; |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | std::string m_oldPib; |
| 85 | std::string m_oldTpm; |
| 86 | }; |
| 87 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 88 | /** |
| 89 | * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation |
| 90 | */ |
| 91 | template<class Path> |
| 92 | class TestHomeFixture : public PibDirFixture<Path> |
| 93 | { |
| 94 | public: |
| 95 | TestHomeFixture() |
| 96 | { |
| 97 | setenv("TEST_HOME", this->m_pibDir.c_str(), true); |
| 98 | } |
| 99 | |
| 100 | ~TestHomeFixture() |
| 101 | { |
| 102 | unsetenv("TEST_HOME"); |
| 103 | } |
| 104 | |
| 105 | void |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 106 | createClientConf(std::initializer_list<std::string> lines) const |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 107 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 108 | auto ndnDir = std::filesystem::path(this->m_pibDir) / ".ndn"; |
| 109 | std::filesystem::create_directories(ndnDir); |
| 110 | std::ofstream of(ndnDir / "client.conf"); |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 111 | for (auto line : lines) { |
| 112 | boost::replace_all(line, "%PATH%", this->m_pibDir); |
| 113 | of << line << std::endl; |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 118 | struct DefaultPibDir |
| 119 | { |
Davide Pesavento | 3fdb02f | 2023-04-12 02:32:38 -0400 | [diff] [blame] | 120 | static constexpr std::string_view PATH{"build/keys"}; |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 123 | } // namespace ndn::tests |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 124 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 125 | #endif // NDN_CXX_TESTS_TEST_HOME_FIXTURE_HPP |