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 | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 30 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 31 | namespace tests { |
| 32 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Pib) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestIdentityContainer, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(AddGetRemove) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 38 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 39 | auto pibImpl = make_shared<PibMemory>(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 41 | { |
| 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 47 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 53 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 54 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 60 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 61 | // 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 69 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 76 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 77 | { |
| 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 83 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 84 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 88 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 89 | 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 92 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 93 | // 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 99 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 100 | // remove another identity |
| 101 | container2.remove(id2); |
| 102 | BOOST_CHECK_EQUAL(container2.size(), 0); |
| 103 | BOOST_CHECK_EQUAL(container2.m_identities.size(), 0); |
| 104 | BOOST_CHECK(container2.find(id2) == container2.end()); |
| 105 | } |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(Iterator) |
| 109 | { |
| 110 | auto pibImpl = make_shared<PibMemory>(); |
| 111 | IdentityContainer container(pibImpl); |
| 112 | container.add(id1); |
| 113 | container.add(id2); |
| 114 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 115 | const std::set<Name> idNames{id1, id2}; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 116 | |
| 117 | IdentityContainer::const_iterator it = container.begin(); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 118 | auto testIt = idNames.begin(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
| 120 | it++; |
| 121 | testIt++; |
| 122 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
| 123 | ++it; |
| 124 | testIt++; |
| 125 | BOOST_CHECK(it == container.end()); |
| 126 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 127 | // test range-based for |
| 128 | int count = 0; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 129 | testIt = idNames.begin(); |
| 130 | for (const auto& identity : container) { |
| 131 | BOOST_CHECK_EQUAL(identity.getName(), *testIt); |
| 132 | testIt++; |
| 133 | count++; |
| 134 | } |
| 135 | BOOST_CHECK_EQUAL(count, 2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 136 | |
| 137 | BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator()); |
| 138 | BOOST_CHECK(IdentityContainer::const_iterator() == container.end()); |
| 139 | BOOST_CHECK(container.end() == IdentityContainer::const_iterator()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 143 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 144 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 145 | |
| 146 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 147 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 148 | } // namespace security |
| 149 | } // namespace ndn |