Add email challenge module
Refs: #4053
Change-Id: Ic5093fa484df967a760d7503eab2436f5794a385
diff --git a/tests/unit-tests/challenge-email.t.cpp b/tests/unit-tests/challenge-email.t.cpp
new file mode 100644
index 0000000..00106ea
--- /dev/null
+++ b/tests/unit-tests/challenge-email.t.cpp
@@ -0,0 +1,209 @@
+/* -*- 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-email.hpp"
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementV2Fixture)
+
+BOOST_AUTO_TEST_CASE(TestChallengeType)
+{
+ ChallengeEmail challenge;
+ BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "EMAIL");
+}
+
+BOOST_AUTO_TEST_CASE(ParseStoredSecret)
+{
+ time::system_clock::TimePoint tp = time::fromIsoString("20170207T120000");
+ JsonSection json;
+ json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
+ json.put(ChallengeEmail::JSON_CODE, "1234");
+ json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
+
+ auto result = ChallengeEmail::parseStoredSecrets(json);
+ BOOST_CHECK_EQUAL(std::get<0>(result), tp);
+ BOOST_CHECK_EQUAL(std::get<1>(result), "1234");
+ BOOST_CHECK_EQUAL(std::get<2>(result), 3);
+}
+
+BOOST_AUTO_TEST_CASE(EmailAddressChecker)
+{
+ BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
+ BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
+ BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
+}
+
+BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithEmail)
+{
+ auto identity = addIdentity(Name("/ndn/site1"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
+ CertificateRequest request(Name("/ndn/site1"), "123", cert);
+
+ JsonSection emailJson;
+ 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");
+ interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
+ Interest interest(interestName);
+
+ ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
+ challenge.handleChallengeRequest(interest, request);
+
+ BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::NEED_CODE);
+ BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
+}
+
+BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithInvalidEmail)
+{
+ auto identity = addIdentity(Name("/ndn/site1"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
+ CertificateRequest request(Name("/ndn/site1"), "123", cert);
+
+ JsonSection emailJson;
+ 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");
+ interestName.append("_SELECT").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
+ Interest interest(interestName);
+
+ ChallengeEmail challenge;
+ challenge.handleChallengeRequest(interest, request);
+
+ BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::FAILURE_INVALID_EMAIL);
+ BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
+}
+
+BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithCode)
+{
+ auto identity = addIdentity(Name("/ndn/site1"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
+ CertificateRequest request(Name("/ndn/site1"), "123", cert);
+ request.setChallengeType("EMAIL");
+ request.setStatus(ChallengeEmail::NEED_CODE);
+
+ time::system_clock::TimePoint tp = time::system_clock::now();
+ JsonSection json;
+ json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
+ json.put(ChallengeEmail::JSON_CODE, "4567");
+ json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
+
+ request.setChallengeSecrets(json);
+
+ JsonSection infoJson;
+ 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");
+ interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
+ Interest interest(interestName);
+
+ ChallengeEmail challenge;
+ challenge.handleChallengeRequest(interest, request);
+
+ BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
+ BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
+}
+
+BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
+{
+ auto identity = addIdentity(Name("/ndn/site1"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
+ CertificateRequest request(Name("/ndn/site1"), "123", cert);
+ request.setChallengeType("EMAIL");
+ request.setStatus(ChallengeEmail::NEED_CODE);
+
+ time::system_clock::TimePoint tp = time::system_clock::now();
+ JsonSection json;
+ json.put(ChallengeEmail::JSON_CODE_TP, time::toIsoString(tp));
+ json.put(ChallengeEmail::JSON_CODE, "4567");
+ json.put(ChallengeEmail::JSON_ATTEMPT_TIMES, std::to_string(3));
+
+ request.setChallengeSecrets(json);
+
+ JsonSection infoJson;
+ 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");
+ interestName.append("_VALIDATE").append("Fake-Request-ID").append("EMAIL").append(jsonContent);
+ Interest interest(interestName);
+
+ ChallengeEmail challenge;
+ challenge.handleChallengeRequest(interest, request);
+
+ BOOST_CHECK_EQUAL(request.getStatus(), ChallengeEmail::WRONG_CODE);
+ BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), false);
+}
+
+BOOST_AUTO_TEST_CASE(ClientSendSelect)
+{
+ ChallengeEmail challenge;
+ auto requirementList = challenge.getSelectRequirements();
+ BOOST_CHECK_EQUAL(requirementList.size(), 1);
+
+ requirementList.clear();
+ requirementList.push_back("zhiyi@cs.ucla.edu");
+
+ auto json = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, requirementList);
+ BOOST_CHECK_EQUAL(json.empty(), false);
+ BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_EMAIL), "zhiyi@cs.ucla.edu");
+}
+
+BOOST_AUTO_TEST_CASE(ClientSendValidate)
+{
+ ChallengeEmail challenge;
+ auto requirementList = challenge.getValidateRequirements(ChallengeEmail::NEED_CODE);
+ BOOST_CHECK_EQUAL(requirementList.size(), 1);
+
+ requirementList.clear();
+ requirementList.push_back("123");
+
+ auto json = challenge.genValidateParamsJson(ChallengeEmail::NEED_CODE, requirementList);
+ BOOST_CHECK_EQUAL(json.empty(), false);
+ BOOST_CHECK_EQUAL(json.get<std::string>(ChallengeEmail::JSON_CODE), "123");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
diff --git a/tests/unit-tests/test-send-email.sh b/tests/unit-tests/test-send-email.sh
new file mode 100755
index 0000000..78117fc
--- /dev/null
+++ b/tests/unit-tests/test-send-email.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+RECEIVER=$1
+SECRET=$2
+
+MESSAGE=$RECEIVER$SECRET
+
+echo $MESSAGE