blob: 49e3e2959e5e3679d7017d15ca90da792a3ed826 [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
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070023#include "test-common.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080024
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040025namespace ndncert::tests {
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080026
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070027BOOST_FIXTURE_TEST_SUITE(TestChallengePin, IdentityManagementFixture)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080028
Davide Pesavento914d05f2019-07-13 16:20:19 -040029BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080030{
31 ChallengePin challenge;
Zhiyi Zhang36706832019-07-04 21:33:03 -070032 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080033}
34
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070035BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080036{
37 auto identity = addIdentity(Name("/ndn/site1"));
38 auto key = identity.getDefaultKey();
39 auto cert = key.getDefaultCertificate();
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080040 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 Zhang65ba9322017-01-19 14:15:03 -080046
47 ChallengePin challenge;
Davide Pesavento0dc02012021-11-23 22:55:03 -050048 challenge.handleChallengeRequest(ndn::makeEmptyBlock(tlv::EncryptedPayload), request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080049
tylerliu7b9185c2020-11-24 12:15:18 -080050 BOOST_CHECK(request.status == Status::CHALLENGE);
51 BOOST_CHECK_EQUAL(request.challengeState->challengeStatus, ChallengePin::NEED_CODE);
52 BOOST_CHECK_EQUAL(request.challengeType, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080053}
54
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080056{
57 auto identity = addIdentity(Name("/ndn/site1"));
58 auto key = identity.getDefaultKey();
59 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070061 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080062 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 Zhang65ba9322017-01-19 14:15:03 -080072
Davide Pesavento0dc02012021-11-23 22:55:03 -050073 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 Zhang65ba9322017-01-19 14:15:03 -080076
77 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -070078 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080079
tylerliu7b9185c2020-11-24 12:15:18 -080080 BOOST_CHECK(request.status == Status::PENDING);
81 BOOST_CHECK(!request.challengeState);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080082}
83
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070084BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode)
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080085{
86 auto identity = addIdentity(Name("/ndn/site1"));
87 auto key = identity.getDefaultKey();
88 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070090 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang1f5e86e2020-12-04 15:07:57 -080091 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 Zhang0df767e2017-02-21 16:05:36 -0800101
Davide Pesavento0dc02012021-11-23 22:55:03 -0500102 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 Zhang0df767e2017-02-21 16:05:36 -0800105
106 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -0700107 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -0800108
tylerliu7b9185c2020-11-24 12:15:18 -0800109 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 Zhang65ba9322017-01-19 14:15:03 -0800112}
113
Davide Pesavento0dc02012021-11-23 22:55:03 -0500114BOOST_AUTO_TEST_SUITE_END() // TestChallengePin
Zhiyi Zhang65ba9322017-01-19 14:15:03 -0800115
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400116} // namespace ndncert::tests