blob: 8a80da8d69bc57e068826eab74a1c9e47911a4fd [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * 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.
10 *
11 * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ndncert, a certificate management system based on NDN.
13 *
14 * ndncert 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, either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received copies of the GNU General Public License along with
23 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#ifndef NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
29#define NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
30
31#include "test-common.hpp"
Zhiyi Zhang8617a792017-01-17 16:45:56 -080032#include <ndn-cxx/security/key-chain.hpp>
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080033#include <ndn-cxx/security/v2/key-chain.hpp>
34#include <ndn-cxx/security/v2/additional-description.hpp>
35#include <ndn-cxx/security/signing-helpers.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080036
37namespace ndn {
38namespace ndncert {
39namespace tests {
40
41/** \brief a fixture that cleans up KeyChain identities and certificate files upon destruction
42 */
43class IdentityManagementFixture : public virtual BaseFixture
44{
45public:
46 IdentityManagementFixture();
47
48 /** \brief deletes created identities and saved certificate files
49 */
50 ~IdentityManagementFixture();
51
52 /** \brief add identity
53 * \return whether successful
54 */
55 bool
56 addIdentity(const Name& identity,
57 const ndn::KeyParams& params = ndn::KeyChain::DEFAULT_KEY_PARAMS);
58
59 /** \brief save identity certificate to a file
60 * \param identity identity name
61 * \param filename file name, should be writable
62 * \param wantAdd if true, add new identity when necessary
63 * \return whether successful
64 */
65 bool
66 saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd = false);
67
68protected:
69 ndn::KeyChain m_keyChain;
70
71private:
72 std::vector<ndn::Name> m_identities;
73 std::vector<std::string> m_certFiles;
74};
75
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080076/**
77 * @brief A test suite level fixture to help with identity management
78 *
79 * Test cases in the suite can use this fixture to create identities. Identities,
80 * certificates, and saved certificates are automatically removed during test teardown.
81 */
82class IdentityManagementV2Fixture
83{
84public:
85 IdentityManagementV2Fixture();
86
87 /**
88 * @brief Add identity @p identityName
89 * @return name of the created self-signed certificate
90 */
91 security::Identity
92 addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
93
94 /**
95 * @brief Save identity certificate to a file
96 * @param identity identity
97 * @param filename file name, should be writable
98 * @return whether successful
99 */
100 bool
101 saveIdentityCertificate(const security::Identity& identity, const std::string& filename);
102
103 /**
104 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
105 *
106 * If identity does not exist, it is created.
107 * A new key is generated as the default key for identity.
108 * A default certificate for the key is signed by the issuer using its default certificate.
109 *
110 * @return the sub identity
111 */
112 security::Identity
113 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
114 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
115
116 /**
117 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
118 */
119 security::v2::Certificate
120 addCertificate(const security::Key& key, const std::string& issuer);
121
122 bool
123 saveCertToFile(const Data& obj, const std::string& filename);
124
125protected:
126 std::set<Name> m_identities;
127 std::set<std::string> m_certFiles;
128 security::v2::KeyChain m_keyChain;
129};
130
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800131/** \brief convenience base class for inheriting from both UnitTestTimeFixture
132 * and IdentityManagementFixture
133 */
134class IdentityManagementTimeFixture : public UnitTestTimeFixture
135 , public IdentityManagementFixture
136{
137};
138
Zhiyi Zhang3b76f2c2017-03-01 10:20:15 -0800139/** \brief convenience base class for inheriting from both UnitTestTimeFixture
140 * and IdentityManagementV2Fixture
141 */
142class IdentityManagementV2TimeFixture : public UnitTestTimeFixture
143 , public IdentityManagementV2Fixture
144{
145};
146
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800147} // namespace tests
148} // namespace ndncert
149} // namespace ndn
150
151#endif // NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP