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 | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 48 | RequestId requestId = {{1,2,3,4,5,6,7,8}}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 49 | std::array<uint8_t, 16> aesKey; |
| 50 | ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert, std::move(aesKey)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 51 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 52 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 55 | |
| 56 | ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 57 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 58 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 59 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 60 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::NEED_CODE); |
| 61 | BOOST_CHECK(request.challengeState->secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != ""); |
| 62 | BOOST_CHECK(request.challengeState->remainingTime.count() != 0); |
| 63 | BOOST_CHECK(request.challengeState->remainingTries != 0); |
| 64 | BOOST_CHECK_EQUAL(request.challengeType, "email"); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 65 | |
| 66 | std::string line = ""; |
| 67 | std::string delimiter = " "; |
| 68 | std::ifstream emailFile("tmp.txt"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 69 | if (emailFile.is_open()) { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 70 | getline(emailFile, line); |
| 71 | emailFile.close(); |
| 72 | } |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 73 | int end = line.find(delimiter); |
| 74 | std::string recipientEmail = line.substr(0, end); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 76 | line = line.substr(end + 1); |
| 77 | |
| 78 | end = line.find(delimiter); |
| 79 | std::string secret = line.substr(0, end); |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 80 | 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] | 81 | BOOST_CHECK_EQUAL(secret, stored_secret); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 82 | 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 Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(certName, cert.getName()); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 91 | std::remove("tmp.txt"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 95 | { |
| 96 | auto identity = addIdentity(Name("/ndn/site1")); |
| 97 | auto key = identity.getDefaultKey(); |
| 98 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 99 | RequestId requestId = {{1,2,3,4,5,6,7,8}}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 100 | std::array<uint8_t, 16> aesKey; |
| 101 | ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert, std::move(aesKey)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 102 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 103 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 106 | |
| 107 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 108 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 109 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 110 | BOOST_CHECK_EQUAL(request.challengeType, "email"); |
| 111 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::INVALID_EMAIL); |
| 112 | BOOST_CHECK_EQUAL(request.challengeState->remainingTries, 2); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 116 | { |
| 117 | auto identity = addIdentity(Name("/ndn/site1")); |
| 118 | auto key = identity.getDefaultKey(); |
| 119 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 120 | JsonSection json; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 121 | json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 122 | RequestId requestId = {{1,2,3,4,5,6,7,8}}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 123 | std::array<uint8_t, 16> aesKey; |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 124 | ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 125 | "email", ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 126 | 3, time::seconds(3600), std::move(json), std::move(aesKey), 0); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 127 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 128 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 131 | |
| 132 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 133 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 134 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 135 | BOOST_CHECK(request.status == Status::PENDING); |
| 136 | BOOST_CHECK(!request.challengeState); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 140 | { |
| 141 | auto identity = addIdentity(Name("/ndn/site1")); |
| 142 | auto key = identity.getDefaultKey(); |
| 143 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 144 | JsonSection json; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 145 | json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 146 | RequestId requestId = {{1,2,3,4,5,6,7,8}}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 147 | std::array<uint8_t, 16> aesKey; |
Zhiyi Zhang | 32d4b4e | 2020-10-28 22:10:49 -0700 | [diff] [blame] | 148 | ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert, |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 149 | "email", ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 150 | 3, time::seconds(3600), std::move(json), std::move(aesKey), 0); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 151 | |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 152 | 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 Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 155 | |
| 156 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 157 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 158 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame^] | 159 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengeEmail::WRONG_CODE); |
| 160 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 161 | BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_SUITE_END() |
| 165 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 166 | } // namespace tests |
| 167 | } // namespace ndncert |
| 168 | } // namespace ndn |