blob: 36ab8076e5a6c6f7db5b9c702e5470d570acd1df [file] [log] [blame]
Vince Lehmane875d1f2014-10-06 17:10:43 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 **/
21 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22/**
23 * Copyright (c) 2013-2014 Regents of the University of California.
24 *
25 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
26 *
27 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
28 * terms of the GNU Lesser General Public License as published by the Free Software
29 * Foundation, either version 3 of the License, or (at your option) any later version.
30 *
31 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
32 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
34 *
35 * You should have received copies of the GNU General Public License and GNU Lesser
36 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
37 * <http://www.gnu.org/licenses/>.
38 *
39 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
40 */
41
42#include <ndn-cxx/security/key-chain.hpp>
43
44#include "boost-test.hpp"
45
46namespace nlsr {
47
48// OSX KeyChain, when used on a headless server,
49// forbids usage of a private key if that key isn't created by the calling process.
50// Therefore, unit testing must create its own key pair.
51
52class IdentityFixture
53{
54public:
55 IdentityFixture()
56 {
57 // save the old default identity
58 try {
59 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
60 m_hasOldDefaultIdentity = true;
61 }
62 catch (ndn::SecPublicInfo::Error& e) {
63 m_hasOldDefaultIdentity = false;
64 }
65
Alexander Afanasyevc0352062015-02-26 00:39:23 -080066 m_newIdentity = "/nlsr-test-identity";
Vince Lehmane875d1f2014-10-06 17:10:43 -050067 m_newIdentity.appendVersion();
68
69 // create the new identity and self-signed certificate
70 m_keyChain.createIdentity(m_newIdentity);
71
72 // set the new identity as default identity,
73 // and the corresponding certificate becomes the default certificate
74 m_keyChain.setDefaultIdentity(m_newIdentity);
75 }
76
77 ~IdentityFixture()
78 {
79 // recover the old default setting
80 if (m_hasOldDefaultIdentity) {
81 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
82 }
83
84 // remove the temporarily created identity and certificates
85 // XXX This has no effect if oldDefaultIdentity doesn't exist.
86 // newIdentity would be kept as default.
87 m_keyChain.deleteIdentity(m_newIdentity);
88 }
89
90private:
91 ndn::KeyChain m_keyChain;
92 bool m_hasOldDefaultIdentity;
93 ndn::Name m_oldDefaultIdentity;
94 ndn::Name m_newIdentity;
95};
96
97BOOST_GLOBAL_FIXTURE(IdentityFixture)
98
Alexander Afanasyevc0352062015-02-26 00:39:23 -080099} // namespace nlsr