blob: 4ae350583b06ea2bae931c6f5c6d6f8af62d64e8 [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/*
3 * Copyright (c) 2013-2018 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
22#include "security/v2/certificate-cache.hpp"
23
24#include "../../identity-management-time-fixture.hpp"
25#include "boost-test.hpp"
26
27namespace ndn {
28namespace security {
29namespace v2 {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(Security)
33BOOST_AUTO_TEST_SUITE(V2)
34
35class CertificateCacheFixture : public ndn::tests::IdentityManagementTimeFixture
36{
37public:
38 CertificateCacheFixture()
Davide Pesavento0f830802018-01-16 23:58:58 -050039 : certCache(10_s)
Qiuhan Ding609f0612015-11-04 14:07:14 -080040 {
41 identity = addIdentity("/TestCertificateCache/");
42 cert = identity.getDefaultKey().getDefaultCertificate();
43 }
44
45public:
46 CertificateCache certCache;
47 Identity identity;
48 Certificate cert;
49};
50
51BOOST_FIXTURE_TEST_SUITE(TestCertificateCache, CertificateCacheFixture)
52
53BOOST_AUTO_TEST_CASE(RemovalTime)
54{
55 // Cache lifetime is capped to 10 seconds during cache construction
56
57 BOOST_CHECK_NO_THROW(certCache.insert(cert));
58 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
59
Davide Pesavento0f830802018-01-16 23:58:58 -050060 advanceClocks(11_s, 1);
Qiuhan Ding609f0612015-11-04 14:07:14 -080061 BOOST_CHECK(certCache.find(cert.getName()) == nullptr);
62
63 BOOST_CHECK_NO_THROW(certCache.insert(cert));
64 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
65
Davide Pesavento0f830802018-01-16 23:58:58 -050066 advanceClocks(5_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080067 BOOST_CHECK(certCache.find(cert.getName()) != nullptr);
68
Davide Pesavento0f830802018-01-16 23:58:58 -050069 advanceClocks(15_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080070 BOOST_CHECK(certCache.find(cert.getName()) == nullptr);
71}
72
73BOOST_AUTO_TEST_CASE(FindByInterest)
74{
75 BOOST_CHECK_NO_THROW(certCache.insert(cert));
76
77 // Find by interest
78 BOOST_CHECK(certCache.find(Interest(cert.getIdentity())) != nullptr);
79 BOOST_CHECK(certCache.find(Interest(cert.getKeyName())) != nullptr);
80 BOOST_CHECK(certCache.find(Interest(Name(cert.getName()).appendVersion())) == nullptr);
81
Davide Pesavento0f830802018-01-16 23:58:58 -050082 advanceClocks(12_s);
Qiuhan Ding609f0612015-11-04 14:07:14 -080083 BOOST_CHECK(certCache.find(Interest(cert.getIdentity())) == nullptr);
84
85 Certificate cert3 = addCertificate(identity.getDefaultKey(), "3");
86 Certificate cert4 = addCertificate(identity.getDefaultKey(), "4");
87 Certificate cert5 = addCertificate(identity.getDefaultKey(), "5");
88
89 certCache.insert(cert3);
90 certCache.insert(cert4);
91 certCache.insert(cert5);
92
93 Interest interest4(cert3.getKeyName());
94 interest4.setExclude(Exclude().excludeOne(cert3.getName().at(Certificate::ISSUER_ID_OFFSET)));
95 BOOST_CHECK(certCache.find(interest4) != nullptr);
96 BOOST_CHECK_NE(certCache.find(interest4)->getName(), cert3.getName());
97
98 // TODO cover more cases with different interests
99}
100
101BOOST_AUTO_TEST_SUITE_END() // TestCertificateCache
102BOOST_AUTO_TEST_SUITE_END() // V2
103BOOST_AUTO_TEST_SUITE_END() // Security
104
105} // namespace tests
106} // namespace v2
107} // namespace security
108} // namespace ndn