blob: 4d3430d0b8712a8b55ba61c13c9f62e3e4c060d2 [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/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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"
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080024#include "util/time-unit-test-clock.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080027#include "../unit-test-time-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080029namespace ndn {
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080030namespace tests {
Yingdi Yu7640cb32014-01-29 20:00:50 -080031
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032BOOST_AUTO_TEST_SUITE(SecurityCertificateCacheTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080033
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080034class CertificateCacheFixture : public UnitTestTimeFixture
Yingdi Yu7640cb32014-01-29 20:00:50 -080035{
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080036public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080037 CertificateCacheFixture()
38 : scheduler(io)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080039 , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)))
40 {
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080041 cert1 = make_shared<IdentityCertificate>();
42 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
43 cert1->setName(certName1);
44 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu58f33712014-04-16 16:57:47 -070045
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080046 cert2 = make_shared<IdentityCertificate>();
47 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
48 cert2->setName(certName2);
49 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080050
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080051 name1 = certName1.getPrefix(-1);
52 name2 = certName2.getPrefix(-1);
53 }
54
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080055public:
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080056 Scheduler scheduler;
Yingdi Yu7640cb32014-01-29 20:00:50 -080057
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080058 shared_ptr<CertificateCacheTtl> cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080059
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080060 shared_ptr<IdentityCertificate> cert1;
61 shared_ptr<IdentityCertificate> cert2;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080063 Name name1;
64 Name name2;
65};
66
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080068{
Yingdi Yu7640cb32014-01-29 20:00:50 -080069 cache->insertCertificate(cert1);
70 cache->insertCertificate(cert2);
71
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080072 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080073 BOOST_CHECK_EQUAL(cache->getSize(), 2);
74
75 scheduler.scheduleEvent(time::milliseconds(200), [&] {
76 BOOST_CHECK_EQUAL(cache->getSize(), 2);
77 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true);
78 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
79 });
80
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080081 advanceClocks(time::milliseconds(200));
Yingdi Yu58f33712014-04-16 16:57:47 -070082
83 // cert1 should removed from the cache
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080084 scheduler.scheduleEvent(time::milliseconds(700), [&] {
85 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false);
86 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
87 });
Yingdi Yu58f33712014-04-16 16:57:47 -070088
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080089 advanceClocks(time::milliseconds(700));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080090 BOOST_CHECK_EQUAL(cache->getSize(), 1);
Yingdi Yu7640cb32014-01-29 20:00:50 -080091
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080092 advanceClocks(time::milliseconds(700));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080093 BOOST_CHECK_EQUAL(cache->getSize(), 0);
94}
Yingdi Yu58f33712014-04-16 16:57:47 -070095
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080096BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080097{
98 cache->insertCertificate(cert1); // 500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -080099
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800100 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800101 BOOST_CHECK_EQUAL(cache->getSize(), 1);
102
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800103 advanceClocks(time::milliseconds(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800104 BOOST_CHECK_EQUAL(cache->getSize(), 1);
105
106 // Refresh certificate in cache
107 cache->insertCertificate(cert1); // +500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800108
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800109 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800110 BOOST_CHECK_EQUAL(cache->getSize(), 1);
111
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800112 advanceClocks(time::milliseconds(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800113 BOOST_CHECK_EQUAL(cache->getSize(), 1);
114
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800115 advanceClocks(time::milliseconds(200));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800116 BOOST_CHECK_EQUAL(cache->getSize(), 0);
117}
118
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800119BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800120{
121 cache->insertCertificate(cert1);
122 cache->insertCertificate(cert2);
123
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800125 BOOST_CHECK_EQUAL(cache->getSize(), 2);
126
127 cache->reset();
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800128
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800129 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800130 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800131}
132
133BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800134
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800135} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800136} // namespace ndn