blob: 772c57fba45644b9579b8afa1f1e5f2b7052aaf5 [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");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070035 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Credential");
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070036
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070037 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070038 BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
39 auto cert = challenge.m_trustAnchors.front();
40 BOOST_CHECK_EQUAL(cert.getName(),
41 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
42}
43
44BOOST_AUTO_TEST_CASE(HandleSelect)
45{
46 // create trust anchor
47 ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070048 auto identity = addIdentity(Name("/trust"));
49 auto key = identity.getDefaultKey();
50 auto trustAnchor = key.getDefaultCertificate();
51 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070052 challenge.m_trustAnchors.front() = trustAnchor;
53
54 // create certificate request
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070055 auto identityA = addIdentity(Name("/example"));
56 auto keyA = identityA.getDefaultKey();
57 auto certA = key.getDefaultCertificate();
58 CertificateRequest request(Name("/example"), "123", certA);
59
60 // create requester's existing cert
61 auto identityB = addIdentity(Name("/trust/cert"));
62 auto keyB = identityB.getDefaultKey();
63 auto certB = key.getDefaultCertificate();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070064
65 // using trust anchor to sign cert request to get credential
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070066 Name credentialName = certB.getKeyName();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070067 credentialName.append("Credential").appendVersion();
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070068 security::v2::Certificate credential = certB;
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070069 credential.setName(credentialName);
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070070 credential.setContent(certB.getContent());
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070071 m_keyChain.sign(credential, signingByCertificate(trustAnchor));
72
73 // generate SELECT interest
74 std::stringstream ss;
75 io::save<security::v2::Certificate>(credential, ss);
76 auto checkCert = *(io::load<security::v2::Certificate>(ss));
77 BOOST_CHECK_EQUAL(checkCert, credential);
78 ss.str("");
79 ss.clear();
80
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070081 std::list<std::string> paramList;
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070082 io::save<security::v2::Certificate>(credential, ss);
83 std::string paramString = ss.str();
84 paramList.push_back(paramString);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070085 ss.str("");
86 ss.clear();
87
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070088 io::save<security::v2::Certificate>(certB, ss);
89 paramString = ss.str();
90 paramList.push_back(paramString);
91 ss.str("");
92 ss.clear();
93 JsonSection credentialJson = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, paramList);
94
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070095 boost::property_tree::write_json(ss, credentialJson);
96 Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str());
97
98 Name interestName("/example/CA");
99 interestName.append("_SELECT").append("Fake-Request-ID").append("CREDENTIAL").append(jsonContent);
100 Interest interest(interestName);
101
102 challenge.processSelectInterest(interest, request);
103 BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS);
104 BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true);
105}
106
107BOOST_AUTO_TEST_SUITE_END()
108
109} // namespace tests
110} // namespace ndncert
111} // namespace ndn