Fix failure status bug: when status is failure, request should be stopped

Change-Id: I699d9ae6bdc34ac67d9a8b3c69e9d789f9709c6d
diff --git a/src/challenge-module/challenge-credential.cpp b/src/challenge-module/challenge-credential.cpp
index 2a35395..2a47e7a 100644
--- a/src/challenge-module/challenge-credential.cpp
+++ b/src/challenge-module/challenge-credential.cpp
@@ -91,7 +91,7 @@
   catch (const std::exception& e) {
     _LOG_TRACE("Cannot load credential parameter: cert" << e.what());
     request.setStatus(FAILURE_INVALID_FORMAT);
-    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
+    return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_INVALID_FORMAT);
   }
   ss1.str("");
   ss1.clear();
@@ -104,7 +104,7 @@
   catch (const std::exception& e) {
     _LOG_TRACE("Cannot load credential parameter: self-signed cert" << e.what());
     request.setStatus(FAILURE_INVALID_FORMAT);
-    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
+    return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_INVALID_FORMAT);
   }
   ss2.str("");
   ss2.clear();
@@ -128,7 +128,7 @@
 ChallengeCredential::processValidateInterest(const Interest& interest, CertificateRequest& request)
 {
   // there is no validate request here, do nothing
-  return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_FORMAT);
+  return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_INVALID_FORMAT);
 }
 
 std::list<std::string>
diff --git a/src/challenge-module/challenge-credential.hpp b/src/challenge-module/challenge-credential.hpp
index a617f18..bcd8b5d 100644
--- a/src/challenge-module/challenge-credential.hpp
+++ b/src/challenge-module/challenge-credential.hpp
@@ -40,8 +40,10 @@
  *   1. Requester provides a certificate signed by that trusted certificate as credential.
  *   2. The challenge module will verify the signature of the credential.
  *
- * There are several challenge status in EMAIL challenge:
- *   FAILURE_INVALID_SIG: When the credential cannot be validated.
+ * Failure info when application fails:
+ *   FAILURE_INVALID_CREDENTIAL: When the cert issued from trust anchor or self-signed cert
+ *     cannot be validated.
+ *   FAILURE_INVALID_FORMAT: When the credential format is wrong.
  */
 class ChallengeCredential : public ChallengeModule
 {
@@ -76,6 +78,7 @@
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   static const std::string FAILURE_INVALID_CREDENTIAL;
   static const std::string FAILURE_INVALID_FORMAT;
+
   static const std::string JSON_CREDENTIAL_CERT;
   static const std::string JSON_CREDENTIAL_SELF;
 
diff --git a/src/challenge-module/challenge-email.cpp b/src/challenge-module/challenge-email.cpp
index 036ccd2..fc25d8c 100644
--- a/src/challenge-module/challenge-email.cpp
+++ b/src/challenge-module/challenge-email.cpp
@@ -31,9 +31,10 @@
 
 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::FAILURE_TIMEOUT = "timeout";
+const std::string ChallengeEmail::FAILURE_MAXRETRY = "max-retry";
 
 const std::string ChallengeEmail::JSON_EMAIL = "email";
 const std::string ChallengeEmail::JSON_CODE_TP = "code-timepoint";
@@ -43,7 +44,7 @@
 ChallengeEmail::ChallengeEmail(const std::string& scriptPath,
                                const size_t& maxAttemptTimes,
                                const time::seconds secretLifetime)
-  : ChallengeModule("EMAIL")
+  : ChallengeModule("Email")
   , m_sendEmailScript(scriptPath)
   , m_maxAttemptTimes(maxAttemptTimes)
   , m_secretLifetime(secretLifetime)
@@ -60,7 +61,7 @@
   if (!isValidEmailAddress(emailAddress)) {
     request.setStatus(FAILURE_INVALID_EMAIL);
     request.setChallengeType(CHALLENGE_TYPE);
-    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_INVALID_EMAIL);
+    return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_INVALID_EMAIL);
   }
 
   std::string emailCode = generateSecretCode();
@@ -69,8 +70,7 @@
   request.setStatus(NEED_CODE);
   request.setChallengeType(CHALLENGE_TYPE);
   request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(),
