blob: 61167d049820606062801cad2ccd28c2cde88653 [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/*
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, 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"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070022#include "test-common.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080023
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028BOOST_FIXTURE_TEST_SUITE(TestChallengePin, IdentityManagementFixture)
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{
38 auto identity = addIdentity(Name("/ndn/site1"));
39 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;
tylerliu50d679e2020-10-14 14:08:39 -070049 challenge.handleChallengeRequest(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{
58 auto identity = addIdentity(Name("/ndn/site1"));
59 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
tylerliu50d679e2020-10-14 14:08:39 -070074 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
75 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
76 paramTLV.push_back(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{
87 auto identity = addIdentity(Name("/ndn/site1"));
88 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
tylerliu50d679e2020-10-14 14:08:39 -0700103 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
104 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
105 paramTLV.push_back(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
115BOOST_AUTO_TEST_SUITE_END()
116
117} // namespace tests
118} // namespace ndncert
119} // namespace ndn