Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "security/key-chain.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 14 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 15 | #include "boost-test.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 16 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
| 19 | // OSX KeyChain, when used on a headless server, |
| 20 | // forbids usage of a private key if that key isn't created by the calling process. |
| 21 | // Therefore, unit testing must create its own key pair. |
| 22 | |
| 23 | class IdentityFixture |
| 24 | { |
| 25 | public: |
| 26 | IdentityFixture() |
| 27 | { |
| 28 | // save the old default identity |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 29 | try { |
| 30 | m_oldDefaultIdentity = m_keyChain.getDefaultIdentity(); |
| 31 | m_hasOldDefaultIdentity = true; |
| 32 | } |
| 33 | catch (SecPublicInfo::Error& e) { |
| 34 | m_hasOldDefaultIdentity = false; |
| 35 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 37 | m_newIdentity.set("/ndn-cxx-test-identity"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 38 | m_newIdentity.appendVersion(); |
| 39 | |
| 40 | // create the new identity and self-signed certificate |
| 41 | m_keyChain.createIdentity(m_newIdentity); |
| 42 | |
| 43 | // set the new identity as default identity, |
| 44 | // and the corresponding certificate becomes the default certificate |
| 45 | m_keyChain.setDefaultIdentity(m_newIdentity); |
| 46 | } |
| 47 | |
| 48 | ~IdentityFixture() |
| 49 | { |
| 50 | // recover the old default setting |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 51 | if (m_hasOldDefaultIdentity) { |
| 52 | m_keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
| 53 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 54 | |
| 55 | // remove the temporarily created identity and certificates |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 56 | // XXX This has no effect if oldDefaultIdentity doesn't exist. |
| 57 | // newIdentity would be kept as default. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 58 | m_keyChain.deleteIdentity(m_newIdentity); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | KeyChain m_keyChain; |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 63 | bool m_hasOldDefaultIdentity; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 64 | Name m_oldDefaultIdentity; |
| 65 | Name m_newIdentity; |
| 66 | }; |
| 67 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 68 | BOOST_GLOBAL_FIXTURE(IdentityFixture) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 69 | |
| 70 | } // namespace ndn |