blob: 4751b994a6dc60c60781b6ab0e88a5a9deef8fef [file] [log] [blame]
Ashlesh Gawande08784d42017-09-06 23:40:21 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesaventoebc02752023-05-09 15:37:05 -04003 * Copyright (c) 2012-2023 University of California, Los Angeles
Ashlesh Gawande08784d42017-09-06 23:40:21 -05004 *
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 Pesavento8663ed12022-07-23 03:04:27 -040025namespace ndn::tests {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050026
27IdentityManagementFixture::IdentityManagementFixture()
28 : m_keyChain("pib-memory:", "tpm-memory:")
29{
30 m_keyChain.createIdentity("/DEFAULT");
31}
32
33IdentityManagementFixture::~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
41bool
42IdentityManagementFixture::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
53bool
54IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
55{
Davide Pesaventoebc02752023-05-09 15:37:05 -040056 ndn::security::Certificate cert;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050057 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 Pesavento8663ed12022-07-23 03:04:27 -040077} // namespace ndn::tests