Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -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 | /* |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2021, Regents of the University of California. |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -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-pin.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 22 | #include "test-common.hpp" |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 23 | |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 24 | namespace ndncert { |
| 25 | namespace tests { |
| 26 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 27 | BOOST_FIXTURE_TEST_SUITE(TestChallengePin, IdentityManagementFixture) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 29 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 30 | { |
| 31 | ChallengePin challenge; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 32 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 36 | { |
| 37 | auto identity = addIdentity(Name("/ndn/site1")); |
| 38 | auto key = identity.getDefaultKey(); |
| 39 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 40 | RequestId requestId = {{101}}; |
| 41 | ca::RequestState request; |
| 42 | request.caPrefix = Name("/ndn/site1"); |
| 43 | request.requestId = requestId; |
| 44 | request.requestType = RequestType::NEW; |
| 45 | request.cert = cert; |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 46 | |
| 47 | ChallengePin challenge; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 48 | challenge.handleChallengeRequest(ndn::makeEmptyBlock(tlv::EncryptedPayload), request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 49 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 50 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 51 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengePin::NEED_CODE); |
| 52 | BOOST_CHECK_EQUAL(request.challengeType, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 56 | { |
| 57 | auto identity = addIdentity(Name("/ndn/site1")); |
| 58 | auto key = identity.getDefaultKey(); |
| 59 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 60 | JsonSection secret; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 61 | secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345"); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 62 | RequestId requestId = {{101}}; |
| 63 | ca::RequestState request; |
| 64 | request.caPrefix = Name("/ndn/site1"); |
| 65 | request.requestId = requestId; |
| 66 | request.requestType = RequestType::NEW; |
| 67 | request.status = Status::CHALLENGE; |
| 68 | request.cert = cert; |
| 69 | request.challengeType = "pin"; |
| 70 | request.challengeState = ca::ChallengeState(ChallengePin::NEED_CODE, time::system_clock::now(), |
| 71 | 3, time::seconds(3600), std::move(secret)); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 72 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 73 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 74 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE)); |
| 75 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "12345")); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 76 | |
| 77 | ChallengePin challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 78 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 79 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 80 | BOOST_CHECK(request.status == Status::PENDING); |
| 81 | BOOST_CHECK(!request.challengeState); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode) |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 85 | { |
| 86 | auto identity = addIdentity(Name("/ndn/site1")); |
| 87 | auto key = identity.getDefaultKey(); |
| 88 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | JsonSection secret; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 90 | secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345"); |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 91 | RequestId requestId = {{101}}; |
| 92 | ca::RequestState request; |
| 93 | request.caPrefix = Name("/ndn/site1"); |
| 94 | request.requestId = requestId; |
| 95 | request.requestType = RequestType::NEW; |
| 96 | request.status = Status::CHALLENGE; |
| 97 | request.cert = cert; |
| 98 | request.challengeType = "pin"; |
| 99 | request.challengeState = ca::ChallengeState(ChallengePin::NEED_CODE, time::system_clock::now(), |
| 100 | 3, time::seconds(3600), std::move(secret)); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 101 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 102 | Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload); |
| 103 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE)); |
| 104 | paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "45678")); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 105 | |
| 106 | ChallengePin challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 107 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 108 | |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 109 | BOOST_CHECK(request.status == Status::CHALLENGE); |
| 110 | BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengePin::WRONG_CODE); |
| 111 | BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 114 | BOOST_AUTO_TEST_SUITE_END() // TestChallengePin |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 115 | |
| 116 | } // namespace tests |
| 117 | } // namespace ndncert |