blob: 834411d730fb52e8b8a67dd212f8b48a4d7c812c [file] [log] [blame]
Zhiyi Zhangdefa9592017-02-21 10:56:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento914d05f2019-07-13 16:20:19 -04002/*
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"
Davide Pesavento914d05f2019-07-13 16:20:19 -040022
Zhiyi Zhang576aad12017-10-03 15:41:53 -070023#include "identity-management-fixture.hpp"
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080024
25namespace ndn {
26namespace ndncert {
27namespace tests {
28
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070029BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080030
Davide Pesavento914d05f2019-07-13 16:20:19 -040031BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080032{
33 ChallengeEmail challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -070034 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080035}
36
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080037BOOST_AUTO_TEST_CASE(EmailAddressChecker)
38{
39 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
40 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
41 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
42}
43
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080045{
46 auto identity = addIdentity(Name("/ndn/site1"));
47 auto key = identity.getDefaultKey();
48 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070049 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080050
Suyong Won44d0cce2020-05-10 04:07:43 -070051 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
52 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::JSON_EMAIL));
53 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080054
55 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070056 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080057
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
59 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::NEED_CODE);
60 BOOST_CHECK(request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE) != "");
61 BOOST_CHECK(request.m_remainingTime != 0);
62 BOOST_CHECK(request.m_remainingTries != 0);
63 BOOST_CHECK(request.m_challengeTp != "");
Zhiyi Zhang36706832019-07-04 21:33:03 -070064 BOOST_CHECK_EQUAL(request.m_challengeType, "email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070065
66 std::string line = "";
67 std::string delimiter = " ";
68 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070069 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070070 getline(emailFile, line);
71 emailFile.close();
72 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070073 int end = line.find(delimiter);
74 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070075 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070076 line = line.substr(end + 1);
77
78 end = line.find(delimiter);
79 std::string secret = line.substr(0, end);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 auto stored_secret = request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070081 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070082 line = line.substr(end + 1);
83
84 end = line.find(delimiter);
85 std::string caName = line.substr(0, end);
86 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
87 line = line.substr(end + 1);
88
89 std::string certName = line;
Davide Pesavento914d05f2019-07-13 16:20:19 -040090 BOOST_CHECK_EQUAL(certName, cert.getName());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070091 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080092}
93
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080095{
96 auto identity = addIdentity(Name("/ndn/site1"));
97 auto key = identity.getDefaultKey();
98 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800100
Suyong Won44d0cce2020-05-10 04:07:43 -0700101 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
102 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::JSON_EMAIL));
103 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800104
105 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700106 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800107
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700108 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::FAILURE_INVALID_EMAIL);
109 BOOST_CHECK_EQUAL(request.m_status, STATUS_FAILURE);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800110}
111
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700112BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800113{
114 auto identity = addIdentity(Name("/ndn/site1"));
115 auto key = identity.getDefaultKey();
116 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800117 JsonSection json;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800118 json.put(ChallengeEmail::JSON_CODE, "4567");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700119 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE,
120 "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800121
Suyong Won44d0cce2020-05-10 04:07:43 -0700122 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
123 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::JSON_CODE));
124 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800125
126 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700127 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800128
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700129 BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
130 BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING);
131 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800132}
133
134BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
135{
136 auto identity = addIdentity(Name("/ndn/site1"));
137 auto key = identity.getDefaultKey();
138 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800139 JsonSection json;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800140 json.put(ChallengeEmail::JSON_CODE, "4567");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700141 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE,
Zhiyi Zhang36706832019-07-04 21:33:03 -0700142 "email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800143
Suyong Won44d0cce2020-05-10 04:07:43 -0700144 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
145 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::JSON_CODE));
146 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800147
148 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700149 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800150
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700151 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::WRONG_CODE);
152 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
153 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800154}
155
156BOOST_AUTO_TEST_SUITE_END()
157
158} // namespace tests
159} // namespace ndncert
160} // namespace ndn