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 | /* |
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 | 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 | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 21 | #include "challenge-module/challenge-pin.hpp" |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame^] | 22 | |
Zhiyi Zhang | 8ce677b | 2018-07-13 14:44:06 -0700 | [diff] [blame] | 23 | #include "identity-management-fixture.hpp" |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -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(TestChallengePin, IdentityManagementFixture) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame^] | 31 | BOOST_AUTO_TEST_CASE(ChallengeType) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 32 | { |
| 33 | ChallengePin challenge; |
| 34 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "PIN"); |
| 35 | } |
| 36 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 38 | { |
| 39 | auto identity = addIdentity(Name("/ndn/site1")); |
| 40 | auto key = identity.getDefaultKey(); |
| 41 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 42 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 43 | |
| 44 | ChallengePin challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 45 | challenge.handleChallengeRequest(JsonSection(), request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 46 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 48 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengePin::NEED_CODE); |
| 49 | BOOST_CHECK_EQUAL(request.m_challengeType, "PIN"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode) |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 53 | { |
| 54 | auto identity = addIdentity(Name("/ndn/site1")); |
| 55 | auto key = identity.getDefaultKey(); |
| 56 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 57 | JsonSection secret; |
| 58 | secret.add(ChallengePin::JSON_PIN_CODE, "12345"); |
| 59 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengePin::NEED_CODE, "PIN", |
| 60 | time::toIsoString(time::system_clock::now()), 3600, 3, secret, cert); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 61 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 62 | JsonSection paramJson; |
| 63 | paramJson.put(ChallengePin::JSON_PIN_CODE, "12345"); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 64 | |
| 65 | ChallengePin challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 66 | challenge.handleChallengeRequest(paramJson, request); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 67 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING); |
| 69 | BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 70 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true); |
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; |
| 79 | secret.add(ChallengePin::JSON_PIN_CODE, "12345"); |
| 80 | CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengePin::NEED_CODE, "PIN", |
| 81 | time::toIsoString(time::system_clock::now()), 3600, 3, secret, cert); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 82 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 83 | JsonSection paramJson; |
| 84 | paramJson.put(ChallengePin::JSON_PIN_CODE, "45678"); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 85 | |
| 86 | ChallengePin challenge; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 87 | challenge.handleChallengeRequest(paramJson, request); |
Zhiyi Zhang | 0df767e | 2017-02-21 16:05:36 -0800 | [diff] [blame] | 88 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE); |
| 90 | BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengePin::WRONG_CODE); |
| 91 | BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false); |
Zhiyi Zhang | 65ba932 | 2017-01-19 14:15:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_SUITE_END() |
| 95 | |
| 96 | } // namespace tests |
| 97 | } // namespace ndncert |
| 98 | } // namespace ndn |