blob: 9b10dbfc562b72c5be3a5d3274a600525d532a15 [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/*
Zhiyi Zhanga3e62bd2020-10-12 17:07:10 -07003 * Copyright (c) 2013-2020 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
tylerliua7bea662020-10-08 18:51:02 -070025#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080026#include <ndn-cxx/security/signing-helpers.hpp>
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070027#include <vector>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080028
29namespace ndn {
30namespace 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{
54public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055 IdentityManagementFixture();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080056
57 /**
58 * @brief Add identity @p identityName
59 * @return name of the created self-signed certificate
60 */
61 security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 addIdentity(const Name& identityName,
tylerliua7bea662020-10-08 18:51:02 -070063 const KeyParams& params = security::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080064
65 /**
66 * @brief Save identity certificate to a file
Zhiyi Zhang6d9eda62020-10-16 17:37:02 -070067 * @param identity identity
68 * @param filename file name, should be writable
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080069 * @return whether successful
70 */
71 bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072 saveCertificate(const security::Identity& identity, const std::string& filename);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080073
74 /**
75 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
76 *
77 * If identity does not exist, it is created.
78 * A new key is generated as the default key for identity.
79 * A default certificate for the key is signed by the issuer using its default certificate.
80 *
81 * @return the sub identity
82 */
83 security::Identity
84 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
tylerliua7bea662020-10-08 18:51:02 -070085 const KeyParams& params = security::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080086
87 /**
88 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
89 */
tylerliua7bea662020-10-08 18:51:02 -070090 security::Certificate
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080091 addCertificate(const security::Key& key, const std::string& issuer);
92
Zhiyi Zhang6f3c58b2020-10-30 08:53:42 -070093protected:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094 KeyChain m_keyChain;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080095};
96
Zhiyi Zhang8617a792017-01-17 16:45:56 -080097} // namespace tests
98} // namespace ndncert
99} // namespace ndn
100
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700101#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP