blob: d88bad6cd3cd7ad238799fed391c0474b36cd010 [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 Pesavento8618c1e2022-05-05 15:20:02 -04003 * Copyright (c) 2013-2022 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 Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/security/security-common.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070028
Mickey Sweatt11314b72015-06-10 17:20:19 -070029#include <boost/filesystem.hpp>
Davide Pesavento77d9e812019-06-03 22:05:54 -040030#include <boost/mpl/vector.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070031
32namespace ndn {
33namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034namespace pib {
Yingdi Yu3bf91f52015-06-12 19:39:40 -070035namespace tests {
36
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070040
Yingdi Yu6ee2d362015-07-16 21:48:05 -070041using pib::Pib;
42
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070043class PibMemoryFixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070044{
45public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070046 PibMemory pib;
Mickey Sweatt11314b72015-06-10 17:20:19 -070047};
48
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070049class PibSqlite3Fixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070050{
51public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070052 ~PibSqlite3Fixture()
Mickey Sweatt11314b72015-06-10 17:20:19 -070053 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050054 boost::filesystem::remove_all(m_path);
Mickey Sweatt11314b72015-06-10 17:20:19 -070055 }
56
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050057private:
58 const boost::filesystem::path m_path{boost::filesystem::path(UNIT_TESTS_TMPDIR) / "TestPibImpl"};
59
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050061 PibSqlite3 pib{m_path.string()};
Mickey Sweatt11314b72015-06-10 17:20:19 -070062};
63
Davide Pesavento77d9e812019-06-03 22:05:54 -040064using PibImpls = boost::mpl::vector<PibMemoryFixture, PibSqlite3Fixture>;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070065
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070066BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070067{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070068 // Basic getting and setting
Davide Pesavento0e768ef2022-05-09 20:03:44 -040069 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "");
70 this->pib.setTpmLocator("tpmLocator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070071 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
Yingdi Yu3bf91f52015-06-12 19:39:40 -070072
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070073 // Add cert, and do not change TPM locator
74 this->pib.addCertificate(this->id1Key1Cert1);
75 BOOST_CHECK(this->pib.hasIdentity(this->id1));
76 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
77 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070078
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070079 // Set TPM locator to the same value, nothing should change
80 this->pib.setTpmLocator("tpmLocator");
81 BOOST_CHECK(this->pib.hasIdentity(this->id1));
82 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
83 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070084
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070085 // Change TPM locator (contents of PIB should not change)
86 this->pib.setTpmLocator("newTpmLocator");
87 BOOST_CHECK(this->pib.hasIdentity(this->id1));
88 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
89 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070090}
91
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070092BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070093{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070094 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070095 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
96
97 // check id1, which should not exist
98 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
99
100 // add id1, should be default
101 this->pib.addIdentity(this->id1);
102 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700103 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
104
105 // add id2, should not be default
106 this->pib.addIdentity(this->id2);
107 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true);
108 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
109
110 // set id2 explicitly as default
111 this->pib.setDefaultIdentity(this->id2);
112 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
113
114 // remove id2, should not have default identity
115 this->pib.removeIdentity(this->id2);
116 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false);
117 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
118
119 // add id2 again, should be default
120 this->pib.addIdentity(this->id2);
121 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
122
Alexander Afanasyev355fd782020-06-10 16:40:33 -0400123 // try to set non-existing identity as a default
124 BOOST_CHECK_THROW(this->pib.setDefaultIdentity("/non-existing-identity"), Pib::Error);
125
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700126 // get all identities, should contain id1 and id2
127 std::set<Name> idNames = this->pib.getIdentities();
128 BOOST_CHECK_EQUAL(idNames.size(), 2);
129 BOOST_CHECK_EQUAL(idNames.count(this->id1), 1);
130 BOOST_CHECK_EQUAL(idNames.count(this->id2), 1);
131}
132
133BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T)
134{
135 this->pib.setTpmLocator("tpmLocator");
136
137 // Add id, key, and cert
138 this->pib.addCertificate(this->id1Key1Cert1);
139 BOOST_CHECK(this->pib.hasIdentity(this->id1));
140 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
141 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
142
143 // Clear identities
144 this->pib.clearIdentities();
145 BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0);
146 BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0);
147 BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0);
148 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
149}
150
151BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T)
152{
153 // no default setting, throw Error
154 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700155
156 // check id1Key1, should not exist, neither should id1.
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700157 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
158 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700159
160 // add id1Key1, should be default, id1 should be added implicitly
Davide Pesavento765abc92021-12-27 00:44:04 -0500161 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700162 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
163 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
164 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
Davide Pesavento0e768ef2022-05-09 20:03:44 -0400165 BOOST_TEST(keyBits == this->id1Key1, boost::test_tools::per_element());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700166 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700167
168 // add id1Key2, should not be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500169 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700170 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
171 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700172
173 // set id1Key2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700174 this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
175 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700176
177 // set a non-existing key as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700178 BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700179 Pib::Error);
180
181 // remove id1Key2, should not have default key
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700182 this->pib.removeKey(this->id1Key2Name);
183 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false);
184 BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error);
185 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700186
187 // add id1Key2 back, should be default
Davide Pesavento765abc92021-12-27 00:44:04 -0500188 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700189 BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
190 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700191
192 // get all the keys: id1Key1 and id1Key2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700193 std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700194 BOOST_CHECK_EQUAL(keyNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700195 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
196 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700197
198 // remove id1, should remove all the keys
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700199 this->pib.removeIdentity(this->id1);
200 keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201 BOOST_CHECK_EQUAL(keyNames.size(), 0);
202}
203
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700204BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700205{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700206 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700207 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700208
209 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700210 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
211 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
212 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700213
214 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700215 this->pib.addCertificate(this->id1Key1Cert1);
216 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
217 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
218 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000219 BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(),
220 this->id1Key1Cert1.wireEncode());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700221 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700222
223 // add id1Key1Cert2, should not be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700224 this->pib.addCertificate(this->id1Key1Cert2);
225 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true);
226 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700227
228 // set id1Key1Cert2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700229 this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
230 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700231
232 // set a non-existing cert as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700233 BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700234 Pib::Error);
235
236 // remove id1Key1Cert2, should not have default cert
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700237 this->pib.removeCertificate(this->id1Key1Cert2.getName());
238 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false);
239 BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error);
240 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700241
242 // add id1Key1Cert2, should be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700243 this->pib.addCertificate(this->id1Key1Cert2);
244 BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName()));
245 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700246
247 // get all certificates: id1Key1Cert1 and id1Key1Cert2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700248 std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700249 BOOST_CHECK_EQUAL(certNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700250 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
251 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700252
253 // remove id1Key1, should remove all the certs
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700254 this->pib.removeKey(this->id1Key1Name);
255 certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700256 BOOST_CHECK_EQUAL(certNames.size(), 0);
257}
258
Yingdi Yu03997682015-11-23 16:41:38 -0800259BOOST_FIXTURE_TEST_CASE_TEMPLATE(DefaultsManagement, T, PibImpls, T)
260{
261 this->pib.addIdentity(this->id1);
262 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
263
264 this->pib.addIdentity(this->id2);
265 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
266
267 this->pib.removeIdentity(this->id1);
268 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
269
Davide Pesavento765abc92021-12-27 00:44:04 -0500270 this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800271 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
272 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
273
Davide Pesavento765abc92021-12-27 00:44:04 -0500274 this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800275 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
276
277 this->pib.removeKey(this->id2Key1Name);
278 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id2), Pib::Error);
279
280 this->pib.addCertificate(this->id2Key2Cert1);
281 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key2Name);
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.addCertificate(this->id2Key2Cert2);
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 this->pib.removeCertificate(this->id2Key2Cert2.getName());
Davide Pesavento77d9e812019-06-03 22:05:54 -0400290 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
291 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800292}
293
294BOOST_FIXTURE_TEST_CASE_TEMPLATE(Overwrite, T, PibImpls, T)
295{
296 // check id1Key1, should not exist
297 this->pib.removeIdentity(this->id1);
298 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
299
300 // add id1Key1
Davide Pesavento765abc92021-12-27 00:44:04 -0500301 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800302 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
303 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
304 BOOST_CHECK(keyBits == this->id1Key1);
305
306 // check overwrite, add a key with the same name.
Davide Pesavento765abc92021-12-27 00:44:04 -0500307 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2);
Yingdi Yu03997682015-11-23 16:41:38 -0800308 const Buffer& keyBits2 = this->pib.getKeyBits(this->id1Key1Name);
309 BOOST_CHECK(keyBits2 == this->id1Key2);
310
311 // check id1Key1Cert1, should not exist
312 this->pib.removeIdentity(this->id1);
313 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
314
315 // add id1Key1Cert1
Davide Pesavento765abc92021-12-27 00:44:04 -0500316 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1);
Yingdi Yu03997682015-11-23 16:41:38 -0800317 this->pib.addCertificate(this->id1Key1Cert1);
318 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
319
320 auto cert = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000321 BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800322
323 // Create a fake cert with the same name
324 auto cert2 = this->id1Key2Cert1;
325 cert2.setName(this->id1Key1Cert1.getName());
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400326 BOOST_CHECK_EQUAL(cert2.getSignatureInfo(), this->id1Key2Cert1.getSignatureInfo());
327 BOOST_CHECK_EQUAL(cert2.getSignatureValue(), this->id1Key2Cert1.getSignatureValue());
Yingdi Yu03997682015-11-23 16:41:38 -0800328 this->pib.addCertificate(cert2);
329
330 auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000331 BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800332
333 // both key and certificate are overwritten
334 Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name);
335 BOOST_CHECK(keyBits3 == this->id1Key2);
336}
337
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100338BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700339BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100340BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700341
342} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700343} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700344} // namespace security
345} // namespace ndn