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