blob: 8c715ebe45381e2239353868f65817b98aa5b82c [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/*
Junxiao Shi9ee770b2022-04-25 23:33:33 +00003 * Copyright (c) 2013-2022 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
28namespace ndn {
29namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040030inline namespace v2 {
Qiuhan Ding609f0612015-11-04 14:07:14 -080031namespace tests {
32
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050033using namespace ndn::tests;
Qiuhan Ding609f0612015-11-04 14:07:14 -080034
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035class CertificateCacheFixture : public ClockFixture, public KeyChainFixture
Qiuhan Ding609f0612015-11-04 14:07:14 -080036{
37public:
38 CertificateCacheFixture()
Davide Pesavento0f830802018-01-16 23:58:58 -050039 : certCache(10_s)
Qiuhan Ding609f0612015-11-04 14:07:14 -080040 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041 identity = m_keyChain.createIdentity("/TestCertificateCache");
Qiuhan Ding609f0612015-11-04 14:07:14 -080042 cert = identity.getDefaultKey().getDefaultCertificate();
43 }
44
Junxiao Shi9ee770b2022-04-25 23:33:33 +000045 void
46 checkFindByInterest(const Name& name, bool canBePrefix, optional<Certificate> expected) const
47 {
48 Interest interest(name);
49 interest.setCanBePrefix(canBePrefix);
50 BOOST_TEST_CONTEXT(interest) {
51 auto found = certCache.find(interest);
52 if (expected) {
53 BOOST_REQUIRE(found != nullptr);
54 BOOST_CHECK_EQUAL(found->getName(), expected->getName());
55 }
56 else {
57 BOOST_CHECK(found == nullptr);
58 }
59 }
60 }
61
Qiuhan Ding609f0612015-11-04 14:07:14 -080062public:
63 CertificateCache certCache;
64 Identity identity;
65 Certificate cert;
66};
67
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050068BOOST_AUTO_TEST_SUITE(Security)
Qiuhan Ding609f0612015-11-04 14:07:14 -080069BOOST_FIXTURE_TEST_SUITE(TestCertificateCache, CertificateCacheFixture)
70
71BOOST_AUTO_TEST_CASE(RemovalTime)
72{
73 // Cache lifetime is capped to 10 seconds during cache construction
74
75 BOOST_CHECK_NO_THROW(certCache.insert(cert));
76 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
77
Davide Pesavento0f830802018-01-16 23:58:58 -050078 advanceClocks(11_s, 1);
Qiuhan Ding609f0612015-11-04 14:07:14 -080079 BOOST_CHECK(certCache.find(cert.getName()) == nullptr);
80
81 BOOST_CHECK_NO_THROW(certCache.insert(cert));
82 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
83
Davide Pesavento0f830802018-01-16 23:58:58 -050084 advanceClocks(5_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080085 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
86
Davide Pesavento0f830802018-01-16 23:58:58 -050087 advanceClocks(15_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080088 BOOST_CHECK(certCache.find(cert.getName()) == nullptr);
89}
90
91BOOST_AUTO_TEST_CASE(FindByInterest)
92{
93 BOOST_CHECK_NO_THROW(certCache.insert(cert));
94
Junxiao Shi9ee770b2022-04-25 23:33:33 +000095 checkFindByInterest(cert.getIdentity(), true, cert);
96 checkFindByInterest(cert.getKeyName(), true, cert);
97 checkFindByInterest(cert.getName(), false, cert);
98 checkFindByInterest(Name(cert.getName()).appendVersion(), true, nullopt);
Qiuhan Ding609f0612015-11-04 14:07:14 -080099
Davide Pesavento0f830802018-01-16 23:58:58 -0500100 advanceClocks(12_s);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000101 checkFindByInterest(cert.getIdentity(), true, nullopt);
Qiuhan Ding609f0612015-11-04 14:07:14 -0800102}
103
104BOOST_AUTO_TEST_SUITE_END() // TestCertificateCache
Qiuhan Ding609f0612015-11-04 14:07:14 -0800105BOOST_AUTO_TEST_SUITE_END() // Security
106
107} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400108} // inline namespace v2
Qiuhan Ding609f0612015-11-04 14:07:14 -0800109} // namespace security
110} // namespace ndn