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 | |
| 25 | #include "security/key-chain.hpp" |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 26 | #include "security/v2/key-chain.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 27 | |
| 28 | #include "boost-test.hpp" |
| 29 | #include "identity-management-fixture.hpp" |
| 30 | |
| 31 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 32 | #include <boost/algorithm/string.hpp> |
| 33 | #include <fstream> |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace tests { |
| 37 | |
| 38 | /** |
| 39 | * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths |
| 40 | * |
| 41 | * Note that the specified PATH will be removed after fixture is destroyed. |
| 42 | * **Do not specify non-temporary paths.** |
| 43 | */ |
| 44 | template<class Path> |
| 45 | class PibDirFixture |
| 46 | { |
| 47 | public: |
| 48 | PibDirFixture() |
| 49 | : m_pibDir(Path().PATH) |
| 50 | { |
| 51 | if (getenv("NDN_CLIENT_PIB") != nullptr) { |
| 52 | m_oldPib = getenv("NDN_CLIENT_PIB"); |
| 53 | } |
| 54 | if (getenv("NDN_CLIENT_TPM") != nullptr) { |
| 55 | m_oldTpm = getenv("NDN_CLIENT_TPM"); |
| 56 | } |
| 57 | |
| 58 | /// @todo Consider change to an in-memory PIB/TPM |
| 59 | setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true); |
| 60 | setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true); |
| 61 | } |
| 62 | |
| 63 | ~PibDirFixture() |
| 64 | { |
| 65 | if (!m_oldPib.empty()) { |
| 66 | setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true); |
| 67 | } |
| 68 | else { |
| 69 | unsetenv("NDN_CLIENT_PIB"); |
| 70 | } |
| 71 | |
| 72 | if (!m_oldTpm.empty()) { |
| 73 | setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true); |
| 74 | } |
| 75 | else { |
| 76 | unsetenv("NDN_CLIENT_TPM"); |
| 77 | } |
| 78 | |
| 79 | boost::filesystem::remove_all(m_pibDir); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 80 | const_cast<std::string&>(security::v2::KeyChain::getDefaultPibLocator()).clear(); |
| 81 | const_cast<std::string&>(security::v2::KeyChain::getDefaultTpmLocator()).clear(); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | protected: |
| 85 | const std::string m_pibDir; |
| 86 | |
| 87 | private: |
| 88 | std::string m_oldPib; |
| 89 | std::string m_oldTpm; |
| 90 | }; |
| 91 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @brief Extension of PibDirFixture to set TEST_HOME variable and allow config file creation |
| 94 | */ |
| 95 | template<class Path> |
| 96 | class TestHomeFixture : public PibDirFixture<Path> |
| 97 | { |
| 98 | public: |
| 99 | TestHomeFixture() |
| 100 | { |
| 101 | setenv("TEST_HOME", this->m_pibDir.c_str(), true); |
| 102 | } |
| 103 | |
| 104 | ~TestHomeFixture() |
| 105 | { |
| 106 | unsetenv("TEST_HOME"); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | createClientConf(std::initializer_list<std::string> lines) |
| 111 | { |
| 112 | boost::filesystem::create_directories(boost::filesystem::path(this->m_pibDir) / ".ndn"); |
| 113 | std::ofstream of((boost::filesystem::path(this->m_pibDir) / ".ndn" / "client.conf").c_str()); |
| 114 | for (auto line : lines) { |
| 115 | boost::replace_all(line, "%PATH%", this->m_pibDir); |
| 116 | of << line << std::endl; |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 122 | struct DefaultPibDir |
| 123 | { |
| 124 | const std::string PATH = "build/keys"; |
| 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * @brief Fixture to create a test KeyChain with default identity |
| 129 | */ |
| 130 | class KeyChainFixture : public PibDirFixture<DefaultPibDir>, |
| 131 | public IdentityManagementFixture |
| 132 | { |
| 133 | public: |
| 134 | KeyChainFixture(); |
| 135 | }; |
| 136 | |
Alexander Afanasyev | cf49055 | 2016-06-27 22:51:36 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 138 | } // namespace tests |
| 139 | } // namespace ndn |
| 140 | |
| 141 | #endif // NDN_TESTS_KEY_CHAIN_FIXTURE_HPP |