blob: 04589bc7e446d9d0fbf914e06f9acd53b4fa4b09 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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"
Junxiao Shi482ccc52014-03-31 13:05:24 -070023
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070025
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070026namespace ndn {
Yingdi Yud9715e32014-06-27 08:48:47 -070027namespace security {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070028
29class IdentityFixture
30{
31public:
32 IdentityFixture()
33 {
Yingdi Yud9715e32014-06-27 08:48:47 -070034 // initialize KeyChain from TEST_HOME
35 if (std::getenv("TEST_HOME"))
36 m_HOME = std::getenv("TEST_HOME");
37 setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1);
38
39 KeyChain keyChain("sqlite3", "file");
40
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070041 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070042 try {
Yingdi Yud9715e32014-06-27 08:48:47 -070043 m_oldDefaultIdentity = keyChain.getDefaultIdentity();
Junxiao Shi5109dee2014-03-27 19:40:30 -070044 m_hasOldDefaultIdentity = true;
45 }
46 catch (SecPublicInfo::Error& e) {
47 m_hasOldDefaultIdentity = false;
48 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049
Alexander Afanasyev766cea72014-04-24 19:16:42 -070050 m_newIdentity.set("/ndn-cxx-test-identity");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070051 m_newIdentity.appendVersion();
52
53 // create the new identity and self-signed certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070054 keyChain.createIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070055
56 // set the new identity as default identity,
57 // and the corresponding certificate becomes the default certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070058 keyChain.setDefaultIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070059 }
60
61 ~IdentityFixture()
62 {
Yingdi Yud9715e32014-06-27 08:48:47 -070063 KeyChain keyChain("sqlite3", "file");
64
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070066 if (m_hasOldDefaultIdentity) {
Yingdi Yud9715e32014-06-27 08:48:47 -070067 keyChain.setDefaultIdentity(m_oldDefaultIdentity);
Junxiao Shi5109dee2014-03-27 19:40:30 -070068 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070069
70 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070071 // XXX This has no effect if oldDefaultIdentity doesn't exist.
72 // newIdentity would be kept as default.
Yingdi Yud9715e32014-06-27 08:48:47 -070073 keyChain.deleteIdentity(m_newIdentity);
74
75 if (!m_HOME.empty())
76 setenv("TEST_HOME", m_HOME.c_str(), 1);
77 else
78 unsetenv("TEST_HOME");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070079 }
80
81private:
Yingdi Yud9715e32014-06-27 08:48:47 -070082 std::string m_HOME;
83
Junxiao Shi5109dee2014-03-27 19:40:30 -070084 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070085 Name m_oldDefaultIdentity;
86 Name m_newIdentity;
87};
88
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070089BOOST_GLOBAL_FIXTURE(IdentityFixture)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070090
Yingdi Yud9715e32014-06-27 08:48:47 -070091} // namespace security
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070092} // namespace ndn