blob: 3f0f14922f5f058d7129554b9bbe1985b79cede8 [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/*
Junxiao Shi4b84a2c2022-04-28 03:17:05 +00003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::tests {
Yanbiao Lic17de832014-11-21 17:51:45 -080035
Davide Pesavento21353752020-11-20 00:43:44 -050036/**
37 * @brief A fixture providing an in-memory KeyChain.
38 *
39 * Test cases can use this fixture to create identities. Identities, certificates, and
40 * saved certificates are automatically removed during test teardown.
Yanbiao Lic17de832014-11-21 17:51:45 -080041 */
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040042class KeyChainFixture
Yanbiao Lic17de832014-11-21 17:51:45 -080043{
Davide Pesavento21353752020-11-20 00:43:44 -050044protected:
45 using Certificate = ndn::security::Certificate;
46 using Identity = ndn::security::Identity;
47 using Key = ndn::security::Key;
48
Yanbiao Lic17de832014-11-21 17:51:45 -080049public:
Davide Pesavento21353752020-11-20 00:43:44 -050050 /**
Davide Pesavento21353752020-11-20 00:43:44 -050051 * @brief Saves an NDN certificate to a file
52 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000053 */
Yanbiao Lic17de832014-11-21 17:51:45 -080054 bool
Davide Pesavento21353752020-11-20 00:43:44 -050055 saveCert(const Data& cert, const std::string& filename);
Yanbiao Lic17de832014-11-21 17:51:45 -080056
Davide Pesavento21353752020-11-20 00:43:44 -050057 /**
58 * @brief Saves the default certificate of @p identity to a file
59 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000060 */
61 bool
Davide Pesavento21353752020-11-20 00:43:44 -050062 saveIdentityCert(const Identity& identity, const std::string& filename);
Junxiao Shid7631272016-08-17 04:16:31 +000063
Davide Pesavento21353752020-11-20 00:43:44 -050064 /**
65 * @brief Saves the default certificate of the identity named @p identityName to a file
66 * @param identityName Name of the identity
67 * @param filename File name, must be writable
68 * @param allowCreate If true, create the identity if it does not exist
69 * @return true if successful, false otherwise
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000070 */
Davide Pesavento21353752020-11-20 00:43:44 -050071 bool
72 saveIdentityCert(const Name& identityName, const std::string& filename,
73 bool allowCreate = false);
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040074
75protected:
76 KeyChainFixture();
77
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040078 ~KeyChainFixture();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000079
Yanbiao Lic17de832014-11-21 17:51:45 -080080protected:
81 ndn::KeyChain m_keyChain;
Junxiao Shid7631272016-08-17 04:16:31 +000082
83private:
Junxiao Shid7631272016-08-17 04:16:31 +000084 std::vector<std::string> m_certFiles;
Yanbiao Lic17de832014-11-21 17:51:45 -080085};
86
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040087} // namespace nfd::tests
Yukai Tu0a49d342015-09-13 12:54:22 +080088
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040089#endif // NFD_TESTS_KEY_CHAIN_FIXTURE_HPP