blob: 420b15f2c3fbd8ba6e59ec1ee56f92c96b66b7b2 [file] [log] [blame]
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07001/**
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 Shi482ccc52014-03-31 13:05:24 -07008
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -07009#include "boost-test.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070010
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070011namespace 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
17class IdentityFixture
18{
19public:
20 IdentityFixture()
21 {
22 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070023 try {
24 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
25 m_hasOldDefaultIdentity = true;
26 }
27 catch (SecPublicInfo::Error& e) {
28 m_hasOldDefaultIdentity = false;
29 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030
31 m_newIdentity.set("/ndn-cpp-dev-test-identity");
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 Shi5109dee2014-03-27 19:40:30 -070045 if (m_hasOldDefaultIdentity) {
46 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
47 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070048
49 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070050 // XXX This has no effect if oldDefaultIdentity doesn't exist.
51 // newIdentity would be kept as default.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070052 m_keyChain.deleteIdentity(m_newIdentity);
53 }
54
55private:
56 KeyChain m_keyChain;
Junxiao Shi5109dee2014-03-27 19:40:30 -070057 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070058 Name m_oldDefaultIdentity;
59 Name m_newIdentity;
60};
61
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070062BOOST_GLOBAL_FIXTURE(IdentityFixture)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070063
64} // namespace ndn