Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "security/key-chain.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 25 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 26 | namespace ndn { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 27 | namespace security { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 28 | |
| 29 | class IdentityFixture |
| 30 | { |
| 31 | public: |
| 32 | IdentityFixture() |
| 33 | { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 34 | // initialize KeyChain from TEST_HOME |
| 35 | if (std::getenv("TEST_HOME")) |
| 36 | m_HOME = std::getenv("TEST_HOME"); |
| 37 | setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1); |
| 38 | |
| 39 | KeyChain keyChain("sqlite3", "file"); |
| 40 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 41 | // save the old default identity |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 42 | try { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 43 | m_oldDefaultIdentity = keyChain.getDefaultIdentity(); |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 44 | m_hasOldDefaultIdentity = true; |
| 45 | } |
| 46 | catch (SecPublicInfo::Error& e) { |
| 47 | m_hasOldDefaultIdentity = false; |
| 48 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 50 | m_newIdentity.set("/ndn-cxx-test-identity"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 51 | m_newIdentity.appendVersion(); |
| 52 | |
| 53 | // create the new identity and self-signed certificate |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 54 | keyChain.createIdentity(m_newIdentity); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 55 | |
| 56 | // set the new identity as default identity, |
| 57 | // and the corresponding certificate becomes the default certificate |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 58 | keyChain.setDefaultIdentity(m_newIdentity); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ~IdentityFixture() |
| 62 | { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 63 | KeyChain keyChain("sqlite3", "file"); |
| 64 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 65 | // recover the old default setting |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 66 | if (m_hasOldDefaultIdentity) { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 67 | keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 68 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 69 | |
| 70 | // remove the temporarily created identity and certificates |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 71 | // XXX This has no effect if oldDefaultIdentity doesn't exist. |
| 72 | // newIdentity would be kept as default. |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 73 | keyChain.deleteIdentity(m_newIdentity); |
| 74 | |
| 75 | if (!m_HOME.empty()) |
| 76 | setenv("TEST_HOME", m_HOME.c_str(), 1); |
| 77 | else |
| 78 | unsetenv("TEST_HOME"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 82 | std::string m_HOME; |
| 83 | |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 84 | bool m_hasOldDefaultIdentity; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 85 | Name m_oldDefaultIdentity; |
| 86 | Name m_newIdentity; |
| 87 | }; |
| 88 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 89 | BOOST_GLOBAL_FIXTURE(IdentityFixture) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 91 | } // namespace security |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 92 | } // namespace ndn |