blob: 8c09170d0ff0fadbe6ffd401beba0e72c52a0655 [file] [log] [blame]
Davide Pesavento66777622020-10-09 18:46:03 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2014-2022, Regents of the University of California.
Davide Pesavento66777622020-10-09 18:46:03 -04004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools 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,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP
21#define NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP
22
23#include <ndn-cxx/security/key-chain.hpp>
24#include <ndn-cxx/security/signing-helpers.hpp>
25
Davide Pesaventob3570c62022-02-19 19:19:00 -050026namespace ndn::tests {
Davide Pesavento66777622020-10-09 18:46:03 -040027
28/**
29 * @brief A fixture providing an in-memory KeyChain.
30 *
31 * Test cases can use this fixture to create identities. Identities, certificates, and
32 * saved certificates are automatically removed during test teardown.
33 */
34class KeyChainFixture
35{
36protected:
37 using Certificate = ndn::security::Certificate;
38 using Identity = ndn::security::Identity;
39 using Key = ndn::security::Key;
40
41public:
42 /**
43 * @brief Creates and returns a certificate for a given key
44 * @param key The key for which to make a certificate
45 * @param issuer The IssuerId to include in the certificate name
46 * @param signingKey The key with which to sign the certificate; if not provided, the
47 * certificate will be self-signed
48 */
49 Certificate
50 makeCert(const Key& key, const std::string& issuer, const Key& signingKey = Key());
51
52 /**
53 * @brief Saves an NDN certificate to a file
54 * @return true if successful, false otherwise
55 */
56 bool
57 saveCert(const Data& cert, const std::string& filename);
58
59 /**
60 * @brief Saves the default certificate of @p identity to a file
61 * @return true if successful, false otherwise
62 */
63 bool
64 saveIdentityCert(const Identity& identity, const std::string& filename);
65
66 /**
67 * @brief Saves the default certificate of the identity named @p identityName to a file
68 * @param identityName Name of the identity
69 * @param filename File name, must be writable
70 * @param allowCreate If true, create the identity if it does not exist
71 * @return true if successful, false otherwise
72 */
73 bool
74 saveIdentityCert(const Name& identityName, const std::string& filename,
75 bool allowCreate = false);
76
77protected:
78 KeyChainFixture();
79
80 ~KeyChainFixture();
81
82protected:
83 ndn::KeyChain m_keyChain;
84
85private:
86 std::vector<std::string> m_certFiles;
87};
88
Davide Pesaventob3570c62022-02-19 19:19:00 -050089} // namespace ndn::tests
Davide Pesavento66777622020-10-09 18:46:03 -040090
91#endif // NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP