Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Yingdi Yu <yingdi0@cs.ucla.edu> |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include <boost/test/unit_test.hpp> |
| 8 | #include <unistd.h> |
| 9 | #include "security/certificate-cache-ttl.hpp" |
| 10 | #include "face.hpp" |
| 11 | |
| 12 | using namespace std; |
| 13 | using namespace ndn; |
| 14 | |
| 15 | |
| 16 | BOOST_AUTO_TEST_SUITE(TestCertificateCache) |
| 17 | |
| 18 | void |
| 19 | getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached) |
| 20 | { |
| 21 | BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached); |
| 22 | } |
| 23 | |
| 24 | |
| 25 | BOOST_AUTO_TEST_CASE (Ttl) |
| 26 | { |
| 27 | shared_ptr<boost::asio::io_service> io = make_shared<boost::asio::io_service>(); |
| 28 | shared_ptr<CertificateCacheTtl> cache = make_shared<CertificateCacheTtl>(io, 1); |
| 29 | Scheduler scheduler(*io); |
| 30 | |
| 31 | shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>(); |
| 32 | Name certName1("/tmp/KEY/ksk-1/ID-CERT/1"); |
| 33 | cert1->setName(certName1); |
| 34 | cert1->setFreshnessPeriod(500); |
| 35 | shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>(); |
| 36 | Name certName2("/tmp/KEY/ksk-2/ID-CERT/2"); |
| 37 | cert2->setName(certName2); |
| 38 | cert2->setFreshnessPeriod(1000); |
| 39 | |
| 40 | Name name1 = certName1.getPrefix(-1); |
| 41 | Name name2 = certName2.getPrefix(-1); |
| 42 | |
| 43 | cache->insertCertificate(cert1); |
| 44 | cache->insertCertificate(cert2); |
| 45 | |
| 46 | scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name1, true)); |
| 47 | scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name2, true)); |
| 48 | scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name1, false)); |
| 49 | scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name2, true)); |
| 50 | scheduler.scheduleEvent(time::seconds(0.6), bind(&CertificateCache::insertCertificate, &*cache, cert2)); |
| 51 | scheduler.scheduleEvent(time::seconds(1.3), bind(&getCertificateTtl, cache, name2, true)); |
| 52 | scheduler.scheduleEvent(time::seconds(1.7), bind(&getCertificateTtl, cache, name2, false)); |
| 53 | |
| 54 | io->run(); |
| 55 | } |
| 56 | |
| 57 | BOOST_AUTO_TEST_SUITE_END() |