blob: 34f20892e973f0d166c82201c45d8e961666f56d [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 Zhang80593022020-11-17 10:55:48 -080048 RequestId requestId = {{1,2,3,4,5,6,7,8}};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070049 std::array<uint8_t, 16> aesKey;
50 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert, std::move(aesKey));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080051
tylerliu50d679e2020-10-14 14:08:39 -070052 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
53 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
54 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi@cs.ucla.edu"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080055
56 ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh");
Suyong Won44d0cce2020-05-10 04:07:43 -070057 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -080058
Zhiyi Zhang48f23782020-09-28 12:11:24 -070059 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070060 BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengeEmail::NEED_CODE);
61 BOOST_CHECK(request.m_challengeState->m_secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != "");
62 BOOST_CHECK(request.m_challengeState->m_remainingTime.count() != 0);
63 BOOST_CHECK(request.m_challengeState->m_remainingTries != 0);
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 Zhanga749f442020-09-29 17:19:51 -070080 auto stored_secret = request.m_challengeState->m_secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_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 Zhang80593022020-11-17 10:55:48 -080099 RequestId requestId = {{1,2,3,4,5,6,7,8}};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700100 std::array<uint8_t, 16> aesKey;
101 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert, std::move(aesKey));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800102
tylerliu50d679e2020-10-14 14:08:39 -0700103 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
104 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL));
105 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "zhiyi@cs"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800106
107 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700108 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800109
Zhiyi Zhangead9f002020-10-03 15:42:52 -0700110 BOOST_CHECK_EQUAL(request.m_challengeType, "email");
111 BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengeEmail::INVALID_EMAIL);
112 BOOST_CHECK_EQUAL(request.m_challengeState->m_remainingTries, 2);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800113}
114
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800116{
117 auto identity = addIdentity(Name("/ndn/site1"));
118 auto key = identity.getDefaultKey();
119 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800120 JsonSection json;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700121 json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
Zhiyi Zhang80593022020-11-17 10:55:48 -0800122 RequestId requestId = {{1,2,3,4,5,6,7,8}};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700123 std::array<uint8_t, 16> aesKey;
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700124 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700125 "email", ChallengeEmail::NEED_CODE, time::system_clock::now(),
126 3, time::seconds(3600), std::move(json), std::move(aesKey), 0);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800127
tylerliu50d679e2020-10-14 14:08:39 -0700128 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
129 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
130 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "4567"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800131
132 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700133 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800134
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700135 BOOST_CHECK(request.m_status == Status::PENDING);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700136 BOOST_CHECK(!request.m_challengeState);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800137}
138
139BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode)
140{
141 auto identity = addIdentity(Name("/ndn/site1"));
142 auto key = identity.getDefaultKey();
143 auto cert = key.getDefaultCertificate();
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800144 JsonSection json;
Zhiyi Zhang46049832020-09-28 17:08:12 -0700145 json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567");
Zhiyi Zhang80593022020-11-17 10:55:48 -0800146 RequestId requestId = {{1,2,3,4,5,6,7,8}};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700147 std::array<uint8_t, 16> aesKey;
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700148 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert,
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -0700149 "email", ChallengeEmail::NEED_CODE, time::system_clock::now(),
150 3, time::seconds(3600), std::move(json), std::move(aesKey), 0);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800151
tylerliu50d679e2020-10-14 14:08:39 -0700152 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
153 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE));
154 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "7890"));
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800155
156 ChallengeEmail challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700157 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800158
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700159 BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengeEmail::WRONG_CODE);
Zhiyi Zhang48f23782020-09-28 12:11:24 -0700160 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhanga749f442020-09-29 17:19:51 -0700161 BOOST_CHECK_EQUAL(request.m_challengeState->m_secrets.empty(), false);
Zhiyi Zhangdefa9592017-02-21 10:56:22 -0800162}
163
164BOOST_AUTO_TEST_SUITE_END()
165
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700166} // namespace tests
167} // namespace ndncert
168} // namespace ndn