blob: 90bd68399c5d26d708c8505769342abbfd6094b2 [file] [log] [blame]
Ashlesh Gawande08784d42017-09-06 23:40:21 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento39026112024-12-14 15:45:05 -05003 * Copyright (c) 2012-2024 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>
Davide Pesavento39026112024-12-14 15:45:05 -050023
24#include <filesystem>
25#include <system_error>
Ashlesh Gawande08784d42017-09-06 23:40:21 -050026
Davide Pesavento8663ed12022-07-23 03:04:27 -040027namespace ndn::tests {
Ashlesh Gawande08784d42017-09-06 23:40:21 -050028
29IdentityManagementFixture::IdentityManagementFixture()
30 : m_keyChain("pib-memory:", "tpm-memory:")
31{
32 m_keyChain.createIdentity("/DEFAULT");
33}
34
35IdentityManagementFixture::~IdentityManagementFixture()
36{
Davide Pesavento39026112024-12-14 15:45:05 -050037 std::error_code ec;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050038 for (const auto& certFile : m_certFiles) {
Davide Pesavento39026112024-12-14 15:45:05 -050039 std::filesystem::remove(certFile, ec); // ignore error
Ashlesh Gawande08784d42017-09-06 23:40:21 -050040 }
41}
42
43bool
44IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
45{
46 try {
47 m_keyChain.createIdentity(identity, params);
48 return true;
49 }
50 catch (const std::runtime_error&) {
51 return false;
52 }
53}
54
55bool
56IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
57{
Davide Pesaventoebc02752023-05-09 15:37:05 -040058 ndn::security::Certificate cert;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050059 try {
60 cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
61 }
62 catch (const ndn::security::Pib::Error&) {
63 if (wantAdd && this->addIdentity(identity)) {
64 return this->saveIdentityCertificate(identity, filename, false);
65 }
66 return false;
67 }
68
69 m_certFiles.push_back(filename);
70 try {
71 ndn::io::save(cert, filename);
72 return true;
73 }
74 catch (const ndn::io::Error&) {
75 return false;
76 }
77}
78
Davide Pesavento8663ed12022-07-23 03:04:27 -040079} // namespace ndn::tests