blob: 8a0cfe53a08db032de4797a360ff71a3a6409742 [file] [log] [blame]
Zhiyi Zhang0a89b722017-04-28 17:56:01 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
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
21#include "identity-management-fixture.hpp"
22#include "challenge-module/challenge-credential.hpp"
23#include <ndn-cxx/security/signing-helpers.hpp>
24#include <ndn-cxx/util/io.hpp>
25
26namespace ndn {
27namespace ndncert {
28namespace tests {
29
30BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementV2Fixture)
31
32BOOST_AUTO_TEST_CASE(LoadConfig)
33{
34 ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
35 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "CREDENTIAL");
36
37 BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
38 auto cert = challenge.m_trustAnchors.front();
39 BOOST_CHECK_EQUAL(cert.getName(),
40 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
41}
42
43BOOST_AUTO_TEST_CASE(HandleSelect)
44{
45 // create trust anchor
46 ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
47 auto identity0 = addIdentity(Name("/trust"));
48 auto key0 = identity0.getDefaultKey();
49 auto trustAnchor = key0.getDefaultCertificate();
50 challenge.m_trustAnchors.front() = trustAnchor;
51
52 // create certificate request
53 auto identity = addIdentity(Name("/example"));
54 auto key = identity.getDefaultKey();
55 auto cert = key.getDefaultCertificate();
56 CertificateRequest request(Name("/example"), "123", cert);
57
58 // using trust anchor to sign cert request to get credential
59 Name credentialName = cert.getKeyName();
60 credentialName.append("Credential").appendVersion();
61 security::v2::Certificate credential = cert;
62 credential.setName(credentialName);
63 credential.setContent(cert.getContent());
64 m_keyChain.sign(credential, signingByCertificate(trustAnchor));
65
66 // generate SELECT interest
67 std::stringstream ss;
68 io::save<security::v2::Certificate>(credential, ss);
69 auto checkCert = *(io::load<security::v2::Certificate>(ss));
70 BOOST_CHECK_EQUAL(checkCert, credential);
71 ss.str("");
72 ss.clear();
73
74 io::save<security::v2::Certificate>(credential, ss);
75 std::list<std::string> paramList;
76 std::string jsonString = ss.str();
77 paramList.push_back(jsonString);
78 JsonSection credentialJson = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, paramList);
79 BOOST_CHECK_EQUAL(credentialJson.get<std::string>(ChallengeCredential::JSON_CREDENTIAL), jsonString);
80 ss.str("");
81 ss.clear();
82
83 boost::property_tree::write_json(ss, credentialJson);
84 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
85
86 Name interestName("/example/CA");
87 interestName.append("_SELECT").append("Fake-Request-ID").append("CREDENTIAL").append(jsonContent);
88 Interest interest(interestName);
89
90 challenge.processSelectInterest(interest, request);
91 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
92 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
93}
94
95BOOST_AUTO_TEST_SUITE_END()
96
97} // namespace tests
98} // namespace ndncert
99} // namespace ndn