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