blob: ad3c6a183f507136513f3ddfde3b50e73ceadd03 [file] [log] [blame]
Davide Pesaventoba3f6892020-12-08 22:18:35 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2020, Regents of the University of California
4 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#ifndef NAC_TESTS_KEY_CHAIN_FIXTURE_HPP
21#define NAC_TESTS_KEY_CHAIN_FIXTURE_HPP
22
23#include <ndn-cxx/security/key-chain.hpp>
24#include <ndn-cxx/security/signing-helpers.hpp>
25
26namespace ndn {
27namespace nac {
28namespace tests {
29
30/**
31 * @brief A fixture providing an in-memory KeyChain.
32 *
33 * Test cases can use this fixture to create identities. Identities, certificates, and
34 * saved certificates are automatically removed during test teardown.
35 */
36class KeyChainFixture
37{
38protected:
39 using Certificate = ndn::security::Certificate;
40 using Identity = ndn::security::Identity;
41 using Key = ndn::security::Key;
42
43public:
44 /**
45 * @brief Creates and returns a certificate for a given key
46 * @param key The key for which to make a certificate
47 * @param issuer The IssuerId to include in the certificate name
48 * @param signingKey The key with which to sign the certificate; if not provided, the
49 * certificate will be self-signed
50 */
51 Certificate
52 makeCert(const Key& key, const std::string& issuer, const Key& signingKey = Key());
53
54 /**
55 * @brief Saves an NDN certificate to a file
56 * @return true if successful, false otherwise
57 */
58 bool
59 saveCert(const Data& cert, const std::string& filename);
60
61 /**
62 * @brief Saves the default certificate of @p identity to a file
63 * @return true if successful, false otherwise
64 */
65 bool
66 saveIdentityCert(const Identity& identity, const std::string& filename);
67
68 /**
69 * @brief Saves the default certificate of the identity named @p identityName to a file
70 * @param identityName Name of the identity
71 * @param filename File name, must be writable
72 * @param allowCreate If true, create the identity if it does not exist
73 * @return true if successful, false otherwise
74 */
75 bool
76 saveIdentityCert(const Name& identityName, const std::string& filename,
77 bool allowCreate = false);
78
79protected:
80 KeyChainFixture();
81
82 ~KeyChainFixture();
83
84protected:
85 ndn::KeyChain m_keyChain;
86
87private:
88 std::vector<std::string> m_certFiles;
89};
90
91} // namespace tests
92} // namespace nac
93} // namespace ndn
94
95#endif // NAC_TESTS_KEY_CHAIN_FIXTURE_HPP