Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 4 | * |
| 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 Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 21 | #include "challenge/challenge-email.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 22 | #include "test-common.hpp" |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | namespace tests { |
| 27 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 28 | BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 31 | { |
| 32 | ChallengeEmail challenge; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 33 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "email"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 36 | BOOST_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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 44 | { |
| 45 | auto identity = addIdentity(Name("/ndn/site1")); |
| 46 | auto key = identity.getDefaultKey(); |
| 47 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 48 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 54 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 55 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 58 | |
| 59 | ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 60 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 61 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 62 | 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 Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 68 | |
| 69 | std::string line = ""; |
| 70 | std::string delimiter = " "; |
| 71 | std::ifstream emailFile("tmp.txt"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 72 | if (emailFile.is_open()) { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 73 | getline(emailFile, line); |
| 74 | emailFile.close(); |
| 75 | } |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 76 | int end = line.find(delimiter); |
| 77 | std::string recipientEmail = line.substr(0, end); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 79 | line = line.substr(end + 1); |
| 80 | |
| 81 | end = line.find(delimiter); |
| 82 | std::string secret = line.substr(0, end); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 83 | auto stored_secret = request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(secret, stored_secret); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 85 | 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 Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(certName, cert.getName()); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 94 | std::remove("tmp.txt"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 98 | { |
| 99 | auto identity = addIdentity(Name("/ndn/site1")); |
| 100 | auto key = identity.getDefaultKey(); |
| 101 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 102 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 108 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 109 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 112 | |
| 113 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 114 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 115 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 116 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 121 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 122 | { |
| 123 | auto identity = addIdentity(Name("/ndn/site1")); |
| 124 | auto key = identity.getDefaultKey(); |
| 125 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 126 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 138 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 139 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 142 | |
| 143 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 144 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 145 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 146 | BOOST_CHECK(request.status == Status::PENDING); |
| 147 | BOOST_CHECK(!request.challengeState); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 151 | { |
| 152 | auto identity = addIdentity(Name("/ndn/site1")); |
| 153 | auto key = identity.getDefaultKey(); |
| 154 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 155 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 167 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 168 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 171 | |
| 172 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 173 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 174 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 175 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() |
| 181 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 182 | } // namespace tests |
| 183 | } // namespace ndncert |
| 184 | } // namespace ndn |