blob: d397e58be19753578e688bcc8418dc628c325485 [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 Zhangdefa9592017-02-21 10:56:22 -080021#include "challenge-module/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 Zhang48f23782020-09-28 12:11:24 -070048 CertificateRequest request(Name("/ndn/site1"), "123", REQUEST_TYPE_NEW, Status::BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080049
Suyong Won44d0cce2020-05-10 04:07:43 -070050 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -070051 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_EMAIL));
Suyong Won44d0cce2020-05-10 04:07:43 -070052 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080053
54 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070055 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080056
Zhiyi Zhang48f23782020-09-28 12:11:24 -070057 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070058 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::NEED_CODE);
Zhiyi Zhang46049832020-09-28 17:08:12 -070059 BOOST_CHECK(request.m_challengeSecrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != "");
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 BOOST_CHECK(request.m_remainingTime != 0);
61 BOOST_CHECK(request.m_remainingTries != 0);
62 BOOST_CHECK(request.m_challengeTp != "");
Zhiyi Zhang36706832019-07-04 21:33:03 -070063 BOOST_CHECK_EQUAL(request.m_challengeType, "email");
Zhiyi Zhang576aad12017-10-03 15:41:53 -070064
65 std::string line = "";
66 std::string delimiter = " ";
67 std::ifstream emailFile("tmp.txt");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070068 if (emailFile.is_open()) {
Zhiyi Zhang576aad12017-10-03 15:41:53 -070069 getline(emailFile, line);
70 emailFile.close();
71 }
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070072 int end = line.find(delimiter);
73 std::string recipientEmail = line.substr(0, end);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070074 BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu");
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070075 line = line.substr(end + 1);
76
77 end = line.find(delimiter);
78 std::string secret = line.substr(0, end);
Zhiyi Zhang46049832020-09-28 17:08:12 -070079 auto stored_secret = request.m_challengeSecrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE);
Zhiyi Zhang576aad12017-10-03 15:41:53 -070080 BOOST_CHECK_EQUAL(secret, stored_secret);
Zhiyi Zhang70d74b42019-06-11 22:27:07 -070081 line = line.substr(end + 1);
82
83 end = line.find(delimiter);
84 std::string caName = line.substr(0, end);
85 BOOST_CHECK_EQUAL(caName, Name("/ndn/site1"));
86 line = line.substr(end + 1);
87
88 std::string certName = line;
Davide Pesavento914d05f2019-07-13 16:20:19 -040089 BOOST_CHECK_EQUAL(certName, cert.getName());
Zhiyi Zhang576aad12017-10-03 15:41:53 -070090 std::remove("tmp.txt");
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080091}
92
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070093BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080094{
95 auto identity = addIdentity(Name("/ndn/site1"));
96 auto key = identity.getDefaultKey();
97 auto cert = key.getDefaultCertificate();
Zhiyi Zhang48f23782020-09-28 12:11:24 -070098 CertificateRequest request(Name("/ndn/site1"), "123", REQUEST_TYPE_NEW, Status::BEFORE_CHALLENGE, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080099
Suyong Won44d0cce2020-05-10 04:07:43 -0700100 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700101 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_EMAIL));
Suyong Won44d0cce2020-05-10 04:07:43 -0700102 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800103
104 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700105 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800106
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700107 BOOST_CHECK(request.m_status == Status::FAILURE);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800108}
109
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800111{
112 auto identity = addIdentity(Name("/ndn/site1"));
113 auto key = identity.getDefaultKey();
114 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800115 JsonSection json;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700116 json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700117 CertificateRequest request(Name("/ndn/site1"), "123", REQUEST_TYPE_NEW, Status::CHALLENGE, ChallengeEmail::NEED_CODE,
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700118 "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800119
Suyong Won44d0cce2020-05-10 04:07:43 -0700120 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700121 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_CODE));
Suyong Won44d0cce2020-05-10 04:07:43 -0700122 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800123
124 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700125 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800126
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700127 BOOST_CHECK(request.m_status == Status::PENDING);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800129}
130
131BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
132{
133 auto identity = addIdentity(Name("/ndn/site1"));
134 auto key = identity.getDefaultKey();
135 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800136 JsonSection json;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700137 json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700138 CertificateRequest request(Name("/ndn/site1"), "123", REQUEST_TYPE_NEW, Status::CHALLENGE, ChallengeEmail::NEED_CODE,
Zhiyi Zhang36706832019-07-04 21:33:03 -0700139 "email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800140
Suyong Won44d0cce2020-05-10 04:07:43 -0700141 Block paramTLV = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang46049832020-09-28 17:08:12 -0700142 paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_CODE));
Suyong Won44d0cce2020-05-10 04:07:43 -0700143 paramTLV.push_back(makeStringBlock(tlv_parameter_value, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800144
145 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700146 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800147
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700148 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::WRONG_CODE);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700149 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700150 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800151}
152
153BOOST_AUTO_TEST_SUITE_END()
154
155} // namespace tests
156} // namespace ndncert
157} // namespace ndn