bug+codestyle: Fix compile bugs and adjust code style

Change-Id: I008bb538441c099fa25b8b967fbf23ffce13a220
diff --git a/test/test-endorse-certificate.cpp b/test/test-endorse-certificate.cpp
new file mode 100644
index 0000000..09eb935
--- /dev/null
+++ b/test/test-endorse-certificate.cpp
@@ -0,0 +1,217 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#if __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wtautological-compare"
+#pragma clang diagnostic ignored "-Wunused-function"
+#elif __GNUC__
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
+
+#include <boost/test/unit_test.hpp>
+
+#include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/encoding/buffer-stream.hpp>
+#include <ndn-cxx/util/time.hpp>
+#include <ndn-cxx/util/io.hpp>
+#include "cryptopp.hpp"
+#include "endorse-certificate.hpp"
+
+namespace chronos {
+
+using std::vector;
+using std::string;
+
+using ndn::KeyChain;
+using ndn::IdentityCertificate;
+
+BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
+
+const string testIdCert("\
+Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\
+ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\
+MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDzIwMTQwMzEzMDkyNzQ3WjBuMA0GA1UE\
+KRMGTXlOYW1lMBIGA1UECxMLTXlJbnN0aXR1dGUwDgYDVQQBEwdNeUdyb3VwMBEG\
+A1UEAxMKTXlIb21lUGFnZTAQBgNVBFATCU15QWR2aXNvcjAUBgkqhkiG9w0BCQET\
+B015RW1haWwwggEgMA0GCSqGSIb3DQEBAQUAA4I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RzL0VuY29kZURl\
+Y29kZQoUCgdhZHZpc29yEglNeUFkdmlzb3IKEAoFZW1haWwSB015RW1haWwKEAoF\
+Z3JvdXASB015R3JvdXAKFgoIaG9tZXBhZ2USCk15SG9tZVBhZ2UKGgoLaW5zdGl0\
+dXRpb24SC015SW5zdGl0dXRlCg4KBG5hbWUSBk15TmFtZTAjBgcrBgEFIAICAQH/\
+BBUKBwoFZW1haWwKCgoIaG9tZXBhZ2UWAxsBABcgS7pYcBk1e4dlsag8minK+UzI\
+L8ViVS87k09gaM6GeUA=");
+
+BOOST_AUTO_TEST_CASE(IdCert)
+{
+  boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
+                                                              testIdCert.size());
+  shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is);
+
+  BOOST_CHECK(static_cast<bool>(idCert));
+
+  const ndn::Certificate::SubjectDescriptionList& subjectDescription =
+    idCert->getSubjectDescriptionList();
+  BOOST_CHECK_EQUAL(subjectDescription.size(), 6);
+
+  ndn::Certificate::SubjectDescriptionList::const_iterator it  = subjectDescription.begin();
+  ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end();
+  int count = 0;
+  for(; it != end; it++)
+    {
+      if(it->getOidString() == "2.5.4.41")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyName");
+          count++;
+        }
+      if(it->getOidString() == "2.5.4.11")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute");
+          count++;
+        }
+      if(it->getOidString() == "2.5.4.1")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyGroup");
+          count++;
+        }
+      if(it->getOidString() == "2.5.4.3")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage");
+          count++;
+        }
+      if(it->getOidString() == "2.5.4.80")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor");
+          count++;
+        }
+      if(it->getOidString() == "1.2.840.113549.1.9.1")
+        {
+          BOOST_CHECK_EQUAL(it->getValue(), "MyEmail");
+          count++;
+        }
+    }
+  BOOST_CHECK_EQUAL(count, 6);
+
+  BOOST_CHECK_EQUAL(idCert->getName().toUri(),
+    "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8");
+
+  ndn::OBufferStream keyOs;
+  {
+    using namespace CryptoPP;
+    StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs)));
+  }
+  ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size());
+  BOOST_CHECK(key == idCert->getPublicKeyInfo());
+}
+
+BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
+{
+  boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
+                                                              testIdCert.size());
+  shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is);
+
+  Profile profile(*idCert);
+  vector<string> endorseList;
+  endorseList.push_back("email");
+  endorseList.push_back("homepage");
+  EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
+
+  KeyChain keyChain("sqlite3", "file");
+  keyChain.signWithSha256(endorseCertificate);
+  const Block& endorseDataBlock = endorseCertificate.wireEncode();
+
+  Data decodedEndorseData;
+  decodedEndorseData.wireDecode(endorseDataBlock);
+  EndorseCertificate decodedEndorse(decodedEndorseData);
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
+                    "/EndorseCertificateTests/EncodeDecode");
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2);
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email");
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage");
+  BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
+                    "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
+  BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
+                    "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
+}
+
+BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
+{
+  boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(),
+                                                              testEndorseCert.size());
+  shared_ptr<Data> rawData = ndn::io::load<Data>(is);
+
+  EndorseCertificate rawEndorse(*rawData);
+  vector<string> endorseList;
+  endorseList.push_back("institution");
+  endorseList.push_back("group");
+  endorseList.push_back("advisor");
+  Name signer("/EndorseCertificateTests/Singer/ksk-1234567890");
+  EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList);
+
+  KeyChain keyChain("sqlite3", "file");
+  keyChain.signWithSha256(endorseCertificate);
+
+  const Block& endorseDataBlock = endorseCertificate.wireEncode();
+
+  Data decodedEndorseData;
+  decodedEndorseData.wireDecode(endorseDataBlock);
+  EndorseCertificate decodedEndorse(decodedEndorseData);
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
+                    "/EndorseCertificateTests/EncodeDecode");
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
+  BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3);
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution");
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group");
+  BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor");
+  BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
+                    "/EndorseCertificateTests/Singer/ksk-1234567890");
+  BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
+                    "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace chronos