blob: 25188a2cb26b0b5b378add2c4383fdf3cdcaddf2 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/pib/key-container.hpp"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040023#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/security/pib/pib.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070032namespace tests {
33
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034using namespace ndn::security::tests;
35
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070037BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080038BOOST_FIXTURE_TEST_SUITE(TestKeyContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039
Yingdi Yu6ee2d362015-07-16 21:48:05 -070040using pib::Pib;
41
Yingdi Yucbe72b02015-11-25 17:35:37 -080042BOOST_AUTO_TEST_CASE(Basic)
Yingdi Yub8f8b342015-04-27 11:06:42 -070043{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070044 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070045
Yingdi Yucbe72b02015-11-25 17:35:37 -080046 // 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 Yub8f8b342015-04-27 11:06:42 -070050
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 // add the first key
Davide Pesavento765abc92021-12-27 00:44:04 -050052 Key key11 = container.add(id1Key1, id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080053 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050054 BOOST_CHECK_EQUAL_COLLECTIONS(key11.getPublicKey().begin(), key11.getPublicKey().end(),
55 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080056 BOOST_CHECK_EQUAL(container.size(), 1);
57 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 1);
58 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070059
Yingdi Yucbe72b02015-11-25 17:35:37 -080060 // add the same key again
Davide Pesavento765abc92021-12-27 00:44:04 -050061 Key key12 = container.add(id1Key1, id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080062 BOOST_CHECK_EQUAL(key12.getName(), id1Key1Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050063 BOOST_CHECK_EQUAL_COLLECTIONS(key12.getPublicKey().begin(), key12.getPublicKey().end(),
64 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080065 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 Pesavento765abc92021-12-27 00:44:04 -050070 Key key21 = container.add(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 BOOST_CHECK_EQUAL(key21.getName(), id1Key2Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050072 BOOST_CHECK_EQUAL_COLLECTIONS(key21.getPublicKey().begin(), key21.getPublicKey().end(),
73 id1Key2.begin(), id1Key2.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070074 BOOST_CHECK_EQUAL(container.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 2);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070076 BOOST_CHECK(container.find(id1Key1Name) != container.end());
77 BOOST_CHECK(container.find(id1Key2Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070078
Yingdi Yucbe72b02015-11-25 17:35:37 -080079 // get keys
80 BOOST_REQUIRE_NO_THROW(container.get(id1Key1Name));
81 BOOST_REQUIRE_NO_THROW(container.get(id1Key2Name));
Davide Pesaventof2cae612021-03-24 18:47:05 -040082 Name id1Key3Name = constructKeyName(id1, name::Component("non-existing-id"));
Yingdi Yucbe72b02015-11-25 17:35:37 -080083 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 Pesavento765abc92021-12-27 00:44:04 -050089 BOOST_CHECK_EQUAL_COLLECTIONS(key1.getPublicKey().begin(), key1.getPublicKey().end(),
90 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080091 BOOST_CHECK_EQUAL(key2.getName(), id1Key2Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050092 BOOST_CHECK_EQUAL_COLLECTIONS(key2.getPublicKey().begin(), key2.getPublicKey().end(),
93 id1Key2.begin(), id1Key2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080094
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
124BOOST_AUTO_TEST_CASE(Errors)
125{
126 auto pibImpl = make_shared<PibMemory>();
127
128 KeyContainer container(id1, pibImpl);
129
Davide Pesavento765abc92021-12-27 00:44:04 -0500130 BOOST_CHECK_THROW(container.add(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800131 BOOST_CHECK_THROW(container.remove(id2Key1Name), std::invalid_argument);
132 BOOST_CHECK_THROW(container.get(id2Key1Name), std::invalid_argument);
133}
134
135BOOST_AUTO_TEST_CASE(Iterator)
136{
137 auto pibImpl = make_shared<PibMemory>();
138 KeyContainer container(id1, pibImpl);
139
Davide Pesavento765abc92021-12-27 00:44:04 -0500140 container.add(id1Key1, id1Key1Name);
141 container.add(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800142
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700143 std::set<Name> keyNames;
144 keyNames.insert(id1Key1Name);
145 keyNames.insert(id1Key2Name);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700146
147 KeyContainer::const_iterator it = container.begin();
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700148 std::set<Name>::const_iterator testIt = keyNames.begin();
149 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150 it++;
151 testIt++;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700152 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153 ++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 Yu6ee2d362015-07-16 21:48:05 -0700161 BOOST_CHECK_EQUAL(key.getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162 testIt++;
163 count++;
164 }
165 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800166
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 Yub8f8b342015-04-27 11:06:42 -0700170}
171
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100172BOOST_AUTO_TEST_SUITE_END() // TestKeyContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700173BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100174BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700175
176} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700177} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700178} // namespace security
179} // namespace ndn