blob: d3d73de8079fef52e2425767e17cb26ed79e8d0c [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Mickey Sweatt11314b72015-06-10 17:20:19 -070028#include <boost/filesystem.hpp>
Davide Pesavento49e1e872023-11-11 00:45:23 -050029#include <boost/mp11/list.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::tests {
32
33using namespace ndn::security::pib;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070034
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070037
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070038class PibMemoryFixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070039{
40public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070041 PibMemory pib;
Mickey Sweatt11314b72015-06-10 17:20:19 -070042};
43
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070044class PibSqlite3Fixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070045{
46public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070047 ~PibSqlite3Fixture()
Mickey Sweatt11314b72015-06-10 17:20:19 -070048 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050049 boost::filesystem::remove_all(m_path);
Mickey Sweatt11314b72015-06-10 17:20:19 -070050 }
51
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050052private:
53 const boost::filesystem::path m_path{boost::filesystem::path(UNIT_TESTS_TMPDIR) / "TestPibImpl"};
54
Davide Pesaventoeee3e822016-11-26 19:19:34 +010055public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050056 PibSqlite3 pib{m_path.string()};
Mickey Sweatt11314b72015-06-10 17:20:19 -070057};
58
Davide Pesavento49e1e872023-11-11 00:45:23 -050059using PibImpls = boost::mp11::mp_list<PibMemoryFixture, PibSqlite3Fixture>;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070060
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070061BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070062{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070063 // Basic getting and setting
Davide Pesavento0e768ef2022-05-09 20:03:44 -040064 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "");
65 this->pib.setTpmLocator("tpmLocator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070066 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
Yingdi Yu3bf91f52015-06-12 19:39:40 -070067
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070068 // 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 Yu3bf91f52015-06-12 19:39:40 -070073
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070074 // 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 Yu3bf91f52015-06-12 19:39:40 -070079
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070080 // 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 Yu3bf91f52015-06-12 19:39:40 -070085}
86
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070087BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070088{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070089 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070090 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 Yu7b3b5e92015-08-13 19:52:35 -070098 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 Afanasyev355fd782020-06-10 16:40:33 -0400118 // try to set non-existing identity as a default
119 BOOST_CHECK_THROW(this->pib.setDefaultIdentity("/non-existing-identity"), Pib::Error);
120
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700121 // 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
128BOOST_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
146BOOST_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 Yu3bf91f52015-06-12 19:39:40 -0700150
151 // check id1Key1, should not exist, neither should id1.
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700152 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
153 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700154
155 // add id1Key1, should be default, id1 should be added implicitly
Davide Pesavento765abc92021-12-27 00:44:04 -0500156 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700157 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 Pesavento0e768ef2022-05-09 20:03:44 -0400160 BOOST_TEST(keyBits == this->id1Key1, boost::test_tools::per_element());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700161 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700162
163 // add id1Key2, should not be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500164 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700165 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
166 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700167
168 // set id1Key2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700169 this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
170 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700171
172 // set a non-existing key as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700173 BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700174 Pib::Error);
175
176 // remove id1Key2, should not have default key
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700177 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 Yu3bf91f52015-06-12 19:39:40 -0700181
182 // add id1Key2 back, should be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500183 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700184 BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
185 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700186
187 // get all the keys: id1Key1 and id1Key2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700188 std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700189 BOOST_CHECK_EQUAL(keyNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700190 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
191 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700192
193 // remove id1, should remove all the keys
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700194 this->pib.removeIdentity(this->id1);
195 keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700196 BOOST_CHECK_EQUAL(keyNames.size(), 0);
197}
198
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700199BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700200{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700202 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700203
204 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700205 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 Yu3bf91f52015-06-12 19:39:40 -0700208
209 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700210 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 Shi72c0c642018-04-20 15:41:09 +0000214 BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(),
215 this->id1Key1Cert1.wireEncode());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700216 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700217
218 // add id1Key1Cert2, should not be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700219 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 Yu3bf91f52015-06-12 19:39:40 -0700222
223 // set id1Key1Cert2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700224 this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
225 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700226
227 // set a non-existing cert as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700228 BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700229 Pib::Error);
230
231 // remove id1Key1Cert2, should not have default cert
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700232 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 Yu3bf91f52015-06-12 19:39:40 -0700236
237 // add id1Key1Cert2, should be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700238 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 Yu3bf91f52015-06-12 19:39:40 -0700241
242 // get all certificates: id1Key1Cert1 and id1Key1Cert2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700243 std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700244 BOOST_CHECK_EQUAL(certNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700245 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
246 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700247
248 // remove id1Key1, should remove all the certs
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700249 this->pib.removeKey(this->id1Key1Name);
250 certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700251 BOOST_CHECK_EQUAL(certNames.size(), 0);
252}
253
Yingdi Yu03997682015-11-23 16:41:38 -0800254BOOST_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 Pesavento765abc92021-12-27 00:44:04 -0500265 this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800266 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
267 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
268
Davide Pesavento765abc92021-12-27 00:44:04 -0500269 this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800270 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 Pesavento77d9e812019-06-03 22:05:54 -0400277 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
278 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800279
280 this->pib.addCertificate(this->id2Key2Cert2);
Davide Pesavento77d9e812019-06-03 22:05:54 -0400281 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
282 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800283
284 this->pib.removeCertificate(this->id2Key2Cert2.getName());
Davide Pesavento77d9e812019-06-03 22:05:54 -0400285 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
286 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800287}
288
289BOOST_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 Pesavento765abc92021-12-27 00:44:04 -0500296 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800297 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 Pesavento765abc92021-12-27 00:44:04 -0500302 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800303 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 Pesavento765abc92021-12-27 00:44:04 -0500311 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800312 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 Shi72c0c642018-04-20 15:41:09 +0000316 BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800317
318 // Create a fake cert with the same name
319 auto cert2 = this->id1Key2Cert1;
320 cert2.setName(this->id1Key1Cert1.getName());
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400321 BOOST_CHECK_EQUAL(cert2.getSignatureInfo(), this->id1Key2Cert1.getSignatureInfo());
322 BOOST_CHECK_EQUAL(cert2.getSignatureValue(), this->id1Key2Cert1.getSignatureValue());
Yingdi Yu03997682015-11-23 16:41:38 -0800323 this->pib.addCertificate(cert2);
324
325 auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000326 BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800327
328 // both key and certificate are overwritten
329 Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name);
330 BOOST_CHECK(keyBits3 == this->id1Key2);
331}
332
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100333BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
334BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700335
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400336} // namespace ndn::tests