Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame^] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2017 University of California, Los Angeles |
| 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "identity-management-fixture.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/util/io.hpp> |
| 23 | #include <boost/filesystem.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace tests { |
| 27 | |
| 28 | IdentityManagementFixture::IdentityManagementFixture() |
| 29 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 30 | { |
| 31 | m_keyChain.createIdentity("/DEFAULT"); |
| 32 | } |
| 33 | |
| 34 | IdentityManagementFixture::~IdentityManagementFixture() |
| 35 | { |
| 36 | boost::system::error_code ec; |
| 37 | for (const auto& certFile : m_certFiles) { |
| 38 | boost::filesystem::remove(certFile, ec); // ignore error |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool |
| 43 | IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params) |
| 44 | { |
| 45 | try { |
| 46 | m_keyChain.createIdentity(identity, params); |
| 47 | return true; |
| 48 | } |
| 49 | catch (const std::runtime_error&) { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | bool |
| 55 | IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd) |
| 56 | { |
| 57 | ndn::security::v2::Certificate cert; |
| 58 | try { |
| 59 | cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); |
| 60 | } |
| 61 | catch (const ndn::security::Pib::Error&) { |
| 62 | if (wantAdd && this->addIdentity(identity)) { |
| 63 | return this->saveIdentityCertificate(identity, filename, false); |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | m_certFiles.push_back(filename); |
| 69 | try { |
| 70 | ndn::io::save(cert, filename); |
| 71 | return true; |
| 72 | } |
| 73 | catch (const ndn::io::Error&) { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } // namespace tests |
| 79 | } // namespace ndn |