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