fw: make strategies understand scope

refs #1253

Change-Id: I57f7a6008e6f08c9817e58f480020eb9219a4aec
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index 8f87270..ac865d0 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -20,19 +20,34 @@
 {
 }
 
+static inline bool
+predicate_PitEntry_canForwardTo_NextHop(shared_ptr<pit::Entry> pitEntry,
+                                        const fib::NextHop& nexthop)
+{
+  return pitEntry->canForwardTo(*nexthop.getFace());
+}
+
 void
 BestRouteStrategy::afterReceiveInterest(const Face& inFace,
                    const Interest& interest,
                    shared_ptr<fib::Entry> fibEntry,
                    shared_ptr<pit::Entry> pitEntry)
 {
+  if (pitEntry->hasUnexpiredOutRecords()) {
+    // not a new Interest, don't forward
+    return;
+  }
+
   const fib::NextHopList& nexthops = fibEntry->getNextHops();
-  if (nexthops.size() == 0) {
+  fib::NextHopList::const_iterator it = std::find_if(nexthops.begin(), nexthops.end(),
+    bind(&predicate_PitEntry_canForwardTo_NextHop, pitEntry, _1));
+
+  if (it == nexthops.end()) {
     this->rejectPendingInterest(pitEntry);
     return;
   }
-  
-  shared_ptr<Face> outFace = nexthops.begin()->getFace();
+
+  shared_ptr<Face> outFace = it->getFace();
   this->sendInterest(pitEntry, outFace);
 }
 
diff --git a/daemon/fw/broadcast-strategy.cpp b/daemon/fw/broadcast-strategy.cpp
index 3e8c2bf..7f5b248 100644
--- a/daemon/fw/broadcast-strategy.cpp
+++ b/daemon/fw/broadcast-strategy.cpp
@@ -28,16 +28,14 @@
 {
   const fib::NextHopList& nexthops = fibEntry->getNextHops();
 
-  bool isPropagated = false;
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     shared_ptr<Face> outFace = it->getFace();
-    if (outFace->getId() != inFace.getId()) {
+    if (pitEntry->canForwardTo(*outFace)) {
       this->sendInterest(pitEntry, outFace);
-      isPropagated = true;
     }
   }
 
-  if (!isPropagated) {
+  if (!pitEntry->hasUnexpiredOutRecords()) {
     this->rejectPendingInterest(pitEntry);
   }
 }
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index a723a17..4ab852e 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -13,9 +13,7 @@
 
 using fw::Strategy;
 
-const ndn::Milliseconds Forwarder::DEFAULT_INTEREST_LIFETIME(static_cast<ndn::Milliseconds>(4000));
 const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
-const Name Forwarder::LOCALHOP_NAME("ndn:/localhop");
 
 Forwarder::Forwarder()
   : m_faceTable(*this)
@@ -35,9 +33,6 @@
   NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() <<
                 " interest=" << interest.getName());
   const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
-  if (interest.getInterestLifetime() < 0) {
-    const_cast<Interest&>(interest).setInterestLifetime(DEFAULT_INTEREST_LIFETIME);
-  }
   m_counters.getInInterest() ++;
 
   // /localhost scope control
@@ -132,22 +127,10 @@
   NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
                 " interest=" << pitEntry->getName());
 
-  // /localhost scope control
-  bool isViolatingLocalhost = !outFace.isLocal() &&
-                              LOCALHOST_NAME.isPrefixOf(pitEntry->getName());
-  if (isViolatingLocalhost) {
+  // scope control
+  if (pitEntry->violatesScope(outFace)) {
     NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
-                  " interest=" << pitEntry->getName() << " violates /localhost");
-    return;
-  }
-
-  // /localhop scope control
-  bool isViolatingLocalhop = !outFace.isLocal() &&
-                             LOCALHOP_NAME.isPrefixOf(pitEntry->getName()) &&
-                             !pitEntry->hasLocalInRecord();
-  if (isViolatingLocalhop) {
-    NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
-                  " interest=" << pitEntry->getName() << " violates /localhop");
+                  " interest=" << pitEntry->getName() << " violates scope");
     return;
   }
 
