table: Processing of PublisherPublicKeyLocator in CS

refs: #1424

Change-Id: Ia4ad2c92a7a6860b73674444ba4a0120a90e5342
diff --git a/daemon/table/cs.cpp b/daemon/table/cs.cpp
index 874ef52..e5da3fa 100644
--- a/daemon/table/cs.cpp
+++ b/daemon/table/cs.cpp
@@ -27,6 +27,7 @@
 #include "cs.hpp"
 #include "core/logger.hpp"
 #include <ndn-cpp-dev/util/crypto.hpp>
+#include "ndn-cpp-dev/security/signature-sha256-with-rsa.hpp"
 
 #define SKIPLIST_MAX_LAYERS 32
 #define SKIPLIST_PROBABILITY 25         // 25% (p = 1/4)
@@ -636,6 +637,24 @@
       return false;
     }
 
+  if (!interest.getPublisherPublicKeyLocator().empty())
+    {
+      if (entry->getData().getSignature().getType() == ndn::Signature::Sha256WithRsa)
+        {
+          ndn::SignatureSha256WithRsa rsaSignature(entry->getData().getSignature());
+          if (rsaSignature.getKeyLocator() != interest.getPublisherPublicKeyLocator())
+            {
+              NFD_LOG_TRACE("violates publisher key selector");
+              return false;
+            }
+        }
+      else
+        {
+          NFD_LOG_TRACE("violates publisher key selector");
+          return false;
+        }
+    }
+
   if (doesInterestContainDigest)
     {
       const ndn::name::Component& lastComponent = entry->getName().get(-1);
diff --git a/tests/table/cs.cpp b/tests/table/cs.cpp
index e8e5377..cdcc0db 100644
--- a/tests/table/cs.cpp
+++ b/tests/table/cs.cpp
@@ -422,6 +422,55 @@
   BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
 }
 
+BOOST_AUTO_TEST_CASE(PublisherKeySelector)
+{
+  Cs cs;
+
+  Name name("/insert/withkey");
+  shared_ptr<Data> data = makeData(name);
+  cs.insert(*data);
+
+  shared_ptr<Interest> interest = make_shared<Interest>(name);
+  Name keyName("/somewhere/key");
+
+  ndn::KeyLocator locator(keyName);
+  interest->setPublisherPublicKeyLocator(locator);
+
+  const Data* found = cs.find(*interest);
+  BOOST_CHECK_EQUAL(found, static_cast<const Data*>(0));
+}
+
+BOOST_AUTO_TEST_CASE(PublisherKeySelector2)
+{
+  Cs cs;
+  Name name("/insert/withkey");
+  shared_ptr<Data> data = makeData(name);
+  cs.insert(*data);
+
+  Name name2("/insert/withkey2");
+  shared_ptr<Data> data2 = make_shared<Data>(name2);
+
+  Name keyName("/somewhere/key");
+  const ndn::KeyLocator locator(keyName);
+
+  ndn::SignatureSha256WithRsa fakeSignature;
+  fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
+                                        reinterpret_cast<const uint8_t*>(0), 0));
+
+  fakeSignature.setKeyLocator(locator);
+  data2->setSignature(fakeSignature);
+
+  cs.insert(*data2);
+
+  shared_ptr<Interest> interest = make_shared<Interest>(name2);
+  interest->setPublisherPublicKeyLocator(locator);
+
+  const Data* found = cs.find(*interest);
+  BOOST_CHECK_NE(found, static_cast<const Data*>(0));
+  BOOST_CHECK_EQUAL(found->getName(), data2->getName());
+}
+
+
 BOOST_AUTO_TEST_CASE(MinMaxComponentsSelector)
 {
   Cs cs;