blob: 77503a0b2297a5838ba5b8a22776bba0995c0cf3 [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
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033using namespace ndn::security::tests;
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070036BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080037BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038
Davide Pesavento56cc0d72022-04-29 23:00:23 -040039BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070040{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070041 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070042
Davide Pesavento56cc0d72022-04-29 23:00:23 -040043 {
44 // start with an empty container
45 IdentityContainer container(pibImpl);
46 BOOST_CHECK_EQUAL(container.size(), 0);
47 BOOST_CHECK_EQUAL(container.m_identities.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070048
Davide Pesavento56cc0d72022-04-29 23:00:23 -040049 // add the first identity
50 Identity identity11 = container.add(id1);
51 BOOST_CHECK_EQUAL(identity11.getName(), id1);
52 BOOST_CHECK_EQUAL(container.size(), 1);
53 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
54 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080055
Davide Pesavento56cc0d72022-04-29 23:00:23 -040056 // add the same identity again
57 Identity identity12 = container.add(id1);
58 BOOST_CHECK_EQUAL(identity12.getName(), id1);
59 BOOST_CHECK_EQUAL(container.size(), 1);
60 BOOST_CHECK_EQUAL(container.m_identities.size(), 1);
61 BOOST_CHECK(container.find(id1) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080062
Davide Pesavento56cc0d72022-04-29 23:00:23 -040063 // add the second identity
64 Identity identity21 = container.add(id2);
65 BOOST_CHECK_EQUAL(identity21.getName(), id2);
66 BOOST_CHECK_EQUAL(container.size(), 2);
67 BOOST_CHECK_EQUAL(container.m_identities.size(), 2);
68 BOOST_CHECK(container.find(id1) != container.end());
69 BOOST_CHECK(container.find(id2) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070070
Davide Pesavento56cc0d72022-04-29 23:00:23 -040071 // check identities
72 Identity identity1 = container.get(id1);
73 Identity identity2 = container.get(id2);
74 BOOST_CHECK_EQUAL(identity1.getName(), id1);
75 BOOST_CHECK_EQUAL(identity2.getName(), id2);
76 BOOST_CHECK_THROW(container.get(Name("/non-existing")), pib::Pib::Error);
77 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080078
Davide Pesavento56cc0d72022-04-29 23:00:23 -040079 {
80 // create a container from an existing (non-empty) PibImpl
81 // names are loaded immediately but identity cache should initially be empty
82 IdentityContainer container2(pibImpl);
83 BOOST_CHECK_EQUAL(container2.size(), 2);
84 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080085
Davide Pesavento56cc0d72022-04-29 23:00:23 -040086 // fetching the identities should populate the cache
87 BOOST_CHECK_EQUAL(container2.get(id1).getName(), id1);
88 BOOST_CHECK_EQUAL(container2.size(), 2);
89 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080090
Davide Pesavento56cc0d72022-04-29 23:00:23 -040091 BOOST_CHECK_EQUAL(container2.get(id2).getName(), id2);
92 BOOST_CHECK_EQUAL(container2.size(), 2);
93 BOOST_CHECK_EQUAL(container2.m_identities.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080094
Davide Pesavento56cc0d72022-04-29 23:00:23 -040095 // remove an identity
96 container2.remove(id1);
97 BOOST_CHECK_EQUAL(container2.size(), 1);
98 BOOST_CHECK_EQUAL(container2.m_identities.size(), 1);
99 BOOST_CHECK(container2.find(id1) == container2.end());
100 BOOST_CHECK(container2.find(id2) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400102 // remove another identity
103 container2.remove(id2);
104 BOOST_CHECK_EQUAL(container2.size(), 0);
105 BOOST_CHECK_EQUAL(container2.m_identities.size(), 0);
106 BOOST_CHECK(container2.find(id2) == container2.end());
107 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108}
109
110BOOST_AUTO_TEST_CASE(Iterator)
111{
112 auto pibImpl = make_shared<PibMemory>();
113 IdentityContainer container(pibImpl);
114 container.add(id1);
115 container.add(id2);
116
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400117 const std::set<Name> idNames{id1, id2};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700118
119 IdentityContainer::const_iterator it = container.begin();
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400120 auto testIt = idNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
122 it++;
123 testIt++;
124 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
125 ++it;
126 testIt++;
127 BOOST_CHECK(it == container.end());
128
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400129 // test range-based for
130 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700131 testIt = idNames.begin();
132 for (const auto& identity : container) {
133 BOOST_CHECK_EQUAL(identity.getName(), *testIt);
134 testIt++;
135 count++;
136 }
137 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138
139 BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator());
140 BOOST_CHECK(IdentityContainer::const_iterator() == container.end());
141 BOOST_CHECK(container.end() == IdentityContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700142}
143
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100144BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700145BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100146BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147
148} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700149} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150} // namespace security
151} // namespace ndn