@@ -323,6 +306,12 @@
 void
 Forwarder::setStragglerTimer(shared_ptr<pit::Entry> pitEntry)
 {
+  if (pitEntry->hasUnexpiredOutRecords()) {
+    NFD_LOG_DEBUG("setStragglerTimer " << pitEntry->getName() <<
+                  " cannot set StragglerTimer when an OutRecord is pending");
+    return;
+  }
+
   time::Duration stragglerTime = time::milliseconds(100);
 
   pitEntry->m_stragglerTimer = scheduler::schedule(stragglerTime,
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index e229dcf..2d4d325 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -157,9 +157,7 @@
   Measurements   m_measurements;
   StrategyChoice m_strategyChoice;
 
-  static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME;
   static const Name LOCALHOST_NAME;
-  static const Name LOCALHOP_NAME;
 
   // allow Strategy (base class) to enter pipelines
   friend class fw::Strategy;
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index d8928f5..79d8b1a 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -53,7 +53,7 @@
 
   shared_ptr<Face> bestFace = measurementsEntryInfo->getBestFace();
   if (static_cast<bool>(bestFace) && fibEntry->hasNextHop(bestFace) &&
-      pitEntry->canForwardTo(bestFace)) {
+      pitEntry->canForwardTo(*bestFace)) {
     // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
     deferFirst = measurementsEntryInfo->m_prediction;
     deferRange = static_cast<time::Duration>((deferFirst + time::microseconds(1)) / 2);
@@ -71,7 +71,7 @@
 
   shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock();
   if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) &&
-      pitEntry->canForwardTo(previousFace)) {
+      pitEntry->canForwardTo(*previousFace)) {
     --nUpstreams;
   }
 
@@ -103,7 +103,7 @@
 
   shared_ptr<Face> previousFace = measurementsEntryInfo->m_previousFace.lock();
   if (static_cast<bool>(previousFace) && fibEntry->hasNextHop(previousFace) &&
-      pitEntry->canForwardTo(previousFace)) {
+      pitEntry->canForwardTo(*previousFace)) {
     this->sendInterest(pitEntry, previousFace);
   }
 
@@ -111,7 +111,7 @@
   bool isForwarded = false;
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     shared_ptr<Face> face = it->getFace();
-    if (pitEntry->canForwardTo(face)) {
+    if (pitEntry->canForwardTo(*face)) {
       isForwarded = true;
       this->sendInterest(pitEntry, face);
       break;
diff --git a/daemon/table/pit-entry.cpp b/daemon/table/pit-entry.cpp
index 7523504..86596df 100644
--- a/daemon/table/pit-entry.cpp
+++ b/daemon/table/pit-entry.cpp
@@ -10,6 +10,9 @@
 namespace nfd {
 namespace pit {
 
+const Name Entry::LOCALHOST_NAME("ndn:/localhost");
+const Name Entry::LOCALHOP_NAME("ndn:/localhop");
+
 Entry::Entry(const Interest& interest)
   : m_interest(interest)
 {
@@ -48,24 +51,24 @@
 }
 
 static inline bool
-predicate_FaceRecord_Face(const FaceRecord& faceRecord, shared_ptr<Face> face)
+predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face)
 {
-  return faceRecord.getFace() == face;
+  return faceRecord.getFace().get() == face;
 }
 
 static inline bool
 predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
-  shared_ptr<Face> face, time::Point now)
+  const Face* face, time::Point now)
 {
-  return faceRecord.getFace() != face && faceRecord.getExpiry() >= now;
+  return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now;
 }
 
 bool
-Entry::canForwardTo(shared_ptr<Face> face) const
+Entry::canForwardTo(const Face& face) const
 {
   OutRecordCollection::const_iterator outIt = std::find_if(
     m_outRecords.begin(), m_outRecords.end(),
-    bind(&predicate_FaceRecord_Face, _1, face));
+    bind(&predicate_FaceRecord_Face, _1, &face));
   bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
                                outIt->getExpiry() >= time::now();
   if (hasUnexpiredOutRecord) {
@@ -74,9 +77,34 @@
 
   InRecordCollection::const_iterator inIt = std::find_if(
     m_inRecords.begin(), m_inRecords.end(),
-    bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, face, time::now()));
+    bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::now()));
   bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
