Add email challenge module

Refs: #4053

Change-Id: Ic5093fa484df967a760d7503eab2436f5794a385
diff --git a/.jenkins.d/01-ndn-cxx.sh b/.jenkins.d/01-ndn-cxx.sh
index 482c155..31bb9e4 100755
--- a/.jenkins.d/01-ndn-cxx.sh
+++ b/.jenkins.d/01-ndn-cxx.sh
@@ -16,6 +16,10 @@
 
 LATEST_VERSION=$((cd ndn-cxx-latest && git rev-parse HEAD) 2>/dev/null || echo UNKNOWN)
 
+if has OSX $NODE_LABELS; then
+    LATEST_VERSION=""
+fi
+
 if [[ $INSTALLED_VERSION != $LATEST_VERSION ]]; then
     sudo rm -Rf ndn-cxx
     mv ndn-cxx-latest ndn-cxx
diff --git a/.jenkins.d/10-build.sh b/.jenkins.d/10-build.sh
index 33c02fa..ef94d90 100755
--- a/.jenkins.d/10-build.sh
+++ b/.jenkins.d/10-build.sh
@@ -35,7 +35,7 @@
 elif ! has OSX-10.9 $NODE_LABELS && ! has OSX-10.11 $NODE_LABELS; then
     ASAN="--with-sanitizer=address"
 fi
-./waf -j1 --color=yes configure --debug --with-tests $COVERAGE $ASAN
+./waf -j1 --color=yes configure --debug --with-tests --with-sanitizer=address $COVERAGE $ASAN
 ./waf -j1 --color=yes build
 
 # (tests will be run against debug version)
