security: Adding CertificateCache and CertificateCacheTtl

Change-Id: I473290af29ab590e2539c1873d23f25190f116fb
diff --git a/src/security/certificate-cache-ttl.cpp b/src/security/certificate-cache-ttl.cpp
new file mode 100644
index 0000000..f5482a3
--- /dev/null
+++ b/src/security/certificate-cache-ttl.cpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "certificate-cache-ttl.hpp"
+
+#include <iostream>
+
+#include "../util/logging.hpp"
+
+
+INIT_LOGGER("CertificateCacheTtl")
+
+using namespace std;
+
+namespace ndn
+{    
+CertificateCacheTtl::CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, int defaultTtl)
+  : m_defaultTtl(defaultTtl)
+  , m_scheduler(*io)
+{}
+
+CertificateCacheTtl::~CertificateCacheTtl()
+{}
+  
+void
+CertificateCacheTtl::insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
+{
+  time::Duration expire = (certificate->getFreshnessPeriod() >= 0 ? time::milliseconds(certificate->getFreshnessPeriod()) : time::seconds(m_defaultTtl));
+
+  Name trackerIndex = certificate->getName().getPrefix(-1);
+  EventTracker::iterator it = m_tracker.find(trackerIndex);
+  if(it != m_tracker.end())
+      m_scheduler.cancelEvent(m_tracker[trackerIndex]);
+
+  m_scheduler.scheduleEvent(time::seconds(0), bind(&CertificateCacheTtl::insert, this, certificate));  
+  m_tracker[trackerIndex] = m_scheduler.scheduleEvent(expire, bind(&CertificateCacheTtl::remove, this, certificate->getName()));
+
+}
+
+void
+CertificateCacheTtl::insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate)
+{ 
+  Name name = certificate->getName().getPrefix(-1);
+  m_cache[name] = certificate;
+}
+
+void
+CertificateCacheTtl::remove(const Name &certificateName)
+{
+  Name name = certificateName.getPrefix(-1);
+  Cache::iterator it = m_cache.find(name);
+  if(it != m_cache.end())
+    m_cache.erase(it);
+}
+
+ptr_lib::shared_ptr<const IdentityCertificate> 
+CertificateCacheTtl::getCertificate(const Name & certificateName)
+{
+  Cache::iterator it = m_cache.find(certificateName);
+  if(it != m_cache.end())
+    return it->second;
+  else
+    return ptr_lib::shared_ptr<IdentityCertificate>();
+}
+
+}//ndn
+
+
diff --git a/src/security/certificate-cache-ttl.hpp b/src/security/certificate-cache-ttl.hpp
new file mode 100644
index 0000000..6d5194b
--- /dev/null
+++ b/src/security/certificate-cache-ttl.hpp
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_CERTIFICATE_CACHE_TTL_HPP
+#define NDN_CERTIFICATE_CACHE_TTL_HPP
+
+#include "certificate-cache.hpp"
+#include "../util/scheduler.hpp"
+#include "../util/time.hpp"
+
+#include <unistd.h>
+#include <map>
+
+namespace ndn
+{
+ 
+class CertificateCacheTtl : public CertificateCache
+{
+public:
+  CertificateCacheTtl(shared_ptr<boost::asio::io_service> io, int defaultTtl = 3600);
+  
+  virtual
+  ~CertificateCacheTtl();
+    
+  virtual void
+  insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate);
+  
+  virtual ptr_lib::shared_ptr<const IdentityCertificate> 
+  getCertificate(const Name & certificateNameWithoutVersion);
+
+private:  
+  void
+  insert(ptr_lib::shared_ptr<const IdentityCertificate> certificate);
+  
+  void
+  remove(const Name &certificateName);
+
+protected:
+  typedef std::map<Name, ptr_lib::shared_ptr<const IdentityCertificate> > Cache;
+  typedef std::map<Name, EventId> EventTracker;
+
+  int m_defaultTtl;
+  Cache m_cache;
+  EventTracker m_tracker;
+  Scheduler m_scheduler;
+};
+
+}//ndn
+
+#endif
diff --git a/src/security/certificate-cache.hpp b/src/security/certificate-cache.hpp
new file mode 100644
index 0000000..14c42d1
--- /dev/null
+++ b/src/security/certificate-cache.hpp
@@ -0,0 +1,32 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_CERTIFICATE_CACHE_HPP
+#define NDN_CERTIFICATE_CACHE_HPP
+
+#include "../name.hpp"
+#include "identity-certificate.hpp"
+
+namespace ndn
+{
+
+class CertificateCache
+{
+public:
+  virtual
+  ~CertificateCache() {}
+    
+  virtual void
+  insertCertificate(ptr_lib::shared_ptr<const IdentityCertificate> certificate) = 0;
+
+  virtual ptr_lib::shared_ptr<const IdentityCertificate> 
+  getCertificate(const Name& certificateNameWithoutVersion) = 0;
+};
+
+}//ndn
+
+#endif
diff --git a/tests/test-certificate-cache.cpp b/tests/test-certificate-cache.cpp
new file mode 100644
index 0000000..29a45fc
--- /dev/null
+++ b/tests/test-certificate-cache.cpp
@@ -0,0 +1,57 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Yingdi Yu <yingdi0@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <boost/test/unit_test.hpp>
+#include <unistd.h>
+#include "security/certificate-cache-ttl.hpp"
+#include "face.hpp"
+
+using namespace std;
+using namespace ndn;
+
+
+BOOST_AUTO_TEST_SUITE(TestCertificateCache)
+
+void
+getCertificateTtl(shared_ptr<CertificateCacheTtl> cache, const Name &name, bool cached)
+{
+  BOOST_CHECK_EQUAL(static_cast<bool>(cache->getCertificate(name)), cached);
+}
+
+
+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, 1);
+  Scheduler scheduler(*io);
+
+  shared_ptr<IdentityCertificate> cert1 = make_shared<IdentityCertificate>();
+  Name certName1("/tmp/KEY/ksk-1/ID-CERT/1");
+  cert1->setName(certName1);
+  cert1->setFreshnessPeriod(500);
+  shared_ptr<IdentityCertificate> cert2 = make_shared<IdentityCertificate>();
+  Name certName2("/tmp/KEY/ksk-2/ID-CERT/2");
+  cert2->setName(certName2);
+  cert2->setFreshnessPeriod(1000);
+
+  Name name1 = certName1.getPrefix(-1);
+  Name name2 = certName2.getPrefix(-1);
+  
+  cache->insertCertificate(cert1);
+  cache->insertCertificate(cert2);
+
+  scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name1, true));
+  scheduler.scheduleEvent(time::seconds(0.3), bind(&getCertificateTtl, cache, name2, true));
+  scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name1, false));
+  scheduler.scheduleEvent(time::seconds(0.6), bind(&getCertificateTtl, cache, name2, true));
+  scheduler.scheduleEvent(time::seconds(0.6), bind(&CertificateCache::insertCertificate, &*cache, cert2));
+  scheduler.scheduleEvent(time::seconds(1.3), bind(&getCertificateTtl, cache, name2, true));
+  scheduler.scheduleEvent(time::seconds(1.7), bind(&getCertificateTtl, cache, name2, false));
+
+  io->run();
+}
+
+BOOST_AUTO_TEST_SUITE_END()