-  return hasUnexpiredOtherInRecord;
+  if (!hasUnexpiredOtherInRecord) {
+    return false;
+  }
+
+  return !this->violatesScope(face);
+}
+
+bool
+Entry::violatesScope(const Face& face) const
+{
+  // /localhost scope
+  bool isViolatingLocalhost = !face.isLocal() &&
+                              LOCALHOST_NAME.isPrefixOf(this->getName());
+  if (isViolatingLocalhost) {
+    return true;
+  }
+
+  // /localhop scope
+  bool isViolatingLocalhop = !face.isLocal() &&
+                             LOCALHOP_NAME.isPrefixOf(this->getName()) &&
+                             !this->hasLocalInRecord();
+  if (isViolatingLocalhop) {
+    return true;
+  }
+
+  return false;
 }
 
 bool
@@ -92,7 +120,7 @@
 Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
 {
   InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
-    m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
+    m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
   if (it == m_inRecords.end()) {
     m_inRecords.push_front(InRecord(face));
     it = m_inRecords.begin();
@@ -112,7 +140,7 @@
 Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
 {
   OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
-    m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
+    m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
   if (it == m_outRecords.end()) {
     m_outRecords.push_front(OutRecord(face));
     it = m_outRecords.begin();
@@ -127,12 +155,25 @@
 Entry::deleteOutRecord(shared_ptr<Face> face)
 {
   OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
-    m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face));
+    m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
   if (it != m_outRecords.end()) {
     m_outRecords.erase(it);
   }
 }
 
+static inline bool
+predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, time::Point now)
+{
+  return faceRecord.getExpiry() >= now;
+}
+
+bool
+Entry::hasUnexpiredOutRecords() const
+{
+  OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(),
+    m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::now()));
+  return it != m_outRecords.end();
+}
 
 } // namespace pit
 } // namespace nfd
diff --git a/daemon/table/pit-entry.hpp b/daemon/table/pit-entry.hpp
index 1e342e2..e72365b 100644
--- a/daemon/table/pit-entry.hpp
+++ b/daemon/table/pit-entry.hpp
@@ -64,10 +64,23 @@
   /** \brief decides whether Interest can be forwarded to face
    *
    *  \return true if OutRecord of this face does not exist or has expired,
-   *          and there is an InRecord not of this face
+   *          and there is an InRecord not of this face,
+   *          and scope is not violated
    */
   bool
-  canForwardTo(shared_ptr<Face> face) const;
+  canForwardTo(const Face& face) const;
+
+  /** \brief decides whether forwarding Interest to face would violate scope
+   *
+   *  \return true if scope control would be violated
+   *  \note canForwardTo has more comprehensive checks (including scope control)
+   *        and should be used by most strategies. Outgoing Interest pipeline
+   *        should only check scope because some strategy (eg. vehicular) needs
+   *        to retransmit sooner than OutRecord expiry, or forward Interest
+   *        back to incoming face
+   */
+  bool
+  violatesScope(const Face& face) const;
 
   /** \brief records a nonce
    *
@@ -101,6 +114,11 @@
   void
   deleteOutRecord(shared_ptr<Face> face);
 
+  /** \return true if there is one or more unexpired OutRecords
+   */
+  bool
+  hasUnexpiredOutRecords() const;
+
 public:
   EventId m_unsatisfyTimer;
   EventId m_stragglerTimer;
