blob: 95c7259aafc2aeac834c77f5d9c316b55f409bdd [file] [log] [blame]
Zhiyi Zhang65ba9322017-01-19 14:15:03 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070022#include "identity-management-fixture.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
30BOOST_AUTO_TEST_CASE(TestGetInitInfo)
31{
32 ChallengePin challenge;
33 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "PIN");
34}
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 Zhangaf7c2902019-03-14 22:13:21 -070041 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_BEFORE_CHALLENGE, cert);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080042
43 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 challenge.handleChallengeRequest(JsonSection(), request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080045
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070046 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
47 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengePin::NEED_CODE);
48 BOOST_CHECK_EQUAL(request.m_challengeType, "PIN");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080049}
50
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051BOOST_AUTO_TEST_CASE(OnChallengeRequestWithCode)
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080052{
53 auto identity = addIdentity(Name("/ndn/site1"));
54 auto key = identity.getDefaultKey();
55 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070056 JsonSection secret;
57 secret.add(ChallengePin::JSON_PIN_CODE, "12345");
58 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengePin::NEED_CODE, "PIN",
59 time::toIsoString(time::system_clock::now()), 3600, 3, secret, cert);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080060
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061 JsonSection paramJson;
62 paramJson.put(ChallengePin::JSON_PIN_CODE, "12345");
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080063
64 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070065 challenge.handleChallengeRequest(paramJson, request);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080066
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067 BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING);
68 BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
69 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), true);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080070}
71
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070072BOOST_AUTO_TEST_CASE(OnChallengeRequestWithWrongCode)
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080073{
74 auto identity = addIdentity(Name("/ndn/site1"));
75 auto key = identity.getDefaultKey();
76 auto cert = key.getDefaultCertificate();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070077 JsonSection secret;
78 secret.add(ChallengePin::JSON_PIN_CODE, "12345");
79 CertificateRequest request(Name("/ndn/site1"), "123", STATUS_CHALLENGE, ChallengePin::NEED_CODE, "PIN",
80 time::toIsoString(time::system_clock::now()), 3600, 3, secret, cert);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080081
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070082 JsonSection paramJson;
83 paramJson.put(ChallengePin::JSON_PIN_CODE, "45678");
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080084
85 ChallengePin challenge;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 challenge.handleChallengeRequest(paramJson, request);
Zhiyi Zhang0df767e2017-02-21 16:05:36 -080087
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070088 BOOST_CHECK_EQUAL(request.m_status, STATUS_CHALLENGE);
89 BOOST_CHECK_EQUAL(request.m_challengeStatus, ChallengePin::WRONG_CODE);
90 BOOST_CHECK_EQUAL(request.m_challengeSecrets.empty(), false);
Zhiyi Zhang65ba9322017-01-19 14:15:03 -080091}
92
93BOOST_AUTO_TEST_SUITE_END()
94
95} // namespace tests
96} // namespace ndncert
97} // namespace ndn