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 | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -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/key-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(TestKeyContainer, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -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 | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 41 | { |
| 42 | // start with an empty container |
| 43 | KeyContainer container(id1, pibImpl); |
| 44 | BOOST_CHECK_EQUAL(container.size(), 0); |
| 45 | BOOST_CHECK_EQUAL(container.m_keys.size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 47 | // add the first key |
| 48 | Key key11 = container.add(id1Key1, id1Key1Name); |
| 49 | BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name); |
| 50 | BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
| 51 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 52 | BOOST_CHECK_EQUAL(container.m_keys.size(), 1); |
| 53 | BOOST_CHECK(container.find(id1Key1Name) != container.end()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 54 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 55 | // add the same key again |
| 56 | Key key12 = container.add(id1Key1, id1Key1Name); |
| 57 | BOOST_CHECK_EQUAL(key12.getName(), id1Key1Name); |
| 58 | BOOST_TEST(key12.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
| 59 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 60 | BOOST_CHECK_EQUAL(container.m_keys.size(), 1); |
| 61 | BOOST_CHECK(container.find(id1Key1Name) != container.end()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 62 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 63 | // add the second key |
| 64 | Key key21 = container.add(id1Key2, id1Key2Name); |
| 65 | BOOST_CHECK_EQUAL(key21.getName(), id1Key2Name); |
| 66 | BOOST_TEST(key21.getPublicKey() == id1Key2, boost::test_tools::per_element()); |
| 67 | BOOST_CHECK_EQUAL(container.size(), 2); |
| 68 | BOOST_CHECK_EQUAL(container.m_keys.size(), 2); |
| 69 | BOOST_CHECK(container.find(id1Key1Name) != container.end()); |
| 70 | BOOST_CHECK(container.find(id1Key2Name) != container.end()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 71 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 72 | // check keys |
| 73 | Key key1 = container.get(id1Key1Name); |
| 74 | Key key2 = container.get(id1Key2Name); |
| 75 | BOOST_CHECK_EQUAL(key1.getName(), id1Key1Name); |
| 76 | BOOST_TEST(key1.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
| 77 | BOOST_CHECK_EQUAL(key2.getName(), id1Key2Name); |
| 78 | BOOST_TEST(key2.getPublicKey() == id1Key2, boost::test_tools::per_element()); |
| 79 | Name id1Key3Name = constructKeyName(id1, name::Component("non-existing-id")); |
| 80 | BOOST_CHECK_THROW(container.get(id1Key3Name), pib::Pib::Error); |
| 81 | } |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 82 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 83 | { |
| 84 | // create a container from an existing (non-empty) PibImpl |
| 85 | // names are loaded immediately but the key cache should initially be empty |
| 86 | KeyContainer container2(id1, pibImpl); |
| 87 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 88 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 0); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 89 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 90 | // fetching the keys should populate the cache |
| 91 | BOOST_CHECK_EQUAL(container2.get(id1Key1Name).getName(), id1Key1Name); |
| 92 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 93 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 94 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(container2.get(id1Key2Name).getName(), id1Key2Name); |
| 96 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 97 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 98 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 99 | // remove a key |
| 100 | container2.remove(id1Key1Name); |
| 101 | BOOST_CHECK_EQUAL(container2.size(), 1); |
| 102 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 1); |
| 103 | BOOST_CHECK(container2.find(id1Key1Name) == container2.end()); |
| 104 | BOOST_CHECK(container2.find(id1Key2Name) != container2.end()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 105 | |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame^] | 106 | // removing the same key again is a no-op |
| 107 | container2.remove(id1Key1Name); |
| 108 | BOOST_CHECK_EQUAL(container2.size(), 1); |
| 109 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 1); |
| 110 | BOOST_CHECK(container2.find(id1Key1Name) == container2.end()); |
| 111 | BOOST_CHECK(container2.find(id1Key2Name) != container2.end()); |
| 112 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 113 | // remove another key |
| 114 | container2.remove(id1Key2Name); |
| 115 | BOOST_CHECK_EQUAL(container2.size(), 0); |
| 116 | BOOST_CHECK_EQUAL(container2.m_keys.size(), 0); |
| 117 | BOOST_CHECK(container2.find(id1Key2Name) == container2.end()); |
| 118 | } |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(Errors) |
| 122 | { |
| 123 | auto pibImpl = make_shared<PibMemory>(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 124 | KeyContainer container(id1, pibImpl); |
| 125 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 126 | BOOST_CHECK_THROW(container.add(id2Key1, id2Key1Name), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 127 | BOOST_CHECK_THROW(container.remove(id2Key1Name), std::invalid_argument); |
| 128 | BOOST_CHECK_THROW(container.get(id2Key1Name), std::invalid_argument); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(Iterator) |
| 132 | { |
| 133 | auto pibImpl = make_shared<PibMemory>(); |
| 134 | KeyContainer container(id1, pibImpl); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 135 | container.add(id1Key1, id1Key1Name); |
| 136 | container.add(id1Key2, id1Key2Name); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 137 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 138 | const std::set<Name> keyNames{id1Key1Name, id1Key2Name}; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 139 | |
| 140 | KeyContainer::const_iterator it = container.begin(); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 141 | auto testIt = keyNames.begin(); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 143 | it++; |
| 144 | testIt++; |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 146 | ++it; |
| 147 | testIt++; |
| 148 | BOOST_CHECK(it == container.end()); |
| 149 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 150 | // test range-based for |
| 151 | int count = 0; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 152 | testIt = keyNames.begin(); |
| 153 | for (const auto& key : container) { |
| 154 | BOOST_CHECK_EQUAL(key.getIdentity(), id1); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(key.getName(), *testIt); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 156 | testIt++; |
| 157 | count++; |
| 158 | } |
| 159 | BOOST_CHECK_EQUAL(count, 2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 160 | |
| 161 | BOOST_CHECK(KeyContainer::const_iterator() == KeyContainer::const_iterator()); |
| 162 | BOOST_CHECK(KeyContainer::const_iterator() == container.end()); |
| 163 | BOOST_CHECK(container.end() == KeyContainer::const_iterator()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 166 | BOOST_AUTO_TEST_SUITE_END() // TestKeyContainer |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 167 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 168 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 169 | |
| 170 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 171 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 172 | } // namespace security |
| 173 | } // namespace ndn |