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" |
| 8 | #include <boost/test/unit_test.hpp> |
| 9 | |
| 10 | namespace ndn { |
| 11 | |
| 12 | // OSX KeyChain, when used on a headless server, |
| 13 | // forbids usage of a private key if that key isn't created by the calling process. |
| 14 | // Therefore, unit testing must create its own key pair. |
| 15 | |
| 16 | class IdentityFixture |
| 17 | { |
| 18 | public: |
| 19 | IdentityFixture() |
| 20 | { |
| 21 | // save the old default identity |
| 22 | m_oldDefaultIdentity = m_keyChain.getDefaultIdentity(); |
| 23 | |
| 24 | m_newIdentity.set("/ndn-cpp-dev-test-identity"); |
| 25 | m_newIdentity.appendVersion(); |
| 26 | |
| 27 | // create the new identity and self-signed certificate |
| 28 | m_keyChain.createIdentity(m_newIdentity); |
| 29 | |
| 30 | // set the new identity as default identity, |
| 31 | // and the corresponding certificate becomes the default certificate |
| 32 | m_keyChain.setDefaultIdentity(m_newIdentity); |
| 33 | } |
| 34 | |
| 35 | ~IdentityFixture() |
| 36 | { |
| 37 | // recover the old default setting |
| 38 | m_keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
| 39 | |
| 40 | // remove the temporarily created identity and certificates |
| 41 | m_keyChain.deleteIdentity(m_newIdentity); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | KeyChain m_keyChain; |
| 46 | Name m_oldDefaultIdentity; |
| 47 | Name m_newIdentity; |
| 48 | }; |
| 49 | |
| 50 | BOOST_GLOBAL_FIXTURE(IdentityFixture); |
| 51 | |
| 52 | } // namespace ndn |