@@ -110,6 +128,10 @@
   const Interest m_interest;
   InRecordCollection m_inRecords;
   OutRecordCollection m_outRecords;
+
+  static const Name LOCALHOST_NAME;
+  static const Name LOCALHOP_NAME;
+
   shared_ptr<name_tree::Entry> m_nameTreeEntry;
 
   friend class nfd::NameTree;
diff --git a/daemon/table/pit-face-record.cpp b/daemon/table/pit-face-record.cpp
index cbcd3b1..5663dce 100644
--- a/daemon/table/pit-face-record.cpp
+++ b/daemon/table/pit-face-record.cpp
@@ -30,7 +30,13 @@
 {
   m_lastNonce = interest.getNonce();
   m_lastRenewed = time::now();
-  m_expiry = m_lastRenewed + time::milliseconds(interest.getInterestLifetime());
+
+  const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME = static_cast<ndn::Milliseconds>(4000);
+  ndn::Milliseconds lifetime = interest.getInterestLifetime();
+  if (lifetime < 0) {
+    lifetime = DEFAULT_INTEREST_LIFETIME;
+  }
+  m_expiry = m_lastRenewed + time::milliseconds(lifetime);
 }
 
 
diff --git a/tests/fw/broadcast-strategy.cpp b/tests/fw/broadcast-strategy.cpp
index b256432..9c69c10 100644
--- a/tests/fw/broadcast-strategy.cpp
+++ b/tests/fw/broadcast-strategy.cpp
@@ -15,7 +15,7 @@
 
 BOOST_FIXTURE_TEST_SUITE(FwBroadcastStrategy, BaseFixture)
 
-BOOST_AUTO_TEST_CASE(ForwardTwo)
+BOOST_AUTO_TEST_CASE(Forward2)
 {
   Forwarder forwarder;
   typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
@@ -29,18 +29,17 @@
   forwarder.addFace(face3);
 
   Fib& fib = forwarder.getFib();
-  std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = fib.insert(Name());
-  shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
+  shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
   fibEntry->addNextHop(face1, 0);
   fibEntry->addNextHop(face2, 0);
   fibEntry->addNextHop(face3, 0);
 
-  Interest interest(Name("ndn:/H0D6i5fc"));
+  shared_ptr<Interest> interest = makeInterest("ndn:/H0D6i5fc");
   Pit& pit = forwarder.getPit();
-  std::pair<shared_ptr<pit::Entry>, bool> pitInsertResult = pit.insert(interest);
-  shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
+  shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
+  pitEntry->insertOrUpdateInRecord(face3, *interest);
 
-  strategy.afterReceiveInterest(*face3, interest, fibEntry, pitEntry);
+  strategy.afterReceiveInterest(*face3, *interest, fibEntry, pitEntry);
   BOOST_CHECK_EQUAL(strategy.m_rejectPendingInterestHistory.size(), 0);
   BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.size(), 2);
   bool hasFace1 = false;
@@ -58,7 +57,32 @@
   BOOST_CHECK(hasFace1 && hasFace2);
 }
 
