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