Merge "Moved and updated gitignore and removed tracked .pyc files"
diff --git a/src/torrent-file.cpp b/src/torrent-file.cpp
new file mode 100644
index 0000000..4302b53
--- /dev/null
+++ b/src/torrent-file.cpp
@@ -0,0 +1,241 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "torrent-file.hpp"
+
+#include <algorithm>
+
+#include <boost/range/adaptors.hpp>
+
+namespace ndn {
+
+namespace ntorrent {
+
+BOOST_CONCEPT_ASSERT((WireEncodable<TorrentFile>));
+BOOST_CONCEPT_ASSERT((WireDecodable<TorrentFile>));
+static_assert(std::is_base_of<Data::Error, TorrentFile::Error>::value,
+                "TorrentFile::Error should inherit from Data::Error");
+
+TorrentFile::TorrentFile(const Name& torrentFileName,
+                         const Name& torrentFilePtr,
+                         const Name& commonPrefix,
+                         const std::vector<ndn::Name>& catalog)
+  : Data(torrentFileName)
+  , m_commonPrefix(commonPrefix)
+  , m_torrentFilePtr(torrentFilePtr)
+  , m_catalog(catalog)
+{
+}
+
+TorrentFile::TorrentFile(const Name& torrentFileName,
+                         const Name& commonPrefix,
+                         const std::vector<ndn::Name>& catalog)
+  : Data(torrentFileName)
+  , m_commonPrefix(commonPrefix)
+  , m_catalog(catalog)
+{
+}
+
+TorrentFile::TorrentFile(const Block& block)
+{
+  this->wireDecode(block);
+}
+
+const Name&
+TorrentFile::getName() const
+{
+  return Data::getName();
+}
+
+const Name&
+TorrentFile::getCommonPrefix() const
+{
+  return m_commonPrefix;
+}
+
+void
+TorrentFile::createSuffixCatalog()
+{
+  for (auto i = m_catalog.begin() ; i != m_catalog.end(); ++i) {
+    m_suffixCatalog.push_back((*i).getSubName(m_commonPrefix.size()));
+  }
+}
+
+shared_ptr<Name>
+TorrentFile::getTorrentFilePtr() const
+{
+  if (this->hasTorrentFilePtr()) {
+    return make_shared<Name>(m_torrentFilePtr);
+  }
+  return nullptr;
+}
+
+void
+TorrentFile::constructLongNames()
+{
+  for (auto i = m_suffixCatalog.begin(); i != m_suffixCatalog.end(); ++i) {
+      Name commonPrefix = m_commonPrefix;
+      m_catalog.push_back(commonPrefix.append((*i)));
+  }
+}
+
+template<encoding::Tag TAG>
+size_t
+TorrentFile::encodeContent(EncodingImpl<TAG>& encoder) const
+{
+  // TorrentFileContent ::= CONTENT-TYPE TLV-LENGTH
+  //                      Suffix+
+  //                      CommonPrefix
+  //                      TorrentFilePtr?
+
+  // Suffix ::= NAME-TYPE TLV-LENGTH
+  //          Name
+
+  // CommonPrefix ::= NAME-TYPE TLV-LENGTH
+  //                Name
+
+  // TorrentFilePtr ::= NAME-TYPE TLV-LENGTH
+  //                  Name
+
+  size_t totalLength = 0;
+  for (const auto& name : m_suffixCatalog |  boost::adaptors::reversed) {
+    size_t fileManifestSuffixLength = 0;
+    fileManifestSuffixLength += name.wireEncode(encoder);
+    totalLength += fileManifestSuffixLength;
+  }
+  totalLength += m_commonPrefix.wireEncode(encoder);
+  if (!m_torrentFilePtr.empty()) {
+    size_t torrentFilePtrLength = 0;
+    torrentFilePtrLength += m_torrentFilePtr.wireEncode(encoder);
+    totalLength += torrentFilePtrLength;
+  }
+
+  totalLength += encoder.prependVarNumber(totalLength);
+  totalLength += encoder.prependVarNumber(tlv::Content);
+  return totalLength;
+}
+
+bool
+TorrentFile::erase(const Name& name)
+{
+  auto found = std::find(m_catalog.begin(), m_catalog.end(), name);
+  if (found != m_catalog.end()) {
+    m_catalog.erase(found);
+    return true;
+  }
+  return false;
+}
+
+void
+TorrentFile::encodeContent()
+{
+  onChanged();
+
+  EncodingEstimator estimator;
+  size_t estimatedSize = encodeContent(estimator);
+
+  EncodingBuffer buffer(estimatedSize, 0);
+  encodeContent(buffer);
+
+  setContentType(tlv::ContentType_Blob);
+  setContent(buffer.block());
+}
+
+void
+TorrentFile::decodeContent()
+{
+  // TorrentFileContent ::= CONTENT-TYPE TLV-LENGTH
+  //                      Suffix+
+  //                      CommonPrefix
+  //                      TorrentFilePtr?
+
+  // Suffix ::= NAME-TYPE TLV-LENGTH
+  //          Name
+
+  // CommonPrefix ::= NAME-TYPE TLV-LENGTH
+  //                Name
+
+  // TorrentFilePtr ::= NAME-TYPE TLV-LENGTH
+  //                  Name
+
+  if (getContentType() != tlv::ContentType_Blob) {
+      BOOST_THROW_EXCEPTION(Error("Expected Content Type Blob"));
+  }
+
+  const Block& content = Data::getContent();
+  content.parse();
+
+  // Check whether there is a TorrentFilePtr
+  auto element = content.elements_begin();
+  if (content.elements_end() == element) {
+    BOOST_THROW_EXCEPTION(Error(".Torrent-file with empty content"));
+  }
+  element->parse();
+  Name name(*element);
+  if (name.empty())
+    BOOST_THROW_EXCEPTION(Error("Empty name included in the .torrent-file"));
+
+  if (name.get(name.size() - 3) == name::Component(".torrent-file")) {
+    m_torrentFilePtr = name;
+    ++element;
+    m_commonPrefix = Name(*element);
+    if (m_commonPrefix.empty()) {
+      BOOST_THROW_EXCEPTION(Error("Common prefix cannot be empty"));
+    }
+  }
+  else {
+    m_commonPrefix = name;
+  }
+  element++;
+  for (; element != content.elements_end(); ++element) {
+    element->parse();
+    Name fileManifestSuffix(*element);
+    if (fileManifestSuffix.empty())
+      BOOST_THROW_EXCEPTION(Error("Empty manifest file name included in the .torrent-file"));
+    this->insertToSuffixCatalog(fileManifestSuffix);
+  }
+  if (m_suffixCatalog.size() == 0) {
+    BOOST_THROW_EXCEPTION(Error(".Torrent-file with empty catalog of file manifest names"));
+  }
+}
+
+void
+TorrentFile::wireDecode(const Block& wire)
+{
+  m_catalog.clear();
+  m_suffixCatalog.clear();
+  Data::wireDecode(wire);
+  this->decodeContent();
+  this->constructLongNames();
+}
+
+const Block&
+TorrentFile::wireEncode()
+{
+  this->createSuffixCatalog();
+  this->encodeContent();
+  m_suffixCatalog.clear();
+  return Data::wireEncode();
+}
+
+} // namespace ntorrent
+
+} // namespace ndn
diff --git a/src/torrent-file.hpp b/src/torrent-file.hpp
new file mode 100644
index 0000000..89d3173
--- /dev/null
+++ b/src/torrent-file.hpp
@@ -0,0 +1,226 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#ifndef TORRENT_FILE_HPP
+#define TORRENT_FILE_HPP
+
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/encoding/block.hpp>
+#include <ndn-cxx/data.hpp>
+
+#include <memory>
+
+namespace ndn {
+
+namespace ntorrent {
+
+class TorrentFile : public Data {
+public:
+  class Error : public Data::Error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : Data::Error(what)
+    {
+    }
+  };
+
+  /**
+   * @brief Create a new empty .TorrentFile.
+   */
+  TorrentFile() = default;
+
+  /**
+   * @brief Create a new .TorrentFile.
+   * @param torrentFileName The name of the .torrent-file
+   * @param torrentFilePtr A pointer (name) to the next segment of the .torrent-file
+   * @param commonPrefix The common name prefix of the manifest file names included in the catalog
+   * @param catalog The catalog containing the name of each file manifest
+   */
+  TorrentFile(const Name& torrentFileName,
+              const Name& torrentFilePtr,
+              const Name& commonPrefix,
+              const std::vector<ndn::Name>& catalog);
+
+  /**
+   * @brief Create a new .TorrentFile.
+   * @param torrentFileName The name of the .torrent-file
+   * @param commonPrefix The common name prefix of the manifest file names included in the catalog
+   * @param catalog The catalog containing the name of each file manifest
+   */
+  TorrentFile(const Name& torrentFileName,
+              const Name& commonPrefix,
+              const std::vector<ndn::Name>& catalog);
+
+  /**
+   * @brief Create a new .TorrentFile
+   * @param block The block format of the .torrent-file
+   */
+  explicit
+  TorrentFile(const Block& block);
+
+  /**
+   * @brief Get the name of the .TorrentFile
+   */
+  const Name&
+  getName() const;
+
+  /**
+   * @brief Get the common prefix of the file manifest names of this .torrent-file
+   */
+  const Name&
+  getCommonPrefix() const;
+
+  /**
+   * @brief Get a shared pointer to the name of the next segment of the .torrent-file.
+   *
+   * If there is no next segment, it returns a nullptr
+   */
+  shared_ptr<Name>
+  getTorrentFilePtr() const;
+
+  /**
+   * @brief Get the catalog of names of the file manifests
+   */
+  const std::vector<Name>&
+  getCatalog() const;
+
+  /**
+   * @brief Decode from wire format
+   */
+  void
+  wireDecode(const Block& wire);
+
+  /**
+   * @brief Encode from wire format
+   */
+  const Block&
+  wireEncode();
+
+  /**
+   * @brief Insert a name to the catalog of file manifest names
+   */
+  void
+  insert(const Name& name);
+
+  /**
+   * @brief Erase a name from the catalog of file manifest names
+   */
+  bool
+  erase(const Name& name);
+
+  /**
+   * @brief Get the size of the catalog of file manifest names
+   */
+  size_t
+  catalogSize() const;
+
+protected:
+  /**
+   * @brief prepend .torrent file as a Content block to the encoder
+   */
+  template<encoding::Tag TAG>
+  size_t
+  encodeContent(EncodingImpl<TAG>& encoder) const;
+
+  void
+  encodeContent();
+
+  void
+  decodeContent();
+
+private:
+  /**
+   * @brief Check whether the .torrent-file has a pointer to the next segment
+   */
+  bool
+  hasTorrentFilePtr() const;
+
+  /**
+   * @brief Create a catalog of suffixes for the names of the file manifests
+   *
+   * To optimize encoding and decoding, we encode the name of the file manifests
+   * as suffixes along with their common prefix.
+   *
+   */
+  void
+  createSuffixCatalog();
+
+  /**
+   * @brief Construct the catalog of long names from a catalog of suffixes for the file
+   *        manifests' name
+   *
+   * After decoding a .torrent-file from its wire format, we construct the catalog of
+   * long names from the decoded common prefix and suffixes
+   *
+   */
+  void
+  constructLongNames();
+
+  /**
+   * @brief Insert a suffix to the suffix catalog
+   */
+  void
+  insertToSuffixCatalog(const PartialName& suffix);
+
+private:
+  Name m_commonPrefix;
+  Name m_torrentFilePtr;
+  std::vector<ndn::Name> m_suffixCatalog;
+  std::vector<ndn::Name> m_catalog;
+};
+
+inline bool
+TorrentFile::hasTorrentFilePtr() const
+{
+  return !m_torrentFilePtr.empty();
+}
+
+inline const std::vector<Name>&
+TorrentFile::getCatalog() const
+{
+  return m_catalog;
+}
+
+inline size_t
+TorrentFile::catalogSize() const
+{
+  return m_catalog.size();
+}
+
+inline void
+TorrentFile::insert(const Name& name)
+{
+  m_catalog.push_back(name);
+}
+
+inline void
+TorrentFile::insertToSuffixCatalog(const PartialName& suffix)
+{
+  m_suffixCatalog.push_back(suffix);
+}
+
+} // namespace ntorrent
+
+} // namespace ndn
+
+#endif // TORRENT_FILE_HPP
diff --git a/tests/unit-tests/dummy.t.cpp b/tests/unit-tests/dummy.t.cpp
index de6d256..b58cb15 100644
--- a/tests/unit-tests/dummy.t.cpp
+++ b/tests/unit-tests/dummy.t.cpp
@@ -21,7 +21,10 @@
 
 #include "boost-test.hpp"
 
-namespace nTorrent {
+namespace ndn {
+
+namespace ntorrent {
+
 namespace tests {
 
 BOOST_AUTO_TEST_SUITE(DummyTest)
@@ -34,4 +37,7 @@
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
-} // namespace nTorrent
+
+} // namespace ntorrent
+
+} // namespace ndn
diff --git a/tests/unit-tests/torrent-file.t.cpp b/tests/unit-tests/torrent-file.t.cpp
new file mode 100644
index 0000000..90b9b6d
--- /dev/null
+++ b/tests/unit-tests/torrent-file.t.cpp
@@ -0,0 +1,290 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "boost-test.hpp"
+
+#include "torrent-file.hpp"
+
+#include <ndn-cxx/data.hpp>
+#include <ndn-cxx/signature.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+
+namespace ndn {
+
+namespace ntorrent {
+
+namespace tests {
+
+
+const static uint8_t TorrentFileTest[] = {
+  0x6, 0xfd,
+  0x1, 0xe4,
+     0x7, 0x25,
+        0x8, 0x8,
+           0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
+        0x8, 0xa,
+           0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
+        0x8, 0xd,
+          0x2e, 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65,
+     0x14, 0x0,
+     0x15, 0x77,
+        0x7, 0x37,
+           0x8, 0x8,
+              0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
+           0x8, 0xa,
+              0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
+           0x8, 0xd,
+              0x2e, 0x74, 0x6f, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x69, 0x6c, 0x65,
+           0x8, 0x8,
+              0x73, 0x65, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x32,
+           0x8, 0x6,
+              0x41, 0x45, 0x33, 0x32, 0x31, 0x43,
+        0x7, 0x16,
+           0x8, 0x8,
+              0x4e, 0x54, 0x4f, 0x52, 0x52, 0x45, 0x4e, 0x54,
+           0x8, 0xa,
+              0x6c, 0x69, 0x6e, 0x75, 0x78, 0x31, 0x35, 0x2e, 0x30, 0x31,
+        0x7, 0x11,
+           0x8, 0x5,
+              0x66, 0x69, 0x6c, 0x65, 0x30,
+           0x8, 0x8,
+              0x31, 0x41, 0x32, 0x42, 0x33, 0x43, 0x34, 0x44,
+        0x7, 0x11,
+           0x8, 0x5,
+              0x66, 0x69, 0x6c, 0x65, 0x31,
+           0x8, 0x8,
+              0x32, 0x41, 0x33, 0x42, 0x34, 0x43, 0x35, 0x45,
+     0x16, 0x3c,
+        0x1b, 0x1,
+           0x1,
+        0x1c, 0x37,
+            0x7, 0x35,
+               0x8, 0xc,
+                  0x74, 0x6d, 0x70, 0x2d, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79,
+               0x8, 0x4,
+                  0x3d, 0xa8, 0x2c, 0x19,
+               0x8, 0x3,
+                  0x4b, 0x45, 0x59,
+               0x8, 0x11,
+                  0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x35, 0x34, 0x39, 0x37, 0x33, 0x38, 0x37, 0x36, 0x31, 0x37, 0x31,
+               0x8, 0x7,
+                  0x49, 0x44, 0x2d, 0x43, 0x45, 0x52, 0x54,
+      0x17, 0xfd,
+         0x1, 0x0, 0x37, 0x61, 0xc9, 0x3b, 0x43, 0xe8, 0xa3, 0xcd, 0xf0, 0xb1,
+         0x14, 0x78, 0x50, 0x6, 0x3c, 0x8a, 0x7, 0x4d, 0xe4, 0xa8, 0xbe, 0xc1,
+         0x8c, 0x40, 0xaf, 0xaa, 0x21, 0x9f, 0x58, 0xed, 0xc0, 0x99, 0x92, 0xb8,
+         0xb2, 0xb8, 0xac, 0x4, 0x3f, 0xa2, 0x25, 0xd7, 0x68, 0xa9, 0x5e, 0xc6,
+         0xf5, 0x20, 0x69, 0xe3, 0xf, 0x37, 0xfc, 0xc7, 0x3f, 0x72, 0x8, 0x74,
+         0x8, 0x5e, 0x10, 0x4d, 0xef, 0xa3, 0x58, 0x59, 0xb6, 0x90, 0x53, 0x1c,
+         0x4b, 0x22, 0xb0, 0x3e, 0x87, 0x95, 0x5b, 0xa6, 0xd, 0x84, 0x1f, 0x60,
+         0xda, 0xe, 0x22, 0x29, 0xda, 0x38, 0x1c, 0x90, 0x4a, 0x2a, 0x60, 0xe4,
+         0xc9, 0x36, 0xb6, 0x4f, 0x9a, 0x17, 0x69, 0x99, 0xf4, 0x84, 0xa5, 0x5b,
+         0xec, 0x70, 0x34, 0x1b, 0xbc, 0x14, 0xee, 0xb1, 0xa7, 0x42, 0xb7, 0xd8,
+         0x9, 0x45, 0xe0, 0x2d, 0x4, 0x8f, 0x66, 0xdb, 0x43, 0x68, 0x8e, 0x53,
+         0x94, 0x1e, 0xcd, 0xaa, 0x47, 0x80, 0xaf, 0xd0, 0x1d, 0x6b, 0x21, 0x1,
+         0x5a, 0x36, 0x97, 0x87, 0x44, 0xc9, 0x31, 0x57, 0xcd, 0x5d, 0xc1, 0xa5,
+         0xfc, 0x35, 0xb1, 0x60, 0x29, 0xc6, 0x82, 0xe9, 0x52, 0x33, 0xac, 0xf9,
+         0xb, 0xca, 0xbe, 0x62, 0x8b, 0x88, 0x49, 0x9a, 0x6f, 0xbd, 0xed, 0x1e,
+         0x8c, 0x4c, 0x63, 0xfb, 0xd9, 0x20, 0xcb, 0x1c, 0x66, 0xa4, 0xb9, 0xc,
+         0x21, 0x61, 0xc4, 0x56, 0x4c, 0xa8, 0x58, 0x4b, 0x7a, 0x81, 0x2c, 0xf0,
+         0x2f, 0xce, 0x7d, 0x1d, 0xbf, 0x16, 0xc, 0x74, 0x35, 0x83, 0x8a, 0x22,
+         0xc4, 0x2, 0xeb, 0x42, 0xd3, 0x76, 0x36, 0x8a, 0x71, 0x7b, 0x6b, 0xb8,
+         0x3f, 0x7a, 0xb8, 0xc6, 0xf6, 0xca, 0xed, 0x74, 0xdb, 0xa4, 0x42, 0x65,
+         0xa5, 0x96, 0xde, 0xd9, 0xfa, 0x45, 0x25, 0x9a, 0xc1, 0xde, 0xd1, 0xf6,
+         0x7a, 0xaf, 0xa3, 0xfd, 0x4e, 0xa9
+};
+
+BOOST_AUTO_TEST_SUITE(TestTorrentFile)
+
+BOOST_AUTO_TEST_CASE(CheckGettersSetters)
+{
+
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                   "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
+                   "/NTORRENT/linux15.01",
+                   {"/NTORRENT/linux15.01/file0/1A2B3C4D",
+                   "/NTORRENT/linux15.01/file1/2A3B4C5E"});
+
+  BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/.torrent-file");
+  BOOST_CHECK_EQUAL(file.getCatalog().size(), 2);
+  BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
+  BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+
+  TorrentFile file2("/NTORRENT/linux15.01/.torrent-file",
+                    "/NTORRENT/linux15.01",
+                    {"/NTORRENT/linux15.01/file0/1A2B3C4D"});
+
+  BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
+  BOOST_CHECK(!file2.getTorrentFilePtr());
+  BOOST_CHECK_EQUAL(file2.getCatalog().size(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(CheckEncodeDecode)
+{
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                   "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
+                   "/NTORRENT/linux15.01",
+                   {"/NTORRENT/linux15.01/file0/1A2B3C4D",
+                   "/NTORRENT/linux15.01/file1/2A3B4C5E"});
+
+  KeyChain keyChain;
+  keyChain.sign(file);
+
+  Block wire = file.wireEncode();
+
+  TorrentFile file2;
+  file2.wireDecode(wire);
+
+  BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
+  BOOST_CHECK_EQUAL(*(file2.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
+  BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
+  BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+  BOOST_CHECK_EQUAL(file2.getCommonPrefix(), "/NTORRENT/linux15.01");
+}
+
+BOOST_AUTO_TEST_CASE(CheckEncodeDecodeNoTorrentFilePtr)
+{
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                   "/NTORRENT/linux15.01",
+                   {"/NTORRENT/linux15.01/file0/1A2B3C4D",
+                   "/NTORRENT/linux15.01/file1/2A3B4C5E"});
+
+  KeyChain keyChain;
+  keyChain.sign(file);
+
+  Block wire = file.wireEncode();
+
+  TorrentFile file2;
+  file2.wireDecode(wire);
+
+  BOOST_CHECK_EQUAL(file2.getName(), "/NTORRENT/linux15.01/.torrent-file");
+  BOOST_CHECK(!file2.getTorrentFilePtr());
+  BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
+  BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+}
+
+BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyTorrentFile)
+{
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                  "",
+                  {});
+
+  KeyChain keyChain;
+  keyChain.sign(file);
+
+  Block wire = file.wireEncode();
+
+  TorrentFile file2;
+
+  BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
+}
+
+
+BOOST_AUTO_TEST_CASE(CheckEncodeDecodeEmptyCatalog)
+{
+
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                  "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
+                  "/NTORRENT/linux15.01",
+                  {});
+
+  KeyChain keyChain;
+  keyChain.sign(file);
+
+  Block wire = file.wireEncode();
+
+  TorrentFile file2;
+
+  BOOST_REQUIRE_THROW(file2.wireDecode(wire), TorrentFile::Error);
+}
+
+BOOST_AUTO_TEST_CASE(DecodeFromWire)
+{
+
+  Block TorrentFileBlock(TorrentFileTest, sizeof(TorrentFileTest));
+  TorrentFile file(TorrentFileBlock);
+
+  BOOST_CHECK_EQUAL(file.getName(), "/NTORRENT/linux15.01/.torrent-file");
+  BOOST_CHECK_EQUAL(*(file.getTorrentFilePtr()), "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C");
+  BOOST_CHECK_EQUAL(file.getCatalog().size(), 2);
+  BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+  BOOST_CHECK_EQUAL(file.getCommonPrefix(), "/NTORRENT/linux15.01");
+}
+
+BOOST_AUTO_TEST_CASE(TestInsertErase)
+{
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                   "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
+                   "/NTORRENT/linux15.01",
+                   {"/NTORRENT/linux15.01/file0/1A2B3C4D",
+                   "/NTORRENT/linux15.01/file1/2A3B4C5E"});
+
+  file.erase("/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+
+  file.erase("/NTORRENT/linux15.01/file1/2A3B4C5E");
+  BOOST_CHECK_EQUAL(file.getCatalog().size(), 0);
+
+  file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
+  BOOST_CHECK_EQUAL(file.getCatalog().size(), 1);
+  BOOST_CHECK_EQUAL(file.getCatalog()[0], "/NTORRENT/linux15.01/file3/AB34C5KA");
+}
+
+BOOST_AUTO_TEST_CASE(TestInsertAndEncodeTwice)
+{
+  TorrentFile file("/NTORRENT/linux15.01/.torrent-file",
+                   "/NTORRENT/linux15.01/.torrent-file/segment2/AE321C",
+                   "/NTORRENT/linux15.01",
+                   {"/NTORRENT/linux15.01/file0/1A2B3C4D",
+                   "/NTORRENT/linux15.01/file1/2A3B4C5E"});
+
+  KeyChain keyChain;
+  keyChain.sign(file);
+  Block wire = file.wireEncode();
+
+  TorrentFile file2;
+  file2.wireDecode(wire);
+  BOOST_CHECK_EQUAL(file2.getCatalog().size(), 2);
+
+  file.insert("/NTORRENT/linux15.01/file3/AB34C5KA");
+  file.insert("/NTORRENT/linux15.01/file4/CB24C3GB");
+  Block wire2 = file.wireEncode();
+
+  file2.wireDecode(wire2);
+  BOOST_CHECK_EQUAL(file2.getCatalog().size(), 4);
+
+  BOOST_CHECK_EQUAL(file2.getCatalog()[0], "/NTORRENT/linux15.01/file0/1A2B3C4D");
+  BOOST_CHECK_EQUAL(file2.getCatalog()[1], "/NTORRENT/linux15.01/file1/2A3B4C5E");
+  BOOST_CHECK_EQUAL(file2.getCatalog()[2], "/NTORRENT/linux15.01/file3/AB34C5KA");
+  BOOST_CHECK_EQUAL(file2.getCatalog()[3], "/NTORRENT/linux15.01/file4/CB24C3GB");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+
+} // namespace ntorrent
+
+} // namespace ndn