face: NDNLP partial message store

refs #1208

Change-Id: Ie10d590ebd2e3631c884a7b6b2d5ffce2758ab4d
diff --git a/tests/face/ndnlp.cpp b/tests/face/ndnlp.cpp
index f3b8ab6..1160cf3 100644
--- a/tests/face/ndnlp.cpp
+++ b/tests/face/ndnlp.cpp
@@ -6,6 +6,7 @@
 
 #include "face/ndnlp-sequence-generator.hpp"
 #include "face/ndnlp-slicer.hpp"
+#include "face/ndnlp-partial-message-store.hpp"
 
 #include <boost/test/unit_test.hpp>
 
@@ -76,7 +77,7 @@
 }
 
 // slice a Block to four NDNLP packets
-BOOST_AUTO_TEST_CASE(SliceData4)
+BOOST_AUTO_TEST_CASE(Slice4)
 {
   uint8_t blockValue[5050];
   memset(blockValue, 0xcc, sizeof(blockValue));
@@ -132,6 +133,85 @@
   BOOST_CHECK_EQUAL(totalPayloadSize, block.size());
 }
 
+class ReassembleFixture
+{
+protected:
+  ReassembleFixture()
+    : m_slicer(1500)
+    , m_scheduler(m_io)
+    , m_partialMessageStore(m_scheduler)
+  {
+    m_partialMessageStore.onReceive += bind(&std::vector<Block>::push_back,
+                                            &m_received, _1);
+  }
+
+  Block
+  makeBlock(size_t valueLength)
+  {
+    uint8_t blockValue[valueLength];
+    memset(blockValue, 0xcc, sizeof(blockValue));
+    return ndn::dataBlock(0x01, blockValue, sizeof(blockValue));
+  }
+  
+protected:
+  boost::asio::io_service m_io;
+  Scheduler m_scheduler;
+  
+  ndnlp::Slicer m_slicer;
+  ndnlp::PartialMessageStore m_partialMessageStore;
+
+  // received network layer packets
+  std::vector<Block> m_received;
+};
+
+// reassemble one NDNLP packets into one Block
+BOOST_FIXTURE_TEST_CASE(Reassemble1, ReassembleFixture)
+{
+  Block block = makeBlock(60);
+  ndnlp::PacketArray pa = m_slicer.slice(block);
+  BOOST_REQUIRE_EQUAL(pa->size(), 1);
+  
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa->at(0));
+  
+  BOOST_REQUIRE_EQUAL(m_received.size(), 1);
+  BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(),
+                                block.begin(),            block.end());
+}
+
+// reassemble four and two NDNLP packets into two Blocks
+BOOST_FIXTURE_TEST_CASE(Reassemble4and2, ReassembleFixture)
+{
+  Block block = makeBlock(5050);
+  ndnlp::PacketArray pa = m_slicer.slice(block);
+  BOOST_REQUIRE_EQUAL(pa->size(), 4);
+  
+  Block block2 = makeBlock(2000);
+  ndnlp::PacketArray pa2 = m_slicer.slice(block2);
+  BOOST_REQUIRE_EQUAL(pa2->size(), 2);
+  
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa->at(0));
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa->at(1));
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa2->at(1));
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa->at(1));
+  BOOST_CHECK_EQUAL(m_received.size(), 0);
+  m_partialMessageStore.receiveNdnlpData(pa2->at(0));
+  BOOST_CHECK_EQUAL(m_received.size(), 1);
+  m_partialMessageStore.receiveNdnlpData(pa->at(3));
+  BOOST_CHECK_EQUAL(m_received.size(), 1);
+  m_partialMessageStore.receiveNdnlpData(pa->at(2));
+  
+  BOOST_REQUIRE_EQUAL(m_received.size(), 2);
+  BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(1).begin(), m_received.at(1).end(),
+                                block.begin(),            block.end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(),
+                                block2.begin(),           block2.end());
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace nfd