Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Davide Pesavento | ebc0275 | 2023-05-09 15:37:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2012-2023 University of California, Los Angeles |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 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 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 25 | namespace ndn::tests { |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 26 | |
| 27 | IdentityManagementFixture::IdentityManagementFixture() |
| 28 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 29 | { |
| 30 | m_keyChain.createIdentity("/DEFAULT"); |
| 31 | } |
| 32 | |
| 33 | IdentityManagementFixture::~IdentityManagementFixture() |
| 34 | { |
| 35 | boost::system::error_code ec; |
| 36 | for (const auto& certFile : m_certFiles) { |
| 37 | boost::filesystem::remove(certFile, ec); // ignore error |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | bool |
| 42 | IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params) |
| 43 | { |
| 44 | try { |
| 45 | m_keyChain.createIdentity(identity, params); |
| 46 | return true; |
| 47 | } |
| 48 | catch (const std::runtime_error&) { |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | bool |
| 54 | IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd) |
| 55 | { |
Davide Pesavento | ebc0275 | 2023-05-09 15:37:05 -0400 | [diff] [blame^] | 56 | ndn::security::Certificate cert; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 57 | try { |
| 58 | cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); |
| 59 | } |
| 60 | catch (const ndn::security::Pib::Error&) { |
| 61 | if (wantAdd && this->addIdentity(identity)) { |
| 62 | return this->saveIdentityCertificate(identity, filename, false); |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | m_certFiles.push_back(filename); |
| 68 | try { |
| 69 | ndn::io::save(cert, filename); |
| 70 | return true; |
| 71 | } |
| 72 | catch (const ndn::io::Error&) { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
Davide Pesavento | 8663ed1 | 2022-07-23 03:04:27 -0400 | [diff] [blame] | 77 | } // namespace ndn::tests |