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 | |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE (Ttl) |
| 32 | { |
| 33 | shared_ptr<boost::asio::io_service> io = make_shared<boost::asio::io_service>(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 34 | shared_ptr<CertificateCacheTtl> cache = make_shared<CertificateCacheTtl>(io, time::seconds(1)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 35 | Scheduler scheduler(*io); |
| 36 | |
| 37 | shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>(); |
| 38 | Name certName1("/tmp/KEY/ksk-1/ID-CERT/1"); |
| 39 | cert1->setName(certName1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 40 | cert1->setFreshnessPeriod(time::milliseconds(500)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 41 | shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>(); |
| 42 | Name certName2("/tmp/KEY/ksk-2/ID-CERT/2"); |
| 43 | cert2->setName(certName2); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 44 | cert2->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 45 | |
| 46 | Name name1 = certName1.getPrefix(-1); |
| 47 | Name name2 = certName2.getPrefix(-1); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 49 | cache->insertCertificate(cert1); |
| 50 | cache->insertCertificate(cert2); |
| 51 | |
Alexander Afanasyev | fcb4f6d | 2014-04-08 00:20:08 -0700 | [diff] [blame] | 52 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name1, true)); |
| 53 | scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name2, true)); |
| 54 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name1, false)); |
| 55 | scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name2, true)); |
| 56 | scheduler.scheduleEvent(time::milliseconds(900), bind(&CertificateCache::insertCertificate, cache, cert2)); |
| 57 | scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true)); |
| 58 | scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false)); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 59 | |
| 60 | io->run(); |
| 61 | } |
| 62 | |
| 63 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 64 | |
| 65 | } // namespace ndn |