blob: b1d43438e84d65777a501422a47d991499350516 [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
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(CertificateContainer::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(TestCertificateContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039
Davide Pesaventob99c7112022-05-01 18:53:23 -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 Pesaventob99c7112022-05-01 18:53:23 -040044 {
45 // start with an empty container
46 CertificateContainer container(id1Key1Name, pibImpl);
47 BOOST_CHECK_EQUAL(container.size(), 0);
48 BOOST_CHECK_EQUAL(container.m_certs.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070049
Davide Pesaventob99c7112022-05-01 18:53:23 -040050 // add one cert
51 container.add(id1Key1Cert1);
52 BOOST_CHECK_EQUAL(container.size(), 1);
53 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
54 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080055
Davide Pesaventob99c7112022-05-01 18:53:23 -040056 // add the same cert again
57 container.add(id1Key1Cert1);
58 BOOST_CHECK_EQUAL(container.size(), 1);
59 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
60 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080061
Davide Pesaventob99c7112022-05-01 18:53:23 -040062 // add another cert
63 container.add(id1Key1Cert2);
64 BOOST_CHECK_EQUAL(container.size(), 2);
65 BOOST_CHECK_EQUAL(container.m_certs.size(), 2);
66 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
67 BOOST_CHECK(container.find(id1Key1Cert2.getName()) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070068
Davide Pesaventob99c7112022-05-01 18:53:23 -040069 // check certs
70 Certificate cert1 = container.get(id1Key1Cert1.getName());
71 Certificate cert2 = container.get(id1Key1Cert2.getName());
72 BOOST_CHECK_EQUAL(cert1, id1Key1Cert1);
73 BOOST_CHECK_EQUAL(cert2, id1Key1Cert2);
74 Name id1Key1Cert3Name = Name(id1Key1Name).append("issuer").appendVersion(3);
75 BOOST_CHECK_THROW(container.get(id1Key1Cert3Name), pib::Pib::Error);
76 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080077
Davide Pesaventob99c7112022-05-01 18:53:23 -040078 {
79 // create a container from an existing (non-empty) PibImpl
80 // names are loaded immediately but the certificate cache should initially be empty
81 CertificateContainer container2(id1Key1Name, pibImpl);
82 BOOST_CHECK_EQUAL(container2.size(), 2);
83 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080084
Davide Pesaventob99c7112022-05-01 18:53:23 -040085 // fetching the certificates should populate the cache
86 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert1.getName()), id1Key1Cert1);
87 BOOST_CHECK_EQUAL(container2.size(), 2);
88 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080089
Davide Pesaventob99c7112022-05-01 18:53:23 -040090 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert2.getName()), id1Key1Cert2);
91 BOOST_CHECK_EQUAL(container2.size(), 2);
92 BOOST_CHECK_EQUAL(container2.m_certs.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080093
Davide Pesaventob99c7112022-05-01 18:53:23 -040094 // remove a certificate
95 container2.remove(id1Key1Cert1.getName());
96 BOOST_CHECK_EQUAL(container2.size(), 1);
97 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
98 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
99 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800100
Davide Pesavento07db0732022-05-06 15:20:26 -0400101 // removing the same certificate again is a no-op
102 container2.remove(id1Key1Cert1.getName());
103 BOOST_CHECK_EQUAL(container2.size(), 1);
104 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
105 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
106 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
107
Davide Pesaventob99c7112022-05-01 18:53:23 -0400108 // remove another certificate
109 container2.remove(id1Key1Cert2.getName());
110 BOOST_CHECK_EQUAL(container2.size(), 0);
111 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
112 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) == container2.end());
113 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800114}
115
116BOOST_AUTO_TEST_CASE(Errors)
117{
118 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800119 CertificateContainer container(id1Key1Name, pibImpl);
120
121 BOOST_CHECK_THROW(container.add(id1Key2Cert1), std::invalid_argument);
122 BOOST_CHECK_THROW(container.remove(id1Key2Cert1.getName()), std::invalid_argument);
123 BOOST_CHECK_THROW(container.get(id1Key2Cert1.getName()), std::invalid_argument);
124}
125
126BOOST_AUTO_TEST_CASE(Iterator)
127{
128 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800129 CertificateContainer container(id1Key1Name, pibImpl);
130 container.add(id1Key1Cert1);
131 container.add(id1Key1Cert2);
132
Davide Pesaventob99c7112022-05-01 18:53:23 -0400133 const std::set<Name> certNames{id1Key1Cert1.getName(), id1Key1Cert2.getName()};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700134
Davide Pesaventob99c7112022-05-01 18:53:23 -0400135 CertificateContainer::const_iterator it = container.begin();
Davide Pesaventof2cae612021-03-24 18:47:05 -0400136 auto testIt = certNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700137 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
138 it++;
139 testIt++;
140 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
141 ++it;
142 testIt++;
143 BOOST_CHECK(it == container.end());
144
Davide Pesaventob99c7112022-05-01 18:53:23 -0400145 // test range-based for
146 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147 testIt = certNames.begin();
148 for (const auto& cert : container) {
149 BOOST_CHECK_EQUAL(cert.getName(), *testIt);
150 testIt++;
151 count++;
152 }
153 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800154
155 BOOST_CHECK(CertificateContainer::const_iterator() == CertificateContainer::const_iterator());
156 BOOST_CHECK(CertificateContainer::const_iterator() == container.end());
157 BOOST_CHECK(container.end() == CertificateContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158}
159
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100160BOOST_AUTO_TEST_SUITE_END() // TestCertificateContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100162BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163
164} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700165} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700166} // namespace security
167} // namespace ndn