-                                                    emailCode,
-                                                    m_maxAttemptTimes));
+                                                    emailCode, m_maxAttemptTimes));
   return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE);
 }
 
@@ -86,7 +86,7 @@
     // secret expires
     request.setStatus(FAILURE_TIMEOUT);
     request.setChallengeSecrets(JsonSection());
-    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_TIMEOUT);
+    return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_TIMEOUT);
   }
   else if (givenCode == std::get<1>(parsedSecret)) {
     request.setStatus(SUCCESS);
@@ -108,7 +108,7 @@
       // run out times
       request.setStatus(FAILURE_MAXRETRY);
       request.setChallengeSecrets(JsonSection());
-      return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_MAXRETRY);
+      return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_MAXRETRY);
     }
   }
 }
@@ -129,7 +129,7 @@
     result.push_back("Please input your verification code:");
   }
   else if (status == WRONG_CODE) {
-    result.push_back("Incorrect PIN code, please input your verification code:");
+    result.push_back("Incorrect PIN code, please try again and input your verification code:");
   }
   return result;
 }
diff --git a/src/challenge-module/challenge-email.hpp b/src/challenge-module/challenge-email.hpp
index b00a3de..0f0d935 100644
--- a/src/challenge-module/challenge-email.hpp
+++ b/src/challenge-module/challenge-email.hpp
@@ -40,9 +40,11 @@
  * 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 info when application fails:
+ *   FAILURE_MAXRETRY: When run out retry 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
 {
@@ -90,8 +92,9 @@
 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_INVALID_EMAIL;
   static const std::string FAILURE_MAXRETRY;
 
   static const std::string JSON_EMAIL;
diff --git a/src/challenge-module/challenge-pin.cpp b/src/challenge-module/challenge-pin.cpp
index 94ea3e7..2bb81ea 100644
--- a/src/challenge-module/challenge-pin.cpp
+++ b/src/challenge-module/challenge-pin.cpp
@@ -32,6 +32,7 @@
 
 const std::string ChallengePin::NEED_CODE = "need-code";
 const std::string ChallengePin::WRONG_CODE = "wrong-code";
+
 const std::string ChallengePin::FAILURE_TIMEOUT = "failure-timeout";
 const std::string ChallengePin::FAILURE_MAXRETRY = "failure-max-retry";
 
@@ -72,7 +73,7 @@
     // secret expires
     request.setStatus(FAILURE_TIMEOUT);
     request.setChallengeSecrets(JsonSection());
-    return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_TIMEOUT);
+    return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_TIMEOUT);
   }
   else if (givenCode == std::get<1>(parsedSecret)) {
     request.setStatus(SUCCESS);
@@ -94,7 +95,7 @@
       // run out times
       request.setStatus(FAILURE_MAXRETRY);
       request.setChallengeSecrets(JsonSection());
-      return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE_MAXRETRY);
+      return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_MAXRETRY);
     }
   }
 }
@@ -114,7 +115,7 @@
     result.push_back("Please input your verification code:");
   }
   else if (status == WRONG_CODE) {
-    result.push_back("Incorrect PIN code, please input your verification code:");
+    result.push_back("Incorrect PIN code, please try again and input your verification code:");
   }
   return result;
 }
diff --git a/src/challenge-module/challenge-pin.hpp b/src/challenge-module/challenge-pin.hpp
index 31ea05f..982ed4e 100644
--- a/src/challenge-module/challenge-pin.hpp
+++ b/src/challenge-module/challenge-pin.hpp
@@ -40,6 +40,8 @@
  * There are four specific status defined in this challenge:
  *   NEED_CODE: When selection is made.
  *   WRONG_CODE: Get wrong verification code but still with secret lifetime and max retry times.
+ *
+ * Failure info when application fails:
  *   FAILURE_TIMEOUT: When secret is out-dated.
  *   FAILURE_MAXRETRY: When requester tries too many times.
  */
@@ -81,6 +83,7 @@
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   static const std::string NEED_CODE;
   static const std::string WRONG_CODE;
+
   static const std::string FAILURE_TIMEOUT;
   static const std::string FAILURE_MAXRETRY;