blob: 1524b1d8797abf1b3273efe2fe41d84316181b63 [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"
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
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070032BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache)
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
67
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080068BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080069{
Yingdi Yu7640cb32014-01-29 20:00:50 -080070 cache->insertCertificate(cert1);
71 cache->insertCertificate(cert2);
72
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080073 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080074 BOOST_CHECK_EQUAL(cache->getSize(), 2);
75
76 scheduler.scheduleEvent(time::milliseconds(200), [&] {
77 BOOST_CHECK_EQUAL(cache->getSize(), 2);
78 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true);
79 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
80 });
81
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080082 advanceClocks(time::milliseconds(200));
Yingdi Yu58f33712014-04-16 16:57:47 -070083
84 // cert1 should removed from the cache
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080085 scheduler.scheduleEvent(time::milliseconds(700), [&] {
86 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false);
87 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
88 });
Yingdi Yu58f33712014-04-16 16:57:47 -070089
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080090 advanceClocks(time::milliseconds(700));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080091 BOOST_CHECK_EQUAL(cache->getSize(), 1);
Yingdi Yu7640cb32014-01-29 20:00:50 -080092
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080093 advanceClocks(time::milliseconds(700));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080094 BOOST_CHECK_EQUAL(cache->getSize(), 0);
95}
Yingdi Yu58f33712014-04-16 16:57:47 -070096
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080097BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080098{
99 cache->insertCertificate(cert1); // 500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800100
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800101 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800102 BOOST_CHECK_EQUAL(cache->getSize(), 1);
103
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800104 advanceClocks(time::milliseconds(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800105 BOOST_CHECK_EQUAL(cache->getSize(), 1);
106
107 // Refresh certificate in cache
108 cache->insertCertificate(cert1); // +500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800109
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800110 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800111 BOOST_CHECK_EQUAL(cache->getSize(), 1);
112
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800113 advanceClocks(time::milliseconds(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800114 BOOST_CHECK_EQUAL(cache->getSize(), 1);
115
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800116 advanceClocks(time::milliseconds(200));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800117 BOOST_CHECK_EQUAL(cache->getSize(), 0);
118}
119
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800120BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800121{
122 cache->insertCertificate(cert1);
123 cache->insertCertificate(cert2);
124
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800126 BOOST_CHECK_EQUAL(cache->getSize(), 2);
127
128 cache->reset();
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800129
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800130 advanceClocks(time::nanoseconds(0));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800131 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800132}
133
134BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800135
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800136} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800137} // namespace ndn