blob: 117e8f2c8d2b00ddc1d1e5e85e1029c3800efb85 [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 Pesaventob99c7112022-05-01 18:53:23 -04003 * Copyright (c) 2013-2022 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"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070027
28namespace ndn {
29namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070030namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070031namespace tests {
32
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033using namespace ndn::security::tests;
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070036BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080037BOOST_FIXTURE_TEST_SUITE(TestCertificateContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038
Davide Pesaventob99c7112022-05-01 18:53:23 -040039BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070040{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070041 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070042
Davide Pesaventob99c7112022-05-01 18:53:23 -040043 {
44 // start with an empty container
45 CertificateContainer container(id1Key1Name, pibImpl);
46 BOOST_CHECK_EQUAL(container.size(), 0);
47 BOOST_CHECK_EQUAL(container.m_certs.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070048
Davide Pesaventob99c7112022-05-01 18:53:23 -040049 // add one cert
50 container.add(id1Key1Cert1);
51 BOOST_CHECK_EQUAL(container.size(), 1);
52 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
53 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080054
Davide Pesaventob99c7112022-05-01 18:53:23 -040055 // add the same cert again
56 container.add(id1Key1Cert1);
57 BOOST_CHECK_EQUAL(container.size(), 1);
58 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
59 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080060
Davide Pesaventob99c7112022-05-01 18:53:23 -040061 // add another cert
62 container.add(id1Key1Cert2);
63 BOOST_CHECK_EQUAL(container.size(), 2);
64 BOOST_CHECK_EQUAL(container.m_certs.size(), 2);
65 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
66 BOOST_CHECK(container.find(id1Key1Cert2.getName()) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070067
Davide Pesaventob99c7112022-05-01 18:53:23 -040068 // check certs
69 Certificate cert1 = container.get(id1Key1Cert1.getName());
70 Certificate cert2 = container.get(id1Key1Cert2.getName());
71 BOOST_CHECK_EQUAL(cert1, id1Key1Cert1);
72 BOOST_CHECK_EQUAL(cert2, id1Key1Cert2);
73 Name id1Key1Cert3Name = Name(id1Key1Name).append("issuer").appendVersion(3);
74 BOOST_CHECK_THROW(container.get(id1Key1Cert3Name), pib::Pib::Error);
75 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080076
Davide Pesaventob99c7112022-05-01 18:53:23 -040077 {
78 // create a container from an existing (non-empty) PibImpl
79 // names are loaded immediately but the certificate cache should initially be empty
80 CertificateContainer container2(id1Key1Name, pibImpl);
81 BOOST_CHECK_EQUAL(container2.size(), 2);
82 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080083
Davide Pesaventob99c7112022-05-01 18:53:23 -040084 // fetching the certificates should populate the cache
85 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert1.getName()), id1Key1Cert1);
86 BOOST_CHECK_EQUAL(container2.size(), 2);
87 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080088
Davide Pesaventob99c7112022-05-01 18:53:23 -040089 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert2.getName()), id1Key1Cert2);
90 BOOST_CHECK_EQUAL(container2.size(), 2);
91 BOOST_CHECK_EQUAL(container2.m_certs.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080092
Davide Pesaventob99c7112022-05-01 18:53:23 -040093 // remove a certificate
94 container2.remove(id1Key1Cert1.getName());
95 BOOST_CHECK_EQUAL(container2.size(), 1);
96 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
97 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
98 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080099
Davide Pesaventob99c7112022-05-01 18:53:23 -0400100 // remove another certificate
101 container2.remove(id1Key1Cert2.getName());
102 BOOST_CHECK_EQUAL(container2.size(), 0);
103 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
104 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) == container2.end());
105 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106}
107
108BOOST_AUTO_TEST_CASE(Errors)
109{
110 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 CertificateContainer container(id1Key1Name, pibImpl);
112
113 BOOST_CHECK_THROW(container.add(id1Key2Cert1), std::invalid_argument);
114 BOOST_CHECK_THROW(container.remove(id1Key2Cert1.getName()), std::invalid_argument);
115 BOOST_CHECK_THROW(container.get(id1Key2Cert1.getName()), std::invalid_argument);
116}
117
118BOOST_AUTO_TEST_CASE(Iterator)
119{
120 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800121 CertificateContainer container(id1Key1Name, pibImpl);
122 container.add(id1Key1Cert1);
123 container.add(id1Key1Cert2);
124
Davide Pesaventob99c7112022-05-01 18:53:23 -0400125 const std::set<Name> certNames{id1Key1Cert1.getName(), id1Key1Cert2.getName()};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700126
Davide Pesaventob99c7112022-05-01 18:53:23 -0400127 CertificateContainer::const_iterator it = container.begin();
Davide Pesaventof2cae612021-03-24 18:47:05 -0400128 auto testIt = certNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700129 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
130 it++;
131 testIt++;
132 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
133 ++it;
134 testIt++;
135 BOOST_CHECK(it == container.end());
136
Davide Pesaventob99c7112022-05-01 18:53:23 -0400137 // test range-based for
138 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700139 testIt = certNames.begin();
140 for (const auto& cert : container) {
141 BOOST_CHECK_EQUAL(cert.getName(), *testIt);
142 testIt++;
143 count++;
144 }
145 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146
147 BOOST_CHECK(CertificateContainer::const_iterator() == CertificateContainer::const_iterator());
148 BOOST_CHECK(CertificateContainer::const_iterator() == container.end());
149 BOOST_CHECK(container.end() == CertificateContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150}
151
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100152BOOST_AUTO_TEST_SUITE_END() // TestCertificateContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700153BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100154BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155
156} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158} // namespace security
159} // namespace ndn