Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -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 | |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/impl/pib-memory.hpp" |
| 23 | #include "ndn-cxx/security/pib/impl/pib-sqlite3.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 28 | #include <filesystem> |
| 29 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 30 | #include <boost/mp11/list.hpp> |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | namespace ndn::tests { |
| 33 | |
| 34 | using namespace ndn::security::pib; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Security) |
| 37 | BOOST_AUTO_TEST_SUITE(TestPibImpl) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 39 | class PibMemoryFixture : public PibDataFixture |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 42 | PibMemory pib; |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 45 | class PibSqlite3Fixture : public PibDataFixture |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 46 | { |
| 47 | public: |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 48 | ~PibSqlite3Fixture() |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 49 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 50 | std::filesystem::remove_all(m_path); |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 53 | private: |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 54 | const std::filesystem::path m_path{std::filesystem::path(UNIT_TESTS_TMPDIR) / "TestPibImpl"}; |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 55 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 56 | public: |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame] | 57 | PibSqlite3 pib{m_path}; |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Davide Pesavento | 49e1e87 | 2023-11-11 00:45:23 -0500 | [diff] [blame] | 60 | using PibImpls = boost::mp11::mp_list<PibMemoryFixture, PibSqlite3Fixture>; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 62 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 63 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 64 | // Basic getting and setting |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), ""); |
| 66 | this->pib.setTpmLocator("tpmLocator"); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator"); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 69 | // Add cert, and do not change TPM locator |
| 70 | this->pib.addCertificate(this->id1Key1Cert1); |
| 71 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 72 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 73 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 74 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 75 | // Set TPM locator to the same value, nothing should change |
| 76 | this->pib.setTpmLocator("tpmLocator"); |
| 77 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 78 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 79 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 80 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 81 | // Change TPM locator (contents of PIB should not change) |
| 82 | this->pib.setTpmLocator("newTpmLocator"); |
| 83 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 84 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 85 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 88 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 89 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 90 | // no default setting, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 91 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 92 | |
| 93 | // check id1, which should not exist |
| 94 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
| 95 | |
| 96 | // add id1, should be default |
| 97 | this->pib.addIdentity(this->id1); |
| 98 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 100 | |
| 101 | // add id2, should not be default |
| 102 | this->pib.addIdentity(this->id2); |
| 103 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true); |
| 104 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 105 | |
| 106 | // set id2 explicitly as default |
| 107 | this->pib.setDefaultIdentity(this->id2); |
| 108 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 109 | |
| 110 | // remove id2, should not have default identity |
| 111 | this->pib.removeIdentity(this->id2); |
| 112 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false); |
| 113 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 114 | |
| 115 | // add id2 again, should be default |
| 116 | this->pib.addIdentity(this->id2); |
| 117 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 118 | |
Alexander Afanasyev | 355fd78 | 2020-06-10 16:40:33 -0400 | [diff] [blame] | 119 | // try to set non-existing identity as a default |
| 120 | BOOST_CHECK_THROW(this->pib.setDefaultIdentity("/non-existing-identity"), Pib::Error); |
| 121 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 122 | // get all identities, should contain id1 and id2 |
| 123 | std::set<Name> idNames = this->pib.getIdentities(); |
| 124 | BOOST_CHECK_EQUAL(idNames.size(), 2); |
| 125 | BOOST_CHECK_EQUAL(idNames.count(this->id1), 1); |
| 126 | BOOST_CHECK_EQUAL(idNames.count(this->id2), 1); |
| 127 | } |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T) |
| 130 | { |
| 131 | this->pib.setTpmLocator("tpmLocator"); |
| 132 | |
| 133 | // Add id, key, and cert |
| 134 | this->pib.addCertificate(this->id1Key1Cert1); |
| 135 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 136 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 137 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
| 138 | |
| 139 | // Clear identities |
| 140 | this->pib.clearIdentities(); |
| 141 | BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0); |
| 142 | BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0); |
| 143 | BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0); |
| 144 | BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator"); |
| 145 | } |
| 146 | |
| 147 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T) |
| 148 | { |
| 149 | // no default setting, throw Error |
| 150 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 151 | |
| 152 | // check id1Key1, should not exist, neither should id1. |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
| 154 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 155 | |
| 156 | // add id1Key1, should be default, id1 should be added implicitly |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 157 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
| 159 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
| 160 | const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 161 | BOOST_TEST(keyBits == this->id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 163 | |
| 164 | // add id1Key2, should not be default |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 165 | this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true); |
| 167 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 168 | |
| 169 | // set id1Key2 explicitly as default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 170 | this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name); |
| 171 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 172 | |
| 173 | // set a non-existing key as default, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 174 | BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 175 | Pib::Error); |
| 176 | |
| 177 | // remove id1Key2, should not have default key |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 178 | this->pib.removeKey(this->id1Key2Name); |
| 179 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false); |
| 180 | BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error); |
| 181 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 182 | |
| 183 | // add id1Key2 back, should be default |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 184 | this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 185 | BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name)); |
| 186 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 187 | |
| 188 | // get all the keys: id1Key1 and id1Key2 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 189 | std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(keyNames.size(), 2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1); |
| 192 | BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 193 | |
| 194 | // remove id1, should remove all the keys |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 195 | this->pib.removeIdentity(this->id1); |
| 196 | keyNames = this->pib.getKeysOfIdentity(this->id1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(keyNames.size(), 0); |
| 198 | } |
| 199 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 200 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 201 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 202 | // no default setting, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 203 | BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 204 | |
| 205 | // check id1Key1Cert1, should not exist, neither should id1 and id1Key1 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 206 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false); |
| 207 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
| 208 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 209 | |
| 210 | // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 211 | this->pib.addCertificate(this->id1Key1Cert1); |
| 212 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true); |
| 213 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
| 214 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 215 | BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(), |
| 216 | this->id1Key1Cert1.wireEncode()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 218 | |
| 219 | // add id1Key1Cert2, should not be default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 220 | this->pib.addCertificate(this->id1Key1Cert2); |
| 221 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true); |
| 222 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 223 | |
| 224 | // set id1Key1Cert2 explicitly as default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 225 | this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName()); |
| 226 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 227 | |
| 228 | // set a non-existing cert as default, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 229 | BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 230 | Pib::Error); |
| 231 | |
| 232 | // remove id1Key1Cert2, should not have default cert |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 233 | this->pib.removeCertificate(this->id1Key1Cert2.getName()); |
| 234 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false); |
| 235 | BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error); |
| 236 | BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 237 | |
| 238 | // add id1Key1Cert2, should be default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 239 | this->pib.addCertificate(this->id1Key1Cert2); |
| 240 | BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName())); |
| 241 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 242 | |
| 243 | // get all certificates: id1Key1Cert1 and id1Key1Cert2 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 244 | std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 245 | BOOST_CHECK_EQUAL(certNames.size(), 2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1); |
| 247 | BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 248 | |
| 249 | // remove id1Key1, should remove all the certs |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 250 | this->pib.removeKey(this->id1Key1Name); |
| 251 | certNames = this->pib.getCertificatesOfKey(this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(certNames.size(), 0); |
| 253 | } |
| 254 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 255 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(DefaultsManagement, T, PibImpls, T) |
| 256 | { |
| 257 | this->pib.addIdentity(this->id1); |
| 258 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 259 | |
| 260 | this->pib.addIdentity(this->id2); |
| 261 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 262 | |
| 263 | this->pib.removeIdentity(this->id1); |
| 264 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 265 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 266 | this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 267 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 268 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name); |
| 269 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 270 | this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name); |
| 272 | |
| 273 | this->pib.removeKey(this->id2Key1Name); |
| 274 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id2), Pib::Error); |
| 275 | |
| 276 | this->pib.addCertificate(this->id2Key2Cert1); |
| 277 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key2Name); |
Davide Pesavento | 77d9e81 | 2019-06-03 22:05:54 -0400 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), |
| 279 | this->id2Key2Cert1.getName()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 280 | |
| 281 | this->pib.addCertificate(this->id2Key2Cert2); |
Davide Pesavento | 77d9e81 | 2019-06-03 22:05:54 -0400 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), |
| 283 | this->id2Key2Cert1.getName()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 284 | |
| 285 | this->pib.removeCertificate(this->id2Key2Cert2.getName()); |
Davide Pesavento | 77d9e81 | 2019-06-03 22:05:54 -0400 | [diff] [blame] | 286 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), |
| 287 | this->id2Key2Cert1.getName()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Overwrite, T, PibImpls, T) |
| 291 | { |
| 292 | // check id1Key1, should not exist |
| 293 | this->pib.removeIdentity(this->id1); |
| 294 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
| 295 | |
| 296 | // add id1Key1 |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 297 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 298 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
| 299 | const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name); |
| 300 | BOOST_CHECK(keyBits == this->id1Key1); |
| 301 | |
| 302 | // check overwrite, add a key with the same name. |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 303 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 304 | const Buffer& keyBits2 = this->pib.getKeyBits(this->id1Key1Name); |
| 305 | BOOST_CHECK(keyBits2 == this->id1Key2); |
| 306 | |
| 307 | // check id1Key1Cert1, should not exist |
| 308 | this->pib.removeIdentity(this->id1); |
| 309 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false); |
| 310 | |
| 311 | // add id1Key1Cert1 |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 312 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 313 | this->pib.addCertificate(this->id1Key1Cert1); |
| 314 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true); |
| 315 | |
| 316 | auto cert = this->pib.getCertificate(this->id1Key1Cert1.getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 317 | BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 318 | |
| 319 | // Create a fake cert with the same name |
| 320 | auto cert2 = this->id1Key2Cert1; |
| 321 | cert2.setName(this->id1Key1Cert1.getName()); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(cert2.getSignatureInfo(), this->id1Key2Cert1.getSignatureInfo()); |
| 323 | BOOST_CHECK_EQUAL(cert2.getSignatureValue(), this->id1Key2Cert1.getSignatureValue()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 324 | this->pib.addCertificate(cert2); |
| 325 | |
| 326 | auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 327 | BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 328 | |
| 329 | // both key and certificate are overwritten |
| 330 | Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name); |
| 331 | BOOST_CHECK(keyBits3 == this->id1Key2); |
| 332 | } |
| 333 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 334 | BOOST_AUTO_TEST_SUITE_END() // TestPibImpl |
| 335 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 336 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 337 | } // namespace ndn::tests |