blob: 9f03ea0b7d378af40a0ca406012dd0eca87fc183 [file] [log] [blame]
Yanbiao Lic17de832014-11-21 17:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +00002/*
Davide Pesavento21353752020-11-20 00:43:44 -05003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * 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.
Yanbiao Lic17de832014-11-21 17:51:45 -080010 *
Junxiao Shi38f4ce92016-08-04 10:01:52 +000011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
Yanbiao Lic17de832014-11-21 17:51:45 -080013 *
Junxiao Shi38f4ce92016-08-04 10:01:52 +000014 * NFD 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.
Yanbiao Lic17de832014-11-21 17:51:45 -080017 *
Junxiao Shi38f4ce92016-08-04 10:01:52 +000018 * NFD 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.
Yanbiao Lic17de832014-11-21 17:51:45 -080021 *
Junxiao Shi38f4ce92016-08-04 10:01:52 +000022 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yanbiao Lic17de832014-11-21 17:51:45 -080024 */
25
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040026#ifndef NFD_TESTS_KEY_CHAIN_FIXTURE_HPP
27#define NFD_TESTS_KEY_CHAIN_FIXTURE_HPP
Yukai Tu0a49d342015-09-13 12:54:22 +080028
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "core/common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040030
Yanbiao Lic17de832014-11-21 17:51:45 -080031#include <ndn-cxx/security/key-chain.hpp>
Davide Pesavento21353752020-11-20 00:43:44 -050032#include <ndn-cxx/security/signing-helpers.hpp>
Yanbiao Lic17de832014-11-21 17:51:45 -080033
34namespace nfd {
35namespace tests {
36
Davide Pesavento21353752020-11-20 00:43:44 -050037/**
38 * @brief A fixture providing an in-memory KeyChain.
39 *
40 * Test cases can use this fixture to create identities. Identities, certificates, and
41 * saved certificates are automatically removed during test teardown.
Yanbiao Lic17de832014-11-21 17:51:45 -080042 */
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040043class KeyChainFixture
Yanbiao Lic17de832014-11-21 17:51:45 -080044{
Davide Pesavento21353752020-11-20 00:43:44 -050045protected:
46 using Certificate = ndn::security::Certificate;
47 using Identity = ndn::security::Identity;
48 using Key = ndn::security::Key;
49
Yanbiao Lic17de832014-11-21 17:51:45 -080050public:
Davide Pesavento21353752020-11-20 00:43:44 -050051 /**
52 * @brief Creates and returns a certificate for a given key
53 * @param key The key for which to make a certificate
54 * @param issuer The IssuerId to include in the certificate name
55 * @param signingKey The key with which to sign the certificate; if not provided, the
56 * certificate will be self-signed
57 */
58 Certificate
59 makeCert(const Key& key, const std::string& issuer, const Key& signingKey = Key());
60
61 /**
62 * @brief Saves an NDN certificate to a file
63 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000064 */
Yanbiao Lic17de832014-11-21 17:51:45 -080065 bool
Davide Pesavento21353752020-11-20 00:43:44 -050066 saveCert(const Data& cert, const std::string& filename);
Yanbiao Lic17de832014-11-21 17:51:45 -080067
Davide Pesavento21353752020-11-20 00:43:44 -050068 /**
69 * @brief Saves the default certificate of @p identity to a file
70 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000071 */
72 bool
Davide Pesavento21353752020-11-20 00:43:44 -050073 saveIdentityCert(const Identity& identity, const std::string& filename);
Junxiao Shid7631272016-08-17 04:16:31 +000074
Davide Pesavento21353752020-11-20 00:43:44 -050075 /**
76 * @brief Saves the default certificate of the identity named @p identityName to a file
77 * @param identityName Name of the identity
78 * @param filename File name, must be writable
79 * @param allowCreate If true, create the identity if it does not exist
80 * @return true if successful, false otherwise
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000081 */
Davide Pesavento21353752020-11-20 00:43:44 -050082 bool
83 saveIdentityCert(const Name& identityName, const std::string& filename,
84 bool allowCreate = false);
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040085
86protected:
87 KeyChainFixture();
88
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040089 ~KeyChainFixture();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000090
Yanbiao Lic17de832014-11-21 17:51:45 -080091protected:
92 ndn::KeyChain m_keyChain;
Junxiao Shid7631272016-08-17 04:16:31 +000093
94private:
Junxiao Shid7631272016-08-17 04:16:31 +000095 std::vector<std::string> m_certFiles;
Yanbiao Lic17de832014-11-21 17:51:45 -080096};
97
98} // namespace tests
99} // namespace nfd
Yukai Tu0a49d342015-09-13 12:54:22 +0800100
Davide Pesavento1d12d2f2019-03-22 12:44:14 -0400101#endif // NFD_TESTS_KEY_CHAIN_FIXTURE_HPP