blob: dc5b981cb91f3d7637e02fe00486c5e27f07ec69 [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento914d05f2019-07-13 16:20:19 -04002/*
Davide Pesavento0d1d11c2022-04-11 22:11:34 -04003 * Copyright (c) 2017-2022, Regents of the University of California.
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08004 *
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 Zhang84e11842020-11-19 20:03:23 -080021#include "challenge/challenge-pin.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040022
Davide Pesavento829aff62022-05-15 20:30:34 -040023#include "tests/boost-test.hpp"
24#include "tests/key-chain-fixture.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080025
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026namespace ndncert::tests {
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080027
Davide Pesavento829aff62022-05-15 20:30:34 -040028BOOST_FIXTURE_TEST_SUITE(TestChallengePin, KeyChainFixture)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080029
Davide Pesavento914d05f2019-07-13 16:20:19 -040030BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080031{
32 ChallengePin challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -070033 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080034}
35
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070036BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080037{
Davide Pesavento829aff62022-05-15 20:30:34 -040038 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080039 auto key = identity.getDefaultKey();
40 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080041 RequestId requestId = {{101}};
42 ca::RequestState request;
43 request.caPrefix = Name("/ndn/site1");
44 request.requestId = requestId;
45 request.requestType = RequestType::NEW;
46 request.cert = cert;
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080047
48 ChallengePin challenge;
Davide Pesavento0dc02012021-11-23 22:55:03 -050049 challenge.handleChallengeRequest(ndn::makeEmptyBlock(tlv::EncryptedPayload), request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080050
tylerliu7b9185c2020-11-24 12:15:18 -080051 BOOST_CHECK(request.status == Status::CHALLENGE);
52 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengePin::NEED_CODE);
53 BOOST_CHECK_EQUAL(request.challengeType, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080054}
55
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080057{
Davide Pesavento829aff62022-05-15 20:30:34 -040058 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080059 auto key = identity.getDefaultKey();
60 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070062 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080063 RequestId requestId = {{101}};
64 ca::RequestState request;
65 request.caPrefix = Name("/ndn/site1");
66 request.requestId = requestId;
67 request.requestType = RequestType::NEW;
68 request.status = Status::CHALLENGE;
69 request.cert = cert;
70 request.challengeType = "pin";
71 request.challengeState = ca::ChallengeState(ChallengePin::NEED_CODE, time::system_clock::now(),
72 3, time::seconds(3600), std::move(secret));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080073
Davide Pesavento0dc02012021-11-23 22:55:03 -050074 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
75 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
76 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "12345"));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080077
78 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -070079 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080080
tylerliu7b9185c2020-11-24 12:15:18 -080081 BOOST_CHECK(request.status == Status::PENDING);
82 BOOST_CHECK(!request.challengeState);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080083}
84
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode)
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080086{
Davide Pesavento829aff62022-05-15 20:30:34 -040087 auto identity = m_keyChain.createIdentity(Name("/ndn/site1"));
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080088 auto key = identity.getDefaultKey();
89 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070091 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080092 RequestId requestId = {{101}};
93 ca::RequestState request;
94 request.caPrefix = Name("/ndn/site1");
95 request.requestId = requestId;
96 request.requestType = RequestType::NEW;
97 request.status = Status::CHALLENGE;
98 request.cert = cert;
99 request.challengeType = "pin";
100 request.challengeState = ca::ChallengeState(ChallengePin::NEED_CODE, time::system_clock::now(),
101 3, time::seconds(3600), std::move(secret));
Zhiyi Zhang0df767e2017-02-21 16:05:36 -0800102
Davide Pesavento0dc02012021-11-23 22:55:03 -0500103 Block paramTLV = ndn::makeEmptyBlock(tlv::EncryptedPayload);
104 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
105 paramTLV.push_back(ndn::makeStringBlock(tlv::ParameterValue, "45678"));
Zhiyi Zhang0df767e2017-02-21 16:05:36 -0800106
107 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700108 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -0800109
tylerliu7b9185c2020-11-24 12:15:18 -0800110 BOOST_CHECK(request.status == Status::CHALLENGE);
111 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengePin::WRONG_CODE);
112 BOOST_CHECK_EQUAL(request.challengeState->secrets.empty(), false);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800113}
114
Davide Pesavento0dc02012021-11-23 22:55:03 -0500115BOOST_AUTO_TEST_SUITE_END() // TestChallengePin
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800116
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400117} // namespace ndncert::tests