Add credential based challenge using existing cert to request new cert

Change-Id: Iee18c64f55b42e73a3c1c201726d546096dcfef8
Refs: #4052
diff --git a/tests/unit-tests/challenge-credential.conf.test b/tests/unit-tests/challenge-credential.conf.test
new file mode 100644
index 0000000..1efa2f5
--- /dev/null
+++ b/tests/unit-tests/challenge-credential.conf.test
@@ -0,0 +1,8 @@
+{
+  "anchor-list":
+  [
+    {
+        "certificate": "Bv0CJAcsCANuZG4IBXNpdGUxCANLRVkICBG8IvRjFf8XCARzZWxmCAn9AAABWcgU2aUUCRgBAhkEADbugBX9AU8wggFLMIIBAwYHKoZIzj0CATCB9wIBATAsBgcqhkjOPQEBAiEA/////wAAAAEAAAAAAAAAAAAAAAD///////////////8wWwQg/////wAAAAEAAAAAAAAAAAAAAAD///////////////wEIFrGNdiqOpPns+u9VXaYhrxlHQawzFOw9jvOPD4n0mBLAxUAxJ02CIbnBJNqZnjhE50mt4GffpAEQQRrF9Hy4SxCR/i85uVjpEDydwN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQEDQgAES9Cb9iANUNYmwt5bjwNW1mZgjzIkDJb6FTCdiYWnkMMIVxh2YDllphoWDEAPS6kqJczzCuhnGYpZCp9tTaYKGxZMGwEDHB0HGwgDbmRuCAVzaXRlMQgDS0VZCAgRvCL0YxX/F/0A/Sb9AP4PMTk3MDAxMDFUMDAwMDAw/QD/DzIwMzcwMTE3VDIxMjg0NhdIMEYCIQDXkR1hF3GiP7yLXq+0JBJfi9QC+hhAu/1Bykx+MWz6RAIhANwelBTxxZr2C5bD15mjfhWudK4I1tOb4b/9xWCHyM7F"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/tests/unit-tests/challenge-credential.t.cpp b/tests/unit-tests/challenge-credential.t.cpp
new file mode 100644
index 0000000..8a0cfe5
--- /dev/null
+++ b/tests/unit-tests/challenge-credential.t.cpp
@@ -0,0 +1,99 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert 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 General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+#include "challenge-module/challenge-credential.hpp"
+#include <ndn-cxx/security/signing-helpers.hpp>
+#include <ndn-cxx/util/io.hpp>
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementV2Fixture)
+
+BOOST_AUTO_TEST_CASE(LoadConfig)
+{
+  ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
+  BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "CREDENTIAL");
+
+  BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
+  auto cert = challenge.m_trustAnchors.front();
+  BOOST_CHECK_EQUAL(cert.getName(),
+                    "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
+}
+
+BOOST_AUTO_TEST_CASE(HandleSelect)
+{
+  // create trust anchor
+  ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
+  auto identity0 = addIdentity(Name("/trust"));
+  auto key0 = identity0.getDefaultKey();
+  auto trustAnchor = key0.getDefaultCertificate();
+  challenge.m_trustAnchors.front() = trustAnchor;
+
+  // create certificate request
+  auto identity = addIdentity(Name("/example"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+  CertificateRequest request(Name("/example"), "123", cert);
+
+  // using trust anchor to sign cert request to get credential
+  Name credentialName = cert.getKeyName();
+  credentialName.append("Credential").appendVersion();
+  security::v2::Certificate credential = cert;
+  credential.setName(credentialName);
+  credential.setContent(cert.getContent());
+  m_keyChain.sign(credential, signingByCertificate(trustAnchor));
+
+  // generate SELECT interest
+  std::stringstream ss;
+  io::save<security::v2::Certificate>(credential, ss);
+  auto checkCert = *(io::load<security::v2::Certificate>(ss));
+  BOOST_CHECK_EQUAL(checkCert, credential);
+  ss.str("");
+  ss.clear();
+
+  io::save<security::v2::Certificate>(credential, ss);
+  std::list<std::string> paramList;
+  std::string jsonString = ss.str();
+  paramList.push_back(jsonString);
+  JsonSection credentialJson = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, paramList);
+  BOOST_CHECK_EQUAL(credentialJson.get<std::string>(ChallengeCredential::JSON_CREDENTIAL), jsonString);
+  ss.str("");
+  ss.clear();
+
+  boost::property_tree::write_json(ss, credentialJson);
+  Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
+
+  Name interestName("/example/CA");
+  interestName.append("_SELECT").append("Fake-Request-ID").append("CREDENTIAL").append(jsonContent);
+  Interest interest(interestName);
+
+  challenge.processSelectInterest(interest, request);
+  BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
+  BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
diff --git a/tests/unit-tests/challenge-email.t.cpp b/tests/unit-tests/challenge-email.t.cpp
index 00106ea..c53e47e 100644
--- a/tests/unit-tests/challenge-email.t.cpp
+++ b/tests/unit-tests/challenge-email.t.cpp
@@ -65,7 +65,6 @@
   emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs.ucla.edu");
   std::stringstream ss;
   boost::property_tree::write_json(ss, emailJson);
-  std::string jsonString = ss.str();
   Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
 
   Name interestName("/ndn/site1/CA");
@@ -90,7 +89,6 @@
   emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs");
   std::stringstream ss;
   boost::property_tree::write_json(ss, emailJson);
-  std::string jsonString = ss.str();
   Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
 
   Name interestName("/ndn/site1/CA");
@@ -125,7 +123,6 @@
   infoJson.put(ChallengeEmail::JSON_CODE, "4567");
   std::stringstream ss;
   boost::property_tree::write_json(ss, infoJson);
-  std::string jsonString = ss.str();
   Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
 
   Name interestName("/ndn/site1/CA");
@@ -160,7 +157,6 @@
   infoJson.put(ChallengeEmail::JSON_CODE, "1234");
   std::stringstream ss;
   boost::property_tree::write_json(ss, infoJson);
-  std::string jsonString = ss.str();
   Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
 
   Name interestName("/ndn/site1/CA");