blob: d98dd4a2639c6ff4f723927071e388297a5a192b [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"
27
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080028namespace ndn {
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080029namespace tests {
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
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080033class UnitTestTimeFixture
Yingdi Yu7640cb32014-01-29 20:00:50 -080034{
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080035public:
36 UnitTestTimeFixture()
37 : steadyClock(make_shared<time::UnitTestSteadyClock>())
38 , scheduler(io)
39 , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)))
40 {
41 time::setCustomClocks(steadyClock);
Yingdi Yu7640cb32014-01-29 20:00:50 -080042
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080043 cert1 = make_shared<IdentityCertificate>();
44 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
45 cert1->setName(certName1);
46 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu58f33712014-04-16 16:57:47 -070047
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080048 cert2 = make_shared<IdentityCertificate>();
49 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
50 cert2->setName(certName2);
51 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080052
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080053 name1 = certName1.getPrefix(-1);
54 name2 = certName2.getPrefix(-1);
55 }
56
57 ~UnitTestTimeFixture()
58 {
59 time::setCustomClocks(nullptr, nullptr);
60 }
61
62public:
63 shared_ptr<time::UnitTestSteadyClock> steadyClock;
64
Yingdi Yu58f33712014-04-16 16:57:47 -070065 boost::asio::io_service io;
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080066 Scheduler scheduler;
Yingdi Yu7640cb32014-01-29 20:00:50 -080067
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080068 shared_ptr<CertificateCacheTtl> cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080069
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080070 shared_ptr<IdentityCertificate> cert1;
71 shared_ptr<IdentityCertificate> cert2;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070072
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080073 Name name1;
74 Name name2;
75};
76
77
78BOOST_FIXTURE_TEST_CASE(Expiration, UnitTestTimeFixture)
79{
Yingdi Yu7640cb32014-01-29 20:00:50 -080080 cache->insertCertificate(cert1);
81 cache->insertCertificate(cert2);
82
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080083 io.poll();
84 BOOST_CHECK_EQUAL(cache->getSize(), 2);
85
86 scheduler.scheduleEvent(time::milliseconds(200), [&] {
87 BOOST_CHECK_EQUAL(cache->getSize(), 2);
88 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true);
89 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
90 });
91
92 steadyClock->advance(time::milliseconds(200));
93 io.poll();
Yingdi Yu58f33712014-04-16 16:57:47 -070094
95 // cert1 should removed from the cache
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -080096 scheduler.scheduleEvent(time::milliseconds(700), [&] {
97 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false);
98 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
99 });
Yingdi Yu58f33712014-04-16 16:57:47 -0700100
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800101 steadyClock->advance(time::milliseconds(700));
102 io.poll();
103 BOOST_CHECK_EQUAL(cache->getSize(), 1);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800104
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800105 steadyClock->advance(time::milliseconds(700));
106 io.poll();
107 BOOST_CHECK_EQUAL(cache->getSize(), 0);
108}
Yingdi Yu58f33712014-04-16 16:57:47 -0700109
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800110BOOST_FIXTURE_TEST_CASE(TtlRefresh, UnitTestTimeFixture)
111{
112 cache->insertCertificate(cert1); // 500ms
113 io.poll();
114 BOOST_CHECK_EQUAL(cache->getSize(), 1);
115
116 steadyClock->advance(time::milliseconds(400));
117 io.poll();
118 BOOST_CHECK_EQUAL(cache->getSize(), 1);
119
120 // Refresh certificate in cache
121 cache->insertCertificate(cert1); // +500ms
122 io.poll();
123 BOOST_CHECK_EQUAL(cache->getSize(), 1);
124
125 steadyClock->advance(time::milliseconds(400));
126 io.poll();
127 BOOST_CHECK_EQUAL(cache->getSize(), 1);
128
129 steadyClock->advance(time::milliseconds(200));
130 io.poll();
131 BOOST_CHECK_EQUAL(cache->getSize(), 0);
132}
133
134BOOST_FIXTURE_TEST_CASE(Reset, UnitTestTimeFixture)
135{
136 cache->insertCertificate(cert1);
137 cache->insertCertificate(cert2);
138
139 io.poll();
140 BOOST_CHECK_EQUAL(cache->getSize(), 2);
141
142 cache->reset();
143 io.poll();
144 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800145}
146
147BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800148
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800149} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800150} // namespace ndn