blob: ea97da0bcf74353494fc3ebad0e4a2baac8a95b1 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento152ef442023-04-22 02:02:29 -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/identity-container.hpp"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040023#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040024#include "ndn-cxx/util/concepts.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070032namespace tests {
33
Davide Pesavento152ef442023-04-22 02:02:29 -040034NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator);
35
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070037BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080038BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039
Davide Pesavento56cc0d72022-04-29 23:00:23 -040040BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070041{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070042 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070043
Davide Pesavento56cc0d72022-04-29 23:00:23 -040044 {
45 // start with an empty container
46 IdentityContainer container(pibImpl);
47 BOOST_CHECK_EQUAL(container.size(), 0);
48 BOOST_CHECK_EQUAL(container.m_identities.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070049
Davide Pesavento56cc0d72022-04-29 23:00:23 -040050 // add the first identity
51 Identity identity11 = container.add(id1);
52 BOOST_CHECK_EQUAL(identity11.getName(), id1);
53 BOOST_CHECK_EQUAL(container.size(), 1);
54 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
55 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080056
Davide Pesavento56cc0d72022-04-29 23:00:23 -040057 // add the same identity again
58 Identity identity12 = container.add(id1);
59 BOOST_CHECK_EQUAL(identity12.getName(), id1);
60 BOOST_CHECK_EQUAL(container.size(), 1);
61 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
62 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080063
Davide Pesavento56cc0d72022-04-29 23:00:23 -040064 // add the second identity
65 Identity identity21 = container.add(id2);
66 BOOST_CHECK_EQUAL(identity21.getName(), id2);
67 BOOST_CHECK_EQUAL(container.size(), 2);
68 BOOST_CHECK_EQUAL(container.m_identities.size(), 2);
69 BOOST_CHECK(container.find(id1) != container.end());
70 BOOST_CHECK(container.find(id2) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070071
Davide Pesavento56cc0d72022-04-29 23:00:23 -040072 // check identities
73 Identity identity1 = container.get(id1);
74 Identity identity2 = container.get(id2);
75 BOOST_CHECK_EQUAL(identity1.getName(), id1);
76 BOOST_CHECK_EQUAL(identity2.getName(), id2);
77 BOOST_CHECK_THROW(container.get(Name("/non-existing")), pib::Pib::Error);
78 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080079
Davide Pesavento56cc0d72022-04-29 23:00:23 -040080 {
81 // create a container from an existing (non-empty) PibImpl
82 // names are loaded immediately but identity cache should initially be empty
83 IdentityContainer container2(pibImpl);
84 BOOST_CHECK_EQUAL(container2.size(), 2);
85 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080086
Davide Pesavento56cc0d72022-04-29 23:00:23 -040087 // fetching the identities should populate the cache
88 BOOST_CHECK_EQUAL(container2.get(id1).getName(), id1);
89 BOOST_CHECK_EQUAL(container2.size(), 2);
90 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080091
Davide Pesavento56cc0d72022-04-29 23:00:23 -040092 BOOST_CHECK_EQUAL(container2.get(id2).getName(), id2);
93 BOOST_CHECK_EQUAL(container2.size(), 2);
94 BOOST_CHECK_EQUAL(container2.m_identities.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080095
Davide Pesavento56cc0d72022-04-29 23:00:23 -040096 // remove an identity
97 container2.remove(id1);
98 BOOST_CHECK_EQUAL(container2.size(), 1);
99 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
100 BOOST_CHECK(container2.find(id1) == container2.end());
101 BOOST_CHECK(container2.find(id2) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800102
Davide Pesavento07db0732022-05-06 15:20:26 -0400103 // removing the same identity again is a no-op
104 container2.remove(id1);
105 BOOST_CHECK_EQUAL(container2.size(), 1);
106 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
107 BOOST_CHECK(container2.find(id1) == container2.end());
108 BOOST_CHECK(container2.find(id2) != container2.end());
109
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400110 // remove another identity
111 container2.remove(id2);
112 BOOST_CHECK_EQUAL(container2.size(), 0);
113 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
114 BOOST_CHECK(container2.find(id2) == container2.end());
115 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800116}
117
118BOOST_AUTO_TEST_CASE(Iterator)
119{
120 auto pibImpl = make_shared<PibMemory>();
121 IdentityContainer container(pibImpl);
122 container.add(id1);
123 container.add(id2);
124
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400125 const std::set<Name> idNames{id1, id2};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126
127 IdentityContainer::const_iterator it = container.begin();
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400128 auto testIt = idNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700129 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
130 it++;
131 testIt++;
132 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
133 ++it;
134 testIt++;
135 BOOST_CHECK(it == container.end());
136
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400137 // test range-based for
138 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 testIt = idNames.begin();
140 for (const auto& identity : container) {
141 BOOST_CHECK_EQUAL(identity.getName(), *testIt);
142 testIt++;
143 count++;
144 }
145 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146
147 BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator());
148 BOOST_CHECK(IdentityContainer::const_iterator() == container.end());
149 BOOST_CHECK(container.end() == IdentityContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150}
151
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100152BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700153BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100154BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155
156} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158} // namespace security
159} // namespace ndn