blob: a82c1b1193b0b3d86fe9b0f3358ea44185bda0bf [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 Zhangdbd9d432020-10-07 15:56:27 -070021#include "identity-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 Zhang8fdb36b2020-10-18 11:58:51 -070041 RequestID requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070042 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::BEFORE_CHALLENGE, cert, makeEmptyBlock(ndn::tlv::ContentType_Key));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080043
44 ChallengePin challenge;
tylerliu50d679e2020-10-14 14:08:39 -070045 challenge.handleChallengeRequest(makeEmptyBlock(tlv::EncryptedPayload), request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080046
Zhiyi Zhang48f23782020-09-28 12:11:24 -070047 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070048 BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengePin::NEED_CODE);
Zhiyi Zhang36706832019-07-04 21:33:03 -070049 BOOST_CHECK_EQUAL(request.m_challengeType, "pin");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080050}
51
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070052BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080053{
54 auto identity = addIdentity(Name("/ndn/site1"));
55 auto key = identity.getDefaultKey();
56 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070057 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070058 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070059 RequestID requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070060 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert,
tylerliu8704d032020-06-23 10:18:15 -070061 "pin", ChallengePin::NEED_CODE, time::system_clock::now(),
Zhiyi Zhang222810b2020-10-16 21:50:35 -070062 3, time::seconds(3600), std::move(secret), makeEmptyBlock(ndn::tlv::ContentType_Key), 0);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080063
tylerliu50d679e2020-10-14 14:08:39 -070064 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
65 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
66 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "12345"));
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080067
68 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -070069 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080070
Zhiyi Zhang48f23782020-09-28 12:11:24 -070071 BOOST_CHECK(request.m_status == Status::PENDING);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070072 BOOST_CHECK(!request.m_challengeState);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080073}
74
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070075BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode)
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080076{
77 auto identity = addIdentity(Name("/ndn/site1"));
78 auto key = identity.getDefaultKey();
79 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 JsonSection secret;
Zhiyi Zhang46049832020-09-28 17:08:12 -070081 secret.add(ChallengePin::PARAMETER_KEY_CODE, "12345");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070082 RequestID requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070083 ca::RequestState request(Name("/ndn/site1"), requestId, RequestType::NEW, Status::CHALLENGE, cert,
tylerliu8704d032020-06-23 10:18:15 -070084 "pin", ChallengePin::NEED_CODE, time::system_clock::now(),
Zhiyi Zhang222810b2020-10-16 21:50:35 -070085 3, time::seconds(3600), std::move(secret), makeEmptyBlock(ndn::tlv::ContentType_Key), 0);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080086
tylerliu50d679e2020-10-14 14:08:39 -070087 Block paramTLV = makeEmptyBlock(tlv::EncryptedPayload);
88 paramTLV.push_back(makeStringBlock(tlv::ParameterKey, ChallengePin::PARAMETER_KEY_CODE));
89 paramTLV.push_back(makeStringBlock(tlv::ParameterValue, "45678"));
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080090
91 ChallengePin challenge;
Suyong Won44d0cce2020-05-10 04:07:43 -070092 challenge.handleChallengeRequest(paramTLV, request);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080093
Zhiyi Zhang48f23782020-09-28 12:11:24 -070094 BOOST_CHECK(request.m_status == Status::CHALLENGE);
Zhiyi Zhanga749f442020-09-29 17:19:51 -070095 BOOST_CHECK_EQUAL(request.m_challengeState->m_challengeStatus, ChallengePin::WRONG_CODE);
96 BOOST_CHECK_EQUAL(request.m_challengeState->m_secrets.empty(), false);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080097}
98
99BOOST_AUTO_TEST_SUITE_END()
100
101} // namespace tests
102} // namespace ndncert
103} // namespace ndn