-BOOST_AUTO_TEST_CASE(Reject)
+BOOST_AUTO_TEST_CASE(RejectScope)
+{
+  Forwarder forwarder;
+  typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
+  BroadcastStrategyTester strategy(forwarder);
+
+  shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
+  shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
+  forwarder.addFace(face1);
+  forwarder.addFace(face2);
+
+  Fib& fib = forwarder.getFib();
+  shared_ptr<fib::Entry> fibEntry = fib.insert("ndn:/localhop/uS09bub6tm").first;
+  fibEntry->addNextHop(face2, 0);
+
+  shared_ptr<Interest> interest = makeInterest("ndn:/localhop/uS09bub6tm/eG3MMoP6z");
+  Pit& pit = forwarder.getPit();
+  shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
+  pitEntry->insertOrUpdateInRecord(face1, *interest);
+
+  strategy.afterReceiveInterest(*face1, *interest, fibEntry, pitEntry);
+  BOOST_CHECK_EQUAL(strategy.m_rejectPendingInterestHistory.size(), 1);
+  BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(RejectLoopback)
 {
   Forwarder forwarder;
   typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
@@ -68,16 +92,15 @@
   forwarder.addFace(face1);
 
   Fib& fib = forwarder.getFib();
-  std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = fib.insert(Name());
-  shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
+  shared_ptr<fib::Entry> fibEntry = fib.insert(Name()).first;
   fibEntry->addNextHop(face1, 0);
 
-  Interest interest(Name("ndn:/H0D6i5fc"));
+  shared_ptr<Interest> interest = makeInterest("ndn:/H0D6i5fc");
   Pit& pit = forwarder.getPit();
-  std::pair<shared_ptr<pit::Entry>, bool> pitInsertResult = pit.insert(interest);
-  shared_ptr<pit::Entry> pitEntry = pitInsertResult.first;
+  shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
+  pitEntry->insertOrUpdateInRecord(face1, *interest);
 
-  strategy.afterReceiveInterest(*face1, interest, fibEntry, pitEntry);
+  strategy.afterReceiveInterest(*face1, *interest, fibEntry, pitEntry);
   BOOST_CHECK_EQUAL(strategy.m_rejectPendingInterestHistory.size(), 1);
   BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory.size(), 0);
 }
diff --git a/tests/fw/client-control-strategy.cpp b/tests/fw/client-control-strategy.cpp
index 42af192..aab5baa 100644
--- a/tests/fw/client-control-strategy.cpp
+++ b/tests/fw/client-control-strategy.cpp
@@ -38,9 +38,10 @@
   Pit& pit = forwarder.getPit();
 
   // Interest with valid NextHopFaceId
-  shared_ptr<Interest> interest1 = make_shared<Interest>("ndn:/0z8r6yDDe");
+  shared_ptr<Interest> interest1 = makeInterest("ndn:/0z8r6yDDe");
   interest1->setNextHopFaceId(face1->getId());
   shared_ptr<pit::Entry> pitEntry1 = pit.insert(*interest1).first;
+  pitEntry1->insertOrUpdateInRecord(face4, *interest1);
 
   strategy.m_sendInterestHistory.clear();
   strategy.afterReceiveInterest(*face4, *interest1, fibEntry, pitEntry1);
@@ -48,8 +49,9 @@
   BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[0].get<1>(), face1);
 
   // Interest without NextHopFaceId
-  shared_ptr<Interest> interest2 = make_shared<Interest>("ndn:/y6JQADGVz");
+  shared_ptr<Interest> interest2 = makeInterest("ndn:/y6JQADGVz");
   shared_ptr<pit::Entry> pitEntry2 = pit.insert(*interest2).first;
+  pitEntry2->insertOrUpdateInRecord(face4, *interest2);
 
   strategy.m_sendInterestHistory.clear();
   strategy.afterReceiveInterest(*face4, *interest2, fibEntry, pitEntry2);
@@ -57,9 +59,10 @@
   BOOST_CHECK_EQUAL(strategy.m_sendInterestHistory[0].get<1>(), face2);
 
   // Interest with invalid NextHopFaceId
-  shared_ptr<Interest> interest3 = make_shared<Interest>("ndn:/0z8r6yDDe");
+  shared_ptr<Interest> interest3 = makeInterest("ndn:/0z8r6yDDe");
   interest3->setNextHopFaceId(face3->getId());
   shared_ptr<pit::Entry> pitEntry3 = pit.insert(*interest3).first;
+  pitEntry3->insertOrUpdateInRecord(face4, *interest3);
 
   forwarder.removeFace(face3); // face3 is removed and its FaceId becomes invalid
   strategy.m_sendInterestHistory.clear();
diff --git a/tests/table/pit.cpp b/tests/table/pit.cpp
index 393c1bc..bf1e073 100644
--- a/tests/table/pit.cpp
+++ b/tests/table/pit.cpp
@@ -32,9 +32,9 @@
   shared_ptr<Interest> interest4 = makeInterest(name);
   interest4->setInterestLifetime(static_cast<ndn::Milliseconds>(8795));
   interest4->setNonce(17365);
-  
+
   pit::Entry entry(*interest);
-  
+
   BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
   BOOST_CHECK_EQUAL(entry.getName(), name);
 
@@ -42,7 +42,7 @@
   BOOST_CHECK_EQUAL(inRecords1.size(), 0);
   const pit::OutRecordCollection& outRecords1 = entry.getOutRecords();
   BOOST_CHECK_EQUAL(outRecords1.size(), 0);
-  
+
   // insert InRecord
   time::Point before1 = time::now();
   pit::InRecordCollection::iterator in1 =
@@ -58,7 +58,7 @@
   BOOST_CHECK_LE(std::abs(in1->getExpiry() - in1->getLastRenewed()
     - time::milliseconds(interest1->getInterestLifetime())),
     (after1 - before1));
-  
+
   // insert OutRecord
   time::Point before2 = time::now();
   pit::OutRecordCollection::iterator out1 =
@@ -74,7 +74,7 @@
   BOOST_CHECK_LE(std::abs(out1->getExpiry() - out1->getLastRenewed()
     - time::milliseconds(interest1->getInterestLifetime())),
     (after2 - before2));
-  
+
   // update InRecord
   time::Point before3 = time::now();
   pit::InRecordCollection::iterator in2 =
@@ -95,7 +95,7 @@
   const pit::InRecordCollection& inRecords4 = entry.getInRecords();
   BOOST_CHECK_EQUAL(inRecords4.size(), 2);
   BOOST_CHECK_EQUAL(in3->getFace(), face2);
-  
+
   // delete all InRecords
   entry.deleteInRecords();
   const pit::InRecordCollection& inRecords5 = entry.getInRecords();
@@ -107,28 +107,62 @@
   const pit::OutRecordCollection& outRecords3 = entry.getOutRecords();
   BOOST_CHECK_EQUAL(outRecords3.size(), 2);
   BOOST_CHECK_EQUAL(out2->getFace(), face2);
-  
+
   // delete OutRecord
   entry.deleteOutRecord(face2);
   const pit::OutRecordCollection& outRecords4 = entry.getOutRecords();
   BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
   BOOST_CHECK_EQUAL(outRecords4.begin()->getFace(), face1);
-  
 }
 
 BOOST_AUTO_TEST_CASE(EntryNonce)
 {
-  Name name("ndn:/qtCQ7I1c");
-  Interest interest(name);
-  
-  pit::Entry entry(interest);
-  
+  shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c");
+
+  pit::Entry entry(*interest);
+
   BOOST_CHECK_EQUAL(entry.addNonce(25559), true);
   BOOST_CHECK_EQUAL(entry.addNonce(25559), false);
   BOOST_CHECK_EQUAL(entry.addNonce(19004), true);
   BOOST_CHECK_EQUAL(entry.addNonce(19004), false);
 }
 
+BOOST_AUTO_TEST_CASE(EntryLifetime)
+{
+  shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
+  BOOST_ASSERT(interest->getInterestLifetime() < 0); // library uses -1 to indicate unset lifetime
+
+  shared_ptr<Face> face = make_shared<DummyFace>();
+  pit::Entry entry(*interest);
+
+  pit::InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(face, *interest);
+  BOOST_CHECK_GT(inIt->getExpiry(), time::now());
+
+  pit::OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(face, *interest);
+  BOOST_CHECK_GT(outIt->getExpiry(), time::now());
+}
+
+BOOST_AUTO_TEST_CASE(EntryCanForwardTo)
+{
+  shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG");
+  pit::Entry entry(*interest);
+
+  shared_ptr<Face> face1 = make_shared<DummyFace>();
+  shared_ptr<Face> face2 = make_shared<DummyFace>();
+
+  entry.insertOrUpdateInRecord(face1, *interest);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
+
+  entry.insertOrUpdateInRecord(face2, *interest);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), true);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
+
+  entry.insertOrUpdateOutRecord(face1, *interest);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face1), false);
+  BOOST_CHECK_EQUAL(entry.canForwardTo(*face2), true);
+}
+
 BOOST_AUTO_TEST_CASE(Insert)
 {
   Name name1("ndn:/5vzBNnMst");
@@ -161,12 +195,12 @@
   Interest interestJ(name1, -1, -1, exclude0, -1, false, -1, -1.0, 2192);
   // different Name+exclude1
   Interest interestK(name2, -1, -1, exclude1, -1, false, -1, -1.0, 0);
-  
+
   NameTree nameTree(16);
   Pit pit(nameTree);
 
   std::pair<shared_ptr<pit::Entry>, bool> insertResult;
-  
+
   BOOST_CHECK_EQUAL(pit.size(), 0);
 
   insertResult = pit.insert(interestA);
@@ -176,28 +210,28 @@
   insertResult = pit.insert(interestB);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 2);
-  
+
   insertResult = pit.insert(interestC);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 3);
-  
+
   insertResult = pit.insert(interestD);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 4);
-  
+
   insertResult = pit.insert(interestE);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 5);
-  
+
   insertResult = pit.insert(interestF);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 6);
-  
+
   insertResult = pit.insert(interestG);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 7);
 
-  
+
   insertResult = pit.insert(interestH);
   BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
   BOOST_CHECK_EQUAL(pit.size(), 7);
@@ -205,11 +239,11 @@
   insertResult = pit.insert(interestI);
   BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
   BOOST_CHECK_EQUAL(pit.size(), 7);
-  
+
   insertResult = pit.insert(interestJ);
   BOOST_CHECK_EQUAL(insertResult.second, false);// only guiders differ
   BOOST_CHECK_EQUAL(pit.size(), 7);
-  
+
   insertResult = pit.insert(interestK);
   BOOST_CHECK_EQUAL(insertResult.second, true);
   BOOST_CHECK_EQUAL(pit.size(), 8);
@@ -223,7 +257,7 @@
   Pit pit(nameTree);
 
   std::pair<shared_ptr<pit::Entry>, bool> insertResult;
-  
+
   BOOST_CHECK_EQUAL(pit.size(), 0);
 
   insertResult = pit.insert(interest);
@@ -233,7 +267,7 @@
   insertResult = pit.insert(interest);
   BOOST_CHECK_EQUAL(insertResult.second, false);
   BOOST_CHECK_EQUAL(pit.size(), 1);
-  
+
   pit.erase(insertResult.first);
   BOOST_CHECK_EQUAL(pit.size(), 0);
 
@@ -258,7 +292,7 @@
   NameTree nameTree(16);
   Pit pit(nameTree);
   int count = 0;
-  
+
   BOOST_CHECK_EQUAL(pit.size(), 0);
 
   pit.insert(interestA  );
@@ -266,13 +300,13 @@
   pit.insert(interestD  );
 
   nameTree.lookup(nameABCD); // make sure /A/B/C/D is in nameTree
-  
+
   BOOST_CHECK_EQUAL(pit.size(), 3);
 
   Data data(nameABCD);
-  
+
   shared_ptr<pit::DataMatchResult> matches = pit.findAllDataMatches(data);
-  
+
   bool hasA   = false;
   bool hasAB  = false;
   bool hasABC = false;
@@ -288,10 +322,10 @@
 
     if (entry->getName().equals(nameAB))
       hasAB  = true;
-  
+
     if (entry->getName().equals(nameABC))
       hasABC = true;
-    
+
     if (entry->getName().equals(nameD))
       hasD   = true;
   }