blob: c83bf3067f030c35843a53fa2d1ab78462bcc66e [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07002/*
3 * Copyright (c) 2013-2019 Regents of the University of California.
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Zhiyi Zhang8617a792017-01-17 16:45:56 -08006 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07007 * 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.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080010 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070011 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080014 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070015 * 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/>.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080018 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070019 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080020 */
21
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070022#ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
23#define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP
Zhiyi Zhang8617a792017-01-17 16:45:56 -080024
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070025#include "test-home-fixture.hpp"
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070026#include "unit-test-time-fixture.hpp"
27#include "boost-test.hpp"
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080028#include <ndn-cxx/security/v2/key-chain.hpp>
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080029#include <ndn-cxx/security/signing-helpers.hpp>
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070030#include <vector>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080031
32namespace ndn {
33namespace ndncert {
34namespace tests {
35
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir>
37{
38public:
39 ~IdentityManagementBaseFixture();
40
41 bool
42 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
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080049/**
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 */
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055class IdentityManagementFixture : public IdentityManagementBaseFixture
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080056{
57public:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 IdentityManagementFixture();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080059
60 /**
61 * @brief Add identity @p identityName
62 * @return name of the created self-signed certificate
63 */
64 security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 addIdentity(const Name& identityName,
66 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080067
68 /**
69 * @brief Save identity certificate to a file
70 * @param identity identity
71 * @param filename file name, should be writable
72 * @return whether successful
73 */
74 bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075 saveCertificate(const security::Identity& identity, const std::string& filename);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080076
77 /**
78 * @brief Issue a certificate for \p subIdentityName signed by \p issuer
79 *
80 * If identity does not exist, it is created.
81 * A new key is generated as the default key for identity.
82 * A default certificate for the key is signed by the issuer using its default certificate.
83 *
84 * @return the sub identity
85 */
86 security::Identity
87 addSubCertificate(const Name& subIdentityName, const security::Identity& issuer,
88 const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams());
89
90 /**
91 * @brief Add a self-signed certificate to @p key with issuer ID @p issuer
92 */
93 security::v2::Certificate
94 addCertificate(const security::Key& key, const std::string& issuer);
95
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080096protected:
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097 KeyChain m_keyChain;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080098};
99
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700100class IdentityManagementTimeFixture : public UnitTestTimeFixture
101 , public IdentityManagementFixture
Zhiyi Zhang3b76f2c2017-03-01 10:20:15 -0800102{
103};
104
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800105} // namespace tests
106} // namespace ndncert
107} // namespace ndn
108
Zhiyi Zhang42d992d2019-07-07 16:46:50 -0700109#endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP