Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 22 | #include "security/certificate-cache-ttl.hpp" |
| 23 | #include "face.hpp" |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 24 | #include "util/time-unit-test-clock.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 27 | #include "../unit-test-time-fixture.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 30 | namespace tests { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 31 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(SecurityCertificateCacheTtl) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 34 | class CertificateCacheFixture : public UnitTestTimeFixture |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 35 | { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 36 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 37 | CertificateCacheFixture() |
| 38 | : scheduler(io) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 39 | , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1))) |
| 40 | { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 41 | 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 Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 46 | 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 Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 51 | name1 = certName1.getPrefix(-1); |
| 52 | name2 = certName2.getPrefix(-1); |
| 53 | } |
| 54 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 55 | public: |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 56 | Scheduler scheduler; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 58 | shared_ptr<CertificateCacheTtl> cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 60 | shared_ptr<IdentityCertificate> cert1; |
| 61 | shared_ptr<IdentityCertificate> cert2; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 63 | Name name1; |
| 64 | Name name2; |
| 65 | }; |
| 66 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 67 | BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 69 | cache->insertCertificate(cert1); |
| 70 | cache->insertCertificate(cert2); |
| 71 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 72 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 74 | |
| 75 | scheduler.scheduleEvent(time::milliseconds(200), [&] { |
| 76 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 77 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true); |
| 78 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 79 | }); |
| 80 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 81 | advanceClocks(time::milliseconds(200)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 82 | |
| 83 | // cert1 should removed from the cache |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 84 | scheduler.scheduleEvent(time::milliseconds(700), [&] { |
| 85 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false); |
| 86 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 87 | }); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 89 | advanceClocks(time::milliseconds(700)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 92 | advanceClocks(time::milliseconds(700)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 94 | } |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 96 | BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 97 | { |
| 98 | cache->insertCertificate(cert1); // 500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 100 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 102 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 103 | advanceClocks(time::milliseconds(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 105 | |
| 106 | // Refresh certificate in cache |
| 107 | cache->insertCertificate(cert1); // +500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 109 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 111 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 112 | advanceClocks(time::milliseconds(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 113 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 114 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 115 | advanceClocks(time::milliseconds(200)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 117 | } |
| 118 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 119 | BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 120 | { |
| 121 | cache->insertCertificate(cert1); |
| 122 | cache->insertCertificate(cert2); |
| 123 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 124 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 126 | |
| 127 | cache->reset(); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 129 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 135 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 136 | } // namespace ndn |