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