blob: b126bc20e5172295bf52b2e97f3bd82dcbc232fb [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 Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 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 Pesavento152ef442023-04-22 02:02:29 -040024#include "ndn-cxx/util/concepts.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
Davide Pesavento152ef442023-04-22 02:02:29 -040034NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
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
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040040BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070041{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070042 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070043
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040044 {
45 // start with an empty container
46 KeyContainer container(id1, pibImpl);
47 BOOST_CHECK_EQUAL(container.size(), 0);
48 BOOST_CHECK_EQUAL(container.m_keys.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070049
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040050 // add the first key
51 Key key11 = container.add(id1Key1, id1Key1Name);
52 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
53 BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element());
54 BOOST_CHECK_EQUAL(container.size(), 1);
55 BOOST_CHECK_EQUAL(container.m_keys.size(), 1);
56 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070057
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040058 // add the same key again
59 Key key12 = container.add(id1Key1, id1Key1Name);
60 BOOST_CHECK_EQUAL(key12.getName(), id1Key1Name);
61 BOOST_TEST(key12.getPublicKey() == id1Key1, boost::test_tools::per_element());
62 BOOST_CHECK_EQUAL(container.size(), 1);
63 BOOST_CHECK_EQUAL(container.m_keys.size(), 1);
64 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080065
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040066 // add the second key
67 Key key21 = container.add(id1Key2, id1Key2Name);
68 BOOST_CHECK_EQUAL(key21.getName(), id1Key2Name);
69 BOOST_TEST(key21.getPublicKey() == id1Key2, boost::test_tools::per_element());
70 BOOST_CHECK_EQUAL(container.size(), 2);
71 BOOST_CHECK_EQUAL(container.m_keys.size(), 2);
72 BOOST_CHECK(container.find(id1Key1Name) != container.end());
73 BOOST_CHECK(container.find(id1Key2Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070074
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040075 // check keys
76 Key key1 = container.get(id1Key1Name);
77 Key key2 = container.get(id1Key2Name);
78 BOOST_CHECK_EQUAL(key1.getName(), id1Key1Name);
79 BOOST_TEST(key1.getPublicKey() == id1Key1, boost::test_tools::per_element());
80 BOOST_CHECK_EQUAL(key2.getName(), id1Key2Name);
81 BOOST_TEST(key2.getPublicKey() == id1Key2, boost::test_tools::per_element());
82 Name id1Key3Name = constructKeyName(id1, name::Component("non-existing-id"));
83 BOOST_CHECK_THROW(container.get(id1Key3Name), pib::Pib::Error);
84 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080085
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040086 {
87 // create a container from an existing (non-empty) PibImpl
88 // names are loaded immediately but the key cache should initially be empty
89 KeyContainer container2(id1, pibImpl);
90 BOOST_CHECK_EQUAL(container2.size(), 2);
91 BOOST_CHECK_EQUAL(container2.m_keys.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080092
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040093 // fetching the keys should populate the cache
94 BOOST_CHECK_EQUAL(container2.get(id1Key1Name).getName(), id1Key1Name);
95 BOOST_CHECK_EQUAL(container2.size(), 2);
96 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080097
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040098 BOOST_CHECK_EQUAL(container2.get(id1Key2Name).getName(), id1Key2Name);
99 BOOST_CHECK_EQUAL(container2.size(), 2);
100 BOOST_CHECK_EQUAL(container2.m_keys.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400102 // remove a key
103 container2.remove(id1Key1Name);
104 BOOST_CHECK_EQUAL(container2.size(), 1);
105 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
106 BOOST_CHECK(container2.find(id1Key1Name) == container2.end());
107 BOOST_CHECK(container2.find(id1Key2Name) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108
Davide Pesavento07db0732022-05-06 15:20:26 -0400109 // removing the same key again is a no-op
110 container2.remove(id1Key1Name);
111 BOOST_CHECK_EQUAL(container2.size(), 1);
112 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
113 BOOST_CHECK(container2.find(id1Key1Name) == container2.end());
114 BOOST_CHECK(container2.find(id1Key2Name) != container2.end());
115
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400116 // remove another key
117 container2.remove(id1Key2Name);
118 BOOST_CHECK_EQUAL(container2.size(), 0);
119 BOOST_CHECK_EQUAL(container2.m_keys.size(), 0);
120 BOOST_CHECK(container2.find(id1Key2Name) == container2.end());
121 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800122}
123
124BOOST_AUTO_TEST_CASE(Errors)
125{
126 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800127 KeyContainer container(id1, pibImpl);
128
Davide Pesavento765abc92021-12-27 00:44:04 -0500129 BOOST_CHECK_THROW(container.add(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 BOOST_CHECK_THROW(container.remove(id2Key1Name), std::invalid_argument);
131 BOOST_CHECK_THROW(container.get(id2Key1Name), std::invalid_argument);
132}
133
134BOOST_AUTO_TEST_CASE(Iterator)
135{
136 auto pibImpl = make_shared<PibMemory>();
137 KeyContainer container(id1, pibImpl);
Davide Pesavento765abc92021-12-27 00:44:04 -0500138 container.add(id1Key1, id1Key1Name);
139 container.add(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800140
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400141 const std::set<Name> keyNames{id1Key1Name, id1Key2Name};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700142
143 KeyContainer::const_iterator it = container.begin();
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400144 auto testIt = keyNames.begin();
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700145 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700146 it++;
147 testIt++;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700148 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700149 ++it;
150 testIt++;
151 BOOST_CHECK(it == container.end());
152
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400153 // test range-based for
154 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155 testIt = keyNames.begin();
156 for (const auto& key : container) {
157 BOOST_CHECK_EQUAL(key.getIdentity(), id1);
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700158 BOOST_CHECK_EQUAL(key.getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700159 testIt++;
160 count++;
161 }
162 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800163
164 BOOST_CHECK(KeyContainer::const_iterator() == KeyContainer::const_iterator());
165 BOOST_CHECK(KeyContainer::const_iterator() == container.end());
166 BOOST_CHECK(container.end() == KeyContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700167}
168
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100169BOOST_AUTO_TEST_SUITE_END() // TestKeyContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100171BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700172
173} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700174} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700175} // namespace security
176} // namespace ndn