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 | /* |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 25 | #include "security/v2/key-chain.hpp" |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 26 | #include "security/signing-helpers.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 27 | |
| 28 | #include "boost-test.hpp" |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 29 | #include "test-home-fixture.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 33 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 34 | namespace tests { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 36 | class IdentityManagementBaseFixture : public TestHomeFixture<DefaultPibDir> |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 37 | { |
| 38 | public: |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 39 | ~IdentityManagementBaseFixture(); |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 40 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 41 | bool |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 42 | saveCertToFile(const Data& obj, const std::string& filename); |
| 43 | |
| 44 | protected: |
| 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. |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 54 | */ |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame^] | 55 | class IdentityManagementFixture : public IdentityManagementBaseFixture |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 56 | { |
| 57 | public: |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame^] | 58 | IdentityManagementFixture(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * @brief Add identity @p identityName |
| 62 | * @return name of the created self-signed certificate |
| 63 | */ |
| 64 | security::Identity |
| 65 | addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 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 |