add check of verification for content

Change-Id: If3404c76bb001fb65ebb9a231020bc7a319a0142
diff --git a/ccnx/ccnx-pco.cpp b/ccnx/ccnx-pco.cpp
index 2c4e351..591f1d3 100644
--- a/ccnx/ccnx-pco.cpp
+++ b/ccnx/ccnx-pco.cpp
@@ -34,22 +34,26 @@
   {
     boost::throw_exception(MisformedContentObjectException());
   }
+
 }
 
-ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len)
+ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len, bool verified)
             : m_comps(NULL)
+            , m_verified(verified)
 {
   init(data, len);
 }
 
-ParsedContentObject::ParsedContentObject(const Bytes &bytes)
+ParsedContentObject::ParsedContentObject(const Bytes &bytes, bool verified)
             : m_comps(NULL)
+            , m_verified(verified)
 {
   init(head(bytes), bytes.size());
 }
 
-ParsedContentObject::ParsedContentObject(const ParsedContentObject &other)
+ParsedContentObject::ParsedContentObject(const ParsedContentObject &other, bool verified)
             : m_comps(NULL)
+            , m_verified(verified)
 {
   init(head(other.m_bytes), other.m_bytes.size());
 }
diff --git a/ccnx/ccnx-pco.h b/ccnx/ccnx-pco.h
index cdb2a7e..28b039d 100644
--- a/ccnx/ccnx-pco.h
+++ b/ccnx/ccnx-pco.h
@@ -33,10 +33,10 @@
 class ParsedContentObject
 {
 public:
-  ParsedContentObject(const unsigned char *data, size_t len);
-  ParsedContentObject(const unsigned char *data, const ccn_parsed_ContentObject &pco);
-  ParsedContentObject(const Bytes &bytes);
-  ParsedContentObject(const ParsedContentObject &other);
+  ParsedContentObject(const unsigned char *data, size_t len, bool verified = false);
+  ParsedContentObject(const unsigned char *data, const ccn_parsed_ContentObject &pco, bool verified = false);
+  ParsedContentObject(const Bytes &bytes, bool verified = false);
+  ParsedContentObject(const ParsedContentObject &other, bool verified = false);
   virtual ~ParsedContentObject();
 
   Bytes
@@ -51,6 +51,18 @@
   inline const Bytes &
   buf () const;
 
+  bool
+  verified() const { return m_verified; }
+
+  void
+  setVerified(bool verified) { m_verified = verified; }
+
+  const unsigned char *
+  msg() const { return head(m_bytes); }
+
+  const ccn_parsed_ContentObject *
+  pco() const { return &m_pco; }
+
 private:
   void
   init(const unsigned char *data, size_t len);
@@ -59,6 +71,7 @@
   ccn_parsed_ContentObject m_pco;
   ccn_indexbuf *m_comps;
   Bytes m_bytes;
+  bool m_verified;
 };
 
 const Bytes &
diff --git a/ccnx/ccnx-wrapper.cpp b/ccnx/ccnx-wrapper.cpp
index a9f0821..91148de 100644
--- a/ccnx/ccnx-wrapper.cpp
+++ b/ccnx/ccnx-wrapper.cpp
@@ -475,6 +475,8 @@
   tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
   tie (cp, executor, selectors) = *realData;
 
+  bool verified = false;
+
   switch (kind)
     {
     case CCN_UPCALL_FINAL:  // effecitve in unit tests
@@ -486,6 +488,7 @@
       return CCN_UPCALL_RESULT_OK;
 
     case CCN_UPCALL_CONTENT:
+      verified = true;
       _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
       break;
 
@@ -512,7 +515,7 @@
       return CCN_UPCALL_RESULT_OK;
     }
 
-  PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
+  PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], verified);
 
   // this will be run in executor
   executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
@@ -694,4 +697,12 @@
   return Name(retval);
 }
 
+bool
+CcnxWrapper::verifyPco(PcoPtr &pco)
+{
+  bool verified = ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco());
+  pco->setVerified(verified);
+  return verified;
+}
+
 }
diff --git a/ccnx/ccnx-wrapper.h b/ccnx/ccnx-wrapper.h
index 6cd1791..1bf6f4c 100644
--- a/ccnx/ccnx-wrapper.h
+++ b/ccnx/ccnx-wrapper.h
@@ -86,6 +86,9 @@
   int
   putToCcnd (const Bytes &contentObject);
 
+  bool
+  verifyPco(PcoPtr &pco);
+
 private:
   CcnxWrapper(const CcnxWrapper &other) {}
 
diff --git a/src/fetcher.cc b/src/fetcher.cc
index be9064a..8f78323 100644
--- a/src/fetcher.cc
+++ b/src/fetcher.cc
@@ -132,8 +132,8 @@
 
   if (m_forwardingHint == Name ())
   {
-    // invoke callback
-    if (!m_segmentCallback.empty ())
+    // check whether data is verified in this case; if verified invoke callback
+    if (!m_segmentCallback.empty () && data->verified())
       {
         m_segmentCallback (m_deviceName, m_name, seqno, data);
       }
@@ -141,9 +141,12 @@
   }
   else
     {
+      // in this case we don't care whether "data" is verified, in fact, we expect it is unverified
       try {
         PcoPtr pco = make_shared<ParsedContentObject> (*data->contentPtr ());
-        if (!m_segmentCallback.empty ())
+
+        // we need to verify this pco and apply callback only when verified
+        if (!m_segmentCallback.empty () && m_ccnx->verifyPco(pco))
           {
             m_segmentCallback (m_deviceName, m_name, seqno, pco);
           }