util: fix SegmentFetcher undefined behavior caused by uncanceled pending interest

Change-Id: I4ca80ad00fc52024f35908b21d7c42bffc8a5396
Refs: #4770
diff --git a/tests/unit/util/segment-fetcher.t.cpp b/tests/unit/util/segment-fetcher.t.cpp
index 22aa0b7..f870030 100644
--- a/tests/unit/util/segment-fetcher.t.cpp
+++ b/tests/unit/util/segment-fetcher.t.cpp
@@ -855,6 +855,28 @@
   BOOST_CHECK_EQUAL(nAfterSegmentTimedOut, 2);
 }
 
+BOOST_AUTO_TEST_CASE(UncanceledPendingInterestBug) // Bug #4770
+{
+  DummyValidator acceptValidator;
+  auto fetcher = SegmentFetcher::start(face, Interest("/hello/world"), acceptValidator);
+  connectSignals(fetcher);
+
+  // Fetcher will send the first interest immediately
+  // and the second interest after 1 second by default
+  advanceClocks(1100_ms);
+
+  // Face will give data to the fetcher twice if the first interest is not canceled.
+  // isFinal=false to keep fetcher alive so that it can receive the second data.
+  face.receive(*makeDataSegment("/hello/world/version0", 0, false));
+
+  advanceClocks(1100_ms);
+
+  face.receive(*makeDataSegment("/hello/world/version0", 1, true));
+
+  BOOST_CHECK_EQUAL(nErrors, 0);
+  BOOST_CHECK_EQUAL(nCompletions, 1);
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestSegmentFetcher
 BOOST_AUTO_TEST_SUITE_END() // Util