Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/identity-container.hpp" |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/pib-memory.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 24 | #include "ndn-cxx/util/concepts.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
| 27 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 31 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 34 | NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator); |
| 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Pib) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 39 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(AddGetRemove) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 41 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 42 | auto pibImpl = make_shared<PibMemory>(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 44 | { |
| 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 49 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 50 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 56 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 57 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 63 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 64 | // 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 71 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 72 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 79 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 80 | { |
| 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 86 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 87 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 91 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 92 | 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 95 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 96 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 102 | |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 103 | // 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 Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 110 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | BOOST_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 Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 125 | const std::set<Name> idNames{id1, id2}; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 126 | |
| 127 | IdentityContainer::const_iterator it = container.begin(); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 128 | auto testIt = idNames.begin(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 129 | 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 Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 137 | // test range-based for |
| 138 | int count = 0; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 139 | 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 146 | |
| 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 152 | BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 153 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 154 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 157 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 158 | } // namespace security |
| 159 | } // namespace ndn |