blob: 8ee9be9443ab9f39125e4d1788aba77217112dc1 [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/*
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, 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
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
Davide Pesavento914d05f2019-07-13 16:20:19 -040030BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080031{
32 ChallengeEmail challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -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 Zhang1f5e86e2020-12-04 15:07:57 -080048 RequestId requestId = {{101}};
49 ca::RequestState request;
50 request.caPrefix = Name("/ndn/site1");
51 request.requestId = requestId;
52 request.requestType = RequestType::NEW;
53 request.cert = cert;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080054
tylerliu50d679e2020-10-14 14:08:39 -070055 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
56 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
57 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080058
59 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070060 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080061
tylerliu7b9185c2020-11-24 12:15:18 -080062 BOOST_CHECK(request.status == Status::CHALLENGE);
63 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::NEED_CODE);
64 BOOST_CHECK(request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != "");
65 BOOST_CHECK(request.challengeState->remainingTime.count() != 0);
66 BOOST_CHECK(request.challengeState->remainingTries != 0);
67 BOOST_CHECK_EQUAL(request.challengeType, "email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070068
69 std::string line = "";
70 std::string delimiter = " ";
71 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070072 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070073 getline(emailFile, line);
74 emailFile.close();
75 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070076 int end = line.find(delimiter);
77 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070078 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070079 line = line.substr(end + 1);
80
81 end = line.find(delimiter);
82 std::string secret = line.substr(0, end);
tylerliu7b9185c2020-11-24 12:15:18 -080083 auto stored_secret = request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070084 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070085 line = line.substr(end + 1);
86
87 end = line.find(delimiter);
88 std::string caName = line.substr(0, end);
89 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
90 line = line.substr(end + 1);
91
92 std::string certName = line;
Davide Pesavento914d05f2019-07-13 16:20:19 -040093 BOOST_CHECK_EQUAL(certName, cert.getName());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070094 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080095}
96
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070097BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080098{
99 auto identity = addIdentity(Name("/ndn/site1"));
100 auto key = identity.getDefaultKey();
101 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800102 RequestId requestId = {{101}};
103 ca::RequestState request;
104 request.caPrefix = Name("/ndn/site1");
105 request.requestId = requestId;
106 request.requestType = RequestType::NEW;
107 request.cert = cert;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800108
tylerliu50d679e2020-10-14 14:08:39 -0700109 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
110 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
111 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi@cs"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800112
113 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700114 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800115
tylerliu7b9185c2020-11-24 12:15:18 -0800116 BOOST_CHECK_EQUAL(request.challengeType, "email");
117 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::INVALID_EMAIL);
118 BOOST_CHECK_EQUAL(request.challengeState->remainingTries, 2);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800119}
120
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800122{
123 auto identity = addIdentity(Name("/ndn/site1"));
124 auto key = identity.getDefaultKey();
125 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800126 JsonSection secret;
127 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
128 RequestId requestId = {{101}};
129 ca::RequestState request;
130 request.caPrefix = Name("/ndn/site1");
131 request.requestId = requestId;
132 request.requestType = RequestType::NEW;
133 request.status = Status::CHALLENGE;
134 request.cert = cert;
135 request.challengeType = "email";
136 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
137 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800138
tylerliu50d679e2020-10-14 14:08:39 -0700139 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
140 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
141 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800142
143 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700144 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800145
tylerliu7b9185c2020-11-24 12:15:18 -0800146 BOOST_CHECK(request.status == Status::PENDING);
147 BOOST_CHECK(!request.challengeState);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800148}
149
150BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
151{
152 auto identity = addIdentity(Name("/ndn/site1"));
153 auto key = identity.getDefaultKey();
154 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800155 JsonSection secret;
156 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
157 RequestId requestId = {{101}};
158 ca::RequestState request;
159 request.caPrefix = Name("/ndn/site1");
160 request.requestId = requestId;
161 request.requestType = RequestType::NEW;
162 request.status = Status::CHALLENGE;
163 request.cert = cert;
164 request.challengeType = "email";
165 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
166 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800167
tylerliu50d679e2020-10-14 14:08:39 -0700168 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
169 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
170 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800171
172 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700173 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800174
tylerliu7b9185c2020-11-24 12:15:18 -0800175 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::WRONG_CODE);
176 BOOST_CHECK(request.status == Status::CHALLENGE);
177 BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800178}
179
180BOOST_AUTO_TEST_SUITE_END()
181
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700182} // namespace tests
183} // namespace ndncert
184} // namespace ndn