blob: 35466ffd080ad7eaa10aa9eb5dd0ce6a860b0f7a [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/*
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2017-2019, 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 Zhang65ba9322017-01-19 14:15:03 -080021#include "challenge-module/challenge-pin.hpp"
Davide Pesavento914d05f2019-07-13 16:20:19 -040022
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070023#include "identity-management-fixture.hpp"
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080024
25namespace ndn {
26namespace ndncert {
27namespace tests {
28
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070029BOOST_FIXTURE_TEST_SUITE(TestChallengePin, IdentityManagementFixture)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080030
Davide Pesavento914d05f2019-07-13 16:20:19 -040031BOOST_AUTO_TEST_CASE(ChallengeType)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080032{
33 ChallengePin challenge;
34 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "PIN");
35}
36
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070037BOOST_AUTO_TEST_CASE(OnChallengeRequestWithEmptyInfo)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080038{
39 auto identity = addIdentity(Name("/ndn/site1"));
40 auto key = identity.getDefaultKey();
41 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080043
44 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070045 challenge.handleChallengeRequest(JsonSection(), request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080046
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070047 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 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;
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 Zhang65ba9322017-01-19 14:15:03 -080061
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070062 JsonSection paramJson;
63 paramJson.put(ChallengePin::JSON_PIN_CODE, "12345");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080064
65 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066 challenge.handleChallengeRequest(paramJson, request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080067
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068 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 Zhang65ba9322017-01-19 14:15:03 -080071}
72
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070073BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode)
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080074{
75 auto identity = addIdentity(Name("/ndn/site1"));
76 auto key = identity.getDefaultKey();
77 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070078 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 Zhang0df767e2017-02-21 16:05:36 -080082
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070083 JsonSection paramJson;
84 paramJson.put(ChallengePin::JSON_PIN_CODE, "45678");
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080085
86 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070087 challenge.handleChallengeRequest(paramJson, request);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080088
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070089 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 Zhang65ba9322017-01-19 14:15:03 -080092}
93
94BOOST_AUTO_TEST_SUITE_END()
95
96} // namespace tests
97} // namespace ndncert
98} // namespace ndn