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