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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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" |
| 27 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 28 | namespace ndn { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 29 | namespace tests { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 33 | class UnitTestTimeFixture |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 35 | public: |
| 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 Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 43 | 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 Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 48 | 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 Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 53 | name1 = certName1.getPrefix(-1); |
| 54 | name2 = certName2.getPrefix(-1); |
| 55 | } |
| 56 | |
| 57 | ~UnitTestTimeFixture() |
| 58 | { |
| 59 | time::setCustomClocks(nullptr, nullptr); |
| 60 | } |
| 61 | |
| 62 | public: |
| 63 | shared_ptr<time::UnitTestSteadyClock> steadyClock; |
| 64 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 65 | boost::asio::io_service io; |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 66 | Scheduler scheduler; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 68 | shared_ptr<CertificateCacheTtl> cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 70 | shared_ptr<IdentityCertificate> cert1; |
| 71 | shared_ptr<IdentityCertificate> cert2; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 73 | Name name1; |
| 74 | Name name2; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | BOOST_FIXTURE_TEST_CASE(Expiration, UnitTestTimeFixture) |
| 79 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 80 | cache->insertCertificate(cert1); |
| 81 | cache->insertCertificate(cert2); |
| 82 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 83 | 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 Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 94 | |
| 95 | // cert1 should removed from the cache |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 96 | 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 Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 101 | steadyClock->advance(time::milliseconds(700)); |
| 102 | io.poll(); |
| 103 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 105 | steadyClock->advance(time::milliseconds(700)); |
| 106 | io.poll(); |
| 107 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 108 | } |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 110 | BOOST_FIXTURE_TEST_CASE(TtlRefresh, UnitTestTimeFixture) |
| 111 | { |
| 112 | cache->insertCertificate(cert1); // 500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 114 | 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 Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 124 | 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 | |
| 136 | BOOST_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 Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 146 | io.poll(); |
| 147 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 152 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 153 | } // namespace ndn |