blob: e12e0e01fe55885e61d331d1ab6a861074f9ed53 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu7640cb32014-01-29 20:00:50 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yu7640cb32014-01-29 20:00:50 -080011 */
12
Yingdi Yu7640cb32014-01-29 20:00:50 -080013#include "security/certificate-cache-ttl.hpp"
14#include "face.hpp"
15
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070016#include "boost-test.hpp"
17
Yingdi Yu7640cb32014-01-29 20:00:50 -080018using namespace std;
Yingdi Yu7640cb32014-01-29 20:00:50 -080019
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu7640cb32014-01-29 20:00:50 -080021
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070022BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache)
Yingdi Yu7640cb32014-01-29 20:00:50 -080023
24void
25getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached)
26{
27 BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached);
28}
29
Yingdi Yu58f33712014-04-16 16:57:47 -070030void
31checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size)
32{
33 BOOST_CHECK_EQUAL(cache->getSize(), size);
34}
35
Yingdi Yu7640cb32014-01-29 20:00:50 -080036
37BOOST_AUTO_TEST_CASE (Ttl)
38{
Yingdi Yu58f33712014-04-16 16:57:47 -070039 boost::asio::io_service io;
40 shared_ptr<CertificateCacheTtl> cache =
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070041 make_shared<CertificateCacheTtl>(ref(io), time::seconds(1));
Yingdi Yu58f33712014-04-16 16:57:47 -070042 Scheduler scheduler(io);
Yingdi Yu7640cb32014-01-29 20:00:50 -080043
44 shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
45 Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
46 cert1->setName(certName1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047 cert1->setFreshnessPeriod(time::milliseconds(500));
Yingdi Yu7640cb32014-01-29 20:00:50 -080048 shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>();
49 Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
50 cert2->setName(certName2);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051 cert2->setFreshnessPeriod(time::milliseconds(1000));
Yingdi Yu7640cb32014-01-29 20:00:50 -080052
53 Name name1 = certName1.getPrefix(-1);
54 Name name2 = certName2.getPrefix(-1);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070055
Yingdi Yu7640cb32014-01-29 20:00:50 -080056 cache->insertCertificate(cert1);
57 cache->insertCertificate(cert2);
58
Yingdi Yu58f33712014-04-16 16:57:47 -070059 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 Afanasyevfcb4f6d2014-04-08 00:20:08 -070071 scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true));
72 scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false));
Yingdi Yu7640cb32014-01-29 20:00:50 -080073
Yingdi Yu58f33712014-04-16 16:57:47 -070074 // 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 Yu7640cb32014-01-29 20:00:50 -080086}
87
88BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080089
90} // namespace ndn