Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [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 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/certificate-cache.hpp" |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | #include "tests/unit/clock-fixture.hpp" |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 30 | class CertificateCacheFixture : public ClockFixture, public KeyChainFixture |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 31 | { |
| 32 | public: |
| 33 | CertificateCacheFixture() |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 34 | : certCache(10_s) |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 35 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 36 | identity = m_keyChain.createIdentity("/TestCertificateCache"); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 37 | cert = identity.getDefaultKey().getDefaultCertificate(); |
| 38 | } |
| 39 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 40 | void |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 41 | checkFindByInterest(const Name& name, bool canBePrefix, std::optional<Certificate> expected) const |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 42 | { |
| 43 | Interest interest(name); |
| 44 | interest.setCanBePrefix(canBePrefix); |
| 45 | BOOST_TEST_CONTEXT(interest) { |
| 46 | auto found = certCache.find(interest); |
| 47 | if (expected) { |
| 48 | BOOST_REQUIRE(found != nullptr); |
| 49 | BOOST_CHECK_EQUAL(found->getName(), expected->getName()); |
| 50 | } |
| 51 | else { |
| 52 | BOOST_CHECK(found == nullptr); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 57 | public: |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 58 | security::CertificateCache certCache; |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 59 | Identity identity; |
| 60 | Certificate cert; |
| 61 | }; |
| 62 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 63 | BOOST_AUTO_TEST_SUITE(Security) |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 64 | BOOST_FIXTURE_TEST_SUITE(TestCertificateCache, CertificateCacheFixture) |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(RemovalTime) |
| 67 | { |
| 68 | // Cache lifetime is capped to 10 seconds during cache construction |
| 69 | |
| 70 | BOOST_CHECK_NO_THROW(certCache.insert(cert)); |
| 71 | BOOST_CHECK(certCache.find(cert.getName()) != nullptr); |
| 72 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 73 | advanceClocks(11_s, 1); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 74 | BOOST_CHECK(certCache.find(cert.getName()) == nullptr); |
| 75 | |
| 76 | BOOST_CHECK_NO_THROW(certCache.insert(cert)); |
| 77 | BOOST_CHECK(certCache.find(cert.getName()) != nullptr); |
| 78 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 79 | advanceClocks(5_s); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 80 | BOOST_CHECK(certCache.find(cert.getName()) != nullptr); |
| 81 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 82 | advanceClocks(15_s); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 83 | BOOST_CHECK(certCache.find(cert.getName()) == nullptr); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(FindByInterest) |
| 87 | { |
| 88 | BOOST_CHECK_NO_THROW(certCache.insert(cert)); |
| 89 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 90 | checkFindByInterest(cert.getIdentity(), true, cert); |
| 91 | checkFindByInterest(cert.getKeyName(), true, cert); |
| 92 | checkFindByInterest(cert.getName(), false, cert); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 93 | checkFindByInterest(Name(cert.getName()).appendVersion(), true, std::nullopt); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 94 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 95 | advanceClocks(12_s); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 96 | checkFindByInterest(cert.getIdentity(), true, std::nullopt); |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateCache |
Qiuhan Ding | 609f061 | 2015-11-04 14:07:14 -0800 | [diff] [blame] | 100 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 101 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 102 | } // namespace ndn::tests |