blob: 113748dbe4d6f4676f4465c23d52e0d95133fd74 [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");
41 BOOST_CHECK_EQUAL(request1.getStatus(), CertificateRequest::Pending);
42 BOOST_CHECK_EQUAL(request1.getChallengeType(), "");
43 BOOST_CHECK_EQUAL(request1.getChallengeStatus(), "");
44 BOOST_CHECK_EQUAL(request1.getChallengeDefinedField(), "");
45 BOOST_CHECK_EQUAL(request1.getChallengeInstruction(), "");
46 BOOST_CHECK_EQUAL(request1.getCert(), cert);
47
48 CertificateRequest request2(Name("/ndn/site1"), "123", CertificateRequest::Verifying,
49 "Email", "NEED_CODE", "123456", cert);
50 BOOST_CHECK_EQUAL(request2.getCaName().toUri(), "/ndn/site1");
51 BOOST_CHECK_EQUAL(request2.getRequestId(), "123");
52 BOOST_CHECK_EQUAL(request2.getStatus(), CertificateRequest::Verifying);
53 BOOST_CHECK_EQUAL(request2.getChallengeType(), "Email");
54 BOOST_CHECK_EQUAL(request2.getChallengeStatus(), "NEED_CODE");
55 BOOST_CHECK_EQUAL(request2.getChallengeDefinedField(), "123456");
56 BOOST_CHECK_EQUAL(request2.getChallengeInstruction(), "");
57 BOOST_CHECK_EQUAL(request2.getCert(), cert);
58}
59
60BOOST_AUTO_TEST_CASE(GetStatusOutput)
61{
62 CertificateRequest::ApplicationStatus status = CertificateRequest::Success;
63 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(status), "success");
64}
65
66BOOST_AUTO_TEST_CASE(GetterSetter)
67{
68 auto identity = addIdentity(Name("/ndn/site1"));
69 auto key = identity.getDefaultKey();
70 auto cert = key.getDefaultCertificate();
71
72 CertificateRequest request(Name("/ndn/site1"), "123", cert);
73 request.setStatus(CertificateRequest::Verifying);
74 request.setChallengeType("Email");
75 request.setChallengeDefinedField("456");
76 request.setChallengeStatus("NEED_EMAIL");
77 request.setChallengeInstruction("Please provide your email address");
78
79 BOOST_CHECK_EQUAL(request.getStatus(), CertificateRequest::Verifying);
80 BOOST_CHECK_EQUAL(request.getChallengeType(), "Email");
81 BOOST_CHECK_EQUAL(request.getChallengeDefinedField(), "456");
82 BOOST_CHECK_EQUAL(request.getChallengeStatus(), "NEED_EMAIL");
83 BOOST_CHECK_EQUAL(request.getChallengeInstruction(), "Please provide your email address");
84}
85
86BOOST_AUTO_TEST_CASE(GetCertificateRequestOutput)
87{
88 const std::string certString = R"_CERT_(
89Bv0BuwczCANuZG4IBXNpdGUxCANLRVkIEWtzay0xNDE2NDI1Mzc3MDk0CAQwMTIz
90CAf9AAABScmLFAkYAQIZBAA27oAVoDCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcC
91gYEAngY+R4WyNDeqhUesAySDtZyoBTokHuuJAbvpm7LDIqxo4/BsAs5opsTQpwaQ
92nKobCB2LQ5ozZ0RtIaMbiJqXXlnEFQvZLL1RB2GCrcG417+bz30kwmPzlxfr/mIl
93ultNisJ6vUOKj7jy8cVqMNNQjMia3+/tNed6Yup2fLsIJscCAREWVRsBARwmByQI
94A25kbggFc2l0ZTEIA0tFWQgRa3NrLTI1MTY0MjUzNzcwOTT9AP0m/QD+DzIwMTUw
95ODE0VDIyMzczOf0A/w8yMDE1MDgxOFQyMjM3MzgXgP//////////////////////
96////////////////////////////////////////////////////////////////
97////////////////////////////////////////////////////////////////
98////////////////////)_CERT_";
99
100 const std::string expectedString = R"_REQUEST_(Request CA name:
101 /ndn/site1
102Request ID:
103 123
104Request Status:
105 pending
106Certificate:
107 Certificate name:
108 /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B
109 Validity:
110 NotBefore: 20150814T223739
111 NotAfter: 20150818T223738
112 Public key bits:
113 MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCeBj5HhbI0N6qFR6wDJIO1nKgF
114 OiQe64kBu+mbssMirGjj8GwCzmimxNCnBpCcqhsIHYtDmjNnRG0hoxuImpdeWcQV
115 C9ksvVEHYYKtwbjXv5vPfSTCY/OXF+v+YiW6W02Kwnq9Q4qPuPLxxWow01CMyJrf
116 7+0153pi6nZ8uwgmxwIBEQ==
117 Signature Information:
118 Signature Type: SignatureSha256WithRsa
119 Key Locator: Name=/ndn/site1/KEY/ksk-2516425377094
120)_REQUEST_";
121
122 std::stringstream ss;
123 ss << certString;
124 auto cert = io::load<security::v2::Certificate>(ss);
125 CertificateRequest request(Name("/ndn/site1"), "123", *cert);
126
127 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(request), expectedString);
128}
129
130BOOST_AUTO_TEST_SUITE_END()
131
132} // namespace tests
133} // namespace ndncert
134} // namespace ndn