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