blob: 92fb45b417a835e8654e534877023c81531017c5 [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/*
Davide Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2017-2021, 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 Zhang84e11842020-11-19 20:03:23 -080021#include "challenge/challenge-email.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070022#include "test-common.hpp"
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080023
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080024namespace ndncert {
25namespace tests {
26
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070027BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080028
Davide Pesavento914d05f2019-07-13 16:20:19 -040029BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080030{
31 ChallengeEmail challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -070032 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080033}
34
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080035BOOST_AUTO_TEST_CASE(EmailAddressChecker)
36{
37 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
38 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
39 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
40}
41
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080043{
44 auto identity = addIdentity(Name("/ndn/site1"));
45 auto key = identity.getDefaultKey();
46 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080047 RequestId requestId = {{101}};
48 ca::RequestState request;
49 request.caPrefix = Name("/ndn/site1");
50 request.requestId = requestId;
51 request.requestType = RequestType::NEW;
52 request.cert = cert;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080053
Davide Pesavento0dc02012021-11-23 22:55:03 -050054 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
55 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
56 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080057
58 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070059 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080060
tylerliu7b9185c2020-11-24 12:15:18 -080061 BOOST_CHECK(request.status == Status::CHALLENGE);
62 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::NEED_CODE);
63 BOOST_CHECK(request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != "");
64 BOOST_CHECK(request.challengeState->remainingTime.count() != 0);
65 BOOST_CHECK(request.challengeState->remainingTries != 0);
66 BOOST_CHECK_EQUAL(request.challengeType, "email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070067
68 std::string line = "";
69 std::string delimiter = " ";
70 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070071 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070072 getline(emailFile, line);
73 emailFile.close();
74 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070075 int end = line.find(delimiter);
76 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070077 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070078 line = line.substr(end + 1);
79
80 end = line.find(delimiter);
81 std::string secret = line.substr(0, end);
tylerliu7b9185c2020-11-24 12:15:18 -080082 auto stored_secret = request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070083 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070084 line = line.substr(end + 1);
85
86 end = line.find(delimiter);
87 std::string caName = line.substr(0, end);
88 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
89 line = line.substr(end + 1);
90
91 std::string certName = line;
Davide Pesavento914d05f2019-07-13 16:20:19 -040092 BOOST_CHECK_EQUAL(certName, cert.getName());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070093 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080094}
95
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080097{
98 auto identity = addIdentity(Name("/ndn/site1"));
99 auto key = identity.getDefaultKey();
100 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800101 RequestId requestId = {{101}};
102 ca::RequestState request;
103 request.caPrefix = Name("/ndn/site1");
104 request.requestId = requestId;
105 request.requestType = RequestType::NEW;
106 request.cert = cert;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800107
Davide Pesavento0dc02012021-11-23 22:55:03 -0500108 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
109 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
110 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi@cs"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800111
112 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700113 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800114
tylerliu7b9185c2020-11-24 12:15:18 -0800115 BOOST_CHECK_EQUAL(request.challengeType, "email");
116 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::INVALID_EMAIL);
117 BOOST_CHECK_EQUAL(request.challengeState->remainingTries, 2);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800118}
119
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700120BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800121{
122 auto identity = addIdentity(Name("/ndn/site1"));
123 auto key = identity.getDefaultKey();
124 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800125 JsonSection secret;
126 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
127 RequestId requestId = {{101}};
128 ca::RequestState request;
129 request.caPrefix = Name("/ndn/site1");
130 request.requestId = requestId;
131 request.requestType = RequestType::NEW;
132 request.status = Status::CHALLENGE;
133 request.cert = cert;
134 request.challengeType = "email";
135 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
136 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800137
Davide Pesavento0dc02012021-11-23 22:55:03 -0500138 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
139 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
140 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800141
142 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700143 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800144
tylerliu7b9185c2020-11-24 12:15:18 -0800145 BOOST_CHECK(request.status == Status::PENDING);
146 BOOST_CHECK(!request.challengeState);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800147}
148
149BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
150{
151 auto identity = addIdentity(Name("/ndn/site1"));
152 auto key = identity.getDefaultKey();
153 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800154 JsonSection secret;
155 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
156 RequestId requestId = {{101}};
157 ca::RequestState request;
158 request.caPrefix = Name("/ndn/site1");
159 request.requestId = requestId;
160 request.requestType = RequestType::NEW;
161 request.status = Status::CHALLENGE;
162 request.cert = cert;
163 request.challengeType = "email";
164 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
165 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800166
Davide Pesavento0dc02012021-11-23 22:55:03 -0500167 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
168 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
169 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800170
171 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700172 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800173
tylerliu7b9185c2020-11-24 12:15:18 -0800174 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::WRONG_CODE);
175 BOOST_CHECK(request.status == Status::CHALLENGE);
176 BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800177}
178
Davide Pesavento0dc02012021-11-23 22:55:03 -0500179BOOST_AUTO_TEST_SUITE_END() // TestChallengeEmail
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800180
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700181} // namespace tests
182} // namespace ndncert