blob: 0a00adf0d1e471796179d0284de4f14fad7467cf [file] [log] [blame]
Qiuhan Ding609f0612015-11-04 14:07:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Qiuhan Ding609f0612015-11-04 14:07:14 -08004 *
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 Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/certificate-cache.hpp"
Qiuhan Ding609f0612015-11-04 14:07:14 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
26#include "tests/unit/clock-fixture.hpp"
Qiuhan Ding609f0612015-11-04 14:07:14 -080027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Qiuhan Ding609f0612015-11-04 14:07:14 -080029
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050030class CertificateCacheFixture : public ClockFixture, public KeyChainFixture
Qiuhan Ding609f0612015-11-04 14:07:14 -080031{
32public:
33 CertificateCacheFixture()
Qiuhan Ding609f0612015-11-04 14:07:14 -080034 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035 identity = m_keyChain.createIdentity("/TestCertificateCache");
Qiuhan Ding609f0612015-11-04 14:07:14 -080036 cert = identity.getDefaultKey().getDefaultCertificate();
37 }
38
Junxiao Shi9ee770b2022-04-25 23:33:33 +000039 void
Davide Pesavento0c526032024-01-31 21:14:01 -050040 checkFindByInterest(const Name& name, bool canBePrefix,
41 const std::optional<Certificate>& expected) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +000042 {
43 Interest interest(name);
44 interest.setCanBePrefix(canBePrefix);
Davide Pesavento0c526032024-01-31 21:14:01 -050045 BOOST_TEST_INFO_SCOPE("Interest = " << interest);
46
47 auto found = certCache.find(interest);
48 if (expected) {
49 BOOST_REQUIRE(found != nullptr);
50 BOOST_CHECK_EQUAL(found->getName(), expected->getName());
51 }
52 else {
53 BOOST_CHECK(found == nullptr);
Junxiao Shi9ee770b2022-04-25 23:33:33 +000054 }
55 }
56
Qiuhan Ding609f0612015-11-04 14:07:14 -080057public:
Davide Pesavento0c526032024-01-31 21:14:01 -050058 security::CertificateCache certCache{10_s};
Qiuhan Ding609f0612015-11-04 14:07:14 -080059 Identity identity;
60 Certificate cert;
61};
62
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050063BOOST_AUTO_TEST_SUITE(Security)
Qiuhan Ding609f0612015-11-04 14:07:14 -080064BOOST_FIXTURE_TEST_SUITE(TestCertificateCache, CertificateCacheFixture)
65
66BOOST_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 Pesavento0f830802018-01-16 23:58:58 -050073 advanceClocks(11_s, 1);
Qiuhan Ding609f0612015-11-04 14:07:14 -080074 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 Pesavento0f830802018-01-16 23:58:58 -050079 advanceClocks(5_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080080 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
81
Davide Pesavento0f830802018-01-16 23:58:58 -050082 advanceClocks(15_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080083 BOOST_CHECK(certCache.find(cert.getName()) == nullptr);
84}
85
86BOOST_AUTO_TEST_CASE(FindByInterest)
87{
88 BOOST_CHECK_NO_THROW(certCache.insert(cert));
89
Junxiao Shi9ee770b2022-04-25 23:33:33 +000090 checkFindByInterest(cert.getIdentity(), true, cert);
91 checkFindByInterest(cert.getKeyName(), true, cert);
92 checkFindByInterest(cert.getName(), false, cert);
Davide Pesaventof6b45892023-03-13 15:00:51 -040093 checkFindByInterest(Name(cert.getName()).appendVersion(), true, std::nullopt);
Qiuhan Ding609f0612015-11-04 14:07:14 -080094
Davide Pesavento0f830802018-01-16 23:58:58 -050095 advanceClocks(12_s);
Davide Pesaventof6b45892023-03-13 15:00:51 -040096 checkFindByInterest(cert.getIdentity(), true, std::nullopt);
Qiuhan Ding609f0612015-11-04 14:07:14 -080097}
98
99BOOST_AUTO_TEST_SUITE_END() // TestCertificateCache
Qiuhan Ding609f0612015-11-04 14:07:14 -0800100BOOST_AUTO_TEST_SUITE_END() // Security
101
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400102} // namespace ndn::tests