blob: 6d3581178d4c5040527341375e0ad004d8f9c70a [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/**
Junxiao Shid5827ce2016-07-14 20:49:37 +00003 * Copyright (c) 2013-2016 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
Junxiao Shid5827ce2016-07-14 20:49:37 +000032BOOST_AUTO_TEST_SUITE(Security)
33BOOST_AUTO_TEST_SUITE(TestCertificateCacheTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080034
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080035class CertificateCacheFixture : public UnitTestTimeFixture
Yingdi Yu7640cb32014-01-29 20:00:50 -080036{
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080037public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080038 CertificateCacheFixture()
39 : scheduler(io)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080040 , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)))
41 {
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080042 cert1 = make_shared<IdentityCertificate>();
43 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
44 cert1->setName(certName1);
45 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu58f33712014-04-16 16:57:47 -070046
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080047 cert2 = make_shared<IdentityCertificate>();
48 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
49 cert2->setName(certName2);
50 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080051
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080052 name1 = certName1.getPrefix(-1);
53 name2 = certName2.getPrefix(-1);
54 }
55
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080056public:
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080057 Scheduler scheduler;
Yingdi Yu7640cb32014-01-29 20:00:50 -080058
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080059 shared_ptr<CertificateCacheTtl> cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080060
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080061 shared_ptr<IdentityCertificate> cert1;
62 shared_ptr<IdentityCertificate> cert2;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070063
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080064 Name name1;
65 Name name2;
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
Junxiao Shid5827ce2016-07-14 20:49:37 +000073 advanceClocks(time::nanoseconds(1));
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
Junxiao Shid5827ce2016-07-14 20:49:37 +0000101 advanceClocks(time::nanoseconds(1));
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
Junxiao Shid5827ce2016-07-14 20:49:37 +0000110 advanceClocks(time::nanoseconds(1));
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
Junxiao Shid5827ce2016-07-14 20:49:37 +0000125 advanceClocks(time::nanoseconds(1));
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
Junxiao Shid5827ce2016-07-14 20:49:37 +0000130 advanceClocks(time::nanoseconds(1));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800131 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800132}
133
Junxiao Shid5827ce2016-07-14 20:49:37 +0000134BOOST_AUTO_TEST_SUITE_END() // TestCertificateCacheTtl
135BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800136
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800137} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800138} // namespace ndn