blob: 8c435a40e6970c632a0ea75b4db578896cb6ec65 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07002/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Zhiyi Zhang8617a792017-01-17 16:45:56 -08006 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07007 * 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.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080010 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070011 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080014 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070015 * 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/>.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080018 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070019 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080020 */
21
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070022#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
23#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
Zhiyi Zhang8617a792017-01-17 16:45:56 -080024
Davide Pesavento0dc02012021-11-23 22:55:03 -050025#include "detail/ndncert-common.hpp"
26
tylerliua7bea662020-10-08 18:51:02 -070027#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080028#include <ndn-cxx/security/signing-helpers.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080029
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040030namespace ndncert::tests {
Zhiyi Zhang8617a792017-01-17 16:45:56 -080031
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070032class IdentityManagementBaseFixture
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070033{
34public:
35 ~IdentityManagementBaseFixture();
36
37 bool
38 saveCertToFile(const Data& obj, const std::string& filename);
39
40protected:
41 std::set<Name> m_identities;
42 std::set<std::string> m_certFiles;
43};
44
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080045/**
46 * @brief A test suite level fixture to help with identity management
47 *
48 * Test cases in the suite can use this fixture to create identities. Identities,
49 * certificates, and saved certificates are automatically removed during test teardown.
50 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051class IdentityManagementFixture : public IdentityManagementBaseFixture
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080052{
Davide Pesavento0dc02012021-11-23 22:55:03 -050053protected:
54 using Identity = ndn::security::Identity;
55 using Key = ndn::security::Key;
56
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080057public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 IdentityManagementFixture();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080059
60 /**
61 * @brief Add identity @p identityName
62 * @return name of the created self-signed certificate
63 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050064 Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 addIdentity(const Name& identityName,
Davide Pesavento0dc02012021-11-23 22:55:03 -050066 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080067
68 /**
69 * @brief Save identity certificate to a file
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070070 * @param identity identity
71 * @param filename file name, should be writable
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080072 * @return whether successful
73 */
74 bool
Davide Pesavento0dc02012021-11-23 22:55:03 -050075 saveCertificate(const Identity& identity, const std::string& filename);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080076
77 /**
78 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
79 *
80 * If identity does not exist, it is created.
81 * A new key is generated as the default key for identity.
82 * A default certificate for the key is signed by the issuer using its default certificate.
83 *
84 * @return the sub identity
85 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050086 Identity
87 addSubCertificate(const Name& subIdentityName, const Identity& issuer,
88 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080089
90 /**
91 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
92 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050093 Certificate
94 addCertificate(const Key& key, const std::string& issuer);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080095
Zhiyi Zhang6f3c58b2020-10-30 08:53:42 -070096protected:
Davide Pesavento0dc02012021-11-23 22:55:03 -050097 ndn::KeyChain m_keyChain;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080098};
99
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400100} // namespace ndncert::tests
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800101
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700102#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP