tests: Rewrite SecurityTestCertificateCache test to use UnitTestClock
Change-Id: I592af5f6a0c8df9c074eed60475ecf5f13fe1301
Refs: #2152
diff --git a/tests/unit-tests/security/test-certificate-cache.cpp b/tests/unit-tests/security/test-certificate-cache.cpp
index 2934a47..d98dd4a 100644
--- a/tests/unit-tests/security/test-certificate-cache.cpp
+++ b/tests/unit-tests/security/test-certificate-cache.cpp
@@ -21,79 +21,130 @@
#include "security/certificate-cache-ttl.hpp"
#include "face.hpp"
+#include "util/time-unit-test-clock.hpp"
#include "boost-test.hpp"
-using namespace std;
-
namespace ndn {
+namespace tests {
BOOST_AUTO_TEST_SUITE(SecurityTestCertificateCache)
-void
-getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached)
+class UnitTestTimeFixture
{
- BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached);
-}
+public:
+ UnitTestTimeFixture()
+ : steadyClock(make_shared<time::UnitTestSteadyClock>())
+ , scheduler(io)
+ , cache(make_shared<CertificateCacheTtl>(ref(io), time::seconds(1)))
+ {
+ time::setCustomClocks(steadyClock);
-void
-checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size)
-{
- BOOST_CHECK_EQUAL(cache->getSize(), size);
-}
+ cert1 = make_shared<IdentityCertificate>();
+ Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
+ cert1->setName(certName1);
+ cert1->setFreshnessPeriod(time::milliseconds(500));
+ cert2 = make_shared<IdentityCertificate>();
+ Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
+ cert2->setName(certName2);
+ cert2->setFreshnessPeriod(time::milliseconds(1000));
-BOOST_AUTO_TEST_CASE (Ttl)
-{
+ name1 = certName1.getPrefix(-1);
+ name2 = certName2.getPrefix(-1);
+ }
+
+ ~UnitTestTimeFixture()
+ {
+ time::setCustomClocks(nullptr, nullptr);
+ }
+
+public:
+ shared_ptr<time::UnitTestSteadyClock> steadyClock;
+
boost::asio::io_service io;
- shared_ptr<CertificateCacheTtl> cache =
- make_shared<CertificateCacheTtl>(ref(io), time::seconds(1));
- Scheduler scheduler(io);
+ Scheduler scheduler;
- shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
- Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
- cert1->setName(certName1);
- cert1->setFreshnessPeriod(time::milliseconds(500));
- shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>();
- Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
- cert2->setName(certName2);
- cert2->setFreshnessPeriod(time::milliseconds(1000));
+ shared_ptr<CertificateCacheTtl> cache;
- Name name1 = certName1.getPrefix(-1);
- Name name2 = certName2.getPrefix(-1);
+ shared_ptr<IdentityCertificate> cert1;
+ shared_ptr<IdentityCertificate> cert2;
+ Name name1;
+ Name name2;
+};
+
+
+BOOST_FIXTURE_TEST_CASE(Expiration, UnitTestTimeFixture)
+{
cache->insertCertificate(cert1);
cache->insertCertificate(cert2);
- scheduler.scheduleEvent(time::milliseconds(200), bind(&checkSize, cache, 2));
- scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name1, true));
- scheduler.scheduleEvent(time::milliseconds(200), bind(&getCertificateTtl, cache, name2, true));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 2);
+
+ scheduler.scheduleEvent(time::milliseconds(200), [&] {
+ BOOST_CHECK_EQUAL(cache->getSize(), 2);
+ BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), true);
+ BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
+ });
+
+ steadyClock->advance(time::milliseconds(200));
+ io.poll();
// cert1 should removed from the cache
- scheduler.scheduleEvent(time::milliseconds(900), bind(&checkSize, cache, 1));
- scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name1, false));
- scheduler.scheduleEvent(time::milliseconds(900), bind(&getCertificateTtl, cache, name2, true));
+ scheduler.scheduleEvent(time::milliseconds(700), [&] {
+ BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name1)), false);
+ BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name2)), true);
+ });
- // Refresh certificate in cache
- scheduler.scheduleEvent(time::milliseconds(900), bind(&CertificateCache::insertCertificate,
- cache, cert2));
- scheduler.scheduleEvent(time::milliseconds(1500), bind(&getCertificateTtl, cache, name2, true));
- scheduler.scheduleEvent(time::milliseconds(2500), bind(&getCertificateTtl, cache, name2, false));
+ steadyClock->advance(time::milliseconds(700));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 1);
- // Purge
- scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate,
- cache, cert1));
- scheduler.scheduleEvent(time::milliseconds(3000), bind(&CertificateCache::insertCertificate,
- cache, cert2));
- scheduler.scheduleEvent(time::milliseconds(3100), bind(&checkSize, cache, 2));
- scheduler.scheduleEvent(time::milliseconds(3200), bind(&CertificateCache::reset, cache));
- scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name1, false));
- scheduler.scheduleEvent(time::milliseconds(3300), bind(&getCertificateTtl, cache, name2, false));
- scheduler.scheduleEvent(time::milliseconds(3400), bind(&checkSize, cache, 0));
+ steadyClock->advance(time::milliseconds(700));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 0);
+}
- io.run();
+BOOST_FIXTURE_TEST_CASE(TtlRefresh, UnitTestTimeFixture)
+{
+ cache->insertCertificate(cert1); // 500ms
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 1);
+
+ steadyClock->advance(time::milliseconds(400));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 1);
+
+ // Refresh certificate in cache
+ cache->insertCertificate(cert1); // +500ms
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 1);
+
+ steadyClock->advance(time::milliseconds(400));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 1);
+
+ steadyClock->advance(time::milliseconds(200));
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(Reset, UnitTestTimeFixture)
+{
+ cache->insertCertificate(cert1);
+ cache->insertCertificate(cert2);
+
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 2);
+
+ cache->reset();
+ io.poll();
+ BOOST_CHECK_EQUAL(cache->getSize(), 0);
}
BOOST_AUTO_TEST_SUITE_END()
+} // namespace tests
} // namespace ndn