tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, 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 <configuration.hpp> |
| 22 | #include <detail/info-encoder.hpp> |
| 23 | #include <detail/error-encoder.hpp> |
| 24 | #include <detail/probe-encoder.hpp> |
| 25 | #include <detail/new-renew-revoke-encoder.hpp> |
| 26 | #include <detail/challenge-encoder.hpp> |
| 27 | #include <identity-management-fixture.hpp> |
| 28 | #include "test-common.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndncert { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(TestProtocolEncoding, IdentityManagementTimeFixture) |
| 35 | BOOST_AUTO_TEST_CASE(InfoEncoding) |
| 36 | { |
| 37 | ca::CaConfig config; |
| 38 | config.load("tests/unit-tests/config-files/config-ca-1"); |
| 39 | |
| 40 | requester::ProfileStorage caCache; |
| 41 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
| 42 | auto& cert = caCache.m_caItems.front().m_cert; |
| 43 | |
| 44 | auto b = InfoEncoder::encodeDataContent(config.m_caItem, *cert); |
| 45 | auto item = InfoEncoder::decodeDataContent(b); |
| 46 | |
| 47 | BOOST_CHECK_EQUAL(*item.m_cert, *cert); |
| 48 | BOOST_CHECK_EQUAL(item.m_caInfo, config.m_caItem.m_caInfo); |
| 49 | BOOST_CHECK_EQUAL(item.m_caPrefix, config.m_caItem.m_caPrefix); |
| 50 | BOOST_CHECK_EQUAL(item.m_probeParameterKeys.size(), config.m_caItem.m_probeParameterKeys.size()); |
| 51 | for (auto it1 = item.m_probeParameterKeys.begin(), it2 = config.m_caItem.m_probeParameterKeys.begin(); |
| 52 | it1 != item.m_probeParameterKeys.end() && it2 != config.m_caItem.m_probeParameterKeys.end(); it1 ++, it2 ++) { |
| 53 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 54 | } |
| 55 | BOOST_CHECK_EQUAL(item.m_maxValidityPeriod, config.m_caItem.m_maxValidityPeriod); |
| 56 | } |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(ErrorEncoding) |
| 59 | { |
| 60 | std::string msg = "Just to test"; |
| 61 | auto b = ErrorEncoder::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg); |
| 62 | auto item = ErrorEncoder::decodefromDataContent(b); |
| 63 | BOOST_CHECK_EQUAL(std::get<0>(item), ErrorCode::NAME_NOT_ALLOWED); |
| 64 | BOOST_CHECK_EQUAL(std::get<1>(item), msg); |
| 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(ProbeEncodingAppParam) |
| 68 | { |
| 69 | std::vector<std::tuple<std::string, std::string>> parameters; |
| 70 | parameters.emplace_back("key1", "value1"); |
| 71 | parameters.emplace_back("key2", "value2"); |
Zhiyi Zhang | db1ec76 | 2020-10-30 08:58:39 -0700 | [diff] [blame] | 72 | auto appParam = ProbeEncoder::encodeApplicationParameters(std::move(parameters)); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 73 | auto param1 = ProbeEncoder::decodeApplicationParameters(appParam); |
| 74 | BOOST_CHECK_EQUAL(parameters.size(), param1.size()); |
| 75 | BOOST_CHECK_EQUAL(std::get<0>(parameters[0]), std::get<0>(param1[0])); |
| 76 | BOOST_CHECK_EQUAL(std::get<1>(parameters[0]), std::get<1>(param1[0])); |
| 77 | BOOST_CHECK_EQUAL(std::get<0>(parameters[1]), std::get<0>(param1[1])); |
| 78 | BOOST_CHECK_EQUAL(std::get<1>(parameters[1]), std::get<1>(param1[1])); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(ProbeEncodingData) |
| 82 | { |
| 83 | ca::CaConfig config; |
| 84 | config.load("tests/unit-tests/config-files/config-ca-5"); |
| 85 | std::vector<Name> names; |
| 86 | names.emplace_back("/ndn/1"); |
| 87 | names.emplace_back("/ndn/2"); |
| 88 | auto b = ProbeEncoder::encodeDataContent(names, 2, config.m_redirection); |
| 89 | std::vector<std::pair<Name, int>> retNames; |
| 90 | std::vector<Name> redirection; |
| 91 | ProbeEncoder::decodeDataContent(b, retNames, redirection); |
| 92 | BOOST_CHECK_EQUAL(retNames.size(), names.size()); |
| 93 | auto it1 = retNames.begin(); auto it2 = names.begin(); |
| 94 | for (; it1 != retNames.end() && it2 != names.end(); it1 ++, it2 ++) { |
| 95 | BOOST_CHECK_EQUAL(it1->first, *it2); |
| 96 | BOOST_CHECK_EQUAL(it1->second, 2); |
| 97 | } |
| 98 | BOOST_CHECK_EQUAL(redirection.size(), config.m_redirection->size()); |
| 99 | auto it3 = redirection.begin(); auto it4 = config.m_redirection->begin(); |
| 100 | for (; it3 != redirection.end() && it4 != config.m_redirection->end(); it3 ++, it4 ++) { |
| 101 | BOOST_CHECK_EQUAL(*it3, (*it4)->getFullName()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingParam) |
| 106 | { |
| 107 | requester::ProfileStorage caCache; |
| 108 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
| 109 | auto& certRequest = caCache.m_caItems.front().m_cert; |
| 110 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
| 111 | auto b = NewRenewRevokeEncoder::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest); |
| 112 | std::vector<uint8_t> returnedPub; |
| 113 | std::shared_ptr<security::Certificate> returnedCert; |
| 114 | NewRenewRevokeEncoder::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL(returnedPub.size(), pub.size()); |
| 117 | for (auto it1 = returnedPub.begin(), it2 = pub.begin(); |
| 118 | it1 != returnedPub.end() && it2 != pub.end(); it1 ++, it2 ++) { |
| 119 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 120 | } |
| 121 | BOOST_CHECK_EQUAL(*returnedCert, *certRequest); |
| 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingData) |
| 125 | { |
| 126 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
| 127 | std::array<uint8_t, 32> salt = {101}; |
| 128 | RequestId id = {102}; |
| 129 | std::list<std::string> list; |
| 130 | list.emplace_back("abc"); |
| 131 | list.emplace_back("def"); |
| 132 | auto b = NewRenewRevokeEncoder::encodeDataContent(pub, salt, id, Status::BEFORE_CHALLENGE, list); |
| 133 | std::vector<uint8_t> returnedPub; |
| 134 | std::array<uint8_t, 32> returnedSalt; |
| 135 | RequestId returnedId; |
| 136 | Status s; |
| 137 | auto retlist = NewRenewRevokeEncoder::decodeDataContent(b, returnedPub, returnedSalt, returnedId, s); |
| 138 | BOOST_CHECK_EQUAL(returnedPub.size(), pub.size()); |
| 139 | for (auto it1 = returnedPub.begin(), it2 = pub.begin(); |
| 140 | it1 != returnedPub.end() && it2 != pub.end(); it1 ++, it2 ++) { |
| 141 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 142 | } |
| 143 | BOOST_CHECK_EQUAL(returnedSalt.size(), salt.size()); |
| 144 | for (auto it1 = returnedSalt.begin(), it2 = salt.begin(); |
| 145 | it1 != returnedSalt.end() && it2 != salt.end(); it1 ++, it2 ++) { |
| 146 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 147 | } |
| 148 | BOOST_CHECK_EQUAL(returnedId.size(), id.size()); |
| 149 | for (auto it1 = returnedId.begin(), it2 = id.begin(); |
| 150 | it1 != returnedId.end() && it2 != id.end(); it1 ++, it2 ++) { |
| 151 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 152 | } |
| 153 | BOOST_CHECK_EQUAL(static_cast<size_t>(s), static_cast<size_t>(Status::BEFORE_CHALLENGE)); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(ChallengeEncoding) |
| 157 | { |
| 158 | time::system_clock::TimePoint t = time::system_clock::now(); |
| 159 | requester::ProfileStorage caCache; |
| 160 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
| 161 | security::Certificate certRequest = *caCache.m_caItems.front().m_cert; |
| 162 | RequestId id = {102}; |
| 163 | ca::RequestState state(Name("/ndn/akdnsla"), id, RequestType::NEW, Status::PENDING, |
| 164 | certRequest, "hahaha", "Just a test", t, 3, time::seconds(321), JsonSection(), |
| 165 | Block(), 0); |
| 166 | auto b = ChallengeEncoder::encodeDataContent(state); |
| 167 | b.push_back(makeNestedBlock(tlv::IssuedCertName, Name("/ndn/akdnsla/a/b/c"))); |
| 168 | |
| 169 | requester::RequestContext context(m_keyChain, caCache.m_caItems.front(), RequestType::NEW); |
| 170 | ChallengeEncoder::decodeDataContent(b, context); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(static_cast<size_t>(context.m_status), static_cast<size_t>(Status::PENDING)); |
| 173 | BOOST_CHECK_EQUAL(context.m_challengeStatus, "Just a test"); |
| 174 | BOOST_CHECK_EQUAL(context.m_remainingTries, 3); |
| 175 | BOOST_ASSERT(context.m_freshBefore > time::system_clock::now() + time::seconds(321) - time::milliseconds(100)); |
| 176 | BOOST_ASSERT(context.m_freshBefore < time::system_clock::now() + time::seconds(321) + time::milliseconds(100)); |
| 177 | BOOST_CHECK_EQUAL(context.m_issuedCertName, "/ndn/akdnsla/a/b/c"); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | BOOST_AUTO_TEST_SUITE_END() |
| 183 | |
| 184 | } // namespace tests |
| 185 | } // namespace ndncert |
| 186 | } // namespace ndn |
| 187 | |
| 188 | |