Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_FIXTURE_TEST_SUITE(TestCertificateRequest, IdentityManagementV2Fixture) |
| 31 | |
| 32 | BOOST_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 Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(request1.getStatus(), ""); |
| 42 | BOOST_CHECK_EQUAL(request1.getChallengeSecrets().empty(), true); |
| 43 | BOOST_CHECK_EQUAL(request1.getCert(), cert); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(request1.getCert(), cert); |
| 45 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 46 | 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 Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(request2.getCaName().toUri(), "/ndn/site1"); |
| 54 | BOOST_CHECK_EQUAL(request2.getRequestId(), "123"); |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(request2.getStatus(), "need-verify"); |
| 56 | BOOST_CHECK_EQUAL(request2.getChallengeType(), "EMAIL"); |
| 57 | BOOST_CHECK(request2.getChallengeSecrets() == json); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(request2.getCert(), cert); |
| 59 | } |
| 60 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE(GetterSetter) |
| 62 | { |
| 63 | auto identity = addIdentity(Name("/ndn/site1")); |
| 64 | auto key = identity.getDefaultKey(); |
| 65 | auto cert = key.getDefaultCertificate(); |
| 66 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 67 | JsonSection json; |
| 68 | json.put("code", "1234"); |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 69 | |
Zhiyi Zhang | 3b267e6 | 2017-02-09 17:59:34 -0800 | [diff] [blame] | 70 | 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 Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(GetCertificateRequestOutput) |
| 81 | { |
| 82 | const std::string certString = R"_CERT_( |
| 83 | Bv0BuwczCANuZG4IBXNpdGUxCANLRVkIEWtzay0xNDE2NDI1Mzc3MDk0CAQwMTIz |
| 84 | CAf9AAABScmLFAkYAQIZBAA27oAVoDCBnTANBgkqhkiG9w0BAQEFAAOBiwAwgYcC |
| 85 | gYEAngY+R4WyNDeqhUesAySDtZyoBTokHuuJAbvpm7LDIqxo4/BsAs5opsTQpwaQ |
| 86 | nKobCB2LQ5ozZ0RtIaMbiJqXXlnEFQvZLL1RB2GCrcG417+bz30kwmPzlxfr/mIl |
| 87 | ultNisJ6vUOKj7jy8cVqMNNQjMia3+/tNed6Yup2fLsIJscCAREWVRsBARwmByQI |
| 88 | A25kbggFc2l0ZTEIA0tFWQgRa3NrLTI1MTY0MjUzNzcwOTT9AP0m/QD+DzIwMTUw |
| 89 | ODE0VDIyMzczOf0A/w8yMDE1MDgxOFQyMjM3MzgXgP////////////////////// |
| 90 | //////////////////////////////////////////////////////////////// |
| 91 | //////////////////////////////////////////////////////////////// |
| 92 | ////////////////////)_CERT_"; |
| 93 | |
| 94 | const std::string expectedString = R"_REQUEST_(Request CA name: |
| 95 | /ndn/site1 |
| 96 | Request ID: |
| 97 | 123 |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 98 | Certificate: |
| 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 | |
| 122 | BOOST_AUTO_TEST_SUITE_END() |
| 123 | |
| 124 | } // namespace tests |
| 125 | } // namespace ndncert |
| 126 | } // namespace ndn |