blob: 0ffcb0e86ab445701230dd16589330b290ba939a [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
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 /**
Davide Pesavento21353752020-11-20 00:43:44 -050052 * @brief Saves an NDN certificate to a file
53 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000054 */
Yanbiao Lic17de832014-11-21 17:51:45 -080055 bool
Davide Pesavento21353752020-11-20 00:43:44 -050056 saveCert(const Data& cert, const std::string& filename);
Yanbiao Lic17de832014-11-21 17:51:45 -080057
Davide Pesavento21353752020-11-20 00:43:44 -050058 /**
59 * @brief Saves the default certificate of @p identity to a file
60 * @return true if successful, false otherwise
Junxiao Shid7631272016-08-17 04:16:31 +000061 */
62 bool
Davide Pesavento21353752020-11-20 00:43:44 -050063 saveIdentityCert(const Identity& identity, const std::string& filename);
Junxiao Shid7631272016-08-17 04:16:31 +000064
Davide Pesavento21353752020-11-20 00:43:44 -050065 /**
66 * @brief Saves the default certificate of the identity named @p identityName to a file
67 * @param identityName Name of the identity
68 * @param filename File name, must be writable
69 * @param allowCreate If true, create the identity if it does not exist
70 * @return true if successful, false otherwise
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000071 */
Davide Pesavento21353752020-11-20 00:43:44 -050072 bool
73 saveIdentityCert(const Name& identityName, const std::string& filename,
74 bool allowCreate = false);
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040075
76protected:
77 KeyChainFixture();
78
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040079 ~KeyChainFixture();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000080
Yanbiao Lic17de832014-11-21 17:51:45 -080081protected:
82 ndn::KeyChain m_keyChain;
Junxiao Shid7631272016-08-17 04:16:31 +000083
84private:
Junxiao Shid7631272016-08-17 04:16:31 +000085 std::vector<std::string> m_certFiles;
Yanbiao Lic17de832014-11-21 17:51:45 -080086};
87
88} // namespace tests
89} // namespace nfd
Yukai Tu0a49d342015-09-13 12:54:22 +080090
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040091#endif // NFD_TESTS_KEY_CHAIN_FIXTURE_HPP