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