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 | /* |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, 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" |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 22 | |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 23 | #include "identity-management-fixture.hpp" |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | namespace tests { |
| 28 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 29 | BOOST_FIXTURE_TEST_SUITE(TestChallengeEmail, IdentityManagementFixture) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 31 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 32 | { |
| 33 | ChallengeEmail challenge; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame] | 34 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Email"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(EmailAddressChecker) |
| 38 | { |
| 39 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs.ucla.edu"), true); |
| 40 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi@cs"), false); |
| 41 | BOOST_CHECK_EQUAL(ChallengeEmail::isValidEmailAddress("zhiyi.ucla.edu"), false); |
| 42 | } |
| 43 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 45 | { |
| 46 | auto identity = addIdentity(Name("/ndn/site1")); |
| 47 | auto key = identity.getDefaultKey(); |
| 48 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 49 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 50 | |
| 51 | JsonSection emailJson; |
| 52 | emailJson.put(ChallengeEmail::JSON_EMAIL, "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"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | challenge.handleChallengeRequest(emailJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 56 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 58 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::NEED_CODE); |
| 59 | BOOST_CHECK(request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE) != ""); |
| 60 | BOOST_CHECK(request.m_remainingTime != 0); |
| 61 | BOOST_CHECK(request.m_remainingTries != 0); |
| 62 | BOOST_CHECK(request.m_challengeTp != ""); |
| 63 | BOOST_CHECK_EQUAL(request.m_challengeType, "Email"); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 64 | |
| 65 | std::string line = ""; |
| 66 | std::string delimiter = " "; |
| 67 | std::ifstream emailFile("tmp.txt"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 68 | if (emailFile.is_open()) { |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 69 | getline(emailFile, line); |
| 70 | emailFile.close(); |
| 71 | } |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 72 | int end = line.find(delimiter); |
| 73 | std::string recipientEmail = line.substr(0, end); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(recipientEmail, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | 70d74b4 | 2019-06-11 22:27:07 -0700 | [diff] [blame] | 75 | line = line.substr(end + 1); |
| 76 | |
| 77 | end = line.find(delimiter); |
| 78 | std::string secret = line.substr(0, end); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | auto stored_secret = request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(secret, stored_secret); |
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 caName = line.substr(0, end); |
| 85 | BOOST_CHECK_EQUAL(caName, Name("/ndn/site1")); |
| 86 | line = line.substr(end + 1); |
| 87 | |
| 88 | std::string certName = line; |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(certName, cert.getName()); |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 90 | std::remove("tmp.txt"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithInvalidEmail) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 94 | { |
| 95 | auto identity = addIdentity(Name("/ndn/site1")); |
| 96 | auto key = identity.getDefaultKey(); |
| 97 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 98 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 99 | |
| 100 | JsonSection emailJson; |
| 101 | emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 102 | |
| 103 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 104 | challenge.handleChallengeRequest(emailJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 105 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::FAILURE_INVALID_EMAIL); |
| 107 | BOOST_CHECK_EQUAL(request.m_status, STATUS_FAILURE); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 110 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 111 | { |
| 112 | auto identity = addIdentity(Name("/ndn/site1")); |
| 113 | auto key = identity.getDefaultKey(); |
| 114 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 115 | JsonSection json; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 116 | json.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 117 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE, |
| 118 | "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 119 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 120 | JsonSection requestJson; |
| 121 | requestJson.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 122 | |
| 123 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 124 | challenge.handleChallengeRequest(requestJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 125 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 127 | BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING); |
| 128 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true); |
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 | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 137 | json.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 138 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE, |
| 139 | "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 140 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | JsonSection requestJson; |
| 142 | requestJson.put(ChallengeEmail::JSON_CODE, "7890"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 143 | |
| 144 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 145 | challenge.handleChallengeRequest(requestJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 146 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::WRONG_CODE); |
| 148 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 149 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_SUITE_END() |
| 153 | |
| 154 | } // namespace tests |
| 155 | } // namespace ndncert |
| 156 | } // namespace ndn |