blob: d2d14737a70ab5c2ac0548411eefaf30490778f1 [file] [log] [blame]
Yingdi Yud9715e32014-06-27 08:48:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yud9715e32014-06-27 08:48:47 -07004 *
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
Yingdi Yu41546342014-11-30 23:37:53 -080022#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
23#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
24
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080025#include "security/v1/key-chain.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080026#include "security/v2/key-chain.hpp"
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080027#include "security/signing-helpers.hpp"
Yingdi Yud9715e32014-06-27 08:48:47 -070028#include <vector>
29
30#include "boost-test.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080031#include "test-home-fixture.hpp"
Yingdi Yud9715e32014-06-27 08:48:47 -070032
33namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070034namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070035
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080036class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir>
Yingdi Yud9715e32014-06-27 08:48:47 -070037{
38public:
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080039 ~IdentityManagementBaseFixture();
Yingdi Yud9715e32014-06-27 08:48:47 -070040
Yingdi Yud9715e32014-06-27 08:48:47 -070041 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080042 saveCertToFile(const Data& obj, const std::string& filename);
43
44protected:
45 std::set<Name> m_identities;
46 std::set<std::string> m_certFiles;
47};
48
49/**
50 * @brief A test suite level fixture to help with identity management
51 *
52 * Test cases in the suite can use this fixture to create identities. Identities,
53 * certificates, and saved certificates are automatically removed during test teardown.
54 *
55 * @deprecated Use IdentityManagementV2Fixture
56 */
57class IdentityManagementV1Fixture : public IdentityManagementBaseFixture
58{
59public:
60 ~IdentityManagementV1Fixture();
61
62 /**
63 * @brief Add identity
64 * @return name of the created self-signed certificate
65 */
66 Name
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080067 addIdentity(const Name& identity, const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS);
Yingdi Yud9715e32014-06-27 08:48:47 -070068
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080069 /**
70 * @brief save identity certificate to a file
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080071 * @param identity certificate name
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080072 * @param filename file name, should be writable
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080073 * @return whether successful
74 */
75 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080076 saveIdentityCertificate(const Name& certName, const std::string& filename);
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080077
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080078 /**
79 * @brief issue a certificate for \p subIdentity signed by \p issuer
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080080 *
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 *
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080085 * @return whether success
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080086 */
87 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080088 addSubCertificate(const Name& subIdentity, const Name& issuer,
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080089 const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS);
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080090
Yingdi Yud9715e32014-06-27 08:48:47 -070091protected:
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080092 security::v1::KeyChain m_keyChain;
Yingdi Yud9715e32014-06-27 08:48:47 -070093};
94
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080095/**
96 * @brief A test suite level fixture to help with identity management
97 *
98 * Test cases in the suite can use this fixture to create identities. Identities,
99 * certificates, and saved certificates are automatically removed during test teardown.
100 */
101class IdentityManagementV2Fixture : public IdentityManagementBaseFixture
102{
103public:
104 IdentityManagementV2Fixture();
105
106 /**
107 * @brief Add identity @p identityName
108 * @return name of the created self-signed certificate
109 */
110 security::Identity
111 addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
112
113 /**
114 * @brief Save identity certificate to a file
115 * @param identity identity
116 * @param filename file name, should be writable
117 * @return whether successful
118 */
119 bool
120 saveIdentityCertificate(const security::Identity& identity, const std::string& filename);
121
122 /**
123 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
124 *
125 * If identity does not exist, it is created.
126 * A new key is generated as the default key for identity.
127 * A default certificate for the key is signed by the issuer using its default certificate.
128 *
129 * @return the sub identity
130 */
131 security::Identity
132 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
133 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
134
135protected:
136 security::v2::KeyChain m_keyChain;
137};
138
139using IdentityManagementFixture = IdentityManagementV1Fixture;
140
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700141} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -0700142} // namespace ndn
Yingdi Yu41546342014-11-30 23:37:53 -0800143
144#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP