blob: 61bcd4514e6cdc91694e5226a5997a357cbb42cc [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/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, 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"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040022
Davide Pesavento829aff62022-05-15 20:30:34 -040023#include "tests/boost-test.hpp"
24#include "tests/key-chain-fixture.hpp"
25
26#include <fstream>
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080027
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040028namespace ndncert::tests {
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080029
Davide Pesavento829aff62022-05-15 20:30:34 -040030BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, KeyChainFixture)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080031
Davide Pesavento914d05f2019-07-13 16:20:19 -040032BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080033{
34 ChallengeEmail challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -070035 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "email");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080036}
37
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080038BOOST_AUTO_TEST_CASE(EmailAddressChecker)
39{
40 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true);
41 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false);
42 BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false);
43}
44
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080046{
Davide Pesavento829aff62022-05-15 20:30:34 -040047 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080048 auto key = identity.getDefaultKey();
49 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080050 RequestId requestId = {{101}};
51 ca::RequestState request;
52 request.caPrefix = Name("/ndn/site1");
53 request.requestId = requestId;
54 request.requestType = RequestType::NEW;
55 request.cert = cert;
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080056
Davide Pesavento0dc02012021-11-23 22:55:03 -050057 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
58 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
59 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080060
61 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070062 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080063
tylerliu7b9185c2020-11-24 12:15:18 -080064 BOOST_CHECK(request.status == Status::CHALLENGE);
65 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::NEED_CODE);
66 BOOST_CHECK(request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != "");
67 BOOST_CHECK(request.challengeState->remainingTime.count() != 0);
68 BOOST_CHECK(request.challengeState->remainingTries != 0);
69 BOOST_CHECK_EQUAL(request.challengeType, "email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070070
71 std::string line = "";
72 std::string delimiter = " ";
73 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070074 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070075 getline(emailFile, line);
76 emailFile.close();
77 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070078 int end = line.find(delimiter);
79 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070080 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070081 line = line.substr(end + 1);
82
83 end = line.find(delimiter);
84 std::string secret = line.substr(0, end);
tylerliu7b9185c2020-11-24 12:15:18 -080085 auto stored_secret = request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070086 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070087 line = line.substr(end + 1);
88
89 end = line.find(delimiter);
90 std::string caName = line.substr(0, end);
91 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
92 line = line.substr(end + 1);
93
94 std::string certName = line;
Davide Pesavento914d05f2019-07-13 16:20:19 -040095 BOOST_CHECK_EQUAL(certName, cert.getName());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070096 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080097}
98
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800100{
Davide Pesavento829aff62022-05-15 20:30:34 -0400101 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800102 auto key = identity.getDefaultKey();
103 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800104 JsonSection secret;
105 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
106 RequestId requestId = {{101}};
107 ca::RequestState request;
108 request.caPrefix = Name("/ndn/site1");
109 request.requestId = requestId;
110 request.requestType = RequestType::NEW;
111 request.status = Status::CHALLENGE;
112 request.cert = cert;
113 request.challengeType = "email";
114 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
115 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800116
Davide Pesavento0dc02012021-11-23 22:55:03 -0500117 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
118 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
119 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800120
121 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700122 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800123
tylerliu7b9185c2020-11-24 12:15:18 -0800124 BOOST_CHECK(request.status == Status::PENDING);
125 BOOST_CHECK(!request.challengeState);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800126}
127
128BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
129{
Davide Pesavento829aff62022-05-15 20:30:34 -0400130 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800131 auto key = identity.getDefaultKey();
132 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -0800133 JsonSection secret;
134 secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
135 RequestId requestId = {{101}};
136 ca::RequestState request;
137 request.caPrefix = Name("/ndn/site1");
138 request.requestId = requestId;
139 request.requestType = RequestType::NEW;
140 request.status = Status::CHALLENGE;
141 request.cert = cert;
142 request.challengeType = "email";
143 request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(),
144 3, time::seconds(3600), std::move(secret));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800145
Davide Pesavento0dc02012021-11-23 22:55:03 -0500146 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
147 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
148 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800149
150 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700151 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800152
tylerliu7b9185c2020-11-24 12:15:18 -0800153 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::WRONG_CODE);
154 BOOST_CHECK(request.status == Status::CHALLENGE);
155 BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800156}
157
Davide Pesavento0dc02012021-11-23 22:55:03 -0500158BOOST_AUTO_TEST_SUITE_END() // TestChallengeEmail
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800159
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400160} // namespace ndncert::tests