blob: 858cf235cda15c092ce837b48174e38f619dec70 [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- 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 Zhang42d992d2019-07-07 16:46:50 -070025#include <ndn-cxx/security/v2/key-chain.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070026#include <cstdlib>
27#include <fstream>
28#include <initializer_list>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070029#include <boost/algorithm/string.hpp>
30#include <boost/filesystem.hpp>
31
32namespace ndn {
33namespace ndncert {
34namespace tests {
35
36/**
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070037 * @brief TestHomeFixture to set TEST_HOME variable and allow config file creation
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070038 */
39template<class Path>
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070040class TestHomeFixture
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041{
42public:
43 TestHomeFixture()
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070044 : m_testHomeDir(Path().PATH)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045 {
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070046 setenv("TEST_HOME", m_testHomeDir.c_str(), true);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070047 }
48
49 ~TestHomeFixture()
50 {
51 unsetenv("TEST_HOME");
52 }
53
54 void
55 createClientConf(std::initializer_list<std::string> lines) const
56 {
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070057 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 Zhangaf7c2902019-03-14 22:13:21 -070059 for (auto line : lines) {
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070060 boost::replace_all(line, "%PATH%", m_testHomeDir);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 of << line << std::endl;
62 }
63 }
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070064
65protected:
66 std::string m_testHomeDir;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067};
68
69struct 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