blob: 38c85a88fd2b1fc68f5847f83b4768bd8a52258f [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"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070023#include "../util/test-home-environment-fixture.hpp"
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
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070029class IdentityFixture : public util::TestHomeEnvironmentFixture
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030{
31public:
32 IdentityFixture()
33 {
Yingdi Yud9715e32014-06-27 08:48:47 -070034 // initialize KeyChain from TEST_HOME
Yingdi Yud9715e32014-06-27 08:48:47 -070035 setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1);
36
37 KeyChain keyChain("sqlite3", "file");
38
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070040 try {
Yingdi Yud9715e32014-06-27 08:48:47 -070041 m_oldDefaultIdentity = keyChain.getDefaultIdentity();
Junxiao Shi5109dee2014-03-27 19:40:30 -070042 m_hasOldDefaultIdentity = true;
43 }
44 catch (SecPublicInfo::Error& e) {
45 m_hasOldDefaultIdentity = false;
46 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047
Alexander Afanasyev766cea72014-04-24 19:16:42 -070048 m_newIdentity.set("/ndn-cxx-test-identity");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049 m_newIdentity.appendVersion();
50
51 // create the new identity and self-signed certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070052 keyChain.createIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070053
54 // set the new identity as default identity,
55 // and the corresponding certificate becomes the default certificate
Yingdi Yud9715e32014-06-27 08:48:47 -070056 keyChain.setDefaultIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057 }
58
59 ~IdentityFixture()
60 {
Yingdi Yud9715e32014-06-27 08:48:47 -070061 KeyChain keyChain("sqlite3", "file");
62
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070063 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070064 if (m_hasOldDefaultIdentity) {
Yingdi Yud9715e32014-06-27 08:48:47 -070065 keyChain.setDefaultIdentity(m_oldDefaultIdentity);
Junxiao Shi5109dee2014-03-27 19:40:30 -070066 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070067
68 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070069 // XXX This has no effect if oldDefaultIdentity doesn't exist.
70 // newIdentity would be kept as default.
Yingdi Yud9715e32014-06-27 08:48:47 -070071 keyChain.deleteIdentity(m_newIdentity);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070072 }
73
74private:
Yingdi Yud9715e32014-06-27 08:48:47 -070075 std::string m_HOME;
76
Junxiao Shi5109dee2014-03-27 19:40:30 -070077 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070078 Name m_oldDefaultIdentity;
79 Name m_newIdentity;
80};
81
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070082BOOST_GLOBAL_FIXTURE(IdentityFixture)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083
Yingdi Yud9715e32014-06-27 08:48:47 -070084} // namespace security
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070085} // namespace ndn