blob: d287fc9ddefa7b4305a878184134d872f3409637 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2014-2019, Regents of the University of California,
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 * 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 Zhangaf7c2902019-03-14 22:13:21 -070032#include "test-home-fixture.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
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070041class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir>
42{
43public:
44 ~IdentityManagementBaseFixture();
45
46 bool
47 saveCertToFile(const Data& obj, const std::string& filename);
48
49protected:
50 std::set<Name> m_identities;
51 std::set<std::string> m_certFiles;
52};
53
54
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080055/**
56 * @brief A test suite level fixture to help with identity management
57 *
58 * Test cases in the suite can use this fixture to create identities. Identities,
59 * certificates, and saved certificates are automatically removed during test teardown.
60 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061class IdentityManagementFixture : public IdentityManagementBaseFixture
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080062{
63public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070064 IdentityManagementFixture();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080065
66 /**
67 * @brief Add identity @p identityName
68 * @return name of the created self-signed certificate
69 */
70 security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070071 addIdentity(const Name& identityName,
72 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080073
74 /**
75 * @brief Save identity certificate to a file
76 * @param identity identity
77 * @param filename file name, should be writable
78 * @return whether successful
79 */
80 bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081 saveCertificate(const security::Identity& identity, const std::string& filename);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080082
83 /**
84 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
85 *
86 * If identity does not exist, it is created.
87 * A new key is generated as the default key for identity.
88 * A default certificate for the key is signed by the issuer using its default certificate.
89 *
90 * @return the sub identity
91 */
92 security::Identity
93 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
94 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
95
96 /**
97 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
98 */
99 security::v2::Certificate
100 addCertificate(const security::Key& key, const std::string& issuer);
101
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800102protected:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103 KeyChain m_keyChain;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800104};
105
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800106/** \brief convenience base class for inheriting from both UnitTestTimeFixture
Zhiyi Zhang3b76f2c2017-03-01 10:20:15 -0800107 * and IdentityManagementV2Fixture
108 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109class IdentityManagementTimeFixture : public UnitTestTimeFixture
110 , public IdentityManagementFixture
Zhiyi Zhang3b76f2c2017-03-01 10:20:15 -0800111{
112};
113
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800114} // namespace tests
115} // namespace ndncert
116} // namespace ndn
117
118#endif // NDNCERT_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP