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 | |
| 9 | #ifdef __clang__ |
| 10 | #pragma clang diagnostic push |
| 11 | #pragma clang diagnostic ignored "-Wunneeded-internal-declaration" |
| 12 | #endif // __clang__ |
| 13 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 14 | #include <boost/test/unit_test.hpp> |
| 15 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 16 | #ifdef __clang__ |
| 17 | #pragma clang diagnostic pop |
| 18 | #endif // __clang__ |
| 19 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 20 | namespace ndn { |
| 21 | |
| 22 | // OSX KeyChain, when used on a headless server, |
| 23 | // forbids usage of a private key if that key isn't created by the calling process. |
| 24 | // Therefore, unit testing must create its own key pair. |
| 25 | |
| 26 | class IdentityFixture |
| 27 | { |
| 28 | public: |
| 29 | IdentityFixture() |
| 30 | { |
| 31 | // save the old default identity |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 32 | try { |
| 33 | m_oldDefaultIdentity = m_keyChain.getDefaultIdentity(); |
| 34 | m_hasOldDefaultIdentity = true; |
| 35 | } |
| 36 | catch (SecPublicInfo::Error& e) { |
| 37 | m_hasOldDefaultIdentity = false; |
| 38 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 39 | |
| 40 | m_newIdentity.set("/ndn-cpp-dev-test-identity"); |
| 41 | m_newIdentity.appendVersion(); |
| 42 | |
| 43 | // create the new identity and self-signed certificate |
| 44 | m_keyChain.createIdentity(m_newIdentity); |
| 45 | |
| 46 | // set the new identity as default identity, |
| 47 | // and the corresponding certificate becomes the default certificate |
| 48 | m_keyChain.setDefaultIdentity(m_newIdentity); |
| 49 | } |
| 50 | |
| 51 | ~IdentityFixture() |
| 52 | { |
| 53 | // recover the old default setting |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 54 | if (m_hasOldDefaultIdentity) { |
| 55 | m_keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
| 56 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 57 | |
| 58 | // remove the temporarily created identity and certificates |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 59 | // XXX This has no effect if oldDefaultIdentity doesn't exist. |
| 60 | // newIdentity would be kept as default. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 61 | m_keyChain.deleteIdentity(m_newIdentity); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | KeyChain m_keyChain; |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 66 | bool m_hasOldDefaultIdentity; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 67 | Name m_oldDefaultIdentity; |
| 68 | Name m_newIdentity; |
| 69 | }; |
| 70 | |
| 71 | BOOST_GLOBAL_FIXTURE(IdentityFixture); |
| 72 | |
| 73 | } // namespace ndn |