diff --git a/src/challenge-module.cpp b/src/challenge-module.cpp
index c7ab849..59a0e58 100644
--- a/src/challenge-module.cpp
+++ b/src/challenge-module.cpp
@@ -25,7 +25,7 @@
 namespace ndn {
 namespace ndncert {
 
-_LOG_INIT(ndncert.pinchallenge);
+_LOG_INIT(ndncert.challenge-module);
 
 const std::string ChallengeModule::WAIT_SELECTION = "wait-selection";
 const std::string ChallengeModule::SUCCESS = "success";
diff --git a/src/challenge-module/challenge-email.cpp b/src/challenge-module/challenge-email.cpp
new file mode 100644
index 0000000..59f471d
--- /dev/null
+++ b/src/challenge-module/challenge-email.cpp
@@ -0,0 +1,215 @@
+/* -*- 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 "challenge-email.hpp"
+#include "../logging.hpp"
+#include <boost/regex.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+_LOG_INIT(ndncert.ChallengeEmail);
+
+NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "Email");
+
+const std::string ChallengeEmail::NEED_CODE = "need-code";
+const std::string ChallengeEmail::WRONG_CODE = "wrong-code";
+const std::string ChallengeEmail::FAILURE_TIMEOUT = "failure-timeout";
+const std::string ChallengeEmail::FAILURE_MAXRETRY = "failure-max-retry";
+const std::string ChallengeEmail::FAILURE_INVALID_EMAIL = "failure-invalid-email";
+
+const std::string ChallengeEmail::JSON_EMAIL = "email";
+const std::string ChallengeEmail::JSON_CODE_TP = "code-timepoint";
+const std::string ChallengeEmail::JSON_CODE = "code";
+const std::string ChallengeEmail::JSON_ATTEMPT_TIMES = "attempt-times";
+
+ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
+                               const size_t& maxAttemptTimes,
+                               const time::seconds secretLifetime)
+  : ChallengeModule("EMAIL")
+  , m_sendEmailScript(scriptPath)
+  , m_maxAttemptTimes(maxAttemptTimes)
+  , m_secretLifetime(secretLifetime)
+{
+}
+
+JsonSection
+ChallengeEmail::processSelectInterest(const Interest& interest, CertificateRequest& request)
+{
+  // interest format: /caName/CA/_SELECT/{"request-id":"id"}/EMAIL/{"Email":"email"}/<signature>
+  JsonSection emailJson = getJsonFromNameComponent(interest.getName(),
+                                                   request.getCaName().size() + 4);
+  std::string emailAddress = emailJson.get<std::string>(JSON_EMAIL);
+  if (!isValidEmailAddress(emailAddress)) {
+    request.setStatus(FAILURE_INVALID_EMAIL);
+    request.setChallengeType(CHALLENGE_TYPE);
+    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_EMAIL);
+  }
+
+  std::string emailCode = generateSecretCode();
+  sendEmail(emailAddress, emailCode);
+
+  request.setStatus(NEED_CODE);
+  request.setChallengeType(CHALLENGE_TYPE);
+  request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(),
+                                                    emailCode,
+                                                    m_maxAttemptTimes));
+  return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE);
+}
+
+JsonSection
+ChallengeEmail::processValidateInterest(const Interest& interest, CertificateRequest& request)
+{
+  // interest format: /caName/CA/_VALIDATION/{"request-id":"id"}/EMAIL/{"code":"code"}/<signature>
+  JsonSection infoJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4);
+  std::string givenCode = infoJson.get<std::string>(JSON_CODE);
+
+  const auto parsedSecret = parseStoredSecrets(request.getChallengeSecrets());
+  if (time::system_clock::now() - std::get<0>(parsedSecret) >= m_secretLifetime) {
+    // secret expires
+    request.setStatus(FAILURE_TIMEOUT);
+    request.setChallengeSecrets(JsonSection());
+    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_TIMEOUT);
+  }
+  else if (givenCode == std::get<1>(parsedSecret)) {
+    request.setStatus(SUCCESS);
+    request.setChallengeSecrets(JsonSection());
+    Name downloadName = genDownloadName(request.getCaName(), request.getRequestId());
+    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS, downloadName);
+  }
+  else {
+    // check rest attempt times
+    if (std::get<2>(parsedSecret) > 1) {
+      int restAttemptTimes = std::get<2>(parsedSecret) - 1;
+      request.setStatus(WRONG_CODE);
+      request.setChallengeSecrets(generateStoredSecrets(std::get<0>(parsedSecret),
+                                                        std::get<1>(parsedSecret),
+                                                        restAttemptTimes));
+      return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, WRONG_CODE);
+    }
+    else {
+      // run out times
+      request.setStatus(FAILURE_MAXRETRY);
+      request.setChallengeSecrets(JsonSection());
+      return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_MAXRETRY);
+    }
+  }
+}
+
+std::list<std::string>
+ChallengeEmail::getSelectRequirements()
+{
+  std::list<std::string> result;
+  result.push_back("Please input your email address:");
+  return result;
+}
+
+std::list<std::string>
+ChallengeEmail::getValidateRequirements(const std::string& status)
+{
+  std::list<std::string> result;
+  if (status == NEED_CODE) {
+    result.push_back("Please input your verification code:");
+  }
+  else if (status == WRONG_CODE) {
+    result.push_back("Incorrect PIN code, please input your verification code:");
+  }
+  return result;
+}
+
+JsonSection
+ChallengeEmail::doGenSelectParamsJson(const std::string& status,
+                                      const std::list<std::string>& paramList)
+{
+  JsonSection result;
+  BOOST_ASSERT(paramList.size() == 1);
+  result.put(JSON_EMAIL, paramList.front());
+  return result;
+}
+
+JsonSection
+ChallengeEmail::doGenValidateParamsJson(const std::string& status,
+                                        const std::list<std::string>& paramList)
+{
+  JsonSection result;
+  BOOST_ASSERT(paramList.size() == 1);
+  result.put(JSON_CODE, paramList.front());
+  return result;
+}
+
+std::tuple<time::system_clock::TimePoint, std::string, int>
+ChallengeEmail::parseStoredSecrets(const JsonSection& storedSecrets)
+{
+  auto tp = time::fromIsoString(storedSecrets.get<std::string>(JSON_CODE_TP));
+  std::string rightCode= storedSecrets.get<std::string>(JSON_CODE);
+  int attemptTimes = std::stoi(storedSecrets.get<std::string>(JSON_ATTEMPT_TIMES));
+
+  return std::make_tuple(tp, rightCode, attemptTimes);
+}
+
+JsonSection
+ChallengeEmail::generateStoredSecrets(const time::system_clock::TimePoint& tp,
+                                    const std::string& secretCode, int attempTimes)
+{
+  JsonSection json;
+  json.put(JSON_CODE_TP, time::toIsoString(tp));
+  json.put(JSON_CODE, secretCode);
+  json.put(JSON_ATTEMPT_TIMES, std::to_string(attempTimes));
+  return json;
+}
+
+bool
+ChallengeEmail::isValidEmailAddress(const std::string& emailAddress)
+{
+  std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_";
+  boost::regex emailPattern(pattern);
+  return boost::regex_match(emailAddress, emailPattern);
+}
+
+void
+ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret) const
+{
+  pid_t pid = fork();
+
+  if (pid < 0) {
+    _LOG_TRACE("Cannot fork before trying to call email sending script");
+  }
+  else if (pid == 0) {
+    int ret;
+    std::vector<char> emailParam(emailAddress.begin(), emailAddress.end());
+    emailParam.push_back('\0');
+
+    std::vector<char> secretParam(secret.begin(), secret.end());
+    secretParam.push_back('\0');
+
+    std::vector<char> defaultParam(m_sendEmailScript.begin(), m_sendEmailScript.end());
+    defaultParam.push_back('\0');
+
+    char* argv[] = {&defaultParam[0], &emailParam[0], &secretParam[0], nullptr};
+    ret = execve(m_sendEmailScript.c_str(), argv, nullptr);
+
+    BOOST_THROW_EXCEPTION(Error("Email sending script went wrong, error code: " + std::to_string(ret)));
+  }
+
+  return;
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/challenge-module/challenge-email.hpp b/src/challenge-module/challenge-email.hpp
new file mode 100644
index 0000000..b00a3de
--- /dev/null
+++ b/src/challenge-module/challenge-email.hpp
@@ -0,0 +1,111 @@
+/* -*- 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.
+ */
+
+#ifndef NDNCERT_CHALLENGE_EMAIL_HPP
+#define NDNCERT_CHALLENGE_EMAIL_HPP
+
+#include "../challenge-module.hpp"
+#include <ndn-cxx/util/time.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+/**
+ * @brief Provide Email based challenge
+ *
+ * @sa https://github.com/named-data/ndncert/wiki/NDN-Certificate-Management-Protocol
+ *
+ * The main process of this challenge module is:
+ *   1. Requester provides its email address.
+ *   2. The challenge module will send a verification code to this email address.
+ *   3. Requester provides the verification code to challenge module.
+ *
+ * There are several challenge status in EMAIL challenge:
+ *   NEED_CODE: When email address is provided and the verification code has been sent out.
+ *   WRONG_CODE: Wrong code but still within secret lifetime and within max try times.
+ *   FAILURE_INVALID_EMAIL: When the email is invalid.
+ *   FAILURE_TIMEOUT: When the secret lifetime expires.
+ *   FAILURE_MAXRETRY: When run out retry times.
+ */
+class ChallengeEmail : public ChallengeModule
+{
+public:
+  ChallengeEmail(const std::string& scriptPath = "send-mail.sh",
+                 const size_t& maxAttemptTimes = 3,
+                 const time::seconds secretLifetime = time::minutes(20));
+
+PUBLIC_WITH_TESTS_ELSE_PROTECTED:
+  JsonSection
+  processSelectInterest(const Interest& interest, CertificateRequest& request) override;
+
+  JsonSection
+  processValidateInterest(const Interest& interest, CertificateRequest& request) override;
+
+  std::list<std::string>
+  getSelectRequirements() override;
+
+  std::list<std::string>
+  getValidateRequirements(const std::string& status) override;
+
+  JsonSection
+  doGenSelectParamsJson(const std::string& status,
+                        const std::list<std::string>& paramList) override;
+
+  JsonSection
+  doGenValidateParamsJson(const std::string& status,
+                          const std::list<std::string>& paramList) override;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  static bool
+  isValidEmailAddress(const std::string& emailAddress);
+
+  void
+  sendEmail(const std::string& emailAddress, const std::string& secret) const;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  static std::tuple<time::system_clock::TimePoint, std::string, int>
+  parseStoredSecrets(const JsonSection& storedSecret);
+
+  static JsonSection
+  generateStoredSecrets(const time::system_clock::TimePoint& tp, const std::string& secretCode,
+                        int attempTimes);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  static const std::string NEED_CODE;
+  static const std::string WRONG_CODE;
+  static const std::string FAILURE_INVALID_EMAIL;
+  static const std::string FAILURE_TIMEOUT;
+  static const std::string FAILURE_MAXRETRY;
+
+  static const std::string JSON_EMAIL;
+  static const std::string JSON_CODE_TP;
+  static const std::string JSON_CODE;
+  static const std::string JSON_ATTEMPT_TIMES;
+
+private:
+  std::string m_sendEmailScript;
+  int m_maxAttemptTimes;
+  time::seconds m_secretLifetime;
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CHALLENGE_EMAIL_HPP
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