Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 2 | /* |
Zhiyi Zhang | a3e62bd | 2020-10-12 17:07:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 4 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 6 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 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. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 10 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 14 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 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/>. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 18 | * |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 22 | #ifndef NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 23 | #define NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 24 | |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 25 | #include <ndn-cxx/security/key-chain.hpp> |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 27 | #include <vector> |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndncert { |
| 31 | namespace tests { |
| 32 | |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 33 | class IdentityManagementBaseFixture |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
| 36 | ~IdentityManagementBaseFixture(); |
| 37 | |
| 38 | bool |
| 39 | saveCertToFile(const Data& obj, const std::string& filename); |
| 40 | |
| 41 | protected: |
| 42 | std::set<Name> m_identities; |
| 43 | std::set<std::string> m_certFiles; |
| 44 | }; |
| 45 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 46 | /** |
| 47 | * @brief A test suite level fixture to help with identity management |
| 48 | * |
| 49 | * Test cases in the suite can use this fixture to create identities. Identities, |
| 50 | * certificates, and saved certificates are automatically removed during test teardown. |
| 51 | */ |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | class IdentityManagementFixture : public IdentityManagementBaseFixture |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 53 | { |
| 54 | public: |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | IdentityManagementFixture(); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @brief Add identity @p identityName |
| 59 | * @return name of the created self-signed certificate |
| 60 | */ |
| 61 | security::Identity |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | addIdentity(const Name& identityName, |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 63 | const KeyParams& params = security::KeyChain::getDefaultKeyParams()); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief Save identity certificate to a file |
Zhiyi Zhang | 6d9eda6 | 2020-10-16 17:37:02 -0700 | [diff] [blame] | 67 | * @param identity identity |
| 68 | * @param filename file name, should be writable |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 69 | * @return whether successful |
| 70 | */ |
| 71 | bool |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 72 | saveCertificate(const security::Identity& identity, const std::string& filename); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @brief Issue a certificate for \p subIdentityName signed by \p issuer |
| 76 | * |
| 77 | * If identity does not exist, it is created. |
| 78 | * A new key is generated as the default key for identity. |
| 79 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 80 | * |
| 81 | * @return the sub identity |
| 82 | */ |
| 83 | security::Identity |
| 84 | addSubCertificate(const Name& subIdentityName, const security::Identity& issuer, |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 85 | const KeyParams& params = security::KeyChain::getDefaultKeyParams()); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @brief Add a self-signed certificate to @p key with issuer ID @p issuer |
| 89 | */ |
tylerliu | a7bea66 | 2020-10-08 18:51:02 -0700 | [diff] [blame] | 90 | security::Certificate |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 91 | addCertificate(const security::Key& key, const std::string& issuer); |
| 92 | |
Zhiyi Zhang | 6f3c58b | 2020-10-30 08:53:42 -0700 | [diff] [blame] | 93 | protected: |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 94 | KeyChain m_keyChain; |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 97 | } // namespace tests |
| 98 | } // namespace ndncert |
| 99 | } // namespace ndn |
| 100 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 101 | #endif // NDN_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |