blob: af86a5f18758f447ccb7b9b3831b5ebe36a886f7 [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, Regents of the University of California.
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080021#include "challenge-module/challenge-email.hpp"
Zhiyi Zhang576aad12017-10-03 15:41:53 -070022#include "identity-management-fixture.hpp"
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080023
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080029
30BOOST_AUTO_TEST_CASE(TestChallengeType)
31{
32 ChallengeEmail challenge;
Zhiyi Zhanga9bda732017-05-20 22:58:55 -070033 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080034}
35
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080036BOOST_AUTO_TEST_CASE(EmailAddressChecker)
37{
38 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
39 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
40 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
41}
42
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070043BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080044{
45 auto identity = addIdentity(Name("/ndn/site1"));
46 auto key = identity.getDefaultKey();
47 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080049
50 JsonSection emailJson;
51 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs.ucla.edu");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080052
53 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070054 challenge.handleChallengeRequest(emailJson, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080055
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
57 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::NEED_CODE);
58 BOOST_CHECK(request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE) != "");
59 BOOST_CHECK(request.m_remainingTime != 0);
60 BOOST_CHECK(request.m_remainingTries != 0);
61 BOOST_CHECK(request.m_challengeTp != "");
62 BOOST_CHECK_EQUAL(request.m_challengeType, "Email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070063
64 std::string line = "";
65 std::string delimiter = " ";
66 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070067 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070068 getline(emailFile, line);
69 emailFile.close();
70 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070071 int end = line.find(delimiter);
72 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070073 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070074 line = line.substr(end + 1);
75
76 end = line.find(delimiter);
77 std::string secret = line.substr(0, end);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 auto stored_secret = request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070079 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070080 line = line.substr(end + 1);
81
82 end = line.find(delimiter);
83 std::string caName = line.substr(0, end);
84 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
85 line = line.substr(end + 1);
86
87 std::string certName = line;
88 BOOST_CHECK_EQUAL(certName, cert.getName().toUri());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070089 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080090}
91
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070092BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080093{
94 auto identity = addIdentity(Name("/ndn/site1"));
95 auto key = identity.getDefaultKey();
96 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080098
99 JsonSection emailJson;
100 emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800101
102 ChallengeEmail challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103 challenge.handleChallengeRequest(emailJson, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800104
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700105 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::FAILURE_INVALID_EMAIL);
106 BOOST_CHECK_EQUAL(request.m_status, STATUS_FAILURE);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800107}
108
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800110{
111 auto identity = addIdentity(Name("/ndn/site1"));
112 auto key = identity.getDefaultKey();
113 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800114 JsonSection json;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800115 json.put(ChallengeEmail::JSON_CODE, "4567");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE,
117 "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800118
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 JsonSection requestJson;
120 requestJson.put(ChallengeEmail::JSON_CODE, "4567");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800121
122 ChallengeEmail challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 challenge.handleChallengeRequest(requestJson, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800124
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700125 BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
126 BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING);
127 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800128}
129
130BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
131{
132 auto identity = addIdentity(Name("/ndn/site1"));
133 auto key = identity.getDefaultKey();
134 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800135 JsonSection json;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800136 json.put(ChallengeEmail::JSON_CODE, "4567");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700137 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE,
138 "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800139
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700140 JsonSection requestJson;
141 requestJson.put(ChallengeEmail::JSON_CODE, "7890");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800142
143 ChallengeEmail challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700144 challenge.handleChallengeRequest(requestJson, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800145
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700146 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::WRONG_CODE);
147 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
148 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800149}
150
151BOOST_AUTO_TEST_SUITE_END()
152
153} // namespace tests
154} // namespace ndncert
155} // namespace ndn