Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
| 22 | #ifndef NDN_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 23 | #define NDN_TESTS_KEY_CHAIN_FIXTURE_HPP |
| 24 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 25 | #include "security/v2/key-chain.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 26 | |
| 27 | #include "boost-test.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 28 | |
| 29 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 30 | #include <boost/algorithm/string.hpp> |
| 31 | #include <fstream> |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace tests { |
| 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() |
| 47 | : m_pibDir(Path().PATH) |
| 48 | { |
| 49 | if (getenv("NDN_CLIENT_PIB") != nullptr) { |
| 50 | m_oldPib = getenv("NDN_CLIENT_PIB"); |
| 51 | } |
| 52 | if (getenv("NDN_CLIENT_TPM") != nullptr) { |
| 53 | m_oldTpm = getenv("NDN_CLIENT_TPM"); |
| 54 | } |
| 55 | |
| 56 | /// @todo Consider change to an in-memory PIB/TPM |
| 57 | setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
| 58 | setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
| 59 | } |
| 60 | |
| 61 | ~PibDirFixture() |
| 62 | { |
| 63 | if (!m_oldPib.empty()) { |
| 64 | setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true); |
| 65 | } |
| 66 | else { |
| 67 | unsetenv("NDN_CLIENT_PIB"); |
| 68 | } |
| 69 | |
| 70 | if (!m_oldTpm.empty()) { |
| 71 | setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true); |
| 72 | } |
| 73 | else { |
| 74 | unsetenv("NDN_CLIENT_TPM"); |
| 75 | } |
| 76 | |
| 77 | boost::filesystem::remove_all(m_pibDir); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 78 | const_cast<std::string&>(security::v2::KeyChain::getDefaultPibLocator()).clear(); |
| 79 | const_cast<std::string&>(security::v2::KeyChain::getDefaultTpmLocator()).clear(); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected: |
| 83 | const std::string m_pibDir; |
| 84 | |
| 85 | private: |
| 86 | std::string m_oldPib; |
| 87 | std::string m_oldTpm; |
| 88 | }; |
| 89 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 90 | /** |
| 91 | * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation |
| 92 | */ |
| 93 | template<class Path> |
| 94 | class TestHomeFixture : public PibDirFixture<Path> |
| 95 | { |
| 96 | public: |
| 97 | TestHomeFixture() |
| 98 | { |
| 99 | setenv("TEST_HOME", this->m_pibDir.c_str(), true); |
| 100 | } |
| 101 | |
| 102 | ~TestHomeFixture() |
| 103 | { |
| 104 | unsetenv("TEST_HOME"); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | createClientConf(std::initializer_list<std::string> lines) |
| 109 | { |
| 110 | boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn"); |
| 111 | std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str()); |
| 112 | for (auto line : lines) { |
| 113 | boost::replace_all(line, "%PATH%", this->m_pibDir); |
| 114 | of << line << std::endl; |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 119 | struct DefaultPibDir |
| 120 | { |
| 121 | const std::string PATH = "build/keys"; |
| 122 | }; |
| 123 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 124 | } // namespace tests |
| 125 | } // namespace ndn |
| 126 | |
| 127 | #endif // NDN_TESTS_KEY_CHAIN_FIXTURE_HPP |