blob: bf4e00481ab94b54ee47e443f566857255be2298 [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>
31
32namespace ndn {
33namespace tests {
34
35/**
36 * @brief Fixture to adjust/restore NDN_CLIENT_PIB and NDN_CLIENT_TPM paths
37 *
38 * Note that the specified PATH will be removed after fixture is destroyed.
39 * **Do not specify non-temporary paths.**
40 */
41template<class Path>
42class PibDirFixture
43{
44public:
45 PibDirFixture()
46 : m_pibDir(Path().PATH)
47 {
48 if (getenv("NDN_CLIENT_PIB") != nullptr) {
49 m_oldPib = getenv("NDN_CLIENT_PIB");
50 }
51 if (getenv("NDN_CLIENT_TPM") != nullptr) {
52 m_oldTpm = getenv("NDN_CLIENT_TPM");
53 }
54
55 /// @todo Consider change to an in-memory PIB/TPM
56 setenv("NDN_CLIENT_PIB", ("pib-sqlite3:" + m_pibDir).c_str(), true);
57 setenv("NDN_CLIENT_TPM", ("tpm-file:" + m_pibDir).c_str(), true);
58 }
59
60 ~PibDirFixture()
61 {
62 if (!m_oldPib.empty()) {
63 setenv("NDN_CLIENT_PIB", m_oldPib.c_str(), true);
64 }
65 else {
66 unsetenv("NDN_CLIENT_PIB");
67 }
68
69 if (!m_oldTpm.empty()) {
70 setenv("NDN_CLIENT_TPM", m_oldTpm.c_str(), true);
71 }
72 else {
73 unsetenv("NDN_CLIENT_TPM");
74 }
75
76 boost::filesystem::remove_all(m_pibDir);
77 }
78
79protected:
80 const std::string m_pibDir;
81
82private:
83 std::string m_oldPib;
84 std::string m_oldTpm;
85};
86
87struct DefaultPibDir
88{
89 const std::string PATH = "build/keys";
90};
91
92/**
93 * @brief Fixture to create a test KeyChain with default identity
94 */
95class KeyChainFixture : public PibDirFixture<DefaultPibDir>,
96 public IdentityManagementFixture
97{
98public:
99 KeyChainFixture();
100};
101
102} // namespace tests
103} // namespace ndn
104
105#endif // NDN_TESTS_KEY_CHAIN_FIXTURE_HPP