Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "security/key-chain.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 26 | namespace 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 | |
| 32 | class IdentityFixture |
| 33 | { |
| 34 | public: |
| 35 | IdentityFixture() |
| 36 | { |
| 37 | // save the old default identity |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 38 | try { |
| 39 | m_oldDefaultIdentity = m_keyChain.getDefaultIdentity(); |
| 40 | m_hasOldDefaultIdentity = true; |
| 41 | } |
| 42 | catch (SecPublicInfo::Error& e) { |
| 43 | m_hasOldDefaultIdentity = false; |
| 44 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 46 | m_newIdentity.set("/ndn-cxx-test-identity"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 47 | 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 Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 60 | if (m_hasOldDefaultIdentity) { |
| 61 | m_keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
| 62 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | // remove the temporarily created identity and certificates |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 65 | // XXX This has no effect if oldDefaultIdentity doesn't exist. |
| 66 | // newIdentity would be kept as default. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 67 | m_keyChain.deleteIdentity(m_newIdentity); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | KeyChain m_keyChain; |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 72 | bool m_hasOldDefaultIdentity; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 73 | Name m_oldDefaultIdentity; |
| 74 | Name m_newIdentity; |
| 75 | }; |
| 76 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 77 | BOOST_GLOBAL_FIXTURE(IdentityFixture) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 78 | |
| 79 | } // namespace ndn |