blob: 3282ee9d9f462a062fbd464c73b4f0caddb086ca [file] [log] [blame]
Zhiyi Zhanga41c5732017-01-18 14:06:44 -08001/* -*- 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 "certificate-request.hpp"
23#include <boost/lexical_cast.hpp>
24#include <ndn-cxx/util/io.hpp>
25
26namespace ndn {
27namespace ndncert {
28namespace tests {
29
30BOOST_FIXTURE_TEST_SUITE(TestCertificateRequest, IdentityManagementV2Fixture)
31
32BOOST_AUTO_TEST_CASE(Constructor)
33{
34 auto identity = addIdentity(Name("/ndn/site1"));
35 auto key = identity.getDefaultKey();
36 auto cert = key.getDefaultCertificate();
37
38 CertificateRequest request1(Name("/ndn/site1"), "123", cert);
39 BOOST_CHECK_EQUAL(request1.getCaName().toUri(), "/ndn/site1");
40 BOOST_CHECK_EQUAL(request1.getRequestId(), "123");
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080041 BOOST_CHECK_EQUAL(request1.getStatus(), "");
42 BOOST_CHECK_EQUAL(request1.getChallengeSecrets().empty(), true);
43 BOOST_CHECK_EQUAL(request1.getCert(), cert);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080044 BOOST_CHECK_EQUAL(request1.getCert(), cert);
45
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080046 JsonSection json;
47 json.put("code", "1234");
48 std::stringstream ss;
49 boost::property_tree::write_json(ss, json);
50 std::string jsonValue = ss.str();
51
52 CertificateRequest request2(Name("/ndn/site1"), "123", "need-verify", "EMAIL", jsonValue, cert);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080053 BOOST_CHECK_EQUAL(request2.getCaName().toUri(), "/ndn/site1");
54 BOOST_CHECK_EQUAL(request2.getRequestId(), "123");
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080055 BOOST_CHECK_EQUAL(request2.getStatus(), "need-verify");
56 BOOST_CHECK_EQUAL(request2.getChallengeType(), "EMAIL");
57 BOOST_CHECK(request2.getChallengeSecrets() == json);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080058 BOOST_CHECK_EQUAL(request2.getCert(), cert);
59}
60
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080061BOOST_AUTO_TEST_CASE(GetterSetter)
62{
63 auto identity = addIdentity(Name("/ndn/site1"));
64 auto key = identity.getDefaultKey();
65 auto cert = key.getDefaultCertificate();
66
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080067 JsonSection json;
68 json.put("code", "1234");
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080069
Zhiyi Zhang3b267e62017-02-09 17:59:34 -080070 CertificateRequest request(Name("/ndn/site1"), "123", cert);
71 request.setStatus("need-verify");
72 request.setChallengeType("EMAIL");
73 request.setChallengeSecrets(json);
74
75 BOOST_CHECK_EQUAL(request.getStatus(), "need-verify");
76 BOOST_CHECK_EQUAL(request.getChallengeType(), "EMAIL");
77 BOOST_CHECK(request.getChallengeSecrets() == json);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080078}
79
80BOOST_AUTO_TEST_CASE(GetCertificateRequestOutput)
81{
82 const std::string certString = R"_CERT_(
83Bv0BuwczCANuZG4IBXNpdGUxCANLRVkIEWtzay0xNDE2NDI1Mzc3MDk0CAQwMTIz
84CAf9AAABScmLFAkYAQIZBAA27oAVoDCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcC
85gYEAngY+R4WyNDeqhUesAySDtZyoBTokHuuJAbvpm7LDIqxo4/BsAs5opsTQpwaQ
86nKobCB2LQ5ozZ0RtIaMbiJqXXlnEFQvZLL1RB2GCrcG417+bz30kwmPzlxfr/mIl
87ultNisJ6vUOKj7jy8cVqMNNQjMia3+/tNed6Yup2fLsIJscCAREWVRsBARwmByQI
88A25kbggFc2l0ZTEIA0tFWQgRa3NrLTI1MTY0MjUzNzcwOTT9AP0m/QD+DzIwMTUw
89ODE0VDIyMzczOf0A/w8yMDE1MDgxOFQyMjM3MzgXgP//////////////////////
90////////////////////////////////////////////////////////////////
91////////////////////////////////////////////////////////////////
92////////////////////)_CERT_";
93
94 const std::string expectedString = R"_REQUEST_(Request CA name:
95 /ndn/site1
96Request ID:
97 123
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080098Certificate:
99 Certificate name:
100 /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B
101 Validity:
102 NotBefore: 20150814T223739
103 NotAfter: 20150818T223738
104 Public key bits:
105 MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCeBj5HhbI0N6qFR6wDJIO1nKgF
106 OiQe64kBu+mbssMirGjj8GwCzmimxNCnBpCcqhsIHYtDmjNnRG0hoxuImpdeWcQV
107 C9ksvVEHYYKtwbjXv5vPfSTCY/OXF+v+YiW6W02Kwnq9Q4qPuPLxxWow01CMyJrf
108 7+0153pi6nZ8uwgmxwIBEQ==
109 Signature Information:
110 Signature Type: SignatureSha256WithRsa
111 Key Locator: Name=/ndn/site1/KEY/ksk-2516425377094
112)_REQUEST_";
113
114 std::stringstream ss;
115 ss << certString;
116 auto cert = io::load<security::v2::Certificate>(ss);
117 CertificateRequest request(Name("/ndn/site1"), "123", *cert);
118
119 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(request), expectedString);
120}
121
122BOOST_AUTO_TEST_SUITE_END()
123
124} // namespace tests
125} // namespace ndncert
126} // namespace ndn