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" |
| 24 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 27 | using namespace std; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 29 | namespace ndn { |
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 | |
| 33 | void |
| 34 | getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached) |
| 35 | { |
| 36 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached); |
| 37 | } |
| 38 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 39 | void |
| 40 | checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size) |
| 41 | { |
| 42 | BOOST_CHECK_EQUAL(cache->getSize(), size); |
| 43 | } |
| 44 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 45 | |
| 46 | BOOST_AUTO_TEST_CASE (Ttl) |
| 47 | { |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 48 | boost::asio::io_service io; |
| 49 | shared_ptr<CertificateCacheTtl> cache = |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 50 | make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 51 | Scheduler scheduler(io); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 52 | |
| 53 | shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>(); |
| 54 | Name certName1("/tmp/KEY/ksk-1/ID-CERT/1"); |
| 55 | cert1->setName(certName1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 56 | cert1->setFreshnessPeriod(time::milliseconds(500)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 57 | shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>(); |
| 58 | Name certName2("/tmp/KEY/ksk-2/ID-CERT/2"); |
| 59 | cert2->setName(certName2); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | cert2->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 61 | |
| 62 | Name name1 = certName1.getPrefix(-1); |
| 63 | Name name2 = certName2.getPrefix(-1); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 64 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 65 | cache->insertCertificate(cert1); |
| 66 | cache->insertCertificate(cert2); |
| 67 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 68 | scheduler.scheduleEvent(time::milliseconds(200), bind(&checkSize, cache, 2)); |
| 69 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name1, true)); |
| 70 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name2, true)); |
| 71 | |
| 72 | // cert1 should removed from the cache |
| 73 | scheduler.scheduleEvent(time::milliseconds(900), bind(&checkSize, cache, 1)); |
| 74 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name1, false)); |
| 75 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name2, true)); |
| 76 | |
| 77 | // Refresh certificate in cache |
| 78 | scheduler.scheduleEvent(time::milliseconds(900), bind(&CertificateCache::insertCertificate, |
| 79 | cache, cert2)); |
Alexander Afanasyev | fcb4f6d | 2014-04-08 00:20:08 -0700 | [diff] [blame] | 80 | scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true)); |
| 81 | scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 82 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 83 | // Purge |
| 84 | scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate, |
| 85 | cache, cert1)); |
| 86 | scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate, |
| 87 | cache, cert2)); |
| 88 | scheduler.scheduleEvent(time::milliseconds(3100), bind(&checkSize, cache, 2)); |
| 89 | scheduler.scheduleEvent(time::milliseconds(3200), bind(&CertificateCache::reset, cache)); |
| 90 | scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name1, false)); |
| 91 | scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name2, false)); |
| 92 | scheduler.scheduleEvent(time::milliseconds(3400), bind(&checkSize, cache, 0)); |
| 93 | |
| 94 | io.run(); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 98 | |
| 99 | } // namespace ndn |