blob: 54454e1c975cf0d4d83cd78e5af9ef255368175d [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
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800113
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800114 io.poll();
115 BOOST_CHECK_EQUAL(cache->getSize(), 1);
116
117 steadyClock->advance(time::milliseconds(400));
118 io.poll();
119 BOOST_CHECK_EQUAL(cache->getSize(), 1);
120
121 // Refresh certificate in cache
122 cache->insertCertificate(cert1); // +500ms
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800123
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800124 io.poll();
125 BOOST_CHECK_EQUAL(cache->getSize(), 1);
126
127 steadyClock->advance(time::milliseconds(400));
128 io.poll();
129 BOOST_CHECK_EQUAL(cache->getSize(), 1);
130
131 steadyClock->advance(time::milliseconds(200));
132 io.poll();
133 BOOST_CHECK_EQUAL(cache->getSize(), 0);
134}
135
136BOOST_FIXTURE_TEST_CASE(Reset, UnitTestTimeFixture)
137{
138 cache->insertCertificate(cert1);
139 cache->insertCertificate(cert2);
140
141 io.poll();
142 BOOST_CHECK_EQUAL(cache->getSize(), 2);
143
144 cache->reset();
Alexander Afanasyeveabffdf2014-11-13 13:50:33 -0800145
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800146 io.poll();
147 BOOST_CHECK_EQUAL(cache->getSize(), 0);
Yingdi Yu7640cb32014-01-29 20:00:50 -0800148}
149
150BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800151
Alexander Afanasyev72e4a5d2014-11-11 11:53:08 -0800152} // namespace tests
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800153} // namespace ndn