blob: 8c9b9205a4ba60c681a1db2356fcd59f05af3fa7 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
Yingdi Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Yingdi Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Yingdi Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
22#include "tools/pib/pib.hpp"
Yingdi Yu77627ab2015-07-21 16:13:49 -070023#include "tools/pib/encoding/pib-encoding.hpp"
Davide Pesavento013de9b2016-09-01 12:06:56 +000024
25#include "tests/identity-management-fixture.hpp"
26
27#include <ndn-cxx/security/sec-tpm-file.hpp>
Yingdi Yu77627ab2015-07-21 16:13:49 -070028#include <ndn-cxx/util/io.hpp>
29#include <ndn-cxx/util/dummy-client-face.hpp>
Yingdi Yu77627ab2015-07-21 16:13:49 -070030#include <boost/filesystem.hpp>
31
Yingdi Yu77627ab2015-07-21 16:13:49 -070032namespace ndn {
33namespace pib {
34namespace tests {
35
Yingdi Yu0a312e52015-07-22 13:14:53 -070036class PibTestFixture : public ndn::tests::IdentityManagementTimeFixture
Yingdi Yu77627ab2015-07-21 16:13:49 -070037{
38public:
39 PibTestFixture()
40 : tmpPath(boost::filesystem::path(TMP_TESTS_PATH) / "PibTest")
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000041 , face(io, m_keyChain, {true, true})
Yingdi Yu77627ab2015-07-21 16:13:49 -070042 {
43 }
44
45 ~PibTestFixture()
46 {
47 boost::filesystem::remove_all(tmpPath);
48 }
49
50 template<class Param>
51 shared_ptr<Interest>
52 generateUnsignedInterest(Param& param, const std::string& user)
53 {
54 Name command("/localhost/pib");
55 command.append(user).append(Param::VERB).append(param.wireEncode());
56 shared_ptr<Interest> interest = make_shared<Interest>(command);
57
58 return interest;
59 }
60
61 template<class Param>
62 shared_ptr<Interest>
63 generateSignedInterest(Param& param, const std::string& user, const Name& certName)
64 {
65 shared_ptr<Interest> interest = generateUnsignedInterest(param, user);
66 m_keyChain.sign(*interest, certName);
67
68 return interest;
69 }
70
71 boost::asio::io_service io;
72 std::string owner;
73 boost::filesystem::path tmpPath;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000074 util::DummyClientFace face;
Yingdi Yu77627ab2015-07-21 16:13:49 -070075};
76
Davide Pesavento013de9b2016-09-01 12:06:56 +000077BOOST_AUTO_TEST_SUITE(Pib)
78BOOST_FIXTURE_TEST_SUITE(TestPib, PibTestFixture)
79
80using ndn::pib::Pib;
Yingdi Yu77627ab2015-07-21 16:13:49 -070081
82BOOST_AUTO_TEST_CASE(InitCertTest1)
83{
84 // Create a PIB with full parameters
85 owner = "testUser";
86
Junxiao Shiaa1b3c92016-07-14 14:56:53 +000087 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -070088 tmpPath.string(),
89 m_keyChain.getTpm().getTpmLocator(),
90 owner);
91 advanceClocks(io, time::milliseconds(10), 10);
92
93 BOOST_CHECK_EQUAL(pib.getOwner(), owner);
94 BOOST_CHECK_EQUAL(pib.getDb().getOwnerName(), owner);
95
96 auto mgmtCert = pib.getMgmtCert();
97 BOOST_CHECK_EQUAL(mgmtCert.getName().getPrefix(-3),
98 Name("/localhost/pib/testUser/mgmt/KEY"));
99 BOOST_CHECK_EQUAL(mgmtCert.getName().get(5).toUri().substr(0, 4), "dsk-");
100
101 auto mgmtCert2 = pib.getDb().getMgmtCertificate();
102 BOOST_REQUIRE(mgmtCert2 != nullptr);
103 BOOST_CHECK(mgmtCert.wireEncode() == mgmtCert2->wireEncode());
104
105 BOOST_CHECK_EQUAL(pib.getDb().getTpmLocator(), m_keyChain.getTpm().getTpmLocator());
106
107 GetParam param01;
108 shared_ptr<Interest> interest01 = generateUnsignedInterest(param01, owner);
109
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000110 face.receive(*interest01);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700111 advanceClocks(io, time::milliseconds(10), 10);
112
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000113 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700114 PibUser result01;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000115 BOOST_REQUIRE_NO_THROW(result01.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700116 BOOST_CHECK(result01.getMgmtCert().wireEncode() == mgmtCert.wireEncode());
117 BOOST_CHECK_EQUAL(result01.getTpmLocator(), m_keyChain.getTpm().getTpmLocator());
118}
119
120BOOST_AUTO_TEST_CASE(InitCertTest2)
121{
122 // Create a PIB from a database (assume that the database is configured)
123 std::string dbDir = tmpPath.string();
124 std::string tpmLocator = m_keyChain.getTpm().getTpmLocator();
125 owner = "testUser";
126
127 Name testUser("/localhost/pib/testUser/mgmt");
128
129 addIdentity(testUser);
130 Name testUserCertName = m_keyChain.getDefaultCertificateNameForIdentity(testUser);
131 shared_ptr<IdentityCertificate> testUserCert = m_keyChain.getCertificate(testUserCertName);
132
133 PibDb db(tmpPath.string());
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000134 BOOST_CHECK_NO_THROW(Pib(face, dbDir, tpmLocator, owner));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700135
136 db.updateMgmtCertificate(*testUserCert);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000137 BOOST_CHECK_NO_THROW(Pib(face, dbDir, tpmLocator, owner));
138 BOOST_CHECK_THROW(Pib(face, dbDir, tpmLocator, "wrongUser"), Pib::Error);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700139
140 db.setTpmLocator(m_keyChain.getTpm().getTpmLocator());
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000141 BOOST_CHECK_NO_THROW(Pib(face, dbDir, tpmLocator, owner));
142 BOOST_CHECK_THROW(Pib(face, dbDir, "tpm-file:wrong", owner), Pib::Error);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700143
144 advanceClocks(io, time::milliseconds(10));
145 m_keyChain.deleteIdentity(testUser);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000146 BOOST_CHECK_NO_THROW(Pib(face, dbDir, tpmLocator, owner));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700147}
148
149BOOST_AUTO_TEST_CASE(InitCertTest3)
150{
151 std::string dbDir = tmpPath.string();
152 std::string tpmLocator = m_keyChain.getTpm().getTpmLocator();
153 owner = "testUser";
154
155 Name testUser("/localhost/pib/testUser/mgmt");
156 addIdentity(testUser);
157 Name testUserCertName = m_keyChain.getDefaultCertificateNameForIdentity(testUser);
158 shared_ptr<IdentityCertificate> testUserCert = m_keyChain.getCertificate(testUserCertName);
159
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000160 Pib pib1(face, dbDir, tpmLocator, owner);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700161 BOOST_CHECK_EQUAL(pib1.getMgmtCert().getName().getPrefix(-3),
162 Name("/localhost/pib/testUser/mgmt/KEY"));
163
164 PibDb db(tmpPath.string());
165 db.updateMgmtCertificate(*testUserCert);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000166 Pib pib2(face, dbDir, tpmLocator, owner);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700167 BOOST_CHECK_EQUAL(pib2.getMgmtCert().getName(), testUserCertName);
168
169 advanceClocks(io, time::milliseconds(10));
170 m_keyChain.deleteIdentity(testUser);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000171 Pib pib3(face, dbDir, tpmLocator, owner);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700172 BOOST_CHECK(pib3.getMgmtCert().getName() != testUserCertName);
173 BOOST_CHECK_EQUAL(pib3.getMgmtCert().getName().getPrefix(-3),
174 Name("/localhost/pib/testUser/mgmt/KEY"));
175}
176
177BOOST_AUTO_TEST_CASE(GetCommandTest)
178{
179 owner = "alice";
180
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000181 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700182 tmpPath.string(),
183 m_keyChain.getTpm().getTpmLocator(),
184 owner);
185 advanceClocks(io, time::milliseconds(10), 10);
186 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
187 auto ownerMgmtCert = pib.getMgmtCert();
188 m_keyChain.addCertificate(ownerMgmtCert);
189
190 PibDb db(tmpPath.string());
191
192 Name testId("/test/identity");
193 Name testIdCertName00 = m_keyChain.createIdentity(testId);
194 shared_ptr<IdentityCertificate> cert00 = m_keyChain.getCertificate(testIdCertName00);
195 Name testIdKeyName0 = cert00->getPublicKeyName();
196 advanceClocks(io, time::milliseconds(100));
197 shared_ptr<IdentityCertificate> cert01 = m_keyChain.selfSign(testIdKeyName0);
198 Name testIdCertName01 = cert01->getName();
199
200 advanceClocks(io, time::milliseconds(100));
201 Name testIdKeyName1 = m_keyChain.generateRsaKeyPair(testId);
202 shared_ptr<IdentityCertificate> cert10 = m_keyChain.selfSign(testIdKeyName1);
203 Name testIdCertName10 = cert10->getName();
204 advanceClocks(io, time::milliseconds(100));
205 shared_ptr<IdentityCertificate> cert11 = m_keyChain.selfSign(testIdKeyName1);
206 Name testIdCertName11 = cert11->getName();
207
208 BOOST_CHECK_EQUAL(db.hasIdentity(testId), false);
209 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), false);
210 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), false);
211 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), false);
212 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), false);
213 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), false);
214 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), false);
215
216 db.addCertificate(*cert00);
217 db.addCertificate(*cert01);
218 db.addCertificate(*cert10);
219 db.addCertificate(*cert11);
220 db.setDefaultIdentity(testId);
221 db.setDefaultKeyNameOfIdentity(testIdKeyName0);
222 db.setDefaultCertNameOfKey(testIdCertName00);
223
224 BOOST_CHECK_EQUAL(db.hasIdentity(testId), true);
225 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), true);
226 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), true);
227 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), true);
228 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), true);
229 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), true);
230 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), true);
231
232 // Get Param
233 GetParam param01;
234 shared_ptr<Interest> interest01 = generateUnsignedInterest(param01, owner);
235
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000236 face.sentData.clear();
237 face.receive(*interest01);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700238 advanceClocks(io, time::milliseconds(10), 10);
239
240 BOOST_REQUIRE(cache.find(interest01->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000241 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700242 PibUser result01;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000243 BOOST_REQUIRE_NO_THROW(result01.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700244 BOOST_CHECK(result01.getMgmtCert().wireEncode() == ownerMgmtCert.wireEncode());
245
246
247 GetParam param02;
248 shared_ptr<Interest> interest02 = generateUnsignedInterest(param02, "non-existing");
249
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000250 face.sentData.clear();
251 face.receive(*interest02);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700252 advanceClocks(io, time::milliseconds(10), 10);
253
254 BOOST_CHECK(cache.find(interest02->getName()) == nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000255 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700256
257
258 GetParam param03(TYPE_ID, testId);
259 shared_ptr<Interest> interest03 = generateUnsignedInterest(param03, owner);
260
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000261 face.sentData.clear();
262 face.receive(*interest03);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700263 advanceClocks(io, time::milliseconds(10), 10);
264
265 BOOST_REQUIRE(cache.find(interest03->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000266 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700267 PibIdentity result03;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000268 BOOST_REQUIRE_NO_THROW(result03.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700269 BOOST_CHECK_EQUAL(result03.getIdentity(), testId);
270
271
272 Name wrongId("/wrong/id");
273 GetParam param04(TYPE_ID, wrongId);
274 shared_ptr<Interest> interest04 = generateUnsignedInterest(param04, owner);
275
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000276 face.sentData.clear();
277 face.receive(*interest04);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700278 advanceClocks(io, time::milliseconds(10), 10);
279
280 BOOST_REQUIRE(cache.find(interest04->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000281 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700282 PibError result04;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000283 BOOST_REQUIRE_NO_THROW(result04.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700284 BOOST_CHECK_EQUAL(result04.getErrorCode(), ERR_NON_EXISTING_ID);
285
286
287 GetParam param05(TYPE_KEY, testIdKeyName1);
288 shared_ptr<Interest> interest05 = generateUnsignedInterest(param05, owner);
289
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000290 face.sentData.clear();
291 face.receive(*interest05);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700292 advanceClocks(io, time::milliseconds(10), 10);
293
294 BOOST_REQUIRE(cache.find(interest05->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000295 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700296 PibPublicKey result05;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000297 BOOST_REQUIRE_NO_THROW(result05.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700298 BOOST_CHECK_EQUAL(result05.getKeyName(), testIdKeyName1);
299
300
301 Name wrongKeyName1("/wrong/key/name1");
302 GetParam param06(TYPE_KEY, wrongKeyName1);
303 shared_ptr<Interest> interest06 = generateUnsignedInterest(param06, owner);
304
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000305 face.sentData.clear();
306 face.receive(*interest06);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700307 advanceClocks(io, time::milliseconds(10), 10);
308
309 BOOST_REQUIRE(cache.find(interest06->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000310 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700311 PibError result06;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000312 BOOST_REQUIRE_NO_THROW(result06.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700313 BOOST_CHECK_EQUAL(result06.getErrorCode(), ERR_NON_EXISTING_KEY);
314
315
316 GetParam param07(TYPE_CERT, testIdCertName00);
317 shared_ptr<Interest> interest07 = generateUnsignedInterest(param07, owner);
318
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000319 face.sentData.clear();
320 face.receive(*interest07);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700321 advanceClocks(io, time::milliseconds(10), 10);
322
323 BOOST_REQUIRE(cache.find(interest07->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000324 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700325 PibCertificate result07;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000326 BOOST_REQUIRE_NO_THROW(result07.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700327 BOOST_CHECK_EQUAL(result07.getCertificate().getName(), testIdCertName00);
328
329
330 Name wrongCertName1("/wrong/cert/name1");
331 GetParam param08(TYPE_CERT, wrongCertName1);
332 shared_ptr<Interest> interest08 = generateUnsignedInterest(param08, owner);
333
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000334 face.sentData.clear();
335 face.receive(*interest08);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700336 advanceClocks(io, time::milliseconds(10), 10);
337
338 BOOST_REQUIRE(cache.find(interest08->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000339 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700340 PibError result08;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000341 BOOST_REQUIRE_NO_THROW(result08.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700342 BOOST_CHECK_EQUAL(result08.getErrorCode(), ERR_NON_EXISTING_CERT);
343
344
345 Name wrongKeyName2;
346 GetParam param09(TYPE_KEY, wrongKeyName2);
347 shared_ptr<Interest> interest09 = generateUnsignedInterest(param09, owner);
348
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000349 face.sentData.clear();
350 face.receive(*interest09);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700351 advanceClocks(io, time::milliseconds(10), 10);
352
353 BOOST_REQUIRE(cache.find(interest09->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000354 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700355 PibError result09;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000356 BOOST_REQUIRE_NO_THROW(result09.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700357 BOOST_CHECK_EQUAL(result09.getErrorCode(), ERR_WRONG_PARAM);
358}
359
360BOOST_AUTO_TEST_CASE(DefaultCommandTest)
361{
362 owner = "alice";
363
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000364 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700365 tmpPath.string(),
366 m_keyChain.getTpm().getTpmLocator(),
367 owner);
368 advanceClocks(io, time::milliseconds(10), 10);
369 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
370 auto ownerMgmtCert = pib.getMgmtCert();
371 m_keyChain.addCertificate(ownerMgmtCert);
372
373 PibDb db(tmpPath.string());
374
375 Name testId("/test/identity");
376 Name testIdCertName00 = m_keyChain.createIdentity(testId);
377 shared_ptr<IdentityCertificate> cert00 = m_keyChain.getCertificate(testIdCertName00);
378 Name testIdKeyName0 = cert00->getPublicKeyName();
379 advanceClocks(io, time::milliseconds(100));
380 shared_ptr<IdentityCertificate> cert01 = m_keyChain.selfSign(testIdKeyName0);
381 Name testIdCertName01 = cert01->getName();
382
383 advanceClocks(io, time::milliseconds(100));
384 Name testIdKeyName1 = m_keyChain.generateRsaKeyPair(testId);
385 shared_ptr<IdentityCertificate> cert10 = m_keyChain.selfSign(testIdKeyName1);
386 Name testIdCertName10 = cert10->getName();
387 advanceClocks(io, time::milliseconds(100));
388 shared_ptr<IdentityCertificate> cert11 = m_keyChain.selfSign(testIdKeyName1);
389 Name testIdCertName11 = cert11->getName();
390
391 BOOST_CHECK_EQUAL(db.hasIdentity(testId), false);
392 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), false);
393 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), false);
394 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), false);
395 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), false);
396 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), false);
397 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), false);
398
399 db.addCertificate(*cert00);
400 db.addCertificate(*cert01);
401 db.addCertificate(*cert10);
402 db.addCertificate(*cert11);
403 db.setDefaultIdentity(testId);
404 db.setDefaultKeyNameOfIdentity(testIdKeyName0);
405 db.setDefaultCertNameOfKey(testIdCertName00);
406
407 BOOST_CHECK_EQUAL(db.hasIdentity(testId), true);
408 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), true);
409 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), true);
410 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), true);
411 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), true);
412 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), true);
413 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), true);
414
415 // Default Param
416 DefaultParam param11(TYPE_ID, TYPE_USER);
417 shared_ptr<Interest> interest11 = generateUnsignedInterest(param11, owner);
418
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000419 face.sentData.clear();
420 face.receive(*interest11);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700421 advanceClocks(io, time::milliseconds(10), 10);
422
423 BOOST_REQUIRE(cache.find(interest11->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000424 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700425 PibIdentity result11;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000426 BOOST_REQUIRE_NO_THROW(result11.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700427 BOOST_CHECK_EQUAL(result11.getIdentity(), testId);
428
429
430 DefaultParam param13(TYPE_ID, TYPE_ID);
431 shared_ptr<Interest> interest13 = generateUnsignedInterest(param13, owner);
432
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000433 face.sentData.clear();
434 face.receive(*interest13);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700435 advanceClocks(io, time::milliseconds(10), 10);
436
437 BOOST_REQUIRE(cache.find(interest13->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000438 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700439 PibError result13;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000440 BOOST_REQUIRE_NO_THROW(result13.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700441 BOOST_CHECK_EQUAL(result13.getErrorCode(), ERR_WRONG_PARAM);
442
443
444 DefaultParam param14(TYPE_KEY, TYPE_ID, testId);
445 shared_ptr<Interest> interest14 = generateUnsignedInterest(param14, owner);
446
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000447 face.sentData.clear();
448 face.receive(*interest14);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700449 advanceClocks(io, time::milliseconds(10), 10);
450
451 BOOST_REQUIRE(cache.find(interest14->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000452 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700453 PibPublicKey result14;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000454 BOOST_REQUIRE_NO_THROW(result14.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700455 BOOST_CHECK_EQUAL(result14.getKeyName(), testIdKeyName0);
456
457
458 DefaultParam param15(TYPE_CERT, TYPE_ID, testId);
459 shared_ptr<Interest> interest15 = generateUnsignedInterest(param15, owner);
460
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000461 face.sentData.clear();
462 face.receive(*interest15);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700463 advanceClocks(io, time::milliseconds(10), 10);
464
465 BOOST_REQUIRE(cache.find(interest15->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000466 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700467 PibCertificate result15;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000468 BOOST_REQUIRE_NO_THROW(result15.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700469 BOOST_CHECK_EQUAL(result15.getCertificate().getName(), testIdCertName00);
470
471
472 DefaultParam param16(TYPE_CERT, TYPE_USER);
473 shared_ptr<Interest> interest16 = generateUnsignedInterest(param16, owner);
474
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000475 face.sentData.clear();
476 face.receive(*interest16);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700477 advanceClocks(io, time::milliseconds(10), 10);
478
479 BOOST_REQUIRE(cache.find(interest16->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000480 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700481 PibCertificate result16;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000482 BOOST_REQUIRE_NO_THROW(result16.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700483 BOOST_CHECK_EQUAL(result16.getCertificate().getName(), testIdCertName00);
484
485
486 DefaultParam param17(TYPE_CERT, TYPE_KEY, testIdKeyName1);
487 shared_ptr<Interest> interest17 = generateUnsignedInterest(param17, owner);
488
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000489 face.sentData.clear();
490 face.receive(*interest17);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700491 advanceClocks(io, time::milliseconds(10), 10);
492
493 BOOST_REQUIRE(cache.find(interest17->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000494 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700495 PibCertificate result17;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000496 BOOST_REQUIRE_NO_THROW(result17.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700497 BOOST_CHECK_EQUAL(result17.getCertificate().getName(), testIdCertName10);
498}
499
500BOOST_AUTO_TEST_CASE(ListCommandTest)
501{
502 owner = "alice";
503
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000504 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700505 tmpPath.string(),
506 m_keyChain.getTpm().getTpmLocator(),
507 owner);
508 advanceClocks(io, time::milliseconds(10), 10);
509 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
510 auto ownerMgmtCert = pib.getMgmtCert();
511 m_keyChain.addCertificate(ownerMgmtCert);
512
513 PibDb db(tmpPath.string());
514
515 Name testId("/test/identity");
516 Name testIdCertName00 = m_keyChain.createIdentity(testId);
517 shared_ptr<IdentityCertificate> cert00 = m_keyChain.getCertificate(testIdCertName00);
518 Name testIdKeyName0 = cert00->getPublicKeyName();
519 advanceClocks(io, time::milliseconds(100));
520 shared_ptr<IdentityCertificate> cert01 = m_keyChain.selfSign(testIdKeyName0);
521 Name testIdCertName01 = cert01->getName();
522
523 advanceClocks(io, time::milliseconds(100));
524 Name testIdKeyName1 = m_keyChain.generateRsaKeyPair(testId);
525 shared_ptr<IdentityCertificate> cert10 = m_keyChain.selfSign(testIdKeyName1);
526 Name testIdCertName10 = cert10->getName();
527 advanceClocks(io, time::milliseconds(100));
528 shared_ptr<IdentityCertificate> cert11 = m_keyChain.selfSign(testIdKeyName1);
529 Name testIdCertName11 = cert11->getName();
530
531 BOOST_CHECK_EQUAL(db.hasIdentity(testId), false);
532 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), false);
533 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), false);
534 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), false);
535 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), false);
536 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), false);
537 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), false);
538
539 db.addCertificate(*cert00);
540 db.addCertificate(*cert01);
541 db.addCertificate(*cert10);
542 db.addCertificate(*cert11);
543 db.setDefaultIdentity(testId);
544 db.setDefaultKeyNameOfIdentity(testIdKeyName0);
545 db.setDefaultCertNameOfKey(testIdCertName00);
546
547 BOOST_CHECK_EQUAL(db.hasIdentity(testId), true);
548 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName0), true);
549 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName00), true);
550 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName01), true);
551 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), true);
552 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName10), true);
553 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), true);
554
555 Name wrongId("/wrong/id");
556
557 // List Param
558 ListParam param21;
559 shared_ptr<Interest> interest21 = generateUnsignedInterest(param21, owner);
560
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000561 face.sentData.clear();
562 face.receive(*interest21);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700563 advanceClocks(io, time::milliseconds(10), 10);
564
565 BOOST_REQUIRE(cache.find(interest21->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000566 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700567 PibNameList result21;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000568 BOOST_REQUIRE_NO_THROW(result21.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700569 BOOST_CHECK_EQUAL(result21.getNameList().size(), 1);
570
571
572 ListParam param22(TYPE_ID, testId);
573 shared_ptr<Interest> interest22 = generateUnsignedInterest(param22, owner);
574
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000575 face.sentData.clear();
576 face.receive(*interest22);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700577 advanceClocks(io, time::milliseconds(10), 10);
578
579 BOOST_REQUIRE(cache.find(interest22->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000580 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700581 PibNameList result22;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000582 BOOST_REQUIRE_NO_THROW(result22.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700583 BOOST_CHECK_EQUAL(result22.getNameList().size(), 2);
584
585
586 ListParam param23(TYPE_ID, wrongId);
587 shared_ptr<Interest> interest23 = generateUnsignedInterest(param23, owner);
588
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000589 face.sentData.clear();
590 face.receive(*interest23);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700591 advanceClocks(io, time::milliseconds(10), 10);
592
593 BOOST_REQUIRE(cache.find(interest23->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000594 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700595 PibNameList result23;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000596 BOOST_REQUIRE_NO_THROW(result23.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700597 BOOST_CHECK_EQUAL(result23.getNameList().size(), 0);
598}
599
600BOOST_AUTO_TEST_CASE(IsUpdateAllowedTest1)
601{
602 // This test case is to check the access control of local management key
603 owner = "alice";
604
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000605 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700606 tmpPath.string(),
607 m_keyChain.getTpm().getTpmLocator(),
608 owner);
609
610 UpdateQueryProcessor& pro = pib.m_updateProcessor;
611
612 Name target01("/localhost/pib");
613 Name target02("/localhost/pib/alice/mgmt");
614 Name target03("/localhost/pib/alice/mgmt/ok");
615 Name target04("/localhost/pib/alice");
616 Name target05("/test/id");
617 Name target06("/test/id/ksk-123");
618 Name target07("/test/id/KEY/ksk-123/ID-CERT/version");
619 Name signer01 = pib.getMgmtCert().getName().getPrefix(-1);
620 Name signer02("/localhost/pib/bob/mgmt/KEY/ksk-1234/ID-CERT");
621
622 // TYPE_USER is handled separately, isUpdatedAllowed simply returns false
623 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_USER, target02, signer01, DEFAULT_OPT_NO), false);
624
625 // Test access control of local management key
626 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target01, signer01, DEFAULT_OPT_NO), false);
627 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target02, signer01, DEFAULT_OPT_NO), false);
628 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target03, signer01, DEFAULT_OPT_NO), false);
629 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target04, signer01, DEFAULT_OPT_NO), false);
630 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target05, signer01, DEFAULT_OPT_NO), true);
631 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, target05, signer02, DEFAULT_OPT_NO), false);
632 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, target06, signer01, DEFAULT_OPT_NO), true);
633 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, target06, signer02, DEFAULT_OPT_NO), false);
634 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, target07, signer01, DEFAULT_OPT_NO), true);
635 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, target07, signer02, DEFAULT_OPT_NO), false);
636}
637
638BOOST_AUTO_TEST_CASE(IsUpdateAllowedTest2)
639{
640 // This test case is to check the access control of regular key
641
642 owner = "alice";
643
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000644 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700645 tmpPath.string(),
646 m_keyChain.getTpm().getTpmLocator(),
647 owner);
648 PibDb db(tmpPath.string());
649
650 UpdateQueryProcessor& pro = pib.m_updateProcessor;
651
652 Name parent("/test");
653 addIdentity(parent);
654 Name parentCertName = m_keyChain.getDefaultCertificateNameForIdentity(parent);
655 shared_ptr<IdentityCertificate> parentCert = m_keyChain.getCertificate(parentCertName);
656 Name parentSigner = parentCertName.getPrefix(-1);
657
658 advanceClocks(io, time::milliseconds(100));
659 Name parentKeyName2 = m_keyChain.generateRsaKeyPair(parent);
660 shared_ptr<IdentityCertificate> parentCert2 = m_keyChain.selfSign(parentKeyName2);
661 Name parentSigner2 = parentCert2->getName().getPrefix(-1);
662
663 db.addIdentity(parent);
664 db.addKey(parentCert->getPublicKeyName(), parentCert->getPublicKeyInfo());
665 db.addKey(parentCert2->getPublicKeyName(), parentCert2->getPublicKeyInfo());
666 db.setDefaultKeyNameOfIdentity(parentCert->getPublicKeyName());
667 db.addCertificate(*parentCert);
668 db.setDefaultCertNameOfKey(parentCert->getName());
669 db.addCertificate(*parentCert2);
670 db.setDefaultCertNameOfKey(parentCert2->getName());
671
672 Name testId("/test/id");
673 addIdentity(testId);
674 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(testId);
675 shared_ptr<IdentityCertificate> testCert = m_keyChain.getCertificate(certName);
676 Name testKeyName = testCert->getPublicKeyName();
677 Name testSigner = certName.getPrefix(-1);
678
679 advanceClocks(io, time::milliseconds(100));
680 Name secondKeyName = m_keyChain.generateRsaKeyPair(testId);
681 shared_ptr<IdentityCertificate> secondCert = m_keyChain.selfSign(secondKeyName);
682 Name secondCertName = secondCert->getName();
683 Name secondSigner = secondCertName.getPrefix(-1);
684
685 db.addIdentity(testId);
686 db.addKey(testKeyName, testCert->getPublicKeyInfo());
687 db.addKey(secondKeyName, secondCert->getPublicKeyInfo());
688 db.setDefaultKeyNameOfIdentity(testKeyName);
689 db.addCertificate(*testCert);
690 db.setDefaultCertNameOfKey(testCert->getName());
691 db.addCertificate(*secondCert);
692 db.setDefaultCertNameOfKey(secondCert->getName());
693
694 Name nonSigner("/non-signer/KEY/ksk-123/ID-CERT");
695
696 // for target type = TYPE_ID
697 // one cannot add non-child
698 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, nonSigner, DEFAULT_OPT_NO), false);
699 // parent can add child
700 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, parentSigner, DEFAULT_OPT_NO), true);
701 // non-default parent key cannot add a child
702 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, parentSigner2, DEFAULT_OPT_NO), false);
703 // only DEFAULT_OPT_NO is allowed if target type is TYPE_ID
704 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, parentSigner, DEFAULT_OPT_ID), false);
705 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, parentSigner, DEFAULT_OPT_KEY), false);
706 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_ID, testId, parentSigner, DEFAULT_OPT_USER), false);
707
708 // for target type = TYPE_KEY
709 // one can add its own key
710 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, testSigner, DEFAULT_OPT_NO),
711 true);
712 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, secondKeyName, testSigner, DEFAULT_OPT_NO),
713 true);
714 // one can set its default key
715 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, testSigner, DEFAULT_OPT_ID),
716 true);
717 // non-default key cannot add its own key
718 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, secondKeyName, secondSigner, DEFAULT_OPT_NO),
719 false);
720 // non-default key cannot set its default key
721 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, secondSigner, DEFAULT_OPT_ID),
722 false);
723 // one can add its child's key
724 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, secondKeyName, parentSigner, DEFAULT_OPT_NO),
725 true);
726 // one can set its child's default key
727 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, parentSigner, DEFAULT_OPT_ID),
728 true);
729 // non-default key cannot add its child's key
730 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, secondKeyName, parentSigner2, DEFAULT_OPT_NO),
731 false);
732 // non-default parent key cannot set its child's default key
733 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, parentSigner2, DEFAULT_OPT_ID),
734 false);
735 // DEFAULT_OPT_KEY is not allowed if target type is TYPE_KEY
736 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, testSigner, DEFAULT_OPT_KEY),
737 false);
738 // DEFAULT_OPT_USER is not allowed if signer is no local management key
739 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_KEY, testKeyName, testSigner, DEFAULT_OPT_USER),
740 false);
741
742 // for target type = TYPE_CERT
743 // one can add its own certificate
744 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, testSigner, DEFAULT_OPT_NO),
745 true);
746 // one can set its own default certificate
747 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, testSigner, DEFAULT_OPT_ID),
748 true);
749 // one can set its own key's default certificate
750 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, testSigner, DEFAULT_OPT_KEY),
751 true);
752 // DEFAULT_OPT_USER is not allowed if signer is no local management key
753 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, testSigner, DEFAULT_OPT_USER),
754 false);
755 // non-default key can add other key's certificate
756 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, secondSigner, DEFAULT_OPT_NO),
757 false);
758 // non-default key can add its own certificate
759 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, secondCertName, secondSigner, DEFAULT_OPT_NO),
760 true);
761 // one can add its child's certificate
762 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, parentSigner, DEFAULT_OPT_NO),
763 true);
764 // non-default key cannot add its child's certificate
765 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, certName, parentSigner2, DEFAULT_OPT_NO),
766 false);
767 // non-default key cannot set add its identity default certificate
768 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, secondCertName, secondSigner, DEFAULT_OPT_ID),
769 false);
770 // non-default key can set add its own default certificate
771 BOOST_CHECK_EQUAL(pro.isUpdateAllowed(TYPE_CERT, secondCertName, secondSigner, DEFAULT_OPT_KEY),
772 true);
773}
774
775BOOST_AUTO_TEST_CASE(IsDeleteAllowedTest1)
776{
777 // This test case is to check the access control of local management key
778
779 owner = "alice";
780
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000781 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700782 tmpPath.string(),
783 m_keyChain.getTpm().getTpmLocator(),
784 owner);
785
786 DeleteQueryProcessor& pro = pib.m_deleteProcessor;
787
788 Name target01("/localhost/pib");
789 Name target02("/localhost/pib/alice/Mgmt");
790 Name target03("/localhost/pib/alice/Mgmt/ok");
791 Name target04("/localhost/pib/alice");
792 Name target05("/test/id");
793 Name target06("/test/id/ksk-123");
794 Name target07("/test/id/KEY/ksk-123/ID-CERT/version");
795 Name signer01 = pib.getMgmtCert().getName().getPrefix(-1);
796 Name signer02("/localhost/pib/bob/Mgmt/KEY/ksk-1234/ID-CERT");
797
798 // TYPE_USER is handled separately
799 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_USER, target02, signer01), false);
800
801 // Test access control of local management key
802 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target01, signer01), false);
803 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target02, signer01), false);
804 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target03, signer01), false);
805 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target04, signer01), false);
806 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target05, signer01), true);
807 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, target06, signer01), true);
808 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, target07, signer01), true);
809 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, target05, signer02), false);
810 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, target06, signer02), false);
811 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, target07, signer02), false);
812}
813
814BOOST_AUTO_TEST_CASE(IsDeleteAllowedTest2)
815{
816 // This test case is to check the access control of regular key
817 owner = "alice";
818
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000819 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700820 tmpPath.string(),
821 m_keyChain.getTpm().getTpmLocator(),
822 owner);
823 PibDb db(tmpPath.string());
824 DeleteQueryProcessor& pro = pib.m_deleteProcessor;
825
826 Name parent("/test");
827 addIdentity(parent);
828 Name parentCertName = m_keyChain.getDefaultCertificateNameForIdentity(parent);
829 shared_ptr<IdentityCertificate> parentCert = m_keyChain.getCertificate(parentCertName);
830 Name parentSigner = parentCertName.getPrefix(-1);
831
832 advanceClocks(io, time::milliseconds(100));
833 Name parentKeyName2 = m_keyChain.generateRsaKeyPair(parent);
834 shared_ptr<IdentityCertificate> parentCert2 = m_keyChain.selfSign(parentKeyName2);
835 Name parentSigner2 = parentCert2->getName().getPrefix(-1);
836
837 db.addIdentity(parent);
838 db.addKey(parentCert->getPublicKeyName(), parentCert->getPublicKeyInfo());
839 db.addKey(parentCert2->getPublicKeyName(), parentCert2->getPublicKeyInfo());
840 db.setDefaultKeyNameOfIdentity(parentCert->getPublicKeyName());
841 db.addCertificate(*parentCert);
842 db.setDefaultCertNameOfKey(parentCert->getName());
843 db.addCertificate(*parentCert2);
844 db.setDefaultCertNameOfKey(parentCert2->getName());
845
846 Name testId("/test/id");
847 addIdentity(testId);
848 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(testId);
849 shared_ptr<IdentityCertificate> testCert = m_keyChain.getCertificate(certName);
850 Name testKeyName = testCert->getPublicKeyName();
851 Name testSigner = certName.getPrefix(-1);
852
853 advanceClocks(io, time::milliseconds(100));
854 Name secondKeyName = m_keyChain.generateRsaKeyPair(testId);
855 shared_ptr<IdentityCertificate> secondCert = m_keyChain.selfSign(secondKeyName);
856 Name secondCertName = secondCert->getName();
857 Name secondSigner = secondCertName.getPrefix(-1);
858
859 db.addIdentity(testId);
860 db.addKey(testKeyName, testCert->getPublicKeyInfo());
861 db.addKey(secondKeyName, secondCert->getPublicKeyInfo());
862 db.setDefaultKeyNameOfIdentity(testKeyName);
863 db.addCertificate(*testCert);
864 db.setDefaultCertNameOfKey(testCert->getName());
865 db.addCertificate(*secondCert);
866 db.setDefaultCertNameOfKey(secondCert->getName());
867
868 Name nonSigner("/non-signer/KEY/ksk-123/ID-CERT");
869
870 // one can delete itself
871 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, testId, testSigner), true);
872 // parent can delete its child
873 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, testId, parentSigner), true);
874 // non-default key cannot delete its identity
875 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, testId, secondSigner), false);
876 // non-default key cannot delete its child
877 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, testId, parentSigner2), false);
878 // one cannot delete its parent
879 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_ID, parent, testSigner), false);
880
881 // one can delete its own key
882 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, testKeyName, testSigner), true);
883 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, secondKeyName, testSigner), true);
884 // parent can delete its child's key
885 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, testKeyName, parentSigner), true);
886 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, secondKeyName, parentSigner), true);
887 // non-default key cannot delete other key
888 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, testKeyName, secondSigner), false);
889 // non-default key can delete itself
890 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, secondKeyName, secondSigner), true);
891 // non-default key cannot delete its child's key
892 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_KEY, testKeyName, parentSigner2), false);
893
894 // one can delete its own certificate
895 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, certName, testSigner), true);
896 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, secondCertName, testSigner), true);
897 // non-default key cannot delete other's certificate
898 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, certName, secondSigner), false);
899 // non-default key can delete its own certificate
900 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, secondCertName, secondSigner), true);
901 // parent can delete its child's certificate
902 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, certName, parentSigner), true);
903 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, secondCertName, parentSigner), true);
904 // non-default parent cannot delete its child's certificate
905 BOOST_CHECK_EQUAL(pro.isDeleteAllowed(TYPE_CERT, certName, parentSigner2), false);
906}
907
908
909BOOST_AUTO_TEST_CASE(UpdateUserTest)
910{
911 owner = "alice";
912
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000913 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -0700914 tmpPath.string(),
915 m_keyChain.getTpm().getTpmLocator(),
916 owner);
917
918 advanceClocks(io, time::milliseconds(10), 10);
919 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
920
921 m_keyChain.addCertificate(pib.getMgmtCert());
922
923 PibDb db(tmpPath.string());
924
925 Name bob("/localhost/pib/bob/mgmt");
926 addIdentity(bob);
927 Name bobCertName = m_keyChain.getDefaultCertificateNameForIdentity(bob);
928 shared_ptr<IdentityCertificate> bobCert = m_keyChain.getCertificate(bobCertName);
929
930 // signer is correct, but user name is wrong, should fall
931 PibUser pibUser1;
932 pibUser1.setMgmtCert(*bobCert);
933 UpdateParam param1(pibUser1);
934 auto interest1 = generateSignedInterest(param1, owner, db.getMgmtCertificate()->getName());
935
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000936 face.sentData.clear();
937 face.receive(*interest1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700938 advanceClocks(io, time::milliseconds(10), 10);
939
940 BOOST_REQUIRE(cache.find(interest1->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000941 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700942 PibError result;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000943 BOOST_REQUIRE_NO_THROW(result.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700944 BOOST_CHECK_EQUAL(result.getErrorCode(), ERR_WRONG_PARAM);
945
946 // user name is correct, but signer is wrong, should fail
947 PibUser pibUser2;
948 pibUser2.setMgmtCert(pib.getMgmtCert());
949 UpdateParam param2(pibUser2);
950 auto interest2 = generateSignedInterest(param2, owner, bobCertName);
951
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000952 face.sentData.clear();
953 face.receive(*interest2);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700954 advanceClocks(io, time::milliseconds(10), 10);
955
956 BOOST_CHECK(cache.find(interest2->getName()) == nullptr); // verification should fail, no response
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000957 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700958
959 // update an existing user with a new mgmt key, signed by the old mgmt key.
960 advanceClocks(io, time::milliseconds(100));
961 Name ownerSecondKeyName =
962 m_keyChain.generateRsaKeyPair(Name("/localhost/pib/alice/mgmt"), false);
963 shared_ptr<IdentityCertificate> ownerSecondCert = m_keyChain.selfSign(ownerSecondKeyName);
964 m_keyChain.addCertificate(*ownerSecondCert);
965
966 PibUser pibUser3;
967 pibUser3.setMgmtCert(*ownerSecondCert);
968 UpdateParam param3(pibUser3);
969 auto interest3 = generateSignedInterest(param3, owner, db.getMgmtCertificate()->getName());
970
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000971 face.sentData.clear();
972 face.receive(*interest3);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700973 advanceClocks(io, time::milliseconds(10), 10);
974
975 BOOST_REQUIRE(cache.find(interest3->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000976 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700977 PibError result3;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000978 BOOST_REQUIRE_NO_THROW(result3.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700979 BOOST_CHECK_EQUAL(result3.getErrorCode(), ERR_SUCCESS);
980 BOOST_CHECK(db.getMgmtCertificate()->wireEncode() == ownerSecondCert->wireEncode());
981
982 // Add an cert and set it as user default cert.
983 Name testId("/test/id");
984 Name testIdCertName = m_keyChain.createIdentity(testId);
985 shared_ptr<IdentityCertificate> testIdCert = m_keyChain.getCertificate(testIdCertName);
986 Name testIdKeyName = testIdCert->getPublicKeyName();
987 UpdateParam updateParam(*testIdCert, DEFAULT_OPT_USER);
988 auto interest4 = generateSignedInterest(updateParam, owner, ownerSecondCert->getName());
989
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000990 face.sentData.clear();
991 face.receive(*interest4);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700992 advanceClocks(io, time::milliseconds(10), 10);
993
994 BOOST_REQUIRE(cache.find(interest4->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000995 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -0700996 PibError result4;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +0000997 BOOST_REQUIRE_NO_THROW(result4.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -0700998 BOOST_CHECK_EQUAL(result4.getErrorCode(), ERR_SUCCESS);
999
1000 BOOST_CHECK(pib.getDb().hasCertificate(testIdCertName));
1001 BOOST_CHECK(pib.getDb().hasKey(testIdKeyName));
1002 BOOST_CHECK(pib.getDb().hasIdentity(testId));
1003
1004 BOOST_REQUIRE_NO_THROW(pib.getDb().getDefaultCertNameOfKey(testIdKeyName));
1005 BOOST_REQUIRE_NO_THROW(pib.getDb().getDefaultKeyNameOfIdentity(testId));
1006 BOOST_REQUIRE_NO_THROW(pib.getDb().getDefaultIdentity());
1007
1008 BOOST_CHECK_EQUAL(pib.getDb().getDefaultCertNameOfKey(testIdKeyName), testIdCertName);
1009 BOOST_CHECK_EQUAL(pib.getDb().getDefaultKeyNameOfIdentity(testId), testIdKeyName);
1010 BOOST_CHECK_EQUAL(pib.getDb().getDefaultIdentity(), testId);
1011}
1012
1013BOOST_AUTO_TEST_CASE(UpdateRegularKeyTest)
1014{
1015 owner = "alice";
1016
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001017 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -07001018 tmpPath.string(),
1019 m_keyChain.getTpm().getTpmLocator(),
1020 owner);
1021
1022 advanceClocks(io, time::milliseconds(10), 10);
1023 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
1024 auto ownerMgmtCert = pib.getMgmtCert();
1025 m_keyChain.addCertificate(ownerMgmtCert);
1026
1027 PibDb db(tmpPath.string());
1028
1029 Name id0("/test/identity0");
1030 Name certName000 = m_keyChain.createIdentity(id0);
1031 shared_ptr<IdentityCertificate> cert000 = m_keyChain.getCertificate(certName000);
1032 Name keyName00 = cert000->getPublicKeyName();
1033 advanceClocks(io, time::milliseconds(100));
1034 shared_ptr<IdentityCertificate> cert001 = m_keyChain.selfSign(keyName00);
1035 Name certName001 = cert001->getName();
1036
1037 advanceClocks(io, time::milliseconds(100));
1038 Name keyName01 = m_keyChain.generateRsaKeyPair(id0);
1039 shared_ptr<IdentityCertificate> cert010 = m_keyChain.selfSign(keyName01);
1040 Name certName010 = cert010->getName();
1041 advanceClocks(io, time::milliseconds(100));
1042 shared_ptr<IdentityCertificate> cert011 = m_keyChain.selfSign(keyName01);
1043 Name certName011 = cert011->getName();
1044 m_keyChain.addCertificate(*cert010);
1045
1046 advanceClocks(io, time::milliseconds(100));
1047 Name id1("/test/identity1");
1048 Name certName100 = m_keyChain.createIdentity(id1);
1049 shared_ptr<IdentityCertificate> cert100 = m_keyChain.getCertificate(certName100);
1050 Name keyName10 = cert100->getPublicKeyName();
1051 advanceClocks(io, time::milliseconds(100));
1052 shared_ptr<IdentityCertificate> cert101 = m_keyChain.selfSign(keyName10);
1053 Name certName101 = cert101->getName();
1054
1055 advanceClocks(io, time::milliseconds(100));
1056 Name keyName11 = m_keyChain.generateRsaKeyPair(id1);
1057 shared_ptr<IdentityCertificate> cert110 = m_keyChain.selfSign(keyName11);
1058 Name certName110 = cert110->getName();
1059 advanceClocks(io, time::milliseconds(100));
1060 shared_ptr<IdentityCertificate> cert111 = m_keyChain.selfSign(keyName11);
1061 Name certName111 = cert111->getName();
1062 m_keyChain.addCertificate(*cert111);
1063
1064
1065 // Add a cert
1066 BOOST_CHECK_EQUAL(db.hasIdentity(id0), false);
1067 BOOST_CHECK_EQUAL(db.hasKey(keyName00), false);
1068 BOOST_CHECK_EQUAL(db.hasCertificate(certName000), false);
1069 UpdateParam param1(*cert000);
1070 auto interest1 = generateSignedInterest(param1, owner, ownerMgmtCert.getName());
1071
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001072 face.sentData.clear();
1073 face.receive(*interest1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001074 advanceClocks(io, time::milliseconds(10), 10);
1075
1076 BOOST_CHECK(cache.find(interest1->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001077 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001078 PibError result1;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001079 BOOST_REQUIRE_NO_THROW(result1.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001080 BOOST_CHECK_EQUAL(result1.getErrorCode(), ERR_SUCCESS);
1081 BOOST_CHECK_EQUAL(db.hasIdentity(id0), true);
1082 BOOST_CHECK_EQUAL(db.hasKey(keyName00), true);
1083 BOOST_CHECK_EQUAL(db.hasCertificate(certName000), true);
1084
1085 db.addCertificate(*cert100);
1086 BOOST_CHECK_EQUAL(db.hasIdentity(id1), true);
1087 BOOST_CHECK_EQUAL(db.hasKey(keyName10), true);
1088 BOOST_CHECK_EQUAL(db.hasCertificate(certName100), true);
1089
1090 // Set default
1091 BOOST_CHECK_EQUAL(db.getDefaultIdentity(), id0);
1092 BOOST_CHECK_EQUAL(db.getDefaultKeyNameOfIdentity(id0), keyName00);
1093 BOOST_CHECK_EQUAL(db.getDefaultCertNameOfKey(keyName00), certName000);
1094
1095 UpdateParam param2(id1, DEFAULT_OPT_USER);
1096 auto interest2 = generateSignedInterest(param2, owner, ownerMgmtCert.getName());
1097
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001098 face.sentData.clear();
1099 face.receive(*interest2);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001100 advanceClocks(io, time::milliseconds(10), 10);
1101
1102 BOOST_CHECK(cache.find(interest2->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001103 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001104 PibError result2;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001105 BOOST_REQUIRE_NO_THROW(result2.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001106 BOOST_CHECK_EQUAL(result2.getErrorCode(), ERR_SUCCESS);
1107 BOOST_CHECK_EQUAL(db.getDefaultIdentity(), id1);
1108
1109 db.addCertificate(*cert010);
1110 UpdateParam param3(keyName01, cert010->getPublicKeyInfo(), DEFAULT_OPT_ID);
1111 auto interest3 = generateSignedInterest(param3, owner, ownerMgmtCert.getName());
1112
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001113 face.sentData.clear();
1114 face.receive(*interest3);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001115 advanceClocks(io, time::milliseconds(10), 10);
1116
1117 BOOST_CHECK(cache.find(interest3->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001118 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001119 PibError result3;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001120 BOOST_REQUIRE_NO_THROW(result3.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001121 BOOST_CHECK_EQUAL(result3.getErrorCode(), ERR_SUCCESS);
1122 BOOST_CHECK_EQUAL(db.getDefaultKeyNameOfIdentity(id0), keyName01);
1123
1124 db.addCertificate(*cert011);
1125 UpdateParam param4(*cert011, DEFAULT_OPT_KEY);
1126 auto interest4 = generateSignedInterest(param4, owner, ownerMgmtCert.getName());
1127
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001128 face.sentData.clear();
1129 face.receive(*interest4);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001130 advanceClocks(io, time::milliseconds(10), 10);
1131
1132 BOOST_CHECK(cache.find(interest4->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001133 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001134 PibError result4;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001135 BOOST_REQUIRE_NO_THROW(result4.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001136 BOOST_CHECK_EQUAL(result4.getErrorCode(), ERR_SUCCESS);
1137 BOOST_CHECK_EQUAL(db.getDefaultCertNameOfKey(keyName01), certName011);
1138
1139 // add key and certificate using regular keys.
1140 BOOST_CHECK_EQUAL(db.hasKey(keyName11), false);
1141 UpdateParam param5(keyName11, cert110->getPublicKeyInfo());
1142 auto interest5 = generateSignedInterest(param5, owner, cert100->getName());
1143
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001144 face.sentData.clear();
1145 face.receive(*interest5);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001146 advanceClocks(io, time::milliseconds(10), 10);
1147
1148 BOOST_CHECK(cache.find(interest5->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001149 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001150 PibError result5;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001151 BOOST_REQUIRE_NO_THROW(result5.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001152 BOOST_CHECK_EQUAL(result5.getErrorCode(), ERR_SUCCESS);
1153 BOOST_CHECK_EQUAL(db.hasKey(keyName11), true);
1154
1155 // add cert using its own key which has been added before
1156 BOOST_CHECK_EQUAL(db.hasCertificate(cert101->getName()), false);
1157 UpdateParam param6(*cert101);
1158 auto interest6 = generateSignedInterest(param6, owner, cert100->getName());
1159
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001160 face.sentData.clear();
1161 face.receive(*interest6);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001162 advanceClocks(io, time::milliseconds(10), 10);
1163
1164 BOOST_CHECK(cache.find(interest6->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001165 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001166 PibError result6;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001167 BOOST_REQUIRE_NO_THROW(result6.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001168 BOOST_CHECK_EQUAL(result6.getErrorCode(), ERR_SUCCESS);
1169 BOOST_CHECK_EQUAL(db.hasCertificate(cert101->getName()), true);
1170}
1171
1172BOOST_AUTO_TEST_CASE(DeleteUserTest)
1173{
1174 owner = "alice";
1175
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001176 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -07001177 tmpPath.string(),
1178 m_keyChain.getTpm().getTpmLocator(),
1179 owner);
1180 advanceClocks(io, time::milliseconds(10), 10);
1181 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
1182 auto ownerMgmtCert = pib.getMgmtCert();
1183 m_keyChain.addCertificate(ownerMgmtCert);
1184
1185 PibDb db(tmpPath.string());
1186
1187 // Delete user should fail
1188 DeleteParam param(Name(), TYPE_USER);
1189 auto interest = generateSignedInterest(param, owner, ownerMgmtCert.getName());
1190
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001191 face.receive(*interest);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001192 advanceClocks(io, time::milliseconds(10), 10);
1193
1194 BOOST_CHECK(cache.find(interest->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001195 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001196 PibError result;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001197 BOOST_REQUIRE_NO_THROW(result.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001198 BOOST_CHECK_EQUAL(result.getErrorCode(), ERR_WRONG_PARAM);
1199}
1200
1201BOOST_AUTO_TEST_CASE(DeleteRegularKeyTest)
1202{
1203 owner = "alice";
1204
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001205 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -07001206 tmpPath.string(),
1207 m_keyChain.getTpm().getTpmLocator(),
1208 owner);
1209 advanceClocks(io, time::milliseconds(10), 10);
1210 util::InMemoryStoragePersistent& cache = pib.getResponseCache();
1211 auto ownerMgmtCert = pib.getMgmtCert();
1212 m_keyChain.addCertificate(ownerMgmtCert);
1213
1214 PibDb& db = pib.getDb();
1215
1216 Name testId("/test/identity");
1217 Name testIdCertName00 = m_keyChain.createIdentity(testId);
1218 shared_ptr<IdentityCertificate> cert00 = m_keyChain.getCertificate(testIdCertName00);
1219 Name testIdKeyName0 = cert00->getPublicKeyName();
1220 advanceClocks(io, time::milliseconds(100));
1221 shared_ptr<IdentityCertificate> cert01 = m_keyChain.selfSign(testIdKeyName0);
1222 Name testIdCertName01 = cert01->getName();
1223
1224 advanceClocks(io, time::milliseconds(100));
1225 Name testIdKeyName1 = m_keyChain.generateRsaKeyPair(testId);
1226 shared_ptr<IdentityCertificate> cert10 = m_keyChain.selfSign(testIdKeyName1);
1227 Name testIdCertName10 = cert10->getName();
1228 advanceClocks(io, time::milliseconds(100));
1229 shared_ptr<IdentityCertificate> cert11 = m_keyChain.selfSign(testIdKeyName1);
1230 Name testIdCertName11 = cert11->getName();
1231 m_keyChain.addCertificate(*cert11);
1232
1233 db.addCertificate(*cert00);
1234 db.addCertificate(*cert01);
1235 db.addCertificate(*cert10);
1236 db.addCertificate(*cert11);
1237 db.setDefaultIdentity(testId);
1238 db.setDefaultKeyNameOfIdentity(testIdKeyName0);
1239 db.setDefaultCertNameOfKey(testIdCertName00);
1240 db.setDefaultCertNameOfKey(testIdCertName10);
1241
1242 // delete a certificate itself
1243 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), true);
1244 DeleteParam param1(testIdCertName11, TYPE_CERT);
1245 auto interest1 = generateSignedInterest(param1, owner, testIdCertName11);
1246
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001247 face.sentData.clear();
1248 face.receive(*interest1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001249 advanceClocks(io, time::milliseconds(10), 10);
1250
1251 BOOST_CHECK(cache.find(interest1->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001252 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001253 PibError result1;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001254 BOOST_REQUIRE_NO_THROW(result1.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001255 BOOST_CHECK_EQUAL(result1.getErrorCode(), ERR_SUCCESS);
1256 BOOST_CHECK_EQUAL(db.hasCertificate(testIdCertName11), false);
1257
1258 // delete a key itself
1259 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), true);
1260 DeleteParam param2(testIdKeyName1, TYPE_KEY);
1261 auto interest2 = generateSignedInterest(param2, owner, testIdCertName11);
1262
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001263 face.sentData.clear();
1264 face.receive(*interest2);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001265 advanceClocks(io, time::milliseconds(10), 10);
1266
1267 BOOST_CHECK(cache.find(interest2->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001268 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001269 PibError result2;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001270 BOOST_REQUIRE_NO_THROW(result2.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001271 BOOST_CHECK_EQUAL(result2.getErrorCode(), ERR_SUCCESS);
1272 BOOST_CHECK_EQUAL(db.hasKey(testIdKeyName1), false);
1273
1274 // delete an identity using non-default key, should fail
1275 db.addCertificate(*cert11);
1276 BOOST_CHECK_EQUAL(db.hasIdentity(testId), true);
1277 DeleteParam param3(testId, TYPE_ID);
1278 auto interest3 = generateSignedInterest(param3, owner, testIdCertName11);
1279
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001280 face.sentData.clear();
1281 face.receive(*interest3);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001282 advanceClocks(io, time::milliseconds(10), 10);
1283
1284 BOOST_CHECK(cache.find(interest3->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001285 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001286 PibError result3;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001287 BOOST_REQUIRE_NO_THROW(result3.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001288 BOOST_CHECK_EQUAL(result3.getErrorCode(), ERR_WRONG_SIGNER);
1289 BOOST_CHECK_EQUAL(db.hasIdentity(testId), true);
1290
1291 // delete an identity using identity default key, should succeed
1292 DeleteParam param4(testId, TYPE_ID);
1293 auto interest4 = generateSignedInterest(param4, owner, testIdCertName00);
1294
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001295 face.sentData.clear();
1296 face.receive(*interest4);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001297 advanceClocks(io, time::milliseconds(10), 10);
1298
1299 BOOST_CHECK(cache.find(interest4->getName()) != nullptr);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001300 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001301 PibError result4;
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001302 BOOST_REQUIRE_NO_THROW(result4.wireDecode(face.sentData[0].getContent().blockFromValue()));
Yingdi Yu77627ab2015-07-21 16:13:49 -07001303 BOOST_CHECK_EQUAL(result4.getErrorCode(), ERR_SUCCESS);
1304 BOOST_CHECK_EQUAL(db.hasIdentity(testId), false);
1305}
1306
1307BOOST_AUTO_TEST_CASE(ReadCommandTest2)
1308{
1309 // Read Certificates;
1310 owner = "alice";
1311
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001312 Pib pib(face,
Yingdi Yu77627ab2015-07-21 16:13:49 -07001313 tmpPath.string(),
1314 m_keyChain.getTpm().getTpmLocator(),
1315 owner);
1316
1317 advanceClocks(io, time::milliseconds(10), 100);
1318 auto ownerMgmtCert = pib.getMgmtCert();
1319 m_keyChain.addCertificate(ownerMgmtCert);
1320
1321 Name testId("/test/identity");
1322 Name testIdCertName00 = m_keyChain.createIdentity(testId);
1323 shared_ptr<IdentityCertificate> cert00 = m_keyChain.getCertificate(testIdCertName00);
1324 Name testIdKeyName0 = cert00->getPublicKeyName();
1325 advanceClocks(io, time::milliseconds(100));
1326 shared_ptr<IdentityCertificate> cert01 = m_keyChain.selfSign(testIdKeyName0);
1327 Name testIdCertName01 = cert01->getName();
1328
1329 advanceClocks(io, time::milliseconds(100));
1330 Name testIdKeyName1 = m_keyChain.generateRsaKeyPair(testId);
1331 shared_ptr<IdentityCertificate> cert10 = m_keyChain.selfSign(testIdKeyName1);
1332 Name testIdCertName10 = cert10->getName();
1333 advanceClocks(io, time::milliseconds(100));
1334 shared_ptr<IdentityCertificate> cert11 = m_keyChain.selfSign(testIdKeyName1);
1335 Name testIdCertName11 = cert11->getName();
1336
1337
1338 UpdateParam param00(*cert00);
1339 UpdateParam param01(*cert01);
1340 UpdateParam param10(*cert10);
1341 UpdateParam param11(*cert11);
1342 auto interest00 = generateSignedInterest(param00, owner, ownerMgmtCert.getName());
1343 auto interest01 = generateSignedInterest(param01, owner, ownerMgmtCert.getName());
1344 auto interest10 = generateSignedInterest(param10, owner, ownerMgmtCert.getName());
1345 auto interest11 = generateSignedInterest(param11, owner, ownerMgmtCert.getName());
1346
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001347 face.sentData.clear();
1348 face.receive(*interest00);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001349 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001350 face.receive(*interest01);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001351 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001352 face.receive(*interest10);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001353 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001354 face.receive(*interest11);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001355 advanceClocks(io, time::milliseconds(10), 10);
1356
1357 auto interest1 = make_shared<Interest>(testIdCertName11);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001358 face.sentData.clear();
1359 face.receive(*interest1);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001360 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001361 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
1362 BOOST_CHECK(face.sentData[0].wireEncode() == cert11->wireEncode());
Yingdi Yu77627ab2015-07-21 16:13:49 -07001363
1364 auto interest2 = make_shared<Interest>(testIdCertName11.getPrefix(-1));
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001365 face.sentData.clear();
1366 face.receive(*interest2);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001367 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001368 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
1369 BOOST_CHECK_EQUAL(face.sentData[0].getName().getPrefix(-1),
Yingdi Yu77627ab2015-07-21 16:13:49 -07001370 cert11->getName().getPrefix(-1));
1371
1372 auto interest3 = make_shared<Interest>(testIdCertName11.getPrefix(-1));
1373 pib.getDb().deleteCertificate(testIdCertName11);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001374 face.sentData.clear();
1375 face.receive(*interest3);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001376 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001377 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
1378 BOOST_CHECK(face.sentData[0].wireEncode() == cert10->wireEncode());
Yingdi Yu77627ab2015-07-21 16:13:49 -07001379
1380 auto interest4 = make_shared<Interest>(testIdCertName11);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001381 face.sentData.clear();
1382 face.receive(*interest4);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001383 advanceClocks(io, time::milliseconds(10), 10);
Junxiao Shiaa1b3c92016-07-14 14:56:53 +00001384 BOOST_REQUIRE_EQUAL(face.sentData.size(), 0);
Yingdi Yu77627ab2015-07-21 16:13:49 -07001385}
1386
Davide Pesavento013de9b2016-09-01 12:06:56 +00001387BOOST_AUTO_TEST_SUITE_END() // TestPib
1388BOOST_AUTO_TEST_SUITE_END() // Pib
Yingdi Yu77627ab2015-07-21 16:13:49 -07001389
1390} // namespace tests
1391} // namespace pib
1392} // namespace ndn