Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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" |
Zhiyi Zhang | 576aad1 | 2017-10-03 15:41:53 -0700 | [diff] [blame] | 22 | #include "identity-management-fixture.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 | |
| 30 | BOOST_AUTO_TEST_CASE(TestChallengeType) |
| 31 | { |
| 32 | ChallengeEmail challenge; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 49 | |
| 50 | JsonSection emailJson; |
| 51 | emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs.ucla.edu"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 52 | |
| 53 | ChallengeEmail challenge("./tests/unit-tests/test-send-email.sh"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 54 | challenge.handleChallengeRequest(emailJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 55 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 57 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::NEED_CODE); |
| 58 | BOOST_CHECK(request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_CODE) != ""); |
| 59 | BOOST_CHECK(request.m_remainingTime != 0); |
| 60 | BOOST_CHECK(request.m_remainingTries != 0); |
| 61 | BOOST_CHECK(request.m_challengeTp != ""); |
| 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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 78 | auto stored_secret = request.m_challengeSecrets.get<std::string>(ChallengeEmail::JSON_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; |
| 88 | BOOST_CHECK_EQUAL(certName, cert.getName().toUri()); |
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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 97 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 98 | |
| 99 | JsonSection emailJson; |
| 100 | emailJson.put(ChallengeEmail::JSON_EMAIL, "zhiyi@cs"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 101 | |
| 102 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 103 | challenge.handleChallengeRequest(emailJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 104 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::FAILURE_INVALID_EMAIL); |
| 106 | BOOST_CHECK_EQUAL(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 | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 115 | json.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 116 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE, |
| 117 | "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 118 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 119 | JsonSection requestJson; |
| 120 | requestJson.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 121 | |
| 122 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | challenge.handleChallengeRequest(requestJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 124 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 126 | BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING); |
| 127 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 131 | { |
| 132 | auto identity = addIdentity(Name("/ndn/site1")); |
| 133 | auto key = identity.getDefaultKey(); |
| 134 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 135 | JsonSection json; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 136 | json.put(ChallengeEmail::JSON_CODE, "4567"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 137 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengeEmail::NEED_CODE, |
| 138 | "Email", time::toIsoString(time::system_clock::now()), 3600, 3, json, cert); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 139 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 140 | JsonSection requestJson; |
| 141 | requestJson.put(ChallengeEmail::JSON_CODE, "7890"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 142 | |
| 143 | ChallengeEmail challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 144 | challenge.handleChallengeRequest(requestJson, request); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 145 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengeEmail::WRONG_CODE); |
| 147 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 148 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_SUITE_END() |
| 152 | |
| 153 | } // namespace tests |
| 154 | } // namespace ndncert |
| 155 | } // namespace ndn |