Alexander Afanasyev | 77f6ae1 | 2018-06-14 17:54:17 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California |
| 4 | * |
| 5 | * NAC library is free software: you can redistribute it and/or modify it under the |
| 6 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 7 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 14 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 15 | * <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * See AUTHORS.md for complete list of NAC library authors and contributors. |
| 18 | */ |
| 19 | |
| 20 | #ifndef NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 21 | #define NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |
| 22 | |
| 23 | #include "boost-test.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/security/v2/key-chain.hpp> |
| 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 27 | |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace nac { |
| 32 | namespace tests { |
| 33 | |
| 34 | /** |
| 35 | * @brief A test suite level fixture to help with identity management |
| 36 | * |
| 37 | * Test cases in the suite can use this fixture to create identities. Identities, |
| 38 | * certificates, and saved certificates are automatically removed during test teardown. |
| 39 | */ |
| 40 | class IdentityManagementFixture |
| 41 | { |
| 42 | public: |
| 43 | IdentityManagementFixture(); |
| 44 | |
| 45 | ~IdentityManagementFixture(); |
| 46 | |
| 47 | /** |
| 48 | * @brief Add identity @p identityName |
| 49 | * @return name of the created self-signed certificate |
| 50 | */ |
| 51 | security::Identity |
| 52 | addIdentity(const Name& identityName, const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 53 | |
| 54 | /** |
| 55 | * @brief Save identity certificate to a file |
| 56 | * @param identity identity |
| 57 | * @param filename file name, should be writable |
| 58 | * @return whether successful |
| 59 | */ |
| 60 | bool |
| 61 | saveIdentityCertificate(const security::Identity& identity, const std::string& filename); |
| 62 | |
| 63 | /** |
| 64 | * @brief Issue a certificate for \p subIdentityName signed by \p issuer |
| 65 | * |
| 66 | * If identity does not exist, it is created. |
| 67 | * A new key is generated as the default key for identity. |
| 68 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 69 | * |
| 70 | * @return the sub identity |
| 71 | */ |
| 72 | security::Identity |
| 73 | addSubCertificate(const Name& subIdentityName, const security::Identity& issuer, |
| 74 | const KeyParams& params = security::v2::KeyChain::getDefaultKeyParams()); |
| 75 | |
| 76 | /** |
| 77 | * @brief Add a self-signed certificate to @p key with issuer ID @p issuer |
| 78 | */ |
| 79 | security::v2::Certificate |
| 80 | addCertificate(const security::Key& key, const std::string& issuer); |
| 81 | |
| 82 | bool |
| 83 | saveCertToFile(const Data& obj, const std::string& filename); |
| 84 | |
| 85 | protected: |
| 86 | security::v2::KeyChain m_keyChain; |
| 87 | std::set<Name> m_identities; |
| 88 | std::set<std::string> m_certFiles; |
| 89 | }; |
| 90 | |
| 91 | } // namespace tests |
| 92 | } // namespace nac |
| 93 | } // namespace ndn |
| 94 | |
| 95 | #endif // NAC_TESTS_IDENTITY_MANAGEMENT_FIXTURE_HPP |