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 | /** |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 30 | namespace security { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 31 | namespace tests { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 33 | using namespace ndn::tests; |
| 34 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Security) |
| 36 | BOOST_AUTO_TEST_SUITE(TestCertificateCacheTtl) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 38 | class CertificateCacheFixture : public UnitTestTimeFixture |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 40 | public: |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 41 | CertificateCacheFixture() |
| 42 | : scheduler(io) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 43 | , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1))) |
| 44 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 45 | cert1 = make_shared<v1::IdentityCertificate>(); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 46 | Name certName1("/tmp/KEY/ksk-1/ID-CERT/1"); |
| 47 | cert1->setName(certName1); |
| 48 | cert1->setFreshnessPeriod(time::milliseconds(500)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 50 | cert2 = make_shared<v1::IdentityCertificate>(); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 51 | Name certName2("/tmp/KEY/ksk-2/ID-CERT/2"); |
| 52 | cert2->setName(certName2); |
| 53 | cert2->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 55 | name1 = certName1.getPrefix(-1); |
| 56 | name2 = certName2.getPrefix(-1); |
| 57 | } |
| 58 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 59 | public: |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 60 | Scheduler scheduler; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 62 | shared_ptr<CertificateCacheTtl> cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 64 | shared_ptr<v1::IdentityCertificate> cert1; |
| 65 | shared_ptr<v1::IdentityCertificate> cert2; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 66 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 67 | Name name1; |
| 68 | Name name2; |
| 69 | }; |
| 70 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 71 | BOOST_FIXTURE_TEST_CASE(Expiration, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 72 | { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 73 | cache->insertCertificate(cert1); |
| 74 | cache->insertCertificate(cert2); |
| 75 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 76 | advanceClocks(time::nanoseconds(1)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 78 | |
| 79 | scheduler.scheduleEvent(time::milliseconds(200), [&] { |
| 80 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 81 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true); |
| 82 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 83 | }); |
| 84 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 85 | advanceClocks(time::milliseconds(200)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 86 | |
| 87 | // cert1 should removed from the cache |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 88 | scheduler.scheduleEvent(time::milliseconds(700), [&] { |
| 89 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false); |
| 90 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true); |
| 91 | }); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [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(), 1); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 96 | advanceClocks(time::milliseconds(700)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 98 | } |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 100 | BOOST_FIXTURE_TEST_CASE(TtlRefresh, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 101 | { |
| 102 | cache->insertCertificate(cert1); // 500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 103 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 104 | advanceClocks(time::nanoseconds(1)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 106 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 107 | advanceClocks(time::milliseconds(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 109 | |
| 110 | // Refresh certificate in cache |
| 111 | cache->insertCertificate(cert1); // +500ms |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 112 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 113 | advanceClocks(time::nanoseconds(1)); |
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(400)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(cache->getSize(), 1); |
| 118 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 119 | advanceClocks(time::milliseconds(200)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
| 121 | } |
| 122 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 123 | BOOST_FIXTURE_TEST_CASE(Reset, CertificateCacheFixture) |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 124 | { |
| 125 | cache->insertCertificate(cert1); |
| 126 | cache->insertCertificate(cert2); |
| 127 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 128 | advanceClocks(time::nanoseconds(1)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 129 | BOOST_CHECK_EQUAL(cache->getSize(), 2); |
| 130 | |
| 131 | cache->reset(); |
Alexander Afanasyev | eabffdf | 2014-11-13 13:50:33 -0800 | [diff] [blame] | 132 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 133 | advanceClocks(time::nanoseconds(1)); |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(cache->getSize(), 0); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 137 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateCacheTtl |
| 138 | BOOST_AUTO_TEST_SUITE_END() // Security |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 72e4a5d | 2014-11-11 11:53:08 -0800 | [diff] [blame] | 140 | } // namespace tests |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 141 | } // namespace security |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 142 | } // namespace ndn |