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); |
tylerliu | 2670ba9 | 2020-10-30 10:25:03 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL_COLLECTIONS(item.m_probeParameterKeys.begin(), item.m_probeParameterKeys.end(), |
| 52 | config.m_caItem.m_probeParameterKeys.begin(), config.m_caItem.m_probeParameterKeys.end()) |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(item.m_maxValidityPeriod, config.m_caItem.m_maxValidityPeriod); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(ErrorEncoding) |
| 57 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 58 | std::string msg = "Just to test"; |
| 59 | auto b = ErrorEncoder::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg); |
| 60 | auto item = ErrorEncoder::decodefromDataContent(b); |
| 61 | BOOST_CHECK_EQUAL(std::get<0>(item), ErrorCode::NAME_NOT_ALLOWED); |
| 62 | BOOST_CHECK_EQUAL(std::get<1>(item), msg); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(ProbeEncodingAppParam) |
| 66 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 67 | std::vector<std::tuple<std::string, std::string>> parameters; |
| 68 | parameters.emplace_back("key1", "value1"); |
| 69 | parameters.emplace_back("key2", "value2"); |
| 70 | auto appParam = ProbeEncoder::encodeApplicationParameters(std::move(parameters)); |
| 71 | auto param1 = ProbeEncoder::decodeApplicationParameters(appParam); |
| 72 | BOOST_CHECK_EQUAL(parameters.size(), param1.size()); |
| 73 | BOOST_CHECK_EQUAL(std::get<0>(parameters[0]), std::get<0>(param1[0])); |
| 74 | BOOST_CHECK_EQUAL(std::get<1>(parameters[0]), std::get<1>(param1[0])); |
| 75 | BOOST_CHECK_EQUAL(std::get<0>(parameters[1]), std::get<0>(param1[1])); |
| 76 | BOOST_CHECK_EQUAL(std::get<1>(parameters[1]), std::get<1>(param1[1])); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(ProbeEncodingData) |
| 80 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 81 | ca::CaConfig config; |
| 82 | config.load("tests/unit-tests/config-files/config-ca-5"); |
| 83 | std::vector<Name> names; |
| 84 | names.emplace_back("/ndn/1"); |
| 85 | names.emplace_back("/ndn/2"); |
| 86 | auto b = ProbeEncoder::encodeDataContent(names, 2, config.m_redirection); |
| 87 | std::vector<std::pair<Name, int>> retNames; |
| 88 | std::vector<Name> redirection; |
| 89 | ProbeEncoder::decodeDataContent(b, retNames, redirection); |
| 90 | BOOST_CHECK_EQUAL(retNames.size(), names.size()); |
| 91 | auto it1 = retNames.begin(); |
| 92 | auto it2 = names.begin(); |
| 93 | for (; it1 != retNames.end() && it2 != names.end(); it1++, it2++) { |
| 94 | BOOST_CHECK_EQUAL(it1->first, *it2); |
| 95 | BOOST_CHECK_EQUAL(it1->second, 2); |
| 96 | } |
| 97 | BOOST_CHECK_EQUAL(redirection.size(), config.m_redirection->size()); |
| 98 | auto it3 = redirection.begin(); |
| 99 | 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 | } |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingParam) |
| 106 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 115 | |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingData) |
| 125 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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); |
tylerliu | 2670ba9 | 2020-10-30 10:25:03 -0700 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end()); |
| 139 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end()); |
| 140 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 141 | 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] | 142 | } |
| 143 | |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 144 | BOOST_AUTO_TEST_CASE(ChallengeEncoding) |
| 145 | { |
| 146 | const uint8_t key[] = {0x23, 0x70, 0xe3, 0x20, 0xd4, 0x34, 0x42, 0x08, |
| 147 | 0xe0, 0xff, 0x56, 0x83, 0xf2, 0x43, 0xb2, 0x13}; |
| 148 | time::system_clock::TimePoint t = time::system_clock::now(); |
| 149 | requester::ProfileStorage caCache; |
| 150 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
| 151 | security::Certificate certRequest = *caCache.m_caItems.front().m_cert; |
| 152 | RequestId id = {102}; |
| 153 | std::array<uint8_t, 16> aesKey; |
| 154 | std::memcpy(aesKey.data(), key, sizeof(key)); |
| 155 | ca::RequestState state(Name("/ndn/ucla"), id, RequestType::NEW, Status::PENDING, |
| 156 | certRequest, "pin", "test", t, 3, time::seconds(321), JsonSection(), |
| 157 | std::move(aesKey), 0); |
| 158 | auto b = ChallengeEncoder::encodeDataContent(state, Name("/ndn/ucla/a/b/c")); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 159 | |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 160 | requester::RequestContext context(m_keyChain, caCache.m_caItems.front(), RequestType::NEW); |
| 161 | std::memcpy(context.m_aesKey.data(), key, sizeof(key)); |
| 162 | ChallengeEncoder::decodeDataContent(b, context); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 163 | |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame^] | 164 | BOOST_CHECK_EQUAL(static_cast<size_t>(context.m_status), static_cast<size_t>(Status::PENDING)); |
| 165 | BOOST_CHECK_EQUAL(context.m_challengeStatus, "test"); |
| 166 | BOOST_CHECK_EQUAL(context.m_remainingTries, 3); |
| 167 | BOOST_ASSERT(context.m_freshBefore > time::system_clock::now() + time::seconds(321) - time::milliseconds(100)); |
| 168 | BOOST_ASSERT(context.m_freshBefore < time::system_clock::now() + time::seconds(321) + time::milliseconds(100)); |
| 169 | BOOST_CHECK_EQUAL(context.m_issuedCertName, "/ndn/ucla/a/b/c"); |
| 170 | } |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 171 | |
| 172 | BOOST_AUTO_TEST_SUITE_END() |
| 173 | |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 174 | } // namespace tests |
| 175 | } // namespace ndncert |
| 176 | } // namespace ndn |