Vince Lehman | e875d1f | 2014-10-06 17:10:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
Vince Lehman | e875d1f | 2014-10-06 17:10:43 -0500 | [diff] [blame] | 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 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame^] | 23 | * Copyright (c) 2013-2017 Regents of the University of California. |
Vince Lehman | e875d1f | 2014-10-06 17:10:43 -0500 | [diff] [blame] | 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 | |
| 46 | namespace 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 | |
| 52 | class IdentityFixture |
| 53 | { |
| 54 | public: |
| 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 Afanasyev | c035206 | 2015-02-26 00:39:23 -0800 | [diff] [blame] | 66 | m_newIdentity = "/nlsr-test-identity"; |
Vince Lehman | e875d1f | 2014-10-06 17:10:43 -0500 | [diff] [blame] | 67 | 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 | |
| 90 | private: |
| 91 | ndn::KeyChain m_keyChain; |
| 92 | bool m_hasOldDefaultIdentity; |
| 93 | ndn::Name m_oldDefaultIdentity; |
| 94 | ndn::Name m_newIdentity; |
| 95 | }; |
| 96 | |
| 97 | BOOST_GLOBAL_FIXTURE(IdentityFixture) |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 98 | #if BOOST_VERSION >= 105900 |
| 99 | ; |
| 100 | #endif // BOOST_VERSION >= 105900 |
Vince Lehman | e875d1f | 2014-10-06 17:10:43 -0500 | [diff] [blame] | 101 | |
Alexander Afanasyev | c035206 | 2015-02-26 00:39:23 -0800 | [diff] [blame] | 102 | } // namespace nlsr |