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 | /* |
| 3 | * Copyright (c) 2013-2018 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" |
| 23 | #include "ndn-cxx/security/pib/pib.hpp" |
| 24 | #include "ndn-cxx/security/pib/pib-memory.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 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 34 | using namespace ndn::security::tests; |
| 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 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 40 | using pib::Pib; |
| 41 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(Basic) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 44 | auto pibImpl = make_shared<PibMemory>(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 46 | // start with an empty container |
| 47 | IdentityContainer container(pibImpl); |
| 48 | BOOST_CHECK_EQUAL(container.size(), 0); |
| 49 | BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 51 | // add the first identity |
| 52 | Identity identity11 = container.add(id1); |
| 53 | BOOST_CHECK_EQUAL(identity11.getName(), id1); |
| 54 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 55 | BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1); |
| 56 | BOOST_CHECK(container.find(id1) != container.end()); |
| 57 | |
| 58 | // add the same identity again |
| 59 | Identity identity12 = container.add(id1); |
| 60 | BOOST_CHECK_EQUAL(identity12.getName(), id1); |
| 61 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 62 | BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1); |
| 63 | BOOST_CHECK(container.find(id1) != container.end()); |
| 64 | |
| 65 | // add the second identity |
| 66 | Identity identity21 = container.add(id2); |
| 67 | BOOST_CHECK_EQUAL(identity21.getName(), id2); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(container.size(), 2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 2); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 70 | BOOST_CHECK(container.find(id1) != container.end()); |
| 71 | BOOST_CHECK(container.find(id2) != container.end()); |
| 72 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 73 | // get identities |
| 74 | BOOST_REQUIRE_NO_THROW(container.get(id1)); |
| 75 | BOOST_REQUIRE_NO_THROW(container.get(id2)); |
| 76 | BOOST_CHECK_THROW(container.get(Name("/non-existing")), Pib::Error); |
| 77 | |
| 78 | // check identity |
| 79 | Identity identity1 = container.get(id1); |
| 80 | Identity identity2 = container.get(id2); |
| 81 | BOOST_CHECK_EQUAL(identity1.getName(), id1); |
| 82 | BOOST_CHECK_EQUAL(identity2.getName(), id2); |
| 83 | |
| 84 | // create another container from the same PibImpl |
| 85 | // cache should be empty |
| 86 | IdentityContainer container2(pibImpl); |
| 87 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 88 | BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0); |
| 89 | |
| 90 | // get key, cache should be filled |
| 91 | BOOST_REQUIRE_NO_THROW(container2.get(id1)); |
| 92 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 93 | BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1); |
| 94 | |
| 95 | BOOST_REQUIRE_NO_THROW(container2.get(id2)); |
| 96 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 97 | BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 2); |
| 98 | |
| 99 | // remove a key |
| 100 | container2.remove(id1); |
| 101 | BOOST_CHECK_EQUAL(container2.size(), 1); |
| 102 | BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1); |
| 103 | BOOST_CHECK(container2.find(id1) == container2.end()); |
| 104 | BOOST_CHECK(container2.find(id2) != container2.end()); |
| 105 | |
| 106 | // remove another key |
| 107 | container2.remove(id2); |
| 108 | BOOST_CHECK_EQUAL(container2.size(), 0); |
| 109 | BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0); |
| 110 | BOOST_CHECK(container2.find(id2) == container2.end()); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(Iterator) |
| 115 | { |
| 116 | auto pibImpl = make_shared<PibMemory>(); |
| 117 | IdentityContainer container(pibImpl); |
| 118 | container.add(id1); |
| 119 | container.add(id2); |
| 120 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 121 | std::set<Name> idNames; |
| 122 | idNames.insert(id1); |
| 123 | idNames.insert(id2); |
| 124 | |
| 125 | IdentityContainer::const_iterator it = container.begin(); |
| 126 | std::set<Name>::const_iterator testIt = idNames.begin(); |
| 127 | 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 | |
| 135 | size_t count = 0; |
| 136 | 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 143 | |
| 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 149 | BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 150 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 151 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 152 | |
| 153 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 154 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 155 | } // namespace security |
| 156 | } // namespace ndn |