security: Clean up ValidatorConfig when it is re-loaded.

Refs: #1492

Change-Id: I203a57c0cf18c3c5993abfa9c3c7ca9decbed6c2
diff --git a/tests/security/test-certificate-cache.cpp b/tests/security/test-certificate-cache.cpp
index 11b20fb..2f8c1a2 100644
--- a/tests/security/test-certificate-cache.cpp
+++ b/tests/security/test-certificate-cache.cpp
@@ -27,12 +27,19 @@
   BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached);
 }
 
+void
+checkSize(shared_ptr<CertificateCacheTtl> cache, size_t size)
+{
+  BOOST_CHECK_EQUAL(cache->getSize(), size);
+}
+
 
 BOOST_AUTO_TEST_CASE (Ttl)
 {
-  shared_ptr<boost::asio::io_service> io = make_shared<boost::asio::io_service>();
-  shared_ptr<CertificateCacheTtl> cache = make_shared<CertificateCacheTtl>(io, time::seconds(1));
-  Scheduler scheduler(*io);
+  boost::asio::io_service io;
+  shared_ptr<CertificateCacheTtl> cache =
+    make_shared<CertificateCacheTtl>(boost::ref(io), time::seconds(1));
+  Scheduler scheduler(io);
 
   shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
   Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
@@ -49,15 +56,33 @@
   cache->insertCertificate(cert1);
   cache->insertCertificate(cert2);
 
-  scheduler.scheduleEvent(time::milliseconds(200),  bind(&getCertificateTtl, cache, name1, true));
-  scheduler.scheduleEvent(time::milliseconds(200),  bind(&getCertificateTtl, cache, name2, true));
-  scheduler.scheduleEvent(time::milliseconds(900),  bind(&getCertificateTtl, cache, name1, false));
-  scheduler.scheduleEvent(time::milliseconds(900),  bind(&getCertificateTtl, cache, name2, true));
-  scheduler.scheduleEvent(time::milliseconds(900),  bind(&CertificateCache::insertCertificate, cache, 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));
+
+  // 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));
+
+  // 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));
 
-  io->run();
+  // 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));
+
+  io.run();
 }
 
 BOOST_AUTO_TEST_SUITE_END()