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