blob: 1c286e205f8ca24c1b9e9623709ef1afcce343b4 [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"
8#include <boost/test/unit_test.hpp>
9
10namespace 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
16class IdentityFixture
17{
18public:
19 IdentityFixture()
20 {
21 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070022 try {
23 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
24 m_hasOldDefaultIdentity = true;
25 }
26 catch (SecPublicInfo::Error& e) {
27 m_hasOldDefaultIdentity = false;
28 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029
30 m_newIdentity.set("/ndn-cpp-dev-test-identity");
31 m_newIdentity.appendVersion();
32
33 // create the new identity and self-signed certificate
34 m_keyChain.createIdentity(m_newIdentity);
35
36 // set the new identity as default identity,
37 // and the corresponding certificate becomes the default certificate
38 m_keyChain.setDefaultIdentity(m_newIdentity);
39 }
40
41 ~IdentityFixture()
42 {
43 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070044 if (m_hasOldDefaultIdentity) {
45 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
46 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047
48 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070049 // XXX This has no effect if oldDefaultIdentity doesn't exist.
50 // newIdentity would be kept as default.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070051 m_keyChain.deleteIdentity(m_newIdentity);
52 }
53
54private:
55 KeyChain m_keyChain;
Junxiao Shi5109dee2014-03-27 19:40:30 -070056 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057 Name m_oldDefaultIdentity;
58 Name m_newIdentity;
59};
60
61BOOST_GLOBAL_FIXTURE(IdentityFixture);
62
63} // namespace ndn