blob: 899f5de8f9e7f52b41563b7f5d7b83936dba0e7d [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
9#ifdef __clang__
10#pragma clang diagnostic push
11#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
12#endif // __clang__
13
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070014#include <boost/test/unit_test.hpp>
15
Junxiao Shi482ccc52014-03-31 13:05:24 -070016#ifdef __clang__
17#pragma clang diagnostic pop
18#endif // __clang__
19
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070020namespace 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
26class IdentityFixture
27{
28public:
29 IdentityFixture()
30 {
31 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070032 try {
33 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
34 m_hasOldDefaultIdentity = true;
35 }
36 catch (SecPublicInfo::Error& e) {
37 m_hasOldDefaultIdentity = false;
38 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039
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 Shi5109dee2014-03-27 19:40:30 -070054 if (m_hasOldDefaultIdentity) {
55 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
56 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057
58 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070059 // XXX This has no effect if oldDefaultIdentity doesn't exist.
60 // newIdentity would be kept as default.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070061 m_keyChain.deleteIdentity(m_newIdentity);
62 }
63
64private:
65 KeyChain m_keyChain;
Junxiao Shi5109dee2014-03-27 19:40:30 -070066 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070067 Name m_oldDefaultIdentity;
68 Name m_newIdentity;
69};
70
71BOOST_GLOBAL_FIXTURE(IdentityFixture);
72
73} // namespace ndn