blob: 7856bf9d0bfe2bb27900046e44b5191d2364fd4a [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 Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2013-2021 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
Zhiyi Zhang8617a792017-01-17 16:45:56 -080030namespace ndncert {
31namespace tests {
32
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070033class IdentityManagementBaseFixture
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070034{
35public:
36 ~IdentityManagementBaseFixture();
37
38 bool
39 saveCertToFile(const Data& obj, const std::string& filename);
40
41protected:
42 std::set<Name> m_identities;
43 std::set<std::string> m_certFiles;
44};
45
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080046/**
47 * @brief A test suite level fixture to help with identity management
48 *
49 * Test cases in the suite can use this fixture to create identities. Identities,
50 * certificates, and saved certificates are automatically removed during test teardown.
51 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052class IdentityManagementFixture : public IdentityManagementBaseFixture
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080053{
Davide Pesavento0dc02012021-11-23 22:55:03 -050054protected:
55 using Identity = ndn::security::Identity;
56 using Key = ndn::security::Key;
57
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080058public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070059 IdentityManagementFixture();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080060
61 /**
62 * @brief Add identity @p identityName
63 * @return name of the created self-signed certificate
64 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050065 Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 addIdentity(const Name& identityName,
Davide Pesavento0dc02012021-11-23 22:55:03 -050067 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080068
69 /**
70 * @brief Save identity certificate to a file
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070071 * @param identity identity
72 * @param filename file name, should be writable
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080073 * @return whether successful
74 */
75 bool
Davide Pesavento0dc02012021-11-23 22:55:03 -050076 saveCertificate(const Identity& identity, const std::string& filename);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080077
78 /**
79 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
80 *
81 * If identity does not exist, it is created.
82 * A new key is generated as the default key for identity.
83 * A default certificate for the key is signed by the issuer using its default certificate.
84 *
85 * @return the sub identity
86 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050087 Identity
88 addSubCertificate(const Name& subIdentityName, const Identity& issuer,
89 const ndn::KeyParams& params = ndn::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080090
91 /**
92 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
93 */
Davide Pesavento0dc02012021-11-23 22:55:03 -050094 Certificate
95 addCertificate(const Key& key, const std::string& issuer);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080096
Zhiyi Zhang6f3c58b2020-10-30 08:53:42 -070097protected:
Davide Pesavento0dc02012021-11-23 22:55:03 -050098 ndn::KeyChain m_keyChain;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080099};
100
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800101} // namespace tests
102} // namespace ndncert
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800103
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700104#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP