Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 25 | #include "security/v1/key-chain.hpp" |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 26 | #include "security/v2/key-chain.hpp" |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 27 | #include "security/signing-helpers.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include "boost-test.hpp" |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 31 | #include "test-home-fixture.hpp" |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 32 | |
| 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. |
| 54 | * |
| 55 | * @deprecated Use IdentityManagementV2Fixture |
| 56 | */ |
| 57 | class IdentityManagementV1Fixture : public IdentityManagementBaseFixture |
| 58 | { |
| 59 | public: |
| 60 | ~IdentityManagementV1Fixture(); |
| 61 | |
| 62 | /** |
| 63 | * @brief Add identity |
| 64 | * @return name of the created self-signed certificate |
| 65 | */ |
| 66 | Name |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 67 | addIdentity(const Name& identity, const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS); |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 68 | |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 69 | /** |
| 70 | * @brief save identity certificate to a file |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 71 | * @param identity certificate name |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 72 | * @param filename file name, should be writable |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 73 | * @return whether successful |
| 74 | */ |
| 75 | bool |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 76 | saveIdentityCertificate(const Name& certName, const std::string& filename); |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 78 | /** |
| 79 | * @brief issue a certificate for \p subIdentity signed by \p issuer |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 80 | * |
| 81 | * If identity does not exist, it is created. |
| 82 | * A new key is generated as the default key for identity. |
| 83 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 84 | * |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 85 | * @return whether success |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 86 | */ |
| 87 | bool |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 88 | addSubCertificate(const Name& subIdentity, const Name& issuer, |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 89 | const KeyParams& params = security::v1::KeyChain::DEFAULT_KEY_PARAMS); |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 90 | |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 91 | protected: |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 92 | security::v1::KeyChain m_keyChain; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 95 | /** |
| 96 | * @brief A test suite level fixture to help with identity management |
| 97 | * |
| 98 | * Test cases in the suite can use this fixture to create identities. Identities, |
| 99 | * certificates, and saved certificates are automatically removed during test teardown. |
| 100 | */ |
| 101 | class IdentityManagementV2Fixture : public IdentityManagementBaseFixture |
| 102 | { |
| 103 | public: |
| 104 | IdentityManagementV2Fixture(); |
| 105 | |
| 106 | /** |
| 107 | * @brief Add identity @p identityName |
| 108 | * @return name of the created self-signed certificate |
| 109 | */ |
| 110 | security::Identity |
| 111 | addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 112 | |
| 113 | /** |
| 114 | * @brief Save identity certificate to a file |
| 115 | * @param identity identity |
| 116 | * @param filename file name, should be writable |
| 117 | * @return whether successful |
| 118 | */ |
| 119 | bool |
| 120 | saveIdentityCertificate(const security::Identity& identity, const std::string& filename); |
| 121 | |
| 122 | /** |
| 123 | * @brief Issue a certificate for \p subIdentityName signed by \p issuer |
| 124 | * |
| 125 | * If identity does not exist, it is created. |
| 126 | * A new key is generated as the default key for identity. |
| 127 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 128 | * |
| 129 | * @return the sub identity |
| 130 | */ |
| 131 | security::Identity |
| 132 | addSubCertificate(const Name& subIdentityName, const security::Identity& issuer, |
| 133 | const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 134 | |
| 135 | protected: |
| 136 | security::v2::KeyChain m_keyChain; |
| 137 | }; |
| 138 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame^] | 139 | using IdentityManagementFixture = IdentityManagementV2Fixture; |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 141 | } // namespace tests |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 142 | } // namespace ndn |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 143 | |
| 144 | #endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |