Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
| 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_TEST_HOME_FIXTURE_HPP |
| 23 | #define NDN_TESTS_TEST_HOME_FIXTURE_HPP |
| 24 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/v2/key-chain.hpp> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | #include <fstream> |
| 28 | #include <initializer_list> |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 29 | #include <boost/algorithm/string.hpp> |
| 30 | #include <boost/filesystem.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace ndncert { |
| 34 | namespace tests { |
| 35 | |
| 36 | /** |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 37 | * @brief TestHomeFixture to set TEST_HOME variable and allow config file creation |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 38 | */ |
| 39 | template<class Path> |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 40 | class TestHomeFixture |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | TestHomeFixture() |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 44 | : m_testHomeDir(Path().PATH) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | { |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 46 | setenv("TEST_HOME", m_testHomeDir.c_str(), true); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ~TestHomeFixture() |
| 50 | { |
| 51 | unsetenv("TEST_HOME"); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | createClientConf(std::initializer_list<std::string> lines) const |
| 56 | { |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 57 | boost::filesystem::create_directories(boost::filesystem::path(m_testHomeDir) / ".ndn"); |
| 58 | std::ofstream of((boost::filesystem::path(m_testHomeDir) / ".ndn" / "client.conf").c_str()); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 59 | for (auto line : lines) { |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 60 | boost::replace_all(line, "%PATH%", m_testHomeDir); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 61 | of << line << std::endl; |
| 62 | } |
| 63 | } |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 64 | |
| 65 | protected: |
| 66 | std::string m_testHomeDir; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct DefaultPibDir |
| 70 | { |
| 71 | const std::string PATH = "build/keys"; |
| 72 | }; |
| 73 | |
| 74 | } // namespace tests |
| 75 | } // namespace ndncert |
| 76 | } // namespace ndn |
| 77 | |
| 78 | #endif // NDN_TESTS_TEST_HOME_FIXTURE_HPP |