blob: a89eb8d6438e1848b3d781438a78b80ef8091d6b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070020 */
21
22#include "security/key-chain.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070023#include "../util/test-home-environment-fixture.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080024#include <boost/filesystem.hpp>
25
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070027
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070028namespace ndn {
Yingdi Yud9715e32014-06-27 08:48:47 -070029namespace security {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070031class IdentityFixture : public util::TestHomeEnvironmentFixture
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070032{
33public:
34 IdentityFixture()
35 {
Yingdi Yu41546342014-11-30 23:37:53 -080036 using namespace boost::filesystem;
Yingdi Yud9715e32014-06-27 08:48:47 -070037
Yingdi Yu41546342014-11-30 23:37:53 -080038 // initialize KeyChain from test specific HOME: tests/unit-tests/security/tmp-home
39 if (std::getenv("HOME"))
40 m_HOME = std::getenv("HOME");
41 if (std::getenv("OLD_HOME"))
42 m_OLD_HOME = std::getenv("OLD_HOME");
43
44 setenv("HOME", "tests/unit-tests/security/tmp-home", 1);
45 setenv("OLD_HOME", m_HOME.c_str(), 1);
46
47 KeyChain keyChain;
Yingdi Yud9715e32014-06-27 08:48:47 -070048
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070050 try {
Yingdi Yud9715e32014-06-27 08:48:47 -070051 m_oldDefaultIdentity = keyChain.getDefaultIdentity();
Junxiao Shi5109dee2014-03-27 19:40:30 -070052 m_hasOldDefaultIdentity = true;
53 }
54 catch (SecPublicInfo::Error& e) {
55 m_hasOldDefaultIdentity = false;
56 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057
Alexander Afanasyevc89efb42015-02-10 18:26:42 -080058 m_newIdentity = "/ndn-cxx-test-identity";
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070059 m_newIdentity.appendVersion();
60
61 // create the new identity and self-signed certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070062 keyChain.createIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070063
64 // set the new identity as default identity,
65 // and the corresponding certificate becomes the default certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070066 keyChain.setDefaultIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070067 }
68
69 ~IdentityFixture()
70 {
Yingdi Yu41546342014-11-30 23:37:53 -080071 using namespace boost::filesystem;
72
73 KeyChain keyChain;
Yingdi Yud9715e32014-06-27 08:48:47 -070074
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070075 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070076 if (m_hasOldDefaultIdentity) {
Yingdi Yud9715e32014-06-27 08:48:47 -070077 keyChain.setDefaultIdentity(m_oldDefaultIdentity);
Junxiao Shi5109dee2014-03-27 19:40:30 -070078 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070079
80 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070081 // XXX This has no effect if oldDefaultIdentity doesn't exist.
82 // newIdentity would be kept as default.
Yingdi Yud9715e32014-06-27 08:48:47 -070083 keyChain.deleteIdentity(m_newIdentity);
Yingdi Yu41546342014-11-30 23:37:53 -080084
85 path pibPath(absolute(std::getenv("HOME")));
86 pibPath /= ".ndn/ndnsec-public-info.db";
87
88 boost::filesystem::remove(pibPath);
89
90 path tpmPath(absolute(std::getenv("HOME")));
91 tpmPath /= ".ndn/ndnsec-tpm-file";
92
93 boost::filesystem::remove_all(tpmPath);
94
95 if (!m_HOME.empty())
96 setenv("HOME", m_HOME.c_str(), 1);
97 else
98 unsetenv("HOME");
99
100 if (!m_OLD_HOME.empty())
101 setenv("OLD_HOME", m_OLD_HOME.c_str(), 1);
102 else
103 unsetenv("OLD_HOME");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700104 }
105
106private:
Yingdi Yu41546342014-11-30 23:37:53 -0800107 std::string m_OLD_HOME;
Yingdi Yud9715e32014-06-27 08:48:47 -0700108 std::string m_HOME;
109
Junxiao Shi5109dee2014-03-27 19:40:30 -0700110 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700111 Name m_oldDefaultIdentity;
112 Name m_newIdentity;
113};
114
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700115BOOST_GLOBAL_FIXTURE(IdentityFixture)
Alexander Afanasyev03c170e2015-10-01 11:24:06 -0700116#if BOOST_VERSION >= 105900
117;
118#endif // BOOST_VERSION >= 105900
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700119
Yingdi Yud9715e32014-06-27 08:48:47 -0700120} // namespace security
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700121} // namespace ndn