blob: 0ef6ff8c732dc747f2e167ba039c64f0f9605bb1 [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 /**
Davide Pesavento66777622020-10-09 18:46:03 -040043 * @brief Saves an NDN certificate to a file
44 * @return true if successful, false otherwise
45 */
46 bool
47 saveCert(const Data& cert, const std::string& filename);
48
49 /**
50 * @brief Saves the default certificate of @p identity to a file
51 * @return true if successful, false otherwise
52 */
53 bool
54 saveIdentityCert(const Identity& identity, const std::string& filename);
55
56 /**
57 * @brief Saves the default certificate of the identity named @p identityName to a file
58 * @param identityName Name of the identity
59 * @param filename File name, must be writable
60 * @param allowCreate If true, create the identity if it does not exist
61 * @return true if successful, false otherwise
62 */
63 bool
64 saveIdentityCert(const Name& identityName, const std::string& filename,
65 bool allowCreate = false);
66
67protected:
68 KeyChainFixture();
69
70 ~KeyChainFixture();
71
72protected:
73 ndn::KeyChain m_keyChain;
74
75private:
76 std::vector<std::string> m_certFiles;
77};
78
Davide Pesaventob3570c62022-02-19 19:19:00 -050079} // namespace ndn::tests
Davide Pesavento66777622020-10-09 18:46:03 -040080
81#endif // NDN_TOOLS_TESTS_KEY_CHAIN_FIXTURE_HPP