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 | /* |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, 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 | 8bd8e5b | 2020-09-29 17:40:40 -0700 | [diff] [blame] | 21 | #include "challenge-modules/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 | |
| 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(TestChallengePin, IdentityManagementFixture) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 31 | { |
| 32 | ChallengePin challenge; |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 33 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 37 | { |
| 38 | auto identity = addIdentity(Name("/ndn/site1")); |
| 39 | auto key = identity.getDefaultKey(); |
| 40 | auto cert = key.getDefaultCertificate(); |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame^] | 41 | RequestState request(Name("/ndn/site1"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, cert, makeEmptyBlock(tlv::ContentType_Key)); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 42 | |
| 43 | ChallengePin challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 44 | challenge.handleChallengeRequest(makeEmptyBlock(tlv_encrypted_payload), request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 45 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 46 | BOOST_CHECK(request.m_status == Status::CHALLENGE); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengePin::NEED_CODE); |
Zhiyi Zhang | 3670683 | 2019-07-04 21:33:03 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(request.m_challengeType, "pin"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 52 | { |
| 53 | auto identity = addIdentity(Name("/ndn/site1")); |
| 54 | auto key = identity.getDefaultKey(); |
| 55 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 56 | JsonSection secret; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 57 | secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345"); |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 58 | RequestState request(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 59 | "pin", ChallengePin::NEED_CODE, time::system_clock::now(), |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame^] | 60 | 3, time::seconds(3600), std::move(secret), makeEmptyBlock(tlv::ContentType_Key)); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 61 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 62 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 63 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengePin::PARAMETER_KEY_CODE)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 64 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "12345")); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 65 | |
| 66 | ChallengePin challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 67 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 68 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 69 | BOOST_CHECK(request.m_status == Status::PENDING); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 70 | BOOST_CHECK(!request.m_challengeState); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode) |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 74 | { |
| 75 | auto identity = addIdentity(Name("/ndn/site1")); |
| 76 | auto key = identity.getDefaultKey(); |
| 77 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 78 | JsonSection secret; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 79 | secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345"); |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame] | 80 | RequestState request(Name("/ndn/site1"), "123", RequestType::NEW, Status::CHALLENGE, cert, |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 81 | "pin", ChallengePin::NEED_CODE, time::system_clock::now(), |
tylerliu | 8e170d6 | 2020-09-30 01:31:53 -0700 | [diff] [blame^] | 82 | 3, time::seconds(3600), std::move(secret), makeEmptyBlock(tlv::ContentType_Key)); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 83 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 84 | Block paramTLV = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 85 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, ChallengePin::PARAMETER_KEY_CODE)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 86 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "45678")); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 87 | |
| 88 | ChallengePin challenge; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 89 | challenge.handleChallengeRequest(paramTLV, request); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 90 | |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 91 | BOOST_CHECK(request.m_status == Status::CHALLENGE); |
Zhiyi Zhang | a749f44 | 2020-09-29 17:19:51 -0700 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengePin::WRONG_CODE); |
| 93 | BOOST_CHECK_EQUAL(request.m_challengeState->m_secrets.empty(), false); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_SUITE_END() |
| 97 | |
| 98 | } // namespace tests |
| 99 | } // namespace ndncert |
| 100 | } // namespace ndn |