blob: 3a665c3c88c98a651173d8e9f283be0ddb4534a2 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
29
30using namespace ndn::security::pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -070031
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070033BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070034
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070035BOOST_AUTO_TEST_CASE(TpmLocator)
36{
Davide Pesavento0e768ef2022-05-09 20:03:44 -040037 Pib pib("pib-memory:", make_shared<PibMemory>());
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070038
39 BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:");
Davide Pesavento0e768ef2022-05-09 20:03:44 -040040 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070041
42 pib.setTpmLocator("test-tpm-locator");
Davide Pesavento0e768ef2022-05-09 20:03:44 -040043 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "test-tpm-locator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070044
45 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
46 pib.addIdentity(id1);
47 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
48
49 pib.setTpmLocator("another-tpm-locator");
50 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Davide Pesavento0e768ef2022-05-09 20:03:44 -040051 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "another-tpm-locator");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070052
53 pib.addIdentity(id1);
54 BOOST_CHECK_NO_THROW(pib.getIdentity(id1));
55 pib.reset();
56 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Davide Pesavento0e768ef2022-05-09 20:03:44 -040057 BOOST_CHECK_EQUAL(pib.getTpmLocator(), "");
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070058}
59
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060BOOST_AUTO_TEST_CASE(IdentityOperations)
Yingdi Yub8f8b342015-04-27 11:06:42 -070061{
Davide Pesavento0e768ef2022-05-09 20:03:44 -040062 Pib pib("pib-memory:", make_shared<PibMemory>());
Yingdi Yub8f8b342015-04-27 11:06:42 -070063
Davide Pesavento1c628842022-04-28 23:21:44 -040064 // PIB starts with no identities
65 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070066 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080067 // get default identity when it is not set yet, throw Pib::Error
68 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
69
70 // add identity
71 pib.addIdentity(id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
Davide Pesavento1c628842022-04-28 23:21:44 -040073 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
74 // add another
75 pib.addIdentity(id2);
76 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
77 BOOST_CHECK_EQUAL(pib.getIdentity(id2).getName(), id2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080078
Davide Pesavento1c628842022-04-28 23:21:44 -040079 // first identity implicitly becomes the default when there was no default identity
Yingdi Yucbe72b02015-11-25 17:35:37 -080080 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
81
Davide Pesavento1c628842022-04-28 23:21:44 -040082 // remove both identities
83 pib.removeIdentity(id2);
84 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
85 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
86 BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error);
87 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070088 pib.removeIdentity(id1);
Davide Pesavento1c628842022-04-28 23:21:44 -040089 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070090 BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070091 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080092
Davide Pesavento1c628842022-04-28 23:21:44 -040093 // set default identity (and implicitly create it)
94 pib.setDefaultIdentity(id1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070095 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080096 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
Davide Pesavento1c628842022-04-28 23:21:44 -040097 pib.setDefaultIdentity(id2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080098 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2);
99 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
100
101 // remove default identity
102 pib.removeIdentity(id2);
Davide Pesavento1c628842022-04-28 23:21:44 -0400103 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1);
104 BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800105 BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error);
106 BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error);
Davide Pesavento1c628842022-04-28 23:21:44 -0400107
108 // adding an identity now makes it the default
109 pib.addIdentity(id2);
110 BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2);
111 BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700112}
113
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700114BOOST_AUTO_TEST_SUITE_END() // TestPib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100115BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400117} // namespace ndn::tests