blob: 2bf11f4a1e9ca7a50186844d06b6ce4cba57f224 [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 Afanasyev2fa59392016-07-29 17:24:23 -070030namespace security {
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080031namespace tests {
Yingdi Yu7640cb32014-01-29 20:00:50 -080032
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070033using namespace ndn::tests;
34
Junxiao Shid5827ce2016-07-14 20:49:37 +000035BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(TestCertificateCacheTtl)
Yingdi Yu7640cb32014-01-29 20:00:50 -080037
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080038class CertificateCacheFixture : public UnitTestTimeFixture
Yingdi Yu7640cb32014-01-29 20:00:50 -080039{
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080040public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080041 CertificateCacheFixture()
42 : scheduler(io)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080043 , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)))
44 {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070045 cert1 = make_shared<v1::IdentityCertificate>();
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080046 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
47 cert1->setName(certName1);
48 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu58f33712014-04-16 16:57:47 -070049
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070050 cert2 = make_shared<v1::IdentityCertificate>();
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080051 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
52 cert2->setName(certName2);
53 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080054
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080055 name1 = certName1.getPrefix(-1);
56 name2 = certName2.getPrefix(-1);
57 }
58
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080059public:
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080060 Scheduler scheduler;
Yingdi Yu7640cb32014-01-29 20:00:50 -080061
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080062 shared_ptr<CertificateCacheTtl> cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080063
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070064 shared_ptr<v1::IdentityCertificate> cert1;
65 shared_ptr<v1::IdentityCertificate> cert2;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070066
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080067 Name name1;
68 Name name2;
69};
70
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080071BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080072{
Yingdi Yu7640cb32014-01-29 20:00:50 -080073 cache->insertCertificate(cert1);
74 cache->insertCertificate(cert2);
75
Junxiao Shid5827ce2016-07-14 20:49:37 +000076 advanceClocks(time::nanoseconds(1));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080077 BOOST_CHECK_EQUAL(cache->getSize(), 2);
78
79 scheduler.scheduleEvent(time::milliseconds(200), [&] {
80 BOOST_CHECK_EQUAL(cache->getSize(), 2);
81 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true);
82 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
83 });
84
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080085 advanceClocks(time::milliseconds(200));
Yingdi Yu58f33712014-04-16 16:57:47 -070086
87 // cert1 should removed from the cache
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080088 scheduler.scheduleEvent(time::milliseconds(700), [&] {
89 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false);
90 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
91 });
Yingdi Yu58f33712014-04-16 16:57:47 -070092
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(), 1);
Yingdi Yu7640cb32014-01-29 20:00:50 -080095
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080096 advanceClocks(time::milliseconds(700));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080097 BOOST_CHECK_EQUAL(cache->getSize(), 0);
98}
Yingdi Yu58f33712014-04-16 16:57:47 -070099
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800100BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800101{
102 cache->insertCertificate(cert1); // 500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800103
Junxiao Shid5827ce2016-07-14 20:49:37 +0000104 advanceClocks(time::nanoseconds(1));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800105 BOOST_CHECK_EQUAL(cache->getSize(), 1);
106
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800107 advanceClocks(time::milliseconds(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800108 BOOST_CHECK_EQUAL(cache->getSize(), 1);
109
110 // Refresh certificate in cache
111 cache->insertCertificate(cert1); // +500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800112
Junxiao Shid5827ce2016-07-14 20:49:37 +0000113 advanceClocks(time::nanoseconds(1));
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(400));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800117 BOOST_CHECK_EQUAL(cache->getSize(), 1);
118
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800119 advanceClocks(time::milliseconds(200));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800120 BOOST_CHECK_EQUAL(cache->getSize(), 0);
121}
122
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800123BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture)
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800124{
125 cache->insertCertificate(cert1);
126 cache->insertCertificate(cert2);
127
Junxiao Shid5827ce2016-07-14 20:49:37 +0000128 advanceClocks(time::nanoseconds(1));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800129 BOOST_CHECK_EQUAL(cache->getSize(), 2);
130
131 cache->reset();
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800132
Junxiao Shid5827ce2016-07-14 20:49:37 +0000133 advanceClocks(time::nanoseconds(1));
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800134 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800135}
136
Junxiao Shid5827ce2016-07-14 20:49:37 +0000137BOOST_AUTO_TEST_SUITE_END() // TestCertificateCacheTtl
138BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800139
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800140} // namespace tests
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700141} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800142} // namespace ndn