blob: 3684cad31377a15ce89e07a56d57c7eb472ddd91 [file] [log] [blame]
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2022, Regents of the University of California,
4 * 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
32namespace nlsr {
33namespace test {
34
35/**
36 * @brief A fixture providing an in-memory KeyChain.
37 *
38 * Test cases can use this fixture to create identities. Identities, certificates, and
39 * saved certificates are automatically removed during test teardown.
40 */
41class KeyChainFixture
42{
43protected:
44 using Certificate = ndn::security::Certificate;
45 using Identity = ndn::security::Identity;
46 using Key = ndn::security::Key;
47
48public:
49 /**
50 * @brief Saves an NDN certificate to a file
51 * @return true if successful, false otherwise
52 */
53 bool
54 saveCert(const ndn::Data& cert, const std::string& filename);
55
56 /**
57 * @brief Saves the default certificate of @p identity to a file
58 * @return true if successful, false otherwise
59 */
60 bool
61 saveIdentityCert(const Identity& identity, const std::string& filename);
62
63 /**
64 * @brief Saves the default certificate of the identity named @p identityName to a file
65 * @param identityName Name of the identity
66 * @param filename File name, must be writable
67 * @param allowCreate If true, create the identity if it does not exist
68 * @return true if successful, false otherwise
69 */
70 bool
71 saveIdentityCert(const ndn::Name& identityName, const std::string& filename,
72 bool allowCreate = false);
73
74 /**
75 * @brief Issue a certificate for \p subidentityName signed by \p issuer
76 *
77 * If identity does not exist, it is created.
78 * A new key is generated as the default key for identity.
79 * A default certificate for the key is signed by the issuer using its default certificate.
80 *
81 * @return the sub identity
82 */
83 Identity
84 addSubCertificate(const ndn::Name& identityName, const Identity& issuer,
85 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
86
87protected:
88 KeyChainFixture();
89
90 ~KeyChainFixture();
91
92protected:
93 ndn::KeyChain m_keyChain;
94
95private:
96 std::vector<std::string> m_certFiles;
97};
98
99} // namespace test
100} // namespace nlsr
101
102#endif // NLSR_TESTS_KEY_CHAIN_FIXTURE_HPP