blob: 2934a47634573fa9fc752f35371741d9ec5aebfb [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu7640cb32014-01-29 20:00:50 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu7640cb32014-01-29 20:00:50 -080020 */
21
Yingdi Yu7640cb32014-01-29 20:00:50 -080022#include "security/certificate-cache-ttl.hpp"
23#include "face.hpp"
24
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
26
Yingdi Yu7640cb32014-01-29 20:00:50 -080027using namespace std;
Yingdi Yu7640cb32014-01-29 20:00:50 -080028
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080029namespace ndn {
Yingdi Yu7640cb32014-01-29 20:00:50 -080030
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070031BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache)
Yingdi Yu7640cb32014-01-29 20:00:50 -080032
33void
34getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached)
35{
36 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached);
37}
38
Yingdi Yu58f33712014-04-16 16:57:47 -070039void
40checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size)
41{
42 BOOST_CHECK_EQUAL(cache->getSize(), size);
43}
44
Yingdi Yu7640cb32014-01-29 20:00:50 -080045
46BOOST_AUTO_TEST_CASE (Ttl)
47{
Yingdi Yu58f33712014-04-16 16:57:47 -070048 boost::asio::io_service io;
49 shared_ptr<CertificateCacheTtl> cache =
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070050 make_shared<CertificateCacheTtl>(ref(io), time::seconds(1));
Yingdi Yu58f33712014-04-16 16:57:47 -070051 Scheduler scheduler(io);
Yingdi Yu7640cb32014-01-29 20:00:50 -080052
53 shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
54 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
55 cert1->setName(certName1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu7640cb32014-01-29 20:00:50 -080057 shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>();
58 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
59 cert2->setName(certName2);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080061
62 Name name1 = certName1.getPrefix(-1);
63 Name name2 = certName2.getPrefix(-1);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070064
Yingdi Yu7640cb32014-01-29 20:00:50 -080065 cache->insertCertificate(cert1);
66 cache->insertCertificate(cert2);
67
Yingdi Yu58f33712014-04-16 16:57:47 -070068 scheduler.scheduleEvent(time::milliseconds(200), bind(&checkSize, cache, 2));
69 scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name1, true));
70 scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name2, true));
71
72 // cert1 should removed from the cache
73 scheduler.scheduleEvent(time::milliseconds(900), bind(&checkSize, cache, 1));
74 scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name1, false));
75 scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name2, true));
76
77 // Refresh certificate in cache
78 scheduler.scheduleEvent(time::milliseconds(900), bind(&CertificateCache::insertCertificate,
79 cache, cert2));
Alexander Afanasyevfcb4f6d2014-04-08 00:20:08 -070080 scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true));
81 scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false));
Yingdi Yu7640cb32014-01-29 20:00:50 -080082
Yingdi Yu58f33712014-04-16 16:57:47 -070083 // Purge
84 scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate,
85 cache, cert1));
86 scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate,
87 cache, cert2));
88 scheduler.scheduleEvent(time::milliseconds(3100), bind(&checkSize, cache, 2));
89 scheduler.scheduleEvent(time::milliseconds(3200), bind(&CertificateCache::reset, cache));
90 scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name1, false));
91 scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name2, false));
92 scheduler.scheduleEvent(time::milliseconds(3400), bind(&checkSize, cache, 0));
93
94 io.run();
Yingdi Yu7640cb32014-01-29 20:00:50 -080095}
96
97BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080098
99} // namespace ndn