blob: 5b4aff3787a10cbda6c569f77af3ef9a287519ee [file] [log] [blame]
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Alexander Afanasyev60514ec2020-06-03 14:18:53 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 *
Yumin Xia2c509c22017-02-09 14:37:36 -08005 * This file is part of NDNS (Named Data Networking Domain Name Service).
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08006 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Yumin Xia2c509c22017-02-09 14:37:36 -080020#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
21#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080022
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include "boost-test.hpp"
24#include "test-home-fixture.hpp"
25
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040026#include <ndn-cxx/security/key-chain.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080027#include <ndn-cxx/security/signing-helpers.hpp>
28
29#include <vector>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080030
31namespace ndn {
32namespace ndns {
33namespace tests {
34
Yumin Xia2c509c22017-02-09 14:37:36 -080035class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080036{
37public:
Yumin Xia2c509c22017-02-09 14:37:36 -080038 ~IdentityManagementBaseFixture();
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080039
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080040 bool
Yumin Xia2c509c22017-02-09 14:37:36 -080041 saveCertToFile(const Data& obj, const std::string& filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080042
43protected:
Yumin Xia2c509c22017-02-09 14:37:36 -080044 std::set<Name> m_identities;
45 std::set<std::string> m_certFiles;
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080046};
47
Yumin Xia2c509c22017-02-09 14:37:36 -080048/**
49 * @brief A test suite level fixture to help with identity management
50 *
51 * Test cases in the suite can use this fixture to create identities. Identities,
52 * certificates, and saved certificates are automatically removed during test teardown.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080053 */
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040054class IdentityManagementFixture : public IdentityManagementBaseFixture
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080055{
Yumin Xia2c509c22017-02-09 14:37:36 -080056public:
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040057 IdentityManagementFixture();
Yumin Xia2c509c22017-02-09 14:37:36 -080058
59 /**
60 * @brief Add identity @p identityName
61 * @return name of the created self-signed certificate
62 */
63 security::Identity
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040064 addIdentity(const Name& identityName, const KeyParams& params = security::KeyChain::getDefaultKeyParams());
Yumin Xia2c509c22017-02-09 14:37:36 -080065
66 /**
67 * @brief Save identity certificate to a file
68 * @param identity identity
69 * @param filename file name, should be writable
70 * @return whether successful
71 */
72 bool
73 saveIdentityCertificate(const security::Identity& identity, const std::string& filename);
74
75 /**
76 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
77 *
78 * If identity does not exist, it is created.
79 * A new key is generated as the default key for identity.
80 * A default certificate for the key is signed by the issuer using its default certificate.
81 *
82 * @return the sub identity
83 */
84 security::Identity
85 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040086 const KeyParams& params = security::KeyChain::getDefaultKeyParams());
Yumin Xia2c509c22017-02-09 14:37:36 -080087
88 /**
89 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
90 */
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040091 security::Certificate
Yumin Xia2c509c22017-02-09 14:37:36 -080092 addCertificate(const security::Key& key, const std::string& issuer);
93
94protected:
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040095 security::KeyChain m_keyChain;
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080096};
97
98} // namespace tests
99} // namespace ndns
100} // namespace ndn
101
Yumin Xia2c509c22017-02-09 14:37:36 -0800102#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP