security: Update Signature abstraction to match 0.1.1 spec

This commit removes intermediate SignatureWithPublicKey data
structure.  All uses of such data structure should be replaced with just
Signature and check that KeyLocator is present (hasKeyLocator()).

Change-Id: I1157e05c6a717ea28df91c4bfd76c7145b520a15
Refs: #1750
diff --git a/tests/unit-tests/security/test-digest-sha256.cpp b/tests/unit-tests/security/test-digest-sha256.cpp
index 94ded5b..264078c 100644
--- a/tests/unit-tests/security/test-digest-sha256.cpp
+++ b/tests/unit-tests/security/test-digest-sha256.cpp
@@ -26,26 +26,25 @@
 
 #include "boost-test.hpp"
 
-using namespace std;
 namespace ndn {
 
 BOOST_AUTO_TEST_SUITE(SecurityTestDigestSha256)
 
-string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
+std::string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
 
-BOOST_AUTO_TEST_CASE (Sha256)
+BOOST_AUTO_TEST_CASE(Sha256)
 {
   using namespace CryptoPP;
 
   char content[6] = "1234\n";
   ConstBufferPtr buf = crypto::sha256(reinterpret_cast<uint8_t*>(content), 5);
-  string result;
+  std::string result;
   StringSource(buf->buf(), buf->size(), true, new HexEncoder(new StringSink(result), false));
 
-  BOOST_REQUIRE_EQUAL(SHA256_RESULT, result);
+  BOOST_CHECK_EQUAL(SHA256_RESULT, result);
 }
 
-BOOST_AUTO_TEST_CASE (Signature)
+BOOST_AUTO_TEST_CASE(Signature)
 {
   using namespace CryptoPP;
 
@@ -63,7 +62,9 @@
 
   DigestSha256 sig(testData.getSignature());
 
-  BOOST_REQUIRE(Validator::verifySignature(testData, sig));
+  BOOST_CHECK(Validator::verifySignature(testData, sig));
+
+  BOOST_CHECK_THROW(sig.getKeyLocator(), ndn::SignatureInfo::Error);
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit-tests/security/test-signature-sha256-ecdsa.cpp b/tests/unit-tests/security/test-signature-sha256-ecdsa.cpp
index bcf01be..34f25bf 100644
--- a/tests/unit-tests/security/test-signature-sha256-ecdsa.cpp
+++ b/tests/unit-tests/security/test-signature-sha256-ecdsa.cpp
@@ -57,17 +57,18 @@
   Block sigValueBlock(sigValue, sizeof(sigValue));
 
   Signature sig(sigInfoBlock, sigValueBlock);
-  BOOST_REQUIRE_NO_THROW(SignatureSha256WithEcdsa(sig));
+  BOOST_CHECK_NO_THROW(SignatureSha256WithEcdsa(sig));
+  BOOST_CHECK_NO_THROW(sig.getKeyLocator());
 }
 
 BOOST_AUTO_TEST_CASE(Encoding)
 {
-  SignatureSha256WithEcdsa sig;
-
   Name name("/test/key/locator");
   KeyLocator keyLocator(name);
 
-  sig.setKeyLocator(keyLocator);
+  SignatureSha256WithEcdsa sig(keyLocator);
+
+  BOOST_CHECK_NO_THROW(sig.getKeyLocator());
 
   const Block& encodeSigInfoBlock = sig.getInfo();
 
diff --git a/tests/unit-tests/security/test-signature-sha256-rsa.cpp b/tests/unit-tests/security/test-signature-sha256-rsa.cpp
index e1ea73c..cd717d7 100644
--- a/tests/unit-tests/security/test-signature-sha256-rsa.cpp
+++ b/tests/unit-tests/security/test-signature-sha256-rsa.cpp
@@ -62,17 +62,18 @@
   Block sigValueBlock(sigValue, sizeof(sigValue));
 
   Signature sig(sigInfoBlock, sigValueBlock);
-  BOOST_REQUIRE_NO_THROW(SignatureSha256WithRsa(sig));
+  BOOST_CHECK_NO_THROW(SignatureSha256WithRsa(sig));
+  BOOST_CHECK_NO_THROW(sig.getKeyLocator());
 }
 
 BOOST_AUTO_TEST_CASE(Encoding)
 {
-  SignatureSha256WithRsa sig;
-
   Name name("/test/key/locator");
   KeyLocator keyLocator(name);
 
-  sig.setKeyLocator(keyLocator);
+  SignatureSha256WithRsa sig(keyLocator);
+
+  BOOST_CHECK_NO_THROW(sig.getKeyLocator());
 
   const Block& encodeSigInfoBlock = sig.getInfo();
 
diff --git a/tests/unit-tests/test-interest.cpp b/tests/unit-tests/test-interest.cpp
index 0b397cf..2f465ae 100644
--- a/tests/unit-tests/test-interest.cpp
+++ b/tests/unit-tests/test-interest.cpp
@@ -466,8 +466,7 @@
           .setExclude(Exclude().excludeBefore(name::Component("C")));
 
   Data data("ndn:/A/D");
-  SignatureSha256WithRsa signature;
-  signature.setKeyLocator(KeyLocator("ndn:/B"));
+  SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
   data.setSignature(signature);
   data.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data), true);
@@ -491,8 +490,7 @@
   interest.setMaxSuffixComponents(2);
 
   Data data3 = data;
-  SignatureSha256WithRsa signature3;
-  signature3.setKeyLocator(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
+  SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
   data3.setSignature(signature3);
   data3.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
diff --git a/tests/unit-tests/test-signature-info.cpp b/tests/unit-tests/test-signature-info.cpp
new file mode 100644
index 0000000..605b325
--- /dev/null
+++ b/tests/unit-tests/test-signature-info.cpp
@@ -0,0 +1,174 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library 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.
+ *
+ * ndn-cxx library 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 ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "signature-info.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
+
+const uint8_t sigInfoRsa[] = {
+0x16, 0x1b, // SignatureInfo
+  0x1b, 0x01, // SignatureType
+    0x01, // Sha256WithRsa
+  0x1c, 0x16, // KeyLocator
+    0x07, 0x14, // Name
+      0x08, 0x04,
+        0x74, 0x65, 0x73, 0x74,
+      0x08, 0x03,
+        0x6b, 0x65, 0x79,
+      0x08, 0x07,
+        0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
+};
+
+BOOST_AUTO_TEST_CASE(Constructor)
+{
+  SignatureInfo info;
+  BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
+  BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
+  BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
+
+  SignatureInfo sha256Info(Tlv::DigestSha256);
+  BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), Tlv::DigestSha256);
+  BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
+  BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
+
+  KeyLocator keyLocator("/test/key/locator");
+  SignatureInfo sha256RsaInfo(Tlv::SignatureSha256WithRsa, keyLocator);
+  BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), Tlv::SignatureSha256WithRsa);
+  BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
+  BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
+  BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
+
+  const Block& encoded = sha256RsaInfo.wireEncode();
+  Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
+
+  BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
+                                sigInfoBlock.wire() + sigInfoBlock.size(),
+                                encoded.wire(),
+                                encoded.wire() + encoded.size());
+
+  sha256RsaInfo = SignatureInfo(sigInfoBlock);
+  BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), Tlv::SignatureSha256WithRsa);
+  BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
+  BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
+  BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
+}
+
+BOOST_AUTO_TEST_CASE(ConstructorError)
+{
+  const uint8_t error1[] = {
+    0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
+      0x1b, 0x01, // SignatureType
+        0x01, // Sha256WithRsa
+      0x1c, 0x16, // KeyLocator
+        0x07, 0x14, // Name
+          0x08, 0x04,
+            0x74, 0x65, 0x73, 0x74,
+          0x08, 0x03,
+            0x6b, 0x65, 0x79,
+          0x08, 0x07,
+            0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
+  };
+  Block errorBlock1(error1, sizeof(error1));
+  BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), Tlv::Error);
+
+  const uint8_t error2[] = {
+    0x16, 0x01, // SignatureInfo
+      0x01 // Wrong SignatureInfo value
+  };
+  Block errorBlock2(error2, sizeof(error2));
+  BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), Tlv::Error);
+
+  const uint8_t error3[] = {
+    0x16, 0x01, // SignatureInfo
+      0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
+        0x01, // Sha256WithRsa
+      0x1c, 0x16, // KeyLocator
+        0x07, 0x14, // Name
+          0x08, 0x04,
+            0x74, 0x65, 0x73, 0x74,
+          0x08, 0x03,
+            0x6b, 0x65, 0x79,
+          0x08, 0x07,
+            0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
+  };
+  Block errorBlock3(error3, sizeof(error3));
+  BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), Tlv::Error);
+
+  const uint8_t error4[] = {
+    0x16, 0x00 // Empty SignatureInfo
+  };
+  Block errorBlock4(error4, sizeof(error4));
+  BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), Tlv::Error);
+
+}
+
+BOOST_AUTO_TEST_CASE(SetterGetter)
+{
+  SignatureInfo info;
+  BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
+  BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
+  BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
+
+  info.setSignatureType(Tlv::SignatureSha256WithRsa);
+  BOOST_CHECK_EQUAL(info.getSignatureType(), Tlv::SignatureSha256WithRsa);
+  BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
+
+  KeyLocator keyLocator("/test/key/locator");
+  info.setKeyLocator(keyLocator);
+  BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
+  BOOST_CHECK_NO_THROW(info.getKeyLocator());
+  BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
+
+  const Block& encoded = info.wireEncode();
+  Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
+
+  BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
+                                sigInfoBlock.wire() + sigInfoBlock.size(),
+                                encoded.wire(),
+                                encoded.wire() + encoded.size());
+}
+
+BOOST_AUTO_TEST_CASE(OtherTlvs)
+{
+  SignatureInfo info;
+  BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
+  BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
+  BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
+
+  const uint8_t tlv1[] = {
+    0x81, // T
+    0x01, // L
+    0x01, // V
+  };
+  Block block1(tlv1, sizeof(tlv1));
+
+  info.appendTypeSpecificTlv(block1);
+  BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
+  BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn