blob: 9b10dbfc562b72c5be3a5d3274a600525d532a15 [file] [log] [blame]
Zhiyi Zhang63589b82020-10-10 10:27:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2020 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * 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/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
23#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
24
25#include <ndn-cxx/security/key-chain.hpp>
26#include <ndn-cxx/security/signing-helpers.hpp>
27#include <vector>
28
29namespace ndn {
30namespace ndncert {
31namespace tests {
32
33class IdentityManagementBaseFixture
34{
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
46/**
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 */
52class IdentityManagementFixture : public IdentityManagementBaseFixture
53{
54public:
55 IdentityManagementFixture();
56
57 /**
58 * @brief Add identity @p identityName
59 * @return name of the created self-signed certificate
60 */
61 security::Identity
62 addIdentity(const Name& identityName,
63 const KeyParams& params = security::KeyChain::getDefaultKeyParams());
64
65 /**
66 * @brief Save identity certificate to a file
67 * @param identity identity
68 * @param filename file name, should be writable
69 * @return whether successful
70 */
71 bool
72 saveCertificate(const security::Identity& identity, const std::string& filename);
73
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,
85 const KeyParams& params = security::KeyChain::getDefaultKeyParams());
86
87 /**
88 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
89 */
90 security::Certificate
91 addCertificate(const security::Key& key, const std::string& issuer);
92
93protected:
94 KeyChain m_keyChain;
95};
96
97} // namespace tests
98} // namespace ndncert
99} // namespace ndn
100
101#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP