blob: 79daace1a058fd6a6bc14f7341c9bec1f6099025 [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 Pesavento56cc0d72022-04-29 23:00:23 -04003 * 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/identity-container.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 Yucbe72b02015-11-25 17:35:37 -080035BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070036
Davide Pesavento56cc0d72022-04-29 23:00:23 -040037BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070039 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
Davide Pesavento56cc0d72022-04-29 23:00:23 -040041 {
42 // start with an empty container
43 IdentityContainer container(pibImpl);
44 BOOST_CHECK_EQUAL(container.size(), 0);
45 BOOST_CHECK_EQUAL(container.m_identities.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070046
Davide Pesavento56cc0d72022-04-29 23:00:23 -040047 // add the first identity
48 Identity identity11 = container.add(id1);
49 BOOST_CHECK_EQUAL(identity11.getName(), id1);
50 BOOST_CHECK_EQUAL(container.size(), 1);
51 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
52 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080053
Davide Pesavento56cc0d72022-04-29 23:00:23 -040054 // add the same identity again
55 Identity identity12 = container.add(id1);
56 BOOST_CHECK_EQUAL(identity12.getName(), id1);
57 BOOST_CHECK_EQUAL(container.size(), 1);
58 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
59 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080060
Davide Pesavento56cc0d72022-04-29 23:00:23 -040061 // add the second identity
62 Identity identity21 = container.add(id2);
63 BOOST_CHECK_EQUAL(identity21.getName(), id2);
64 BOOST_CHECK_EQUAL(container.size(), 2);
65 BOOST_CHECK_EQUAL(container.m_identities.size(), 2);
66 BOOST_CHECK(container.find(id1) != container.end());
67 BOOST_CHECK(container.find(id2) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070068
Davide Pesavento56cc0d72022-04-29 23:00:23 -040069 // check identities
70 Identity identity1 = container.get(id1);
71 Identity identity2 = container.get(id2);
72 BOOST_CHECK_EQUAL(identity1.getName(), id1);
73 BOOST_CHECK_EQUAL(identity2.getName(), id2);
74 BOOST_CHECK_THROW(container.get(Name("/non-existing")), pib::Pib::Error);
75 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080076
Davide Pesavento56cc0d72022-04-29 23:00:23 -040077 {
78 // create a container from an existing (non-empty) PibImpl
79 // names are loaded immediately but identity cache should initially be empty
80 IdentityContainer container2(pibImpl);
81 BOOST_CHECK_EQUAL(container2.size(), 2);
82 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080083
Davide Pesavento56cc0d72022-04-29 23:00:23 -040084 // fetching the identities should populate the cache
85 BOOST_CHECK_EQUAL(container2.get(id1).getName(), id1);
86 BOOST_CHECK_EQUAL(container2.size(), 2);
87 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080088
Davide Pesavento56cc0d72022-04-29 23:00:23 -040089 BOOST_CHECK_EQUAL(container2.get(id2).getName(), id2);
90 BOOST_CHECK_EQUAL(container2.size(), 2);
91 BOOST_CHECK_EQUAL(container2.m_identities.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080092
Davide Pesavento56cc0d72022-04-29 23:00:23 -040093 // remove an identity
94 container2.remove(id1);
95 BOOST_CHECK_EQUAL(container2.size(), 1);
96 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
97 BOOST_CHECK(container2.find(id1) == container2.end());
98 BOOST_CHECK(container2.find(id2) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080099
Davide Pesavento07db0732022-05-06 15:20:26 -0400100 // removing the same identity again is a no-op
101 container2.remove(id1);
102 BOOST_CHECK_EQUAL(container2.size(), 1);
103 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
104 BOOST_CHECK(container2.find(id1) == container2.end());
105 BOOST_CHECK(container2.find(id2) != container2.end());
106
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400107 // remove another identity
108 container2.remove(id2);
109 BOOST_CHECK_EQUAL(container2.size(), 0);
110 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
111 BOOST_CHECK(container2.find(id2) == container2.end());
112 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800113}
114
115BOOST_AUTO_TEST_CASE(Iterator)
116{
117 auto pibImpl = make_shared<PibMemory>();
118 IdentityContainer container(pibImpl);
119 container.add(id1);
120 container.add(id2);
121
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400122 const std::set<Name> idNames{id1, id2};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700123
124 IdentityContainer::const_iterator it = container.begin();
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400125 auto testIt = idNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
127 it++;
128 testIt++;
129 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
130 ++it;
131 testIt++;
132 BOOST_CHECK(it == container.end());
133
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400134 // test range-based for
135 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700136 testIt = idNames.begin();
137 for (const auto& identity : container) {
138 BOOST_CHECK_EQUAL(identity.getName(), *testIt);
139 testIt++;
140 count++;
141 }
142 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143
144 BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator());
145 BOOST_CHECK(IdentityContainer::const_iterator() == container.end());
146 BOOST_CHECK(container.end() == IdentityContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147}
148
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100149BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700150BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100151BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152
153} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700154} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155} // namespace security
156} // namespace ndn