blob: 84a4d3f03862389a8b33953d99552f1c0541f592 [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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::security::pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -070032
Davide Pesavento152ef442023-04-22 02:02:29 -040033NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yucbe72b02015-11-25 17:35:37 -080036BOOST_FIXTURE_TEST_SUITE(TestKeyContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070037
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040038BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070040 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070041
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040042 {
43 // start with an empty container
44 KeyContainer container(id1, pibImpl);
45 BOOST_CHECK_EQUAL(container.size(), 0);
46 BOOST_CHECK_EQUAL(container.m_keys.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070047
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040048 // add the first key
49 Key key11 = container.add(id1Key1, id1Key1Name);
50 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
51 BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element());
52 BOOST_CHECK_EQUAL(container.size(), 1);
53 BOOST_CHECK_EQUAL(container.m_keys.size(), 1);
54 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070055
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040056 // add the same key again
57 Key key12 = container.add(id1Key1, id1Key1Name);
58 BOOST_CHECK_EQUAL(key12.getName(), id1Key1Name);
59 BOOST_TEST(key12.getPublicKey() == id1Key1, boost::test_tools::per_element());
60 BOOST_CHECK_EQUAL(container.size(), 1);
61 BOOST_CHECK_EQUAL(container.m_keys.size(), 1);
62 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080063
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040064 // add the second key
65 Key key21 = container.add(id1Key2, id1Key2Name);
66 BOOST_CHECK_EQUAL(key21.getName(), id1Key2Name);
67 BOOST_TEST(key21.getPublicKey() == id1Key2, boost::test_tools::per_element());
68 BOOST_CHECK_EQUAL(container.size(), 2);
69 BOOST_CHECK_EQUAL(container.m_keys.size(), 2);
70 BOOST_CHECK(container.find(id1Key1Name) != container.end());
71 BOOST_CHECK(container.find(id1Key2Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070072
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040073 // check keys
74 Key key1 = container.get(id1Key1Name);
75 Key key2 = container.get(id1Key2Name);
76 BOOST_CHECK_EQUAL(key1.getName(), id1Key1Name);
77 BOOST_TEST(key1.getPublicKey() == id1Key1, boost::test_tools::per_element());
78 BOOST_CHECK_EQUAL(key2.getName(), id1Key2Name);
79 BOOST_TEST(key2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040080 Name id1Key3Name = security::constructKeyName(id1, name::Component("non-existing-id"));
81 BOOST_CHECK_THROW(container.get(id1Key3Name), Pib::Error);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040082 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080083
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040084 {
85 // create a container from an existing (non-empty) PibImpl
86 // names are loaded immediately but the key cache should initially be empty
87 KeyContainer container2(id1, pibImpl);
88 BOOST_CHECK_EQUAL(container2.size(), 2);
89 BOOST_CHECK_EQUAL(container2.m_keys.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080090
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040091 // fetching the keys should populate the cache
92 BOOST_CHECK_EQUAL(container2.get(id1Key1Name).getName(), id1Key1Name);
93 BOOST_CHECK_EQUAL(container2.size(), 2);
94 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080095
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040096 BOOST_CHECK_EQUAL(container2.get(id1Key2Name).getName(), id1Key2Name);
97 BOOST_CHECK_EQUAL(container2.size(), 2);
98 BOOST_CHECK_EQUAL(container2.m_keys.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080099
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400100 // remove a key
101 container2.remove(id1Key1Name);
102 BOOST_CHECK_EQUAL(container2.size(), 1);
103 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
104 BOOST_CHECK(container2.find(id1Key1Name) == container2.end());
105 BOOST_CHECK(container2.find(id1Key2Name) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106
Davide Pesavento07db0732022-05-06 15:20:26 -0400107 // removing the same key again is a no-op
108 container2.remove(id1Key1Name);
109 BOOST_CHECK_EQUAL(container2.size(), 1);
110 BOOST_CHECK_EQUAL(container2.m_keys.size(), 1);
111 BOOST_CHECK(container2.find(id1Key1Name) == container2.end());
112 BOOST_CHECK(container2.find(id1Key2Name) != container2.end());
113
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400114 // remove another key
115 container2.remove(id1Key2Name);
116 BOOST_CHECK_EQUAL(container2.size(), 0);
117 BOOST_CHECK_EQUAL(container2.m_keys.size(), 0);
118 BOOST_CHECK(container2.find(id1Key2Name) == container2.end());
119 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800120}
121
122BOOST_AUTO_TEST_CASE(Errors)
123{
124 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 KeyContainer container(id1, pibImpl);
126
Davide Pesavento765abc92021-12-27 00:44:04 -0500127 BOOST_CHECK_THROW(container.add(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800128 BOOST_CHECK_THROW(container.remove(id2Key1Name), std::invalid_argument);
129 BOOST_CHECK_THROW(container.get(id2Key1Name), std::invalid_argument);
130}
131
132BOOST_AUTO_TEST_CASE(Iterator)
133{
134 auto pibImpl = make_shared<PibMemory>();
135 KeyContainer container(id1, pibImpl);
Davide Pesavento765abc92021-12-27 00:44:04 -0500136 container.add(id1Key1, id1Key1Name);
137 container.add(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400139 const std::set<Name> keyNames{id1Key1Name, id1Key2Name};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700140
141 KeyContainer::const_iterator it = container.begin();
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400142 auto testIt = keyNames.begin();
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700143 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144 it++;
145 testIt++;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700146 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147 ++it;
148 testIt++;
149 BOOST_CHECK(it == container.end());
150
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400151 // test range-based for
152 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153 testIt = keyNames.begin();
154 for (const auto& key : container) {
155 BOOST_CHECK_EQUAL(key.getIdentity(), id1);
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700156 BOOST_CHECK_EQUAL(key.getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157 testIt++;
158 count++;
159 }
160 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800161
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 Yub8f8b342015-04-27 11:06:42 -0700165}
166
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100167BOOST_AUTO_TEST_SUITE_END() // TestKeyContainer
168BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700169
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400170} // namespace ndn::tests