blob: 7f624fd707f3042c1edc1f370e9123eac56cde86 [file] [log] [blame]
Yingdi Yu3bf91f52015-06-12 19:39:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesavento51974f62024-12-21 20:42:45 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Yingdi Yu3bf91f52015-06-12 19:39:40 -07004 *
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 Pesavento4fb35d82019-10-31 19:33:10 -040022#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
23#include "ndn-cxx/security/pib/impl/pib-sqlite3.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070027
Davide Pesavento51974f62024-12-21 20:42:45 -050028#include <filesystem>
29
Davide Pesavento49e1e872023-11-11 00:45:23 -050030#include <boost/mp11/list.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032namespace ndn::tests {
33
34using namespace ndn::security::pib;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070035
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070038
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070039class PibMemoryFixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070040{
41public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070042 PibMemory pib;
Mickey Sweatt11314b72015-06-10 17:20:19 -070043};
44
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070045class PibSqlite3Fixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070046{
47public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070048 ~PibSqlite3Fixture()
Mickey Sweatt11314b72015-06-10 17:20:19 -070049 {
Davide Pesavento51974f62024-12-21 20:42:45 -050050 std::filesystem::remove_all(m_path);
Mickey Sweatt11314b72015-06-10 17:20:19 -070051 }
52
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050053private:
Davide Pesavento51974f62024-12-21 20:42:45 -050054 const std::filesystem::path m_path{std::filesystem::path(UNIT_TESTS_TMPDIR) / "TestPibImpl"};
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050055
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056public:
Davide Pesavento51974f62024-12-21 20:42:45 -050057 PibSqlite3 pib{m_path};
Mickey Sweatt11314b72015-06-10 17:20:19 -070058};
59
Davide Pesavento49e1e872023-11-11 00:45:23 -050060using PibImpls = boost::mp11::mp_list<PibMemoryFixture, PibSqlite3Fixture>;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070061
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070062BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070063{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070064 // Basic getting and setting
Davide Pesavento0e768ef2022-05-09 20:03:44 -040065 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "");
66 this->pib.setTpmLocator("tpmLocator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070067 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
Yingdi Yu3bf91f52015-06-12 19:39:40 -070068
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070069 // 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 Yu3bf91f52015-06-12 19:39:40 -070074
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070075 // 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 Yu3bf91f52015-06-12 19:39:40 -070080
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070081 // 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 Yu3bf91f52015-06-12 19:39:40 -070086}
87
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070088BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070089{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070090 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070091 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 Yu7b3b5e92015-08-13 19:52:35 -070099 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 Afanasyev355fd782020-06-10 16:40:33 -0400119 // try to set non-existing identity as a default
120 BOOST_CHECK_THROW(this->pib.setDefaultIdentity("/non-existing-identity"), Pib::Error);
121
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700122 // 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
129BOOST_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
147BOOST_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 Yu3bf91f52015-06-12 19:39:40 -0700151
152 // check id1Key1, should not exist, neither should id1.
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700153 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
154 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700155
156 // add id1Key1, should be default, id1 should be added implicitly
Davide Pesavento765abc92021-12-27 00:44:04 -0500157 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700158 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 Pesavento0e768ef2022-05-09 20:03:44 -0400161 BOOST_TEST(keyBits == this->id1Key1, boost::test_tools::per_element());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700162 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700163
164 // add id1Key2, should not be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500165 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700166 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
167 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700168
169 // set id1Key2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700170 this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
171 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700172
173 // set a non-existing key as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700174 BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700175 Pib::Error);
176
177 // remove id1Key2, should not have default key
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700178 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 Yu3bf91f52015-06-12 19:39:40 -0700182
183 // add id1Key2 back, should be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500184 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700185 BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
186 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700187
188 // get all the keys: id1Key1 and id1Key2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700189 std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700190 BOOST_CHECK_EQUAL(keyNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700191 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
192 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700193
194 // remove id1, should remove all the keys
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700195 this->pib.removeIdentity(this->id1);
196 keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700197 BOOST_CHECK_EQUAL(keyNames.size(), 0);
198}
199
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700200BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700202 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700203 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700204
205 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700206 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 Yu3bf91f52015-06-12 19:39:40 -0700209
210 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700211 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 Shi72c0c642018-04-20 15:41:09 +0000215 BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(),
216 this->id1Key1Cert1.wireEncode());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700217 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700218
219 // add id1Key1Cert2, should not be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700220 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 Yu3bf91f52015-06-12 19:39:40 -0700223
224 // set id1Key1Cert2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700225 this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
226 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700227
228 // set a non-existing cert as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700229 BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700230 Pib::Error);
231
232 // remove id1Key1Cert2, should not have default cert
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700233 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 Yu3bf91f52015-06-12 19:39:40 -0700237
238 // add id1Key1Cert2, should be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700239 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 Yu3bf91f52015-06-12 19:39:40 -0700242
243 // get all certificates: id1Key1Cert1 and id1Key1Cert2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700244 std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700245 BOOST_CHECK_EQUAL(certNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700246 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
247 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700248
249 // remove id1Key1, should remove all the certs
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700250 this->pib.removeKey(this->id1Key1Name);
251 certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700252 BOOST_CHECK_EQUAL(certNames.size(), 0);
253}
254
Yingdi Yu03997682015-11-23 16:41:38 -0800255BOOST_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 Pesavento765abc92021-12-27 00:44:04 -0500266 this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800267 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
268 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
269
Davide Pesavento765abc92021-12-27 00:44:04 -0500270 this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800271 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 Pesavento77d9e812019-06-03 22:05:54 -0400278 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
279 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800280
281 this->pib.addCertificate(this->id2Key2Cert2);
Davide Pesavento77d9e812019-06-03 22:05:54 -0400282 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
283 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800284
285 this->pib.removeCertificate(this->id2Key2Cert2.getName());
Davide Pesavento77d9e812019-06-03 22:05:54 -0400286 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
287 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800288}
289
290BOOST_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 Pesavento765abc92021-12-27 00:44:04 -0500297 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800298 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 Pesavento765abc92021-12-27 00:44:04 -0500303 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800304 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 Pesavento765abc92021-12-27 00:44:04 -0500312 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800313 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 Shi72c0c642018-04-20 15:41:09 +0000317 BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800318
319 // Create a fake cert with the same name
320 auto cert2 = this->id1Key2Cert1;
321 cert2.setName(this->id1Key1Cert1.getName());
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400322 BOOST_CHECK_EQUAL(cert2.getSignatureInfo(), this->id1Key2Cert1.getSignatureInfo());
323 BOOST_CHECK_EQUAL(cert2.getSignatureValue(), this->id1Key2Cert1.getSignatureValue());
Yingdi Yu03997682015-11-23 16:41:38 -0800324 this->pib.addCertificate(cert2);
325
326 auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000327 BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800328
329 // both key and certificate are overwritten
330 Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name);
331 BOOST_CHECK(keyBits3 == this->id1Key2);
332}
333
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100334BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
335BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700336
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400337} // namespace ndn::tests