Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 13 | #include "security/certificate-cache-ttl.hpp" |
| 14 | #include "face.hpp" |
| 15 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 16 | #include "boost-test.hpp" |
| 17 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 18 | using namespace std; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 20 | namespace ndn { |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 22 | BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache) |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 23 | |
| 24 | void |
| 25 | getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached) |
| 26 | { |
| 27 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached); |
| 28 | } |
| 29 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 30 | void |
| 31 | checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size) |
| 32 | { |
| 33 | BOOST_CHECK_EQUAL(cache->getSize(), size); |
| 34 | } |
| 35 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_CASE (Ttl) |
| 38 | { |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 39 | boost::asio::io_service io; |
| 40 | shared_ptr<CertificateCacheTtl> cache = |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 41 | make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)); |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 42 | Scheduler scheduler(io); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 43 | |
| 44 | shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>(); |
| 45 | Name certName1("/tmp/KEY/ksk-1/ID-CERT/1"); |
| 46 | cert1->setName(certName1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | cert1->setFreshnessPeriod(time::milliseconds(500)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 48 | shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>(); |
| 49 | Name certName2("/tmp/KEY/ksk-2/ID-CERT/2"); |
| 50 | cert2->setName(certName2); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 51 | cert2->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 52 | |
| 53 | Name name1 = certName1.getPrefix(-1); |
| 54 | Name name2 = certName2.getPrefix(-1); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 55 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 56 | cache->insertCertificate(cert1); |
| 57 | cache->insertCertificate(cert2); |
| 58 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 59 | scheduler.scheduleEvent(time::milliseconds(200), bind(&checkSize, cache, 2)); |
| 60 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name1, true)); |
| 61 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name2, true)); |
| 62 | |
| 63 | // cert1 should removed from the cache |
| 64 | scheduler.scheduleEvent(time::milliseconds(900), bind(&checkSize, cache, 1)); |
| 65 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name1, false)); |
| 66 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name2, true)); |
| 67 | |
| 68 | // Refresh certificate in cache |
| 69 | scheduler.scheduleEvent(time::milliseconds(900), bind(&CertificateCache::insertCertificate, |
| 70 | cache, cert2)); |
Alexander Afanasyev | fcb4f6d | 2014-04-08 00:20:08 -0700 | [diff] [blame] | 71 | scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true)); |
| 72 | scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 73 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 74 | // Purge |
| 75 | scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate, |
| 76 | cache, cert1)); |
| 77 | scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate, |
| 78 | cache, cert2)); |
| 79 | scheduler.scheduleEvent(time::milliseconds(3100), bind(&checkSize, cache, 2)); |
| 80 | scheduler.scheduleEvent(time::milliseconds(3200), bind(&CertificateCache::reset, cache)); |
| 81 | scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name1, false)); |
| 82 | scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name2, false)); |
| 83 | scheduler.scheduleEvent(time::milliseconds(3400), bind(&checkSize, cache, 0)); |
| 84 | |
| 85 | io.run(); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 89 | |
| 90 | } // namespace ndn |