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