Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 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 | |
| 21 | #include "identity-management-fixture.hpp" |
| 22 | #include "challenge-module/challenge-pin.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndncert { |
| 26 | namespace tests { |
| 27 | |
| 28 | BOOST_FIXTURE_TEST_SUITE(TestChallengePin, IdentityManagementV2Fixture) |
| 29 | |
| 30 | BOOST_AUTO_TEST_CASE(TestGetInitInfo) |
| 31 | { |
| 32 | ChallengePin challenge; |
| 33 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "PIN"); |
| 34 | } |
| 35 | |
| 36 | BOOST_AUTO_TEST_CASE(ParseStoredSecret) |
| 37 | { |
| 38 | time::system_clock::TimePoint tp = time::fromIsoString("20170207T120000"); |
| 39 | JsonSection json; |
| 40 | json.put(ChallengePin::JSON_CODE_TP, time::toIsoString(tp)); |
| 41 | json.put(ChallengePin::JSON_PIN_CODE, "1234"); |
| 42 | json.put(ChallengePin::JSON_ATTEMPT_TIMES, std::to_string(3)); |
| 43 | |
| 44 | auto result = ChallengePin::parseStoredSecrets(json); |
| 45 | BOOST_CHECK_EQUAL(std::get<0>(result), tp); |
| 46 | BOOST_CHECK_EQUAL(std::get<1>(result), "1234"); |
| 47 | BOOST_CHECK_EQUAL(std::get<2>(result), 3); |
| 48 | } |
| 49 | |
| 50 | BOOST_AUTO_TEST_CASE(OnSelectInterestComingWithEmptyInfo) |
| 51 | { |
| 52 | auto identity = addIdentity(Name("/ndn/site1")); |
| 53 | auto key = identity.getDefaultKey(); |
| 54 | auto cert = key.getDefaultCertificate(); |
| 55 | CertificateRequest request(Name("/ndn/site1"), "123", cert); |
| 56 | |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 57 | Name interestName("/ndn/site1/CA"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 58 | interestName.append("_SELECT").append("Fake-Request-ID").append("PIN"); |
| 59 | Interest interest(interestName); |
| 60 | |
| 61 | ChallengePin challenge; |
| 62 | challenge.handleChallengeRequest(interest, request); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(request.getStatus(), ChallengePin::NEED_CODE); |
| 65 | BOOST_CHECK_EQUAL(request.getChallengeType(), "PIN"); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithCode) |
| 69 | { |
| 70 | auto identity = addIdentity(Name("/ndn/site1")); |
| 71 | auto key = identity.getDefaultKey(); |
| 72 | auto cert = key.getDefaultCertificate(); |
| 73 | CertificateRequest request(Name("/ndn/site1"), "123", cert); |
| 74 | request.setChallengeType("PIN"); |
| 75 | request.setStatus(ChallengePin::NEED_CODE); |
| 76 | |
| 77 | time::system_clock::TimePoint tp = time::system_clock::now(); |
| 78 | JsonSection json; |
| 79 | json.put(ChallengePin::JSON_CODE_TP, time::toIsoString(tp)); |
| 80 | json.put(ChallengePin::JSON_PIN_CODE, "1234"); |
| 81 | json.put(ChallengePin::JSON_ATTEMPT_TIMES, std::to_string(3)); |
| 82 | |
| 83 | request.setChallengeSecrets(json); |
| 84 | |
| 85 | JsonSection infoJson; |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 86 | infoJson.put(ChallengePin::JSON_PIN_CODE, "1234"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 87 | std::stringstream ss; |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 88 | boost::property_tree::write_json(ss, infoJson); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 89 | std::string jsonString = ss.str(); |
| 90 | Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str()); |
| 91 | |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 92 | Name interestName("/ndn/site1/CA"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 93 | interestName.append("_VALIDATE").append("Fake-Request-ID").append("PIN").append(jsonContent); |
| 94 | Interest interest(interestName); |
| 95 | |
| 96 | ChallengePin challenge; |
| 97 | challenge.handleChallengeRequest(interest, request); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS); |
| 100 | BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true); |
| 101 | } |
| 102 | |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(OnValidateInterestComingWithWrongCode) |
| 104 | { |
| 105 | auto identity = addIdentity(Name("/ndn/site1")); |
| 106 | auto key = identity.getDefaultKey(); |
| 107 | auto cert = key.getDefaultCertificate(); |
| 108 | CertificateRequest request(Name("/ndn/site1"), "123", cert); |
| 109 | request.setChallengeType("PIN"); |
| 110 | request.setStatus(ChallengePin::NEED_CODE); |
| 111 | |
| 112 | time::system_clock::TimePoint tp = time::system_clock::now(); |
| 113 | JsonSection json; |
| 114 | json.put(ChallengePin::JSON_CODE_TP, time::toIsoString(tp)); |
| 115 | json.put(ChallengePin::JSON_PIN_CODE, "1234"); |
| 116 | json.put(ChallengePin::JSON_ATTEMPT_TIMES, std::to_string(3)); |
| 117 | |
| 118 | request.setChallengeSecrets(json); |
| 119 | |
| 120 | JsonSection infoJson; |
| 121 | infoJson.put(ChallengePin::JSON_PIN_CODE, "4567"); |
| 122 | std::stringstream ss; |
| 123 | boost::property_tree::write_json(ss, infoJson); |
| 124 | std::string jsonString = ss.str(); |
| 125 | Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str()); |
| 126 | |
Zhiyi Zhang | e30eb35 | 2017-04-13 15:26:14 -0700 | [diff] [blame] | 127 | Name interestName("/ndn/site1/CA"); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 128 | interestName.append("_VALIDATE").append("Fake-Request-ID").append("PIN").append(jsonContent); |
| 129 | Interest interest(interestName); |
| 130 | |
| 131 | ChallengePin challenge; |
| 132 | challenge.handleChallengeRequest(interest, request); |
| 133 | |
| 134 | BOOST_CHECK_EQUAL(request.getStatus(), ChallengePin::WRONG_CODE); |
| 135 | BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), false); |
| 136 | } |
| 137 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 138 | BOOST_AUTO_TEST_CASE(ClientSendSelect) |
| 139 | { |
| 140 | ChallengePin challenge; |
| 141 | auto requirementList = challenge.getSelectRequirements(); |
| 142 | BOOST_CHECK_EQUAL(requirementList.size(), 0); |
| 143 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 144 | auto json = challenge.doGenSelectParamsJson(ChallengeModule::WAIT_SELECTION, requirementList); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(json.empty(), true); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE(ClientSendValidate) |
| 149 | { |
| 150 | ChallengePin challenge; |
| 151 | auto requirementList = challenge.getValidateRequirements(ChallengePin::NEED_CODE); |
| 152 | BOOST_CHECK_EQUAL(requirementList.size(), 1); |
| 153 | |
| 154 | requirementList.clear(); |
| 155 | requirementList.push_back("123"); |
| 156 | |
Zhiyi Zhang | f72c054 | 2017-03-16 14:45:30 -0700 | [diff] [blame] | 157 | auto json = challenge.doGenValidateParamsJson(ChallengePin::NEED_CODE, requirementList); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(json.empty(), false); |
| 159 | BOOST_CHECK_EQUAL(json.get<std::string>(ChallengePin::JSON_PIN_CODE), "123"); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_SUITE_END() |
| 163 | |
| 164 | } // namespace tests |
| 165 | } // namespace ndncert |
| 166 | } // namespace ndn |