blob: e16f658b72aad9997c7d4823bc5c485ded1ce169 [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/*
Eric Newberrya3c8bd12020-05-15 17:27:07 -07003 * Copyright (c) 2013-2020 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/pib/pib.hpp"
25#include "ndn-cxx/security/security-common.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
28#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070029
Mickey Sweatt11314b72015-06-10 17:20:19 -070030#include <boost/filesystem.hpp>
Davide Pesavento77d9e812019-06-03 22:05:54 -040031#include <boost/mpl/vector.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070032
33namespace ndn {
34namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070035namespace pib {
Yingdi Yu3bf91f52015-06-12 19:39:40 -070036namespace tests {
37
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038using namespace ndn::security::tests;
39
Davide Pesaventoeee3e822016-11-26 19:19:34 +010040BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070041BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070043
Yingdi Yu6ee2d362015-07-16 21:48:05 -070044using pib::Pib;
45
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070046class PibMemoryFixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070047{
48public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070049 PibMemory pib;
Mickey Sweatt11314b72015-06-10 17:20:19 -070050};
51
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070052class PibSqlite3Fixture : public PibDataFixture
Mickey Sweatt11314b72015-06-10 17:20:19 -070053{
54public:
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070055 PibSqlite3Fixture()
Mickey Sweatt11314b72015-06-10 17:20:19 -070056 : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070057 , pib(tmpPath.c_str())
Mickey Sweatt11314b72015-06-10 17:20:19 -070058 {
59 }
60
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070061 ~PibSqlite3Fixture()
Mickey Sweatt11314b72015-06-10 17:20:19 -070062 {
63 boost::filesystem::remove_all(tmpPath);
64 }
65
Davide Pesaventoeee3e822016-11-26 19:19:34 +010066public:
Mickey Sweatt11314b72015-06-10 17:20:19 -070067 boost::filesystem::path tmpPath;
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070068 PibSqlite3 pib;
Mickey Sweatt11314b72015-06-10 17:20:19 -070069};
70
Davide Pesavento77d9e812019-06-03 22:05:54 -040071using PibImpls = boost::mpl::vector<PibMemoryFixture, PibSqlite3Fixture>;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070072
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070073BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070074{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070075 // Basic getting and setting
76 BOOST_CHECK_NO_THROW(this->pib.getTpmLocator());
Yingdi Yu3bf91f52015-06-12 19:39:40 -070077
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070078 BOOST_CHECK_NO_THROW(this->pib.setTpmLocator("tpmLocator"));
79 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
Yingdi Yu3bf91f52015-06-12 19:39:40 -070080
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070081 // Add cert, and do not change TPM locator
82 this->pib.addCertificate(this->id1Key1Cert1);
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
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070087 // Set TPM locator to the same value, nothing should change
88 this->pib.setTpmLocator("tpmLocator");
89 BOOST_CHECK(this->pib.hasIdentity(this->id1));
90 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
91 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070092
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070093 // Change TPM locator (contents of PIB should not change)
94 this->pib.setTpmLocator("newTpmLocator");
95 BOOST_CHECK(this->pib.hasIdentity(this->id1));
96 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
97 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070098}
99
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700100BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700101{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700102 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700103 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
104
105 // check id1, which should not exist
106 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
107
108 // add id1, should be default
109 this->pib.addIdentity(this->id1);
110 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
111 BOOST_CHECK_NO_THROW(this->pib.getDefaultIdentity());
112 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
113
114 // add id2, should not be default
115 this->pib.addIdentity(this->id2);
116 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true);
117 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
118
119 // set id2 explicitly as default
120 this->pib.setDefaultIdentity(this->id2);
121 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
122
123 // remove id2, should not have default identity
124 this->pib.removeIdentity(this->id2);
125 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false);
126 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
127
128 // add id2 again, should be default
129 this->pib.addIdentity(this->id2);
130 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
131
Alexander Afanasyev355fd782020-06-10 16:40:33 -0400132 // try to set non-existing identity as a default
133 BOOST_CHECK_THROW(this->pib.setDefaultIdentity("/non-existing-identity"), Pib::Error);
134
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700135 // get all identities, should contain id1 and id2
136 std::set<Name> idNames = this->pib.getIdentities();
137 BOOST_CHECK_EQUAL(idNames.size(), 2);
138 BOOST_CHECK_EQUAL(idNames.count(this->id1), 1);
139 BOOST_CHECK_EQUAL(idNames.count(this->id2), 1);
140}
141
142BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T)
143{
144 this->pib.setTpmLocator("tpmLocator");
145
146 // Add id, key, and cert
147 this->pib.addCertificate(this->id1Key1Cert1);
148 BOOST_CHECK(this->pib.hasIdentity(this->id1));
149 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
150 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
151
152 // Clear identities
153 this->pib.clearIdentities();
154 BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0);
155 BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0);
156 BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0);
157 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
158}
159
160BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T)
161{
162 // no default setting, throw Error
163 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700164
165 // check id1Key1, should not exist, neither should id1.
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700166 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
167 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700168
169 // add id1Key1, should be default, id1 should be added implicitly
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400170 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700171 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
172 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
173 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174 BOOST_CHECK(keyBits == this->id1Key1);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700175 BOOST_CHECK_NO_THROW(this->pib.getDefaultKeyOfIdentity(this->id1));
176 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700177
178 // add id1Key2, should not be default
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400179 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700180 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
181 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700182
183 // set id1Key2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700184 this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
185 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700186
187 // set a non-existing key as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700188 BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700189 Pib::Error);
190
191 // remove id1Key2, should not have default key
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700192 this->pib.removeKey(this->id1Key2Name);
193 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false);
194 BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error);
195 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700196
197 // add id1Key2 back, should be default
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400198 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700199 BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
200 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700201
202 // get all the keys: id1Key1 and id1Key2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700203 std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700204 BOOST_CHECK_EQUAL(keyNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700205 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
206 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700207
208 // remove id1, should remove all the keys
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700209 this->pib.removeIdentity(this->id1);
210 keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700211 BOOST_CHECK_EQUAL(keyNames.size(), 0);
212}
213
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700214BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700215{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700216 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700217 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700218
219 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700220 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
221 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
222 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700223
224 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700225 this->pib.addCertificate(this->id1Key1Cert1);
226 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
227 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
228 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000229 BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(),
230 this->id1Key1Cert1.wireEncode());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700231 BOOST_CHECK_NO_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name));
232 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700233
234 // add id1Key1Cert2, should not be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700235 this->pib.addCertificate(this->id1Key1Cert2);
236 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true);
237 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700238
239 // set id1Key1Cert2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700240 this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
241 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700242
243 // set a non-existing cert as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700244 BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700245 Pib::Error);
246
247 // remove id1Key1Cert2, should not have default cert
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700248 this->pib.removeCertificate(this->id1Key1Cert2.getName());
249 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false);
250 BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error);
251 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700252
253 // add id1Key1Cert2, should be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700254 this->pib.addCertificate(this->id1Key1Cert2);
255 BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName()));
256 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700257
258 // get all certificates: id1Key1Cert1 and id1Key1Cert2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700259 std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700260 BOOST_CHECK_EQUAL(certNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700261 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
262 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700263
264 // remove id1Key1, should remove all the certs
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700265 this->pib.removeKey(this->id1Key1Name);
266 certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700267 BOOST_CHECK_EQUAL(certNames.size(), 0);
268}
269
Yingdi Yu03997682015-11-23 16:41:38 -0800270BOOST_FIXTURE_TEST_CASE_TEMPLATE(DefaultsManagement, T, PibImpls, T)
271{
272 this->pib.addIdentity(this->id1);
273 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
274
275 this->pib.addIdentity(this->id2);
276 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
277
278 this->pib.removeIdentity(this->id1);
279 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
280
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400281 this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1.data(), this->id2Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800282 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
283 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
284
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400285 this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2.data(), this->id2Key2.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800286 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
287
288 this->pib.removeKey(this->id2Key1Name);
289 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id2), Pib::Error);
290
291 this->pib.addCertificate(this->id2Key2Cert1);
292 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key2Name);
Davide Pesavento77d9e812019-06-03 22:05:54 -0400293 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
294 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800295
296 this->pib.addCertificate(this->id2Key2Cert2);
Davide Pesavento77d9e812019-06-03 22:05:54 -0400297 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
298 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800299
300 this->pib.removeCertificate(this->id2Key2Cert2.getName());
Davide Pesavento77d9e812019-06-03 22:05:54 -0400301 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(),
302 this->id2Key2Cert1.getName());
Yingdi Yu03997682015-11-23 16:41:38 -0800303}
304
305BOOST_FIXTURE_TEST_CASE_TEMPLATE(Overwrite, T, PibImpls, T)
306{
307 // check id1Key1, should not exist
308 this->pib.removeIdentity(this->id1);
309 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
310
311 // add id1Key1
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400312 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800313 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
314 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
315 BOOST_CHECK(keyBits == this->id1Key1);
316
317 // check overwrite, add a key with the same name.
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400318 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800319 const Buffer& keyBits2 = this->pib.getKeyBits(this->id1Key1Name);
320 BOOST_CHECK(keyBits2 == this->id1Key2);
321
322 // check id1Key1Cert1, should not exist
323 this->pib.removeIdentity(this->id1);
324 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
325
326 // add id1Key1Cert1
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400327 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800328 this->pib.addCertificate(this->id1Key1Cert1);
329 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
330
331 auto cert = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000332 BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800333
334 // Create a fake cert with the same name
335 auto cert2 = this->id1Key2Cert1;
336 cert2.setName(this->id1Key1Cert1.getName());
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400337 BOOST_CHECK_EQUAL(cert2.getSignatureInfo(), this->id1Key2Cert1.getSignatureInfo());
338 BOOST_CHECK_EQUAL(cert2.getSignatureValue(), this->id1Key2Cert1.getSignatureValue());
Yingdi Yu03997682015-11-23 16:41:38 -0800339 this->pib.addCertificate(cert2);
340
341 auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000342 BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800343
344 // both key and certificate are overwritten
345 Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name);
346 BOOST_CHECK(keyBits3 == this->id1Key2);
347}
348
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100349BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700350BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100351BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700352
353} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700354} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700355} // namespace security
356} // namespace ndn