blob: 82545f9497fe2d03d5994424848d017c95b1bf97 [file] [log] [blame]
Yingdi Yud9715e32014-06-27 08:48:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08002/*
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
29#include "boost-test.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080030#include "test-home-fixture.hpp"
Yingdi Yud9715e32014-06-27 08:48:47 -070031
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080032#include <vector>
33
Yingdi Yud9715e32014-06-27 08:48:47 -070034namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070035namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070036
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080037class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir>
Yingdi Yud9715e32014-06-27 08:48:47 -070038{
39public:
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080040 ~IdentityManagementBaseFixture();
Yingdi Yud9715e32014-06-27 08:48:47 -070041
Yingdi Yud9715e32014-06-27 08:48:47 -070042 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080043 saveCertToFile(const Data& obj, const std::string& filename);
44
45protected:
46 std::set<Name> m_identities;
47 std::set<std::string> m_certFiles;
48};
49
50/**
51 * @brief A test suite level fixture to help with identity management
52 *
53 * Test cases in the suite can use this fixture to create identities. Identities,
54 * certificates, and saved certificates are automatically removed during test teardown.
55 *
56 * @deprecated Use IdentityManagementV2Fixture
57 */
58class IdentityManagementV1Fixture : public IdentityManagementBaseFixture
59{
60public:
61 ~IdentityManagementV1Fixture();
62
63 /**
64 * @brief Add identity
65 * @return name of the created self-signed certificate
66 */
67 Name
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080068 addIdentity(const Name& identity, const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS);
Yingdi Yud9715e32014-06-27 08:48:47 -070069
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080070 /**
71 * @brief save identity certificate to a file
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080072 * @param identity certificate name
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080073 * @param filename file name, should be writable
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080074 * @return whether successful
75 */
76 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080077 saveIdentityCertificate(const Name& certName, const std::string& filename);
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080078
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080079 /**
80 * @brief issue a certificate for \p subIdentity signed by \p issuer
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080081 *
82 * If identity does not exist, it is created.
83 * A new key is generated as the default key for identity.
84 * A default certificate for the key is signed by the issuer using its default certificate.
85 *
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080086 * @return whether success
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080087 */
88 bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080089 addSubCertificate(const Name& subIdentity, const Name& issuer,
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080090 const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS);
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080091
Yingdi Yud9715e32014-06-27 08:48:47 -070092protected:
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080093 security::v1::KeyChain m_keyChain;
Yingdi Yud9715e32014-06-27 08:48:47 -070094};
95
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080096/**
97 * @brief A test suite level fixture to help with identity management
98 *
99 * Test cases in the suite can use this fixture to create identities. Identities,
100 * certificates, and saved certificates are automatically removed during test teardown.
101 */
102class IdentityManagementV2Fixture : public IdentityManagementBaseFixture
103{
104public:
105 IdentityManagementV2Fixture();
106
107 /**
108 * @brief Add identity @p identityName
109 * @return name of the created self-signed certificate
110 */
111 security::Identity
112 addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
113
114 /**
115 * @brief Save identity certificate to a file
116 * @param identity identity
117 * @param filename file name, should be writable
118 * @return whether successful
119 */
120 bool
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800121 saveCertificate(const security::Identity& identity, const std::string& filename);
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800122
123 /**
124 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
125 *
126 * If identity does not exist, it is created.
127 * A new key is generated as the default key for identity.
128 * A default certificate for the key is signed by the issuer using its default certificate.
129 *
130 * @return the sub identity
131 */
132 security::Identity
133 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
134 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
135
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700136 /**
137 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
138 */
139 security::v2::Certificate
140 addCertificate(const security::Key& key, const std::string& issuer);
141
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800142protected:
143 security::v2::KeyChain m_keyChain;
144};
145
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800146using IdentityManagementFixture = IdentityManagementV2Fixture;
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800147
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700148} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -0700149} // namespace ndn
Yingdi Yu41546342014-11-30 23:37:53 -0800150
151#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP