blob: ecfd0b93fea50f154cb5e0128963f72c49dc5c78 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
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/certificate-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(CertificateContainer::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(TestCertificateContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070037
Davide Pesaventob99c7112022-05-01 18:53:23 -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 Pesaventob99c7112022-05-01 18:53:23 -040042 {
43 // start with an empty container
44 CertificateContainer container(id1Key1Name, pibImpl);
45 BOOST_CHECK_EQUAL(container.size(), 0);
46 BOOST_CHECK_EQUAL(container.m_certs.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070047
Davide Pesaventob99c7112022-05-01 18:53:23 -040048 // add one cert
49 container.add(id1Key1Cert1);
50 BOOST_CHECK_EQUAL(container.size(), 1);
51 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
52 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080053
Davide Pesaventob99c7112022-05-01 18:53:23 -040054 // add the same cert again
55 container.add(id1Key1Cert1);
56 BOOST_CHECK_EQUAL(container.size(), 1);
57 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
58 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080059
Davide Pesaventob99c7112022-05-01 18:53:23 -040060 // add another cert
61 container.add(id1Key1Cert2);
62 BOOST_CHECK_EQUAL(container.size(), 2);
63 BOOST_CHECK_EQUAL(container.m_certs.size(), 2);
64 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
65 BOOST_CHECK(container.find(id1Key1Cert2.getName()) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070066
Davide Pesaventob99c7112022-05-01 18:53:23 -040067 // check certs
68 Certificate cert1 = container.get(id1Key1Cert1.getName());
69 Certificate cert2 = container.get(id1Key1Cert2.getName());
70 BOOST_CHECK_EQUAL(cert1, id1Key1Cert1);
71 BOOST_CHECK_EQUAL(cert2, id1Key1Cert2);
72 Name id1Key1Cert3Name = Name(id1Key1Name).append("issuer").appendVersion(3);
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040073 BOOST_CHECK_THROW(container.get(id1Key1Cert3Name), Pib::Error);
Davide Pesaventob99c7112022-05-01 18:53:23 -040074 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080075
Davide Pesaventob99c7112022-05-01 18:53:23 -040076 {
77 // create a container from an existing (non-empty) PibImpl
78 // names are loaded immediately but the certificate cache should initially be empty
79 CertificateContainer container2(id1Key1Name, pibImpl);
80 BOOST_CHECK_EQUAL(container2.size(), 2);
81 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080082
Davide Pesaventob99c7112022-05-01 18:53:23 -040083 // fetching the certificates should populate the cache
84 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert1.getName()), id1Key1Cert1);
85 BOOST_CHECK_EQUAL(container2.size(), 2);
86 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080087
Davide Pesaventob99c7112022-05-01 18:53:23 -040088 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert2.getName()), id1Key1Cert2);
89 BOOST_CHECK_EQUAL(container2.size(), 2);
90 BOOST_CHECK_EQUAL(container2.m_certs.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080091
Davide Pesaventob99c7112022-05-01 18:53:23 -040092 // remove a certificate
93 container2.remove(id1Key1Cert1.getName());
94 BOOST_CHECK_EQUAL(container2.size(), 1);
95 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
96 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
97 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080098
Davide Pesavento07db0732022-05-06 15:20:26 -040099 // removing the same certificate again is a no-op
100 container2.remove(id1Key1Cert1.getName());
101 BOOST_CHECK_EQUAL(container2.size(), 1);
102 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
103 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
104 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
105
Davide Pesaventob99c7112022-05-01 18:53:23 -0400106 // remove another certificate
107 container2.remove(id1Key1Cert2.getName());
108 BOOST_CHECK_EQUAL(container2.size(), 0);
109 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
110 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) == container2.end());
111 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800112}
113
114BOOST_AUTO_TEST_CASE(Errors)
115{
116 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800117 CertificateContainer container(id1Key1Name, pibImpl);
118
119 BOOST_CHECK_THROW(container.add(id1Key2Cert1), std::invalid_argument);
120 BOOST_CHECK_THROW(container.remove(id1Key2Cert1.getName()), std::invalid_argument);
121 BOOST_CHECK_THROW(container.get(id1Key2Cert1.getName()), std::invalid_argument);
122}
123
124BOOST_AUTO_TEST_CASE(Iterator)
125{
126 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800127 CertificateContainer container(id1Key1Name, pibImpl);
128 container.add(id1Key1Cert1);
129 container.add(id1Key1Cert2);
130
Davide Pesaventob99c7112022-05-01 18:53:23 -0400131 const std::set<Name> certNames{id1Key1Cert1.getName(), id1Key1Cert2.getName()};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700132
Davide Pesaventob99c7112022-05-01 18:53:23 -0400133 CertificateContainer::const_iterator it = container.begin();
Davide Pesaventof2cae612021-03-24 18:47:05 -0400134 auto testIt = certNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700135 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
136 it++;
137 testIt++;
138 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
139 ++it;
140 testIt++;
141 BOOST_CHECK(it == container.end());
142
Davide Pesaventob99c7112022-05-01 18:53:23 -0400143 // test range-based for
144 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145 testIt = certNames.begin();
146 for (const auto& cert : container) {
147 BOOST_CHECK_EQUAL(cert.getName(), *testIt);
148 testIt++;
149 count++;
150 }
151 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800152
153 BOOST_CHECK(CertificateContainer::const_iterator() == CertificateContainer::const_iterator());
154 BOOST_CHECK(CertificateContainer::const_iterator() == container.end());
155 BOOST_CHECK(container.end() == CertificateContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156}
157
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100158BOOST_AUTO_TEST_SUITE_END() // TestCertificateContainer
159BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400161} // namespace ndn::tests