blob: 06284de9ade1dadcd5acf039d1f33de6af5e60dd [file] [log] [blame]
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NLSR (Named-data Link State Routing).
12 * See AUTHORS.md for complete list of NLSR authors and contributors.
13 *
14 * NLSR is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NLSR_TESTS_KEY_CHAIN_FIXTURE_HPP
27#define NLSR_TESTS_KEY_CHAIN_FIXTURE_HPP
28
29#include <ndn-cxx/security/key-chain.hpp>
30#include <ndn-cxx/security/signing-helpers.hpp>
31
Davide Pesavento288141a2024-02-13 17:30:35 -050032namespace nlsr::tests {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040033
34/**
35 * @brief A fixture providing an in-memory KeyChain.
36 *
37 * Test cases can use this fixture to create identities. Identities, certificates, and
38 * saved certificates are automatically removed during test teardown.
39 */
40class KeyChainFixture
41{
42protected:
43 using Certificate = ndn::security::Certificate;
44 using Identity = ndn::security::Identity;
45 using Key = ndn::security::Key;
46
47public:
48 /**
49 * @brief Saves an NDN certificate to a file
50 * @return true if successful, false otherwise
51 */
52 bool
53 saveCert(const ndn::Data& cert, const std::string& filename);
54
55 /**
56 * @brief Saves the default certificate of @p identity to a file
57 * @return true if successful, false otherwise
58 */
59 bool
60 saveIdentityCert(const Identity& identity, const std::string& filename);
61
62 /**
63 * @brief Saves the default certificate of the identity named @p identityName to a file
64 * @param identityName Name of the identity
65 * @param filename File name, must be writable
66 * @param allowCreate If true, create the identity if it does not exist
67 * @return true if successful, false otherwise
68 */
69 bool
70 saveIdentityCert(const ndn::Name& identityName, const std::string& filename,
71 bool allowCreate = false);
72
73 /**
74 * @brief Issue a certificate for \p subidentityName signed by \p issuer
75 *
76 * If identity does not exist, it is created.
77 * A new key is generated as the default key for identity.
78 * A default certificate for the key is signed by the issuer using its default certificate.
79 *
80 * @return the sub identity
81 */
82 Identity
83 addSubCertificate(const ndn::Name& identityName, const Identity& issuer,
84 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
85
86protected:
87 KeyChainFixture();
88
89 ~KeyChainFixture();
90
91protected:
92 ndn::KeyChain m_keyChain;
93
94private:
95 std::vector<std::string> m_certFiles;
96};
97
Davide Pesavento288141a2024-02-13 17:30:35 -050098} // namespace nlsr::tests
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040099
100#endif // NLSR_TESTS_KEY_CHAIN_FIXTURE_HPP