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 | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 21 | #include "challenge-module/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 | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 48 | CertificateRequest request(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 49 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 50 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 51 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_EMAIL)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 52 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs.ucla.edu")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 53 | |
| 54 | ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 55 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 56 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 57 | BOOST_CHECK(request.m_status == Status::CHALLENGE); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 58 | BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengeEmail::NEED_CODE); |
| 59 | BOOST_CHECK(request.m_challengeState->m_secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE) != ""); |
| 60 | BOOST_CHECK(request.m_challengeState->m_remainingTime.count() != 0); |
| 61 | BOOST_CHECK(request.m_challengeState->m_remainingTries != 0); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(request.m_challengeType, "email"); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 63 | |
| 64 | std::string line = ""; |
| 65 | std::string delimiter = " "; |
| 66 | std::ifstream emailFile("tmp.txt"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 67 | if (emailFile.is_open()) { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 68 | getline(emailFile, line); |
| 69 | emailFile.close(); |
| 70 | } |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 71 | int end = line.find(delimiter); |
| 72 | std::string recipientEmail = line.substr(0, end); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 74 | line = line.substr(end + 1); |
| 75 | |
| 76 | end = line.find(delimiter); |
| 77 | std::string secret = line.substr(0, end); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 78 | auto stored_secret = request.m_challengeState->m_secrets.get<std::string>(ChallengeEmail::PARAMETER_KEY_CODE); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(secret, stored_secret); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 80 | line = line.substr(end + 1); |
| 81 | |
| 82 | end = line.find(delimiter); |
| 83 | std::string caName = line.substr(0, end); |
| 84 | BOOST_CHECK_EQUAL(caName, Name("/ndn/site1")); |
| 85 | line = line.substr(end + 1); |
| 86 | |
| 87 | std::string certName = line; |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(certName, cert.getName()); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 89 | std::remove("tmp.txt"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 93 | { |
| 94 | auto identity = addIdentity(Name("/ndn/site1")); |
| 95 | auto key = identity.getDefaultKey(); |
| 96 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | c87d52b | 2020-09-28 22:07:18 -0700 | [diff] [blame] | 97 | CertificateRequest request(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 98 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 99 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 100 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_EMAIL)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 101 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi@cs")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 102 | |
| 103 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 104 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 105 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 106 | BOOST_CHECK(request.m_status == Status::FAILURE); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 110 | { |
| 111 | auto identity = addIdentity(Name("/ndn/site1")); |
| 112 | auto key = identity.getDefaultKey(); |
| 113 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 114 | JsonSection json; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 115 | json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 116 | CertificateRequest request(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert, |
| 117 | "email", ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 118 | 3, time::seconds(3600), std::move(json)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 119 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 120 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 121 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_CODE)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 122 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "4567")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 123 | |
| 124 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 125 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 126 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 127 | BOOST_CHECK(request.m_status == Status::PENDING); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 128 | BOOST_CHECK(!request.m_challengeState); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 132 | { |
| 133 | auto identity = addIdentity(Name("/ndn/site1")); |
| 134 | auto key = identity.getDefaultKey(); |
| 135 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 136 | JsonSection json; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 137 | json.put(ChallengeEmail::PARAMETER_KEY_CODE, "4567"); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 138 | CertificateRequest request(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert, |
| 139 | "email", ChallengeEmail::NEED_CODE, time::system_clock::now(), |
| 140 | 3, time::seconds(3600), std::move(json)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 141 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 142 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 143 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengeEmail::PARAMETER_KEY_CODE)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 144 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "7890")); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 145 | |
| 146 | ChallengeEmail challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 147 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 148 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 149 | BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengeEmail::WRONG_CODE); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 150 | BOOST_CHECK(request.m_status == Status::CHALLENGE); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 151 | BOOST_CHECK_EQUAL(request.m_challengeState->m_secrets.empty(), false); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_SUITE_END() |
| 155 | |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame^] | 156 | } // namespace tests |
| 157 | } // namespace ndncert |
| 158 | } // namespace ndn |