blob: 8d668e5ff86eba2a4ccaf7fd121722a7ed812dfd [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesavento35c63792022-01-17 02:06:03 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/pib/pib.hpp"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040023#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070027
28namespace ndn {
29namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070030namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070031namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070035BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070036
Yingdi Yu6ee2d362015-07-16 21:48:05 -070037using pib::Pib;
38
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070039BOOST_AUTO_TEST_CASE(TpmLocator)
40{
Davide Pesavento0e768ef2022-05-09 20:03:44 -040041 Pib pib("pib-memory:", make_shared<PibMemory>());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070042
43 BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:");
Davide Pesavento0e768ef2022-05-09 20:03:44 -040044 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070045
46 pib.setTpmLocator("test-tpm-locator");
Davide Pesavento0e768ef2022-05-09 20:03:44 -040047 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "test-tpm-locator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070048
49 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
50 pib.addIdentity(id1);
51 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
52
53 pib.setTpmLocator("another-tpm-locator");
54 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Davide Pesavento0e768ef2022-05-09 20:03:44 -040055 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "another-tpm-locator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070056
57 pib.addIdentity(id1);
58 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
59 pib.reset();
60 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Davide Pesavento0e768ef2022-05-09 20:03:44 -040061 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070062}
63
Davide Pesaventoeee3e822016-11-26 19:19:34 +010064BOOST_AUTO_TEST_CASE(IdentityOperations)
Yingdi Yub8f8b342015-04-27 11:06:42 -070065{
Davide Pesavento0e768ef2022-05-09 20:03:44 -040066 Pib pib("pib-memory:", make_shared<PibMemory>());
Yingdi Yub8f8b342015-04-27 11:06:42 -070067
Davide Pesavento1c628842022-04-28 23:21:44 -040068 // PIB starts with no identities
69 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070070 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 // get default identity when it is not set yet, throw Pib::Error
72 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
73
74 // add identity
75 pib.addIdentity(id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080076 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
Davide Pesavento1c628842022-04-28 23:21:44 -040077 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
78 // add another
79 pib.addIdentity(id2);
80 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
81 BOOST_CHECK_EQUAL(pib.getIdentity(id2).getName(), id2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080082
Davide Pesavento1c628842022-04-28 23:21:44 -040083 // first identity implicitly becomes the default when there was no default identity
Yingdi Yucbe72b02015-11-25 17:35:37 -080084 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
85
Davide Pesavento1c628842022-04-28 23:21:44 -040086 // remove both identities
87 pib.removeIdentity(id2);
88 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
89 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
90 BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error);
91 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070092 pib.removeIdentity(id1);
Davide Pesavento1c628842022-04-28 23:21:44 -040093 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070094 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070095 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080096
Davide Pesavento1c628842022-04-28 23:21:44 -040097 // set default identity (and implicitly create it)
98 pib.setDefaultIdentity(id1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070099 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
Davide Pesavento1c628842022-04-28 23:21:44 -0400101 pib.setDefaultIdentity(id2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800102 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2);
103 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
104
105 // remove default identity
106 pib.removeIdentity(id2);
Davide Pesavento1c628842022-04-28 23:21:44 -0400107 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
108 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800109 BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error);
110 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
Davide Pesavento1c628842022-04-28 23:21:44 -0400111
112 // adding an identity now makes it the default
113 pib.addIdentity(id2);
114 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
115 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116}
117
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700118BOOST_AUTO_TEST_SUITE_END() // TestPib
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700119BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100120BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121
122} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700123} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124} // namespace security
125} // namespace ndn