blob: 6094b0edb3c57b77d07e4439c454581df3482ebe [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/*
Junxiao Shi72c0c642018-04-20 15:41:09 +00003 * Copyright (c) 2013-2018 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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#include "security/pib/pib-memory.hpp"
23#include "security/pib/pib-sqlite3.hpp"
24#include "security/pib/pib.hpp"
Yingdi Yu6ee2d362015-07-16 21:48:05 -070025#include "security/security-common.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026
27#include "boost-test.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070028#include "pib-data-fixture.hpp"
29
Mickey Sweatt11314b72015-06-10 17:20:19 -070030#include <boost/filesystem.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070031#include <boost/mpl/list.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
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070071typedef boost::mpl::list<PibMemoryFixture,
72 PibSqlite3Fixture> PibImpls;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070073
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070074BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070075{
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070076 // Basic getting and setting
77 BOOST_CHECK_NO_THROW(this->pib.getTpmLocator());
Yingdi Yu3bf91f52015-06-12 19:39:40 -070078
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070079 BOOST_CHECK_NO_THROW(this->pib.setTpmLocator("tpmLocator"));
80 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
Yingdi Yu3bf91f52015-06-12 19:39:40 -070081
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070082 // Add cert, and do not change TPM locator
83 this->pib.addCertificate(this->id1Key1Cert1);
84 BOOST_CHECK(this->pib.hasIdentity(this->id1));
85 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
86 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070087
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070088 // Set TPM locator to the same value, nothing should change
89 this->pib.setTpmLocator("tpmLocator");
90 BOOST_CHECK(this->pib.hasIdentity(this->id1));
91 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
92 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070093
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070094 // Change TPM locator (contents of PIB should not change)
95 this->pib.setTpmLocator("newTpmLocator");
96 BOOST_CHECK(this->pib.hasIdentity(this->id1));
97 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
98 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
Yingdi Yu3bf91f52015-06-12 19:39:40 -070099}
100
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700101BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700102{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700103 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700104 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
105
106 // check id1, which should not exist
107 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
108
109 // add id1, should be default
110 this->pib.addIdentity(this->id1);
111 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
112 BOOST_CHECK_NO_THROW(this->pib.getDefaultIdentity());
113 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
114
115 // add id2, should not be default
116 this->pib.addIdentity(this->id2);
117 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true);
118 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
119
120 // set id2 explicitly as default
121 this->pib.setDefaultIdentity(this->id2);
122 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
123
124 // remove id2, should not have default identity
125 this->pib.removeIdentity(this->id2);
126 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false);
127 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
128
129 // add id2 again, should be default
130 this->pib.addIdentity(this->id2);
131 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
132
133 // get all identities, should contain id1 and id2
134 std::set<Name> idNames = this->pib.getIdentities();
135 BOOST_CHECK_EQUAL(idNames.size(), 2);
136 BOOST_CHECK_EQUAL(idNames.count(this->id1), 1);
137 BOOST_CHECK_EQUAL(idNames.count(this->id2), 1);
138}
139
140BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T)
141{
142 this->pib.setTpmLocator("tpmLocator");
143
144 // Add id, key, and cert
145 this->pib.addCertificate(this->id1Key1Cert1);
146 BOOST_CHECK(this->pib.hasIdentity(this->id1));
147 BOOST_CHECK(this->pib.hasKey(this->id1Key1Name));
148 BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName()));
149
150 // Clear identities
151 this->pib.clearIdentities();
152 BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0);
153 BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0);
154 BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0);
155 BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator");
156}
157
158BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T)
159{
160 // no default setting, throw Error
161 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700162
163 // check id1Key1, should not exist, neither should id1.
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700164 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
165 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700166
167 // add id1Key1, should be default, id1 should be added implicitly
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400168 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700169 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
170 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
171 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800172 BOOST_CHECK(keyBits == this->id1Key1);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700173 BOOST_CHECK_NO_THROW(this->pib.getDefaultKeyOfIdentity(this->id1));
174 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700175
176 // add id1Key2, should not be default
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400177 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700178 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true);
179 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700180
181 // set id1Key2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700182 this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name);
183 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700184
185 // set a non-existing key as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700186 BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700187 Pib::Error);
188
189 // remove id1Key2, should not have default key
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700190 this->pib.removeKey(this->id1Key2Name);
191 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false);
192 BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error);
193 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700194
195 // add id1Key2 back, should be default
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400196 this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700197 BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name));
198 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700199
200 // get all the keys: id1Key1 and id1Key2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700201 std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700202 BOOST_CHECK_EQUAL(keyNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700203 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1);
204 BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700205
206 // remove id1, should remove all the keys
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700207 this->pib.removeIdentity(this->id1);
208 keyNames = this->pib.getKeysOfIdentity(this->id1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700209 BOOST_CHECK_EQUAL(keyNames.size(), 0);
210}
211
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700212BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T)
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700213{
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700214 // no default setting, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700215 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700216
217 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700218 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
219 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false);
220 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700221
222 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700223 this->pib.addCertificate(this->id1Key1Cert1);
224 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
225 BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true);
226 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000227 BOOST_CHECK_EQUAL(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode(),
228 this->id1Key1Cert1.wireEncode());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700229 BOOST_CHECK_NO_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name));
230 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700231
232 // add id1Key1Cert2, should not be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700233 this->pib.addCertificate(this->id1Key1Cert2);
234 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true);
235 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700236
237 // set id1Key1Cert2 explicitly as default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700238 this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName());
239 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700240
241 // set a non-existing cert as default, throw Error
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700242 BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")),
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700243 Pib::Error);
244
245 // remove id1Key1Cert2, should not have default cert
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700246 this->pib.removeCertificate(this->id1Key1Cert2.getName());
247 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false);
248 BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error);
249 BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700250
251 // add id1Key1Cert2, should be default
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700252 this->pib.addCertificate(this->id1Key1Cert2);
253 BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName()));
254 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700255
256 // get all certificates: id1Key1Cert1 and id1Key1Cert2
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700257 std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700258 BOOST_CHECK_EQUAL(certNames.size(), 2);
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700259 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1);
260 BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700261
262 // remove id1Key1, should remove all the certs
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700263 this->pib.removeKey(this->id1Key1Name);
264 certNames = this->pib.getCertificatesOfKey(this->id1Key1Name);
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700265 BOOST_CHECK_EQUAL(certNames.size(), 0);
266}
267
Yingdi Yu03997682015-11-23 16:41:38 -0800268BOOST_FIXTURE_TEST_CASE_TEMPLATE(DefaultsManagement, T, PibImpls, T)
269{
270 this->pib.addIdentity(this->id1);
271 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
272
273 this->pib.addIdentity(this->id2);
274 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1);
275
276 this->pib.removeIdentity(this->id1);
277 BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error);
278
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400279 this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1.data(), this->id2Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800280 BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2);
281 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
282
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400283 this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2.data(), this->id2Key2.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800284 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name);
285
286 this->pib.removeKey(this->id2Key1Name);
287 BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id2), Pib::Error);
288
289 this->pib.addCertificate(this->id2Key2Cert1);
290 BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key2Name);
291 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName());
292
293 this->pib.addCertificate(this->id2Key2Cert2);
294 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName());
295
296 this->pib.removeCertificate(this->id2Key2Cert2.getName());
297 BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName());
298}
299
300BOOST_FIXTURE_TEST_CASE_TEMPLATE(Overwrite, T, PibImpls, T)
301{
302 // check id1Key1, should not exist
303 this->pib.removeIdentity(this->id1);
304 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false);
305
306 // add id1Key1
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400307 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800308 BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true);
309 const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name);
310 BOOST_CHECK(keyBits == this->id1Key1);
311
312 // check overwrite, add a key with the same name.
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400313 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2.data(), this->id1Key2.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800314 const Buffer& keyBits2 = this->pib.getKeyBits(this->id1Key1Name);
315 BOOST_CHECK(keyBits2 == this->id1Key2);
316
317 // check id1Key1Cert1, should not exist
318 this->pib.removeIdentity(this->id1);
319 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false);
320
321 // add id1Key1Cert1
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400322 this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size());
Yingdi Yu03997682015-11-23 16:41:38 -0800323 this->pib.addCertificate(this->id1Key1Cert1);
324 BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true);
325
326 auto cert = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000327 BOOST_CHECK_EQUAL(cert.wireEncode(), this->id1Key1Cert1.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800328
329 // Create a fake cert with the same name
330 auto cert2 = this->id1Key2Cert1;
331 cert2.setName(this->id1Key1Cert1.getName());
332 cert2.setSignature(this->id1Key2Cert1.getSignature());
333 this->pib.addCertificate(cert2);
334
335 auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName());
Junxiao Shi72c0c642018-04-20 15:41:09 +0000336 BOOST_CHECK_EQUAL(cert3.wireEncode(), cert2.wireEncode());
Yingdi Yu03997682015-11-23 16:41:38 -0800337
338 // both key and certificate are overwritten
339 Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name);
340 BOOST_CHECK(keyBits3 == this->id1Key2);
341}
342
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100343BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700344BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100345BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700346
347} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700348} // namespace pib
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700349} // namespace security
350} // namespace ndn