signature-info: Fix encoding of type-specific TLV

This commit also introduces operator<< for SignatureInfo

Change-Id: Iddf5b77d9dab9f7b1764500726e6d5b1e13aedbb
Refs: #3914
diff --git a/src/signature-info.cpp b/src/signature-info.cpp
index 821d5a7..3d789dc 100644
--- a/src/signature-info.cpp
+++ b/src/signature-info.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -145,7 +145,7 @@
 
   for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin();
        i != m_otherTlvs.rend(); i++) {
-    totalLength += encoder.appendBlock(*i);
+    totalLength += encoder.prependBlock(*i);
   }
 
   if (m_hasKeyLocator)
@@ -230,4 +230,21 @@
           m_otherTlvs == rhs.m_otherTlvs);
 }
 
+std::ostream&
+operator<<(std::ostream& os, const SignatureInfo& info)
+{
+  os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType());
+  if (info.hasKeyLocator()) {
+    os << " " << info.getKeyLocator();
+  }
+  if (!info.m_otherTlvs.empty()) {
+    os << " { ";
+    for (const auto& block : info.m_otherTlvs) {
+      os << block.type() << " ";
+    }
+    os << "}";
+  }
+  return os;
+}
+
 } // namespace ndn
diff --git a/src/signature-info.hpp b/src/signature-info.hpp
index 4ee8e64..fc99542 100644
--- a/src/signature-info.hpp
+++ b/src/signature-info.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -145,8 +145,14 @@
   std::list<Block> m_otherTlvs;
 
   mutable Block m_wire;
+
+  friend std::ostream&
+  operator<<(std::ostream& os, const SignatureInfo& info);
 };
 
+std::ostream&
+operator<<(std::ostream& os, const SignatureInfo& info);
+
 } // namespace ndn
 
 #endif // NDN_SIGNATURE_INFO_HPP
diff --git a/tests/unit-tests/signature-info.t.cpp b/tests/unit-tests/signature-info.t.cpp
index 1eaa665..1945841 100644
--- a/tests/unit-tests/signature-info.t.cpp
+++ b/tests/unit-tests/signature-info.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -22,6 +22,7 @@
 #include "signature-info.hpp"
 
 #include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
 
 namespace ndn {
 namespace tests {
@@ -54,6 +55,8 @@
   BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
   BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
 
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256");
+
   KeyLocator keyLocator("/test/key/locator");
   SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
   BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
@@ -61,6 +64,8 @@
   BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
   BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
 
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo), "SignatureSha256WithRsa Name=/test/key/locator");
+
   const Block& encoded = sha256RsaInfo.wireEncode();
   Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
 
@@ -224,6 +229,36 @@
   BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
 }
 
+BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914
+{
+  SignatureInfo info1;
+  info1.appendTypeSpecificTlv(makeStringBlock(101, "First"));
+  info1.appendTypeSpecificTlv(makeStringBlock(102, "Second"));
+  info1.appendTypeSpecificTlv(makeStringBlock(103, "Third"));
+
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "Unknown Signature Type { 101 102 103 }");
+
+  SignatureInfo info2;
+  info2.wireDecode(info1.wireEncode());
+  BOOST_CHECK_EQUAL(info1, info2);
+
+  // // These octets are obtained by the snippet below.
+  // // This check is intended to detect unexpected encoding change in the future.
+  // for (uint8_t ch : info1.wireEncode()) {
+  //  printf("0x%02x, ", ch);
+  // }
+  const uint8_t infoBytes[] = {
+    0x16, 0x20, 0x1b, 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x65, 0x05, 0x46, 0x69,
+    0x72, 0x73, 0x74, 0x66, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x67, 0x05, 0x54, 0x68, 0x69,
+    0x72, 0x64
+  };
+
+  SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)));
+  BOOST_CHECK_EQUAL(info3, info1);
+  BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
+                                info1.wireEncode().begin(), info1.wireEncode().end());
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
 
 } // namespace tests