blob: 31bd3a238bee946d7327d21f6d455cab8239d168 [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 {
27
28// OSX KeyChain, when used on a headless server,
29// forbids usage of a private key if that key isn't created by the calling process.
30// Therefore, unit testing must create its own key pair.
31
32class IdentityFixture
33{
34public:
35 IdentityFixture()
36 {
37 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070038 try {
39 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
40 m_hasOldDefaultIdentity = true;
41 }
42 catch (SecPublicInfo::Error& e) {
43 m_hasOldDefaultIdentity = false;
44 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070045
Alexander Afanasyev766cea72014-04-24 19:16:42 -070046 m_newIdentity.set("/ndn-cxx-test-identity");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047 m_newIdentity.appendVersion();
48
49 // create the new identity and self-signed certificate
50 m_keyChain.createIdentity(m_newIdentity);
51
52 // set the new identity as default identity,
53 // and the corresponding certificate becomes the default certificate
54 m_keyChain.setDefaultIdentity(m_newIdentity);
55 }
56
57 ~IdentityFixture()
58 {
59 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070060 if (m_hasOldDefaultIdentity) {
61 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
62 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070063
64 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070065 // XXX This has no effect if oldDefaultIdentity doesn't exist.
66 // newIdentity would be kept as default.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070067 m_keyChain.deleteIdentity(m_newIdentity);
68 }
69
70private:
71 KeyChain m_keyChain;
Junxiao Shi5109dee2014-03-27 19:40:30 -070072 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070073 Name m_oldDefaultIdentity;
74 Name m_newIdentity;
75};
76
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070077BOOST_GLOBAL_FIXTURE(IdentityFixture)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070078
79} // namespace ndn