Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 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 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 22 | #ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 23 | #define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame^] | 25 | #include "ndn-cxx/security/v2/key-chain.hpp" |
| 26 | #include "ndn-cxx/security/signing-helpers.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame^] | 28 | #include "tests/test-home-fixture.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 32 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 33 | namespace tests { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 35 | class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir> |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 38 | ~IdentityManagementBaseFixture(); |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 40 | bool |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 41 | saveCertToFile(const Data& obj, const std::string& filename); |
| 42 | |
| 43 | protected: |
| 44 | std::set<Name> m_identities; |
| 45 | std::set<std::string> m_certFiles; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 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 Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 53 | */ |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 54 | class IdentityManagementFixture : public IdentityManagementBaseFixture |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 55 | { |
| 56 | public: |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 57 | IdentityManagementFixture(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @brief Add identity @p identityName |
| 61 | * @return name of the created self-signed certificate |
| 62 | */ |
| 63 | security::Identity |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame^] | 64 | addIdentity(const Name& identityName, |
| 65 | const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * @brief Save identity certificate to a file |
| 69 | * @param identity identity |
| 70 | * @param filename file name, should be writable |
| 71 | * @return whether successful |
| 72 | */ |
| 73 | bool |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 74 | saveCertificate(const security::Identity& identity, const std::string& filename); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * @brief Issue a certificate for \p subIdentityName signed by \p issuer |
| 78 | * |
| 79 | * If identity does not exist, it is created. |
| 80 | * A new key is generated as the default key for identity. |
| 81 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 82 | * |
| 83 | * @return the sub identity |
| 84 | */ |
| 85 | security::Identity |
| 86 | addSubCertificate(const Name& subIdentityName, const security::Identity& issuer, |
| 87 | const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 88 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 89 | /** |
| 90 | * @brief Add a self-signed certificate to @p key with issuer ID @p issuer |
| 91 | */ |
| 92 | security::v2::Certificate |
| 93 | addCertificate(const security::Key& key, const std::string& issuer); |
| 94 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 95 | protected: |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 96 | KeyChain m_keyChain; |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 99 | } // namespace tests |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 100 | } // namespace ndn |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 101 | |
| 102 | #endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |