blob: 55b013e37b447b6e53d0eda30cadff90eea447b7 [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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080035BOOST_FIXTURE_TEST_SUITE(TestCertificateContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070036
Davide Pesaventob99c7112022-05-01 18:53:23 -040037BOOST_AUTO_TEST_CASE(AddGetRemove)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070039 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
Davide Pesaventob99c7112022-05-01 18:53:23 -040041 {
42 // start with an empty container
43 CertificateContainer container(id1Key1Name, pibImpl);
44 BOOST_CHECK_EQUAL(container.size(), 0);
45 BOOST_CHECK_EQUAL(container.m_certs.size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070046
Davide Pesaventob99c7112022-05-01 18:53:23 -040047 // add one cert
48 container.add(id1Key1Cert1);
49 BOOST_CHECK_EQUAL(container.size(), 1);
50 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
51 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080052
Davide Pesaventob99c7112022-05-01 18:53:23 -040053 // add the same cert again
54 container.add(id1Key1Cert1);
55 BOOST_CHECK_EQUAL(container.size(), 1);
56 BOOST_CHECK_EQUAL(container.m_certs.size(), 1);
57 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080058
Davide Pesaventob99c7112022-05-01 18:53:23 -040059 // add another cert
60 container.add(id1Key1Cert2);
61 BOOST_CHECK_EQUAL(container.size(), 2);
62 BOOST_CHECK_EQUAL(container.m_certs.size(), 2);
63 BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end());
64 BOOST_CHECK(container.find(id1Key1Cert2.getName()) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070065
Davide Pesaventob99c7112022-05-01 18:53:23 -040066 // check certs
67 Certificate cert1 = container.get(id1Key1Cert1.getName());
68 Certificate cert2 = container.get(id1Key1Cert2.getName());
69 BOOST_CHECK_EQUAL(cert1, id1Key1Cert1);
70 BOOST_CHECK_EQUAL(cert2, id1Key1Cert2);
71 Name id1Key1Cert3Name = Name(id1Key1Name).append("issuer").appendVersion(3);
72 BOOST_CHECK_THROW(container.get(id1Key1Cert3Name), pib::Pib::Error);
73 }
Yingdi Yucbe72b02015-11-25 17:35:37 -080074
Davide Pesaventob99c7112022-05-01 18:53:23 -040075 {
76 // create a container from an existing (non-empty) PibImpl
77 // names are loaded immediately but the certificate cache should initially be empty
78 CertificateContainer container2(id1Key1Name, pibImpl);
79 BOOST_CHECK_EQUAL(container2.size(), 2);
80 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
Yingdi Yucbe72b02015-11-25 17:35:37 -080081
Davide Pesaventob99c7112022-05-01 18:53:23 -040082 // fetching the certificates should populate the cache
83 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert1.getName()), id1Key1Cert1);
84 BOOST_CHECK_EQUAL(container2.size(), 2);
85 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080086
Davide Pesaventob99c7112022-05-01 18:53:23 -040087 BOOST_CHECK_EQUAL(container2.get(id1Key1Cert2.getName()), id1Key1Cert2);
88 BOOST_CHECK_EQUAL(container2.size(), 2);
89 BOOST_CHECK_EQUAL(container2.m_certs.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080090
Davide Pesaventob99c7112022-05-01 18:53:23 -040091 // remove a certificate
92 container2.remove(id1Key1Cert1.getName());
93 BOOST_CHECK_EQUAL(container2.size(), 1);
94 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
95 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
96 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080097
Davide Pesavento07db0732022-05-06 15:20:26 -040098 // removing the same certificate again is a no-op
99 container2.remove(id1Key1Cert1.getName());
100 BOOST_CHECK_EQUAL(container2.size(), 1);
101 BOOST_CHECK_EQUAL(container2.m_certs.size(), 1);
102 BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end());
103 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end());
104
Davide Pesaventob99c7112022-05-01 18:53:23 -0400105 // remove another certificate
106 container2.remove(id1Key1Cert2.getName());
107 BOOST_CHECK_EQUAL(container2.size(), 0);
108 BOOST_CHECK_EQUAL(container2.m_certs.size(), 0);
109 BOOST_CHECK(container2.find(id1Key1Cert2.getName()) == container2.end());
110 }
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111}
112
113BOOST_AUTO_TEST_CASE(Errors)
114{
115 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800116 CertificateContainer container(id1Key1Name, pibImpl);
117
118 BOOST_CHECK_THROW(container.add(id1Key2Cert1), std::invalid_argument);
119 BOOST_CHECK_THROW(container.remove(id1Key2Cert1.getName()), std::invalid_argument);
120 BOOST_CHECK_THROW(container.get(id1Key2Cert1.getName()), std::invalid_argument);
121}
122
123BOOST_AUTO_TEST_CASE(Iterator)
124{
125 auto pibImpl = make_shared<PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800126 CertificateContainer container(id1Key1Name, pibImpl);
127 container.add(id1Key1Cert1);
128 container.add(id1Key1Cert2);
129
Davide Pesaventob99c7112022-05-01 18:53:23 -0400130 const std::set<Name> certNames{id1Key1Cert1.getName(), id1Key1Cert2.getName()};
Yingdi Yub8f8b342015-04-27 11:06:42 -0700131
Davide Pesaventob99c7112022-05-01 18:53:23 -0400132 CertificateContainer::const_iterator it = container.begin();
Davide Pesaventof2cae612021-03-24 18:47:05 -0400133 auto testIt = certNames.begin();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700134 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
135 it++;
136 testIt++;
137 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
138 ++it;
139 testIt++;
140 BOOST_CHECK(it == container.end());
141
Davide Pesaventob99c7112022-05-01 18:53:23 -0400142 // test range-based for
143 int count = 0;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144 testIt = certNames.begin();
145 for (const auto& cert : container) {
146 BOOST_CHECK_EQUAL(cert.getName(), *testIt);
147 testIt++;
148 count++;
149 }
150 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800151
152 BOOST_CHECK(CertificateContainer::const_iterator() == CertificateContainer::const_iterator());
153 BOOST_CHECK(CertificateContainer::const_iterator() == container.end());
154 BOOST_CHECK(container.end() == CertificateContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155}
156
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100157BOOST_AUTO_TEST_SUITE_END() // TestCertificateContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700158BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100159BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160
161} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700162} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163} // namespace security
164} // namespace ndn