Added encoding/decoding tests to TestTorrentFileGenerator.
Additionally, bug fix of finalizing Torrent segments in TorrentFile::generate()

Change-Id: Iad976e3e1c26e7ec2040d41e52a8fb1e5988beeb
diff --git a/src/torrent-file.cpp b/src/torrent-file.cpp
index 2b8fd15..c230401 100644
--- a/src/torrent-file.cpp
+++ b/src/torrent-file.cpp
@@ -284,12 +284,14 @@
 
   // Sign and append the last torrent-file
   security::KeyChain keyChain;
+  currentTorrentFile.finalize();
   keyChain.sign(currentTorrentFile, signingWithSha256());
   torrentSegments.push_back(currentTorrentFile);
 
   for (auto it = torrentSegments.rbegin() + 1; it != torrentSegments.rend(); ++it) {
     auto next = it - 1;
     it->setTorrentFilePtr(next->getFullName());
+    it->finalize();
     keyChain.sign(*it, signingWithSha256());
   }
 
diff --git a/tests/unit-tests/file-manifest.t.cpp b/tests/unit-tests/file-manifest.t.cpp
index 226ae2b..80a344e 100644
--- a/tests/unit-tests/file-manifest.t.cpp
+++ b/tests/unit-tests/file-manifest.t.cpp
@@ -391,8 +391,7 @@
         BOOST_CHECK_NO_THROW(it->getFullName());
         BOOST_CHECK_EQUAL(it->data_packet_size(), dataPacketSize);
         BOOST_CHECK_EQUAL(it->catalog_prefix(), catalogPrefix);
-        auto block = it->wireEncode();
-        BOOST_CHECK_EQUAL(*it, FileManifest(block));
+        BOOST_CHECK_EQUAL(*it, FileManifest(it->wireEncode()));
         if (it != manifests.end() -1) {
           BOOST_CHECK_EQUAL(it->catalog().size(), subManifestSize);
           BOOST_CHECK_EQUAL(*(it->submanifest_ptr()), (it+1)->getFullName());
diff --git a/tests/unit-tests/torrent-file.t.cpp b/tests/unit-tests/torrent-file.t.cpp
index c946b6c..83e1f2a 100644
--- a/tests/unit-tests/torrent-file.t.cpp
+++ b/tests/unit-tests/torrent-file.t.cpp
@@ -27,6 +27,7 @@
 #include <ndn-cxx/data.hpp>
 #include <ndn-cxx/signature.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/util/io.hpp>
 
 #include <boost/range/adaptors.hpp>
 #include <boost/filesystem.hpp>
@@ -34,6 +35,8 @@
 
 namespace fs = boost::filesystem;
 
+BOOST_TEST_DONT_PRINT_LOG_VALUE(std::nullptr_t)
+
 namespace ndn {
 
 namespace ntorrent {
@@ -387,6 +390,7 @@
                           Name("/NTORRENT" +
                                directoryPathName.getSubName(
                                  directoryPathName.size() - 1).toUri()));
+        BOOST_CHECK_EQUAL(*it, TorrentFile(it->wireEncode()));
         if (it != torrentFileSegments.end() - 1) {
           BOOST_CHECK_EQUAL(it->getCatalog().size(), namesPerSegment);
           BOOST_CHECK_EQUAL(*(it->getTorrentFilePtr()), (it+1)->getFullName());
@@ -426,6 +430,27 @@
       // confirm that they are equal
       BOOST_CHECK_EQUAL_COLLECTIONS(directoryFilesBytes.begin(), directoryFilesBytes.end(),
                                     dataBytes.begin(), dataBytes.end());
+      // test that we can write the torrent file to the disk and read it out again
+      {
+        std::string dirPath = "tests/testdata/temp/";
+        boost::filesystem::create_directory(dirPath);
+        auto fileNum = 0;
+        for (auto &s : torrentFileSegments) {
+          fileNum++;
+          auto filename = dirPath + to_string(fileNum);
+          io::save(s, filename);
+        }
+        // read them back out
+        fileNum = 0;
+        for (auto &s : torrentFileSegments) {
+          fileNum++;
+          auto filename = dirPath + to_string(fileNum);
+          auto torrent_ptr = io::load<TorrentFile>(filename);
+          BOOST_CHECK_NE(torrent_ptr, nullptr);
+          BOOST_CHECK_EQUAL(s, *torrent_ptr);
+        }
+        fs::remove_all(dirPath);
+      }
     }
   }
 }