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 | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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" |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 23 | #include "../util/test-home-environment-fixture.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 24 | #include <boost/filesystem.hpp> |
| 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 28 | namespace ndn { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 29 | namespace security { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 30 | |
Steve DiBenedetto | a8659ff | 2014-12-04 14:50:28 -0700 | [diff] [blame] | 31 | class IdentityFixture : public util::TestHomeEnvironmentFixture |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 32 | { |
| 33 | public: |
| 34 | IdentityFixture() |
| 35 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 36 | using namespace boost::filesystem; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 38 | // initialize KeyChain from test specific HOME: tests/unit-tests/security/tmp-home |
| 39 | if (std::getenv("HOME")) |
| 40 | m_HOME = std::getenv("HOME"); |
| 41 | if (std::getenv("OLD_HOME")) |
| 42 | m_OLD_HOME = std::getenv("OLD_HOME"); |
| 43 | |
| 44 | setenv("HOME", "tests/unit-tests/security/tmp-home", 1); |
| 45 | setenv("OLD_HOME", m_HOME.c_str(), 1); |
| 46 | |
| 47 | KeyChain keyChain; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 48 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 49 | // save the old default identity |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 50 | try { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 51 | m_oldDefaultIdentity = keyChain.getDefaultIdentity(); |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 52 | m_hasOldDefaultIdentity = true; |
| 53 | } |
| 54 | catch (SecPublicInfo::Error& e) { |
| 55 | m_hasOldDefaultIdentity = false; |
| 56 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 58 | m_newIdentity = "/ndn-cxx-test-identity"; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 59 | m_newIdentity.appendVersion(); |
| 60 | |
| 61 | // create the new identity and self-signed certificate |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 62 | keyChain.createIdentity(m_newIdentity); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | // set the new identity as default identity, |
| 65 | // and the corresponding certificate becomes the default certificate |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 66 | keyChain.setDefaultIdentity(m_newIdentity); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ~IdentityFixture() |
| 70 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 71 | using namespace boost::filesystem; |
| 72 | |
| 73 | KeyChain keyChain; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 74 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 75 | // recover the old default setting |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 76 | if (m_hasOldDefaultIdentity) { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 77 | keyChain.setDefaultIdentity(m_oldDefaultIdentity); |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 78 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 79 | |
| 80 | // remove the temporarily created identity and certificates |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 81 | // XXX This has no effect if oldDefaultIdentity doesn't exist. |
| 82 | // newIdentity would be kept as default. |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 83 | keyChain.deleteIdentity(m_newIdentity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 84 | |
| 85 | path pibPath(absolute(std::getenv("HOME"))); |
| 86 | pibPath /= ".ndn/ndnsec-public-info.db"; |
| 87 | |
| 88 | boost::filesystem::remove(pibPath); |
| 89 | |
| 90 | path tpmPath(absolute(std::getenv("HOME"))); |
| 91 | tpmPath /= ".ndn/ndnsec-tpm-file"; |
| 92 | |
| 93 | boost::filesystem::remove_all(tpmPath); |
| 94 | |
| 95 | if (!m_HOME.empty()) |
| 96 | setenv("HOME", m_HOME.c_str(), 1); |
| 97 | else |
| 98 | unsetenv("HOME"); |
| 99 | |
| 100 | if (!m_OLD_HOME.empty()) |
| 101 | setenv("OLD_HOME", m_OLD_HOME.c_str(), 1); |
| 102 | else |
| 103 | unsetenv("OLD_HOME"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 107 | std::string m_OLD_HOME; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 108 | std::string m_HOME; |
| 109 | |
Junxiao Shi | 5109dee | 2014-03-27 19:40:30 -0700 | [diff] [blame] | 110 | bool m_hasOldDefaultIdentity; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 111 | Name m_oldDefaultIdentity; |
| 112 | Name m_newIdentity; |
| 113 | }; |
| 114 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 115 | BOOST_GLOBAL_FIXTURE(IdentityFixture) |
Alexander Afanasyev | 03c170e | 2015-10-01 11:24:06 -0700 | [diff] [blame^] | 116 | #if BOOST_VERSION >= 105900 |
| 117 | ; |
| 118 | #endif // BOOST_VERSION >= 105900 |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 119 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 120 | } // namespace security |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 121 | } // namespace ndn |