rib: put PrefixAnnouncement in Route and RibEntry

refs #4650

Change-Id: I03a846cd6e013d52f080bd7b092cd392b0284f88
diff --git a/tests/rib/rib-entry.t.cpp b/tests/rib/rib-entry.t.cpp
index 65f73dc..a5c9f3c 100644
--- a/tests/rib/rib-entry.t.cpp
+++ b/tests/rib/rib-entry.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,7 +30,9 @@
 namespace rib {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TestRibEntry, nfd::tests::BaseFixture)
+using namespace nfd::tests;
+
+BOOST_FIXTURE_TEST_SUITE(TestRibEntry, BaseFixture)
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
@@ -72,6 +74,100 @@
   BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
 }
 
+BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, UnitTestTimeFixture)
+
+static Route
+makeSimpleRoute(uint64_t faceId)
+{
+  Route route;
+  route.faceId = faceId;
+  return route;
+}
+
+static Route
+makeSimpleRoute(uint64_t faceId, time::steady_clock::Duration expiration)
+{
+  Route route = makeSimpleRoute(faceId);
+  route.expires = time::steady_clock::now() + expiration;
+  return route;
+}
+
+BOOST_AUTO_TEST_CASE(Empty)
+{
+  RibEntry entry;
+  // entry has no Route
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_GE(pa.getExpiration(), 15_s);
+  BOOST_CHECK_LE(pa.getExpiration(), 30_s);
+  // A RibEntry without Route does not have a defined expiration time,
+  // so this test only checks that there's no exception and the expiration period is clamped.
+}
+
+BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
+{
+  RibEntry entry;
+  entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
+  entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
+  entry.insertRoute(makeSimpleRoute(8282, 799_s));
+  entry.insertRoute(makeSimpleRoute(1141));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
+}
+
+BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
+{
+  RibEntry entry;
+  entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
+
+  advanceClocks(100_s);
+  entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
+  // Although face 7557's announcement has a longer expiration period,
+  // the route for face 1010 expires last and thus its announcement is chosen.
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncement)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(6398, 765_s));
+  entry.insertRoute(makeSimpleRoute(2954, 411_s));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(4877, 240_s));
+  entry.insertRoute(makeSimpleRoute(5053));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(4877, 8_s));
+  entry.insertRoute(makeSimpleRoute(5053, 9_s));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
+}
+
+BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
+
 BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
 
 } // namespace tests