blob: 81f9ad047b849b293edec8784943ae5b1b75373e [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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::security::pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -070032
Davide Pesavento152ef442023-04-22 02:02:29 -040033NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator);
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yucbe72b02015-11-25 17:35:37 -080036BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070037
Davide Pesavento56cc0d72022-04-29 23:00:23 -040038BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070040 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070041
Davide Pesavento56cc0d72022-04-29 23:00:23 -040042 {
43 // start with an empty container
44 IdentityContainer container(pibImpl);
45 BOOST_CHECK_EQUAL(container.size(), 0);
46 BOOST_CHECK_EQUAL(container.m_identities.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070047
Davide Pesavento56cc0d72022-04-29 23:00:23 -040048 // add the first identity
49 Identity identity11 = container.add(id1);
50 BOOST_CHECK_EQUAL(identity11.getName(), id1);
51 BOOST_CHECK_EQUAL(container.size(), 1);
52 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
53 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080054
Davide Pesavento56cc0d72022-04-29 23:00:23 -040055 // add the same identity again
56 Identity identity12 = container.add(id1);
57 BOOST_CHECK_EQUAL(identity12.getName(), id1);
58 BOOST_CHECK_EQUAL(container.size(), 1);
59 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
60 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080061
Davide Pesavento56cc0d72022-04-29 23:00:23 -040062 // add the second identity
63 Identity identity21 = container.add(id2);
64 BOOST_CHECK_EQUAL(identity21.getName(), id2);
65 BOOST_CHECK_EQUAL(container.size(), 2);
66 BOOST_CHECK_EQUAL(container.m_identities.size(), 2);
67 BOOST_CHECK(container.find(id1) != container.end());
68 BOOST_CHECK(container.find(id2) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070069
Davide Pesavento56cc0d72022-04-29 23:00:23 -040070 // check identities
71 Identity identity1 = container.get(id1);
72 Identity identity2 = container.get(id2);
73 BOOST_CHECK_EQUAL(identity1.getName(), id1);
74 BOOST_CHECK_EQUAL(identity2.getName(), id2);
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040075 BOOST_CHECK_THROW(container.get(Name("/non-existing")), Pib::Error);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040076 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080077
Davide Pesavento56cc0d72022-04-29 23:00:23 -040078 {
79 // create a container from an existing (non-empty) PibImpl
80 // names are loaded immediately but identity cache should initially be empty
81 IdentityContainer container2(pibImpl);
82 BOOST_CHECK_EQUAL(container2.size(), 2);
83 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080084
Davide Pesavento56cc0d72022-04-29 23:00:23 -040085 // fetching the identities should populate the cache
86 BOOST_CHECK_EQUAL(container2.get(id1).getName(), id1);
87 BOOST_CHECK_EQUAL(container2.size(), 2);
88 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080089
Davide Pesavento56cc0d72022-04-29 23:00:23 -040090 BOOST_CHECK_EQUAL(container2.get(id2).getName(), id2);
91 BOOST_CHECK_EQUAL(container2.size(), 2);
92 BOOST_CHECK_EQUAL(container2.m_identities.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080093
Davide Pesavento56cc0d72022-04-29 23:00:23 -040094 // remove an identity
95 container2.remove(id1);
96 BOOST_CHECK_EQUAL(container2.size(), 1);
97 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
98 BOOST_CHECK(container2.find(id1) == container2.end());
99 BOOST_CHECK(container2.find(id2) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100
Davide Pesavento07db0732022-05-06 15:20:26 -0400101 // removing the same identity again is a no-op
102 container2.remove(id1);
103 BOOST_CHECK_EQUAL(container2.size(), 1);
104 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
105 BOOST_CHECK(container2.find(id1) == container2.end());
106 BOOST_CHECK(container2.find(id2) != container2.end());
107
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400108 // remove another identity
109 container2.remove(id2);
110 BOOST_CHECK_EQUAL(container2.size(), 0);
111 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
112 BOOST_CHECK(container2.find(id2) == container2.end());
113 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114}
115
116BOOST_AUTO_TEST_CASE(Iterator)
117{
118 auto pibImpl = make_shared<PibMemory>();
119 IdentityContainer container(pibImpl);
120 container.add(id1);
121 container.add(id2);
122
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400123 const std::set<Name> idNames{id1, id2};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124
125 IdentityContainer::const_iterator it = container.begin();
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400126 auto testIt = idNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700127 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
128 it++;
129 testIt++;
130 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
131 ++it;
132 testIt++;
133 BOOST_CHECK(it == container.end());
134
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400135 // test range-based for
136 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700137 testIt = idNames.begin();
138 for (const auto& identity : container) {
139 BOOST_CHECK_EQUAL(identity.getName(), *testIt);
140 testIt++;
141 count++;
142 }
143 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800144
145 BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator());
146 BOOST_CHECK(IdentityContainer::const_iterator() == container.end());
147 BOOST_CHECK(container.end() == IdentityContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148}
149
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100150BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer
151BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400153} // namespace ndn::tests