Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/certificate-container.hpp" |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/pib-memory.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/pib/pib.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
| 27 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 31 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 34 | using namespace ndn::security::tests; |
| 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Pib) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestCertificateContainer, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 40 | using pib::Pib; |
| 41 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(Basic) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 44 | auto pibImpl = make_shared<PibMemory>(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 45 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 46 | // start with an empty container |
| 47 | CertificateContainer container(id1Key1Name, pibImpl); |
| 48 | BOOST_CHECK_EQUAL(container.size(), 0); |
| 49 | BOOST_CHECK_EQUAL(container.getCache().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 51 | // add one cert |
| 52 | container.add(id1Key1Cert1); |
| 53 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 54 | BOOST_CHECK_EQUAL(container.getCache().size(), 1); |
| 55 | BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end()); |
| 56 | |
| 57 | // add the same cert again |
| 58 | container.add(id1Key1Cert1); |
| 59 | BOOST_CHECK_EQUAL(container.size(), 1); |
| 60 | BOOST_CHECK_EQUAL(container.getCache().size(), 1); |
| 61 | BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end()); |
| 62 | |
| 63 | // add another cert |
| 64 | container.add(id1Key1Cert2); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(container.size(), 2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(container.getCache().size(), 2); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 67 | BOOST_CHECK(container.find(id1Key1Cert1.getName()) != container.end()); |
| 68 | BOOST_CHECK(container.find(id1Key1Cert2.getName()) != container.end()); |
| 69 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | // get certs |
| 71 | BOOST_REQUIRE_NO_THROW(container.get(id1Key1Cert1.getName())); |
| 72 | BOOST_REQUIRE_NO_THROW(container.get(id1Key1Cert2.getName())); |
| 73 | Name id1Key1Cert3Name = id1Key1Name; |
| 74 | id1Key1Cert3Name.append("issuer").appendVersion(3); |
| 75 | BOOST_CHECK_THROW(container.get(id1Key1Cert3Name), Pib::Error); |
| 76 | |
| 77 | // check cert |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 78 | Certificate cert1 = container.get(id1Key1Cert1.getName()); |
| 79 | Certificate cert2 = container.get(id1Key1Cert2.getName()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(cert1, id1Key1Cert1); |
| 81 | BOOST_CHECK_EQUAL(cert2, id1Key1Cert2); |
| 82 | |
| 83 | // create another container from the same PibImpl |
| 84 | // cache should be empty |
| 85 | CertificateContainer container2(id1Key1Name, pibImpl); |
| 86 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 87 | BOOST_CHECK_EQUAL(container2.getCache().size(), 0); |
| 88 | |
| 89 | // get certificate, cache should be filled |
| 90 | BOOST_REQUIRE_NO_THROW(container2.get(id1Key1Cert1.getName())); |
| 91 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 92 | BOOST_CHECK_EQUAL(container2.getCache().size(), 1); |
| 93 | |
| 94 | BOOST_REQUIRE_NO_THROW(container2.get(id1Key1Cert2.getName())); |
| 95 | BOOST_CHECK_EQUAL(container2.size(), 2); |
| 96 | BOOST_CHECK_EQUAL(container2.getCache().size(), 2); |
| 97 | |
| 98 | // remove a certificate |
| 99 | container2.remove(id1Key1Cert1.getName()); |
| 100 | BOOST_CHECK_EQUAL(container2.size(), 1); |
| 101 | BOOST_CHECK_EQUAL(container2.getCache().size(), 1); |
| 102 | BOOST_CHECK(container2.find(id1Key1Cert1.getName()) == container2.end()); |
| 103 | BOOST_CHECK(container2.find(id1Key1Cert2.getName()) != container2.end()); |
| 104 | |
| 105 | // remove another certificate |
| 106 | container2.remove(id1Key1Cert2.getName()); |
| 107 | BOOST_CHECK_EQUAL(container2.size(), 0); |
| 108 | BOOST_CHECK_EQUAL(container2.getCache().size(), 0); |
| 109 | BOOST_CHECK(container2.find(id1Key1Cert2.getName()) == container2.end()); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(Errors) |
| 113 | { |
| 114 | auto pibImpl = make_shared<PibMemory>(); |
| 115 | |
| 116 | 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 | |
| 123 | BOOST_AUTO_TEST_CASE(Iterator) |
| 124 | { |
| 125 | auto pibImpl = make_shared<PibMemory>(); |
| 126 | |
| 127 | // start with an empty container |
| 128 | CertificateContainer container(id1Key1Name, pibImpl); |
| 129 | container.add(id1Key1Cert1); |
| 130 | container.add(id1Key1Cert2); |
| 131 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | std::set<Name> certNames; |
| 133 | certNames.insert(id1Key1Cert1.getName()); |
| 134 | certNames.insert(id1Key1Cert2.getName()); |
| 135 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 136 | auto it = container.begin(); |
| 137 | auto testIt = certNames.begin(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
| 139 | it++; |
| 140 | testIt++; |
| 141 | BOOST_CHECK_EQUAL((*it).getName(), *testIt); |
| 142 | ++it; |
| 143 | testIt++; |
| 144 | BOOST_CHECK(it == container.end()); |
| 145 | |
| 146 | size_t count = 0; |
| 147 | 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 154 | |
| 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 160 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateContainer |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 161 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 162 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 163 | |
| 164 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 165 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 166 | } // namespace security |
| 167 | } // namespace ndn |