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 | /* |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2021, 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 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 24 | namespace ndncert { |
| 25 | namespace tests { |
| 26 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 27 | BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 29 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 30 | { |
| 31 | ChallengeEmail challenge; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 32 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "email"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(EmailAddressChecker) |
| 36 | { |
| 37 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true); |
| 38 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false); |
| 39 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false); |
| 40 | } |
| 41 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 43 | { |
| 44 | auto identity = addIdentity(Name("/ndn/site1")); |
| 45 | auto key = identity.getDefaultKey(); |
| 46 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 47 | RequestId requestId = {{101}}; |
| 48 | ca::RequestState request; |
| 49 | request.caPrefix = Name("/ndn/site1"); |
| 50 | request.requestId = requestId; |
| 51 | request.requestType = RequestType::NEW; |
| 52 | request.cert = cert; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 53 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 54 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 55 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL)); |
| 56 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi@cs.ucla.edu")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 57 | |
| 58 | ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 59 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 60 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 61 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 62 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::NEED_CODE); |
| 63 | BOOST_CHECK(request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != ""); |
| 64 | BOOST_CHECK(request.challengeState->remainingTime.count() != 0); |
| 65 | BOOST_CHECK(request.challengeState->remainingTries != 0); |
| 66 | BOOST_CHECK_EQUAL(request.challengeType, "email"); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 67 | |
| 68 | std::string line = ""; |
| 69 | std::string delimiter = " "; |
| 70 | std::ifstream emailFile("tmp.txt"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 71 | if (emailFile.is_open()) { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 72 | getline(emailFile, line); |
| 73 | emailFile.close(); |
| 74 | } |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 75 | int end = line.find(delimiter); |
| 76 | std::string recipientEmail = line.substr(0, end); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 78 | line = line.substr(end + 1); |
| 79 | |
| 80 | end = line.find(delimiter); |
| 81 | std::string secret = line.substr(0, end); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 82 | 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] | 83 | BOOST_CHECK_EQUAL(secret, stored_secret); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 84 | line = line.substr(end + 1); |
| 85 | |
| 86 | end = line.find(delimiter); |
| 87 | std::string caName = line.substr(0, end); |
| 88 | BOOST_CHECK_EQUAL(caName, Name("/ndn/site1")); |
| 89 | line = line.substr(end + 1); |
| 90 | |
| 91 | std::string certName = line; |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(certName, cert.getName()); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 93 | std::remove("tmp.txt"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 97 | { |
| 98 | auto identity = addIdentity(Name("/ndn/site1")); |
| 99 | auto key = identity.getDefaultKey(); |
| 100 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 101 | RequestId requestId = {{101}}; |
| 102 | ca::RequestState request; |
| 103 | request.caPrefix = Name("/ndn/site1"); |
| 104 | request.requestId = requestId; |
| 105 | request.requestType = RequestType::NEW; |
| 106 | request.cert = cert; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 107 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 108 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 109 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_EMAIL)); |
| 110 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "zhiyi@cs")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 111 | |
| 112 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 113 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 114 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(request.challengeType, "email"); |
| 116 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::INVALID_EMAIL); |
| 117 | BOOST_CHECK_EQUAL(request.challengeState->remainingTries, 2); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 121 | { |
| 122 | auto identity = addIdentity(Name("/ndn/site1")); |
| 123 | auto key = identity.getDefaultKey(); |
| 124 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 125 | JsonSection secret; |
| 126 | secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
| 127 | RequestId requestId = {{101}}; |
| 128 | ca::RequestState request; |
| 129 | request.caPrefix = Name("/ndn/site1"); |
| 130 | request.requestId = requestId; |
| 131 | request.requestType = RequestType::NEW; |
| 132 | request.status = Status::CHALLENGE; |
| 133 | request.cert = cert; |
| 134 | request.challengeType = "email"; |
| 135 | request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 136 | 3, time::seconds(3600), std::move(secret)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 137 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 138 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 139 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE)); |
| 140 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "4567")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 141 | |
| 142 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 143 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 144 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 145 | BOOST_CHECK(request.status == Status::PENDING); |
| 146 | BOOST_CHECK(!request.challengeState); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 150 | { |
| 151 | auto identity = addIdentity(Name("/ndn/site1")); |
| 152 | auto key = identity.getDefaultKey(); |
| 153 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 154 | JsonSection secret; |
| 155 | secret.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
| 156 | RequestId requestId = {{101}}; |
| 157 | ca::RequestState request; |
| 158 | request.caPrefix = Name("/ndn/site1"); |
| 159 | request.requestId = requestId; |
| 160 | request.requestType = RequestType::NEW; |
| 161 | request.status = Status::CHALLENGE; |
| 162 | request.cert = cert; |
| 163 | request.challengeType = "email"; |
| 164 | request.challengeState = ca::ChallengeState(ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 165 | 3, time::seconds(3600), std::move(secret)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 166 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 167 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 168 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengeEmail::PARAMETER_KEY_CODE)); |
| 169 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "7890")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 170 | |
| 171 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 172 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 173 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::WRONG_CODE); |
| 175 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 176 | BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 179 | BOOST_AUTO_TEST_SUITE_END() // TestChallengeEmail |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 180 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 181 | } // namespace tests |
| 182 | } // namespace ndncert |