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" |
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 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache) |
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 | |
| 67 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 68 | BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 69 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 70 | cache->insertCertificate(cert1); |
| 71 | cache->insertCertificate(cert2); |
| 72 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 73 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 75 | |
| 76 | scheduler.scheduleEvent(time::milliseconds(200), [&] { |
| 77 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 78 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true); |
| 79 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 80 | }); |
| 81 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 82 | advanceClocks(time::milliseconds(200)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 83 | |
| 84 | // cert1 should removed from the cache |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 85 | scheduler.scheduleEvent(time::milliseconds(700), [&] { |
| 86 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false); |
| 87 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 88 | }); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 90 | advanceClocks(time::milliseconds(700)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 91 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 93 | advanceClocks(time::milliseconds(700)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 95 | } |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 97 | BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 98 | { |
| 99 | cache->insertCertificate(cert1); // 500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 101 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 103 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 104 | advanceClocks(time::milliseconds(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 106 | |
| 107 | // Refresh certificate in cache |
| 108 | cache->insertCertificate(cert1); // +500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 110 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 112 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 113 | advanceClocks(time::milliseconds(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 115 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 116 | advanceClocks(time::milliseconds(200)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 118 | } |
| 119 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 120 | BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 121 | { |
| 122 | cache->insertCertificate(cert1); |
| 123 | cache->insertCertificate(cert2); |
| 124 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 125 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 127 | |
| 128 | cache->reset(); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame^] | 130 | advanceClocks(time::nanoseconds(0)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 136 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 137 | } // namespace ndn |