blob: e1ee096a5a7757213c3d18aaf0449cd7e663b237 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070011 */
12
13#include "security/key-chain.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070014
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070015#include "boost-test.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070016
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070017namespace ndn {
18
19// OSX KeyChain, when used on a headless server,
20// forbids usage of a private key if that key isn't created by the calling process.
21// Therefore, unit testing must create its own key pair.
22
23class IdentityFixture
24{
25public:
26 IdentityFixture()
27 {
28 // save the old default identity
Junxiao Shi5109dee2014-03-27 19:40:30 -070029 try {
30 m_oldDefaultIdentity = m_keyChain.getDefaultIdentity();
31 m_hasOldDefaultIdentity = true;
32 }
33 catch (SecPublicInfo::Error& e) {
34 m_hasOldDefaultIdentity = false;
35 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070036
Alexander Afanasyev766cea72014-04-24 19:16:42 -070037 m_newIdentity.set("/ndn-cxx-test-identity");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070038 m_newIdentity.appendVersion();
39
40 // create the new identity and self-signed certificate
41 m_keyChain.createIdentity(m_newIdentity);
42
43 // set the new identity as default identity,
44 // and the corresponding certificate becomes the default certificate
45 m_keyChain.setDefaultIdentity(m_newIdentity);
46 }
47
48 ~IdentityFixture()
49 {
50 // recover the old default setting
Junxiao Shi5109dee2014-03-27 19:40:30 -070051 if (m_hasOldDefaultIdentity) {
52 m_keyChain.setDefaultIdentity(m_oldDefaultIdentity);
53 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054
55 // remove the temporarily created identity and certificates
Junxiao Shi5109dee2014-03-27 19:40:30 -070056 // XXX This has no effect if oldDefaultIdentity doesn't exist.
57 // newIdentity would be kept as default.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070058 m_keyChain.deleteIdentity(m_newIdentity);
59 }
60
61private:
62 KeyChain m_keyChain;
Junxiao Shi5109dee2014-03-27 19:40:30 -070063 bool m_hasOldDefaultIdentity;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070064 Name m_oldDefaultIdentity;
65 Name m_newIdentity;
66};
67
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070068BOOST_GLOBAL_FIXTURE(IdentityFixture)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070069
70} // namespace ndn