tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 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" |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 24 | #include "detail/probe-encoder.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 25 | #include "detail/request-encoder.hpp" |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 26 | #include "detail/ca-configuration.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 27 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 28 | #include "tests/boost-test.hpp" |
| 29 | #include "tests/clock-fixture.hpp" |
| 30 | #include "tests/key-chain-fixture.hpp" |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 32 | namespace ndncert::tests { |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestProtocolEncoding) |
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"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 43 | auto& cert = caCache.getKnownProfiles().front().cert; |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 44 | |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 45 | auto b = infotlv::encodeDataContent(config.caProfile, *cert); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 46 | auto item = infotlv::decodeDataContent(b); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 47 | |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(*item.cert, *cert); |
| 49 | BOOST_CHECK_EQUAL(item.caInfo, config.caProfile.caInfo); |
| 50 | BOOST_CHECK_EQUAL(item.caPrefix, config.caProfile.caPrefix); |
| 51 | BOOST_CHECK_EQUAL_COLLECTIONS(item.probeParameterKeys.begin(), item.probeParameterKeys.end(), |
| 52 | config.caProfile.probeParameterKeys.begin(), config.caProfile.probeParameterKeys.end()); |
| 53 | BOOST_CHECK_EQUAL(item.maxValidityPeriod, config.caProfile.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"; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 59 | auto b = errortlv::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg); |
| 60 | auto item = errortlv::decodefromDataContent(b); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 67 | std::multimap<std::string, std::string> parameters; |
| 68 | parameters.emplace("key1", "value1"); |
| 69 | parameters.emplace("key2", "value2"); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 70 | auto appParam = probetlv::encodeApplicationParameters(std::move(parameters)); |
| 71 | auto param1 = probetlv::decodeApplicationParameters(appParam); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(param1.size(), 2); |
| 73 | BOOST_CHECK_EQUAL(param1.find("key1")->second, "value1"); |
| 74 | BOOST_CHECK_EQUAL(param1.find("key2")->second, "value2"); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(ProbeEncodingData) |
| 78 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 79 | ca::CaConfig config; |
| 80 | config.load("tests/unit-tests/config-files/config-ca-5"); |
| 81 | std::vector<Name> names; |
| 82 | names.emplace_back("/ndn/1"); |
| 83 | names.emplace_back("/ndn/2"); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 84 | std::vector<Name> redirectionNames; |
| 85 | for (const auto& i : config.redirection) redirectionNames.push_back(i.first->getFullName()); |
| 86 | auto b = probetlv::encodeDataContent(names, 2, redirectionNames); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 87 | std::vector<std::pair<Name, int>> retNames; |
| 88 | std::vector<Name> redirection; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 89 | probetlv::decodeDataContent(b, retNames, redirection); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(redirection.size(), config.redirection.size()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 98 | auto it3 = redirection.begin(); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 99 | auto it4 = config.redirection.begin(); |
| 100 | for (; it3 != redirection.end() && it4 != config.redirection.end(); it3++, it4++) { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(*it3, it4->first->getFullName()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 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"); |
Zhiyi Zhang | 44c6a35 | 2020-12-14 10:57:17 -0800 | [diff] [blame] | 109 | auto& certRequest = caCache.getKnownProfiles().front().cert; |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 110 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 111 | auto b = requesttlv::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 112 | std::vector<uint8_t> returnedPub; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 113 | std::shared_ptr<Certificate> returnedCert; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 114 | requesttlv::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 115 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 116 | BOOST_TEST(returnedPub == pub, boost::test_tools::per_element()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(*returnedCert, *certRequest); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingData) |
| 121 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 122 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 123 | std::array<uint8_t, 32> salt = {{101}}; |
| 124 | RequestId id = {{102}}; |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 125 | std::vector<std::string> list; |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 126 | list.emplace_back("abc"); |
| 127 | list.emplace_back("def"); |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame] | 128 | auto b = requesttlv::encodeDataContent(pub, salt, id, list); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 129 | std::vector<uint8_t> returnedPub; |
| 130 | std::array<uint8_t, 32> returnedSalt; |
| 131 | RequestId returnedId; |
Zhiyi Zhang | 1b101b3 | 2021-02-17 14:36:03 -0800 | [diff] [blame] | 132 | auto retlist = requesttlv::decodeDataContent(b, returnedPub, returnedSalt, returnedId); |
tylerliu | 2670ba9 | 2020-10-30 10:25:03 -0700 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end()); |
| 134 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end()); |
| 135 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end()); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 138 | class ChallengeEncodingFixture : public ClockFixture, public KeyChainFixture |
| 139 | { |
| 140 | }; |
| 141 | |
| 142 | BOOST_FIXTURE_TEST_CASE(ChallengeEncoding, ChallengeEncodingFixture) |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 143 | { |
| 144 | const uint8_t key[] = {0x23, 0x70, 0xe3, 0x20, 0xd4, 0x34, 0x42, 0x08, |
| 145 | 0xe0, 0xff, 0x56, 0x83, 0xf2, 0x43, 0xb2, 0x13}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 146 | requester::ProfileStorage caCache; |
| 147 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 148 | auto certRequest = *caCache.getKnownProfiles().front().cert; |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 149 | RequestId id = {{102}}; |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 150 | ca::RequestState state; |
| 151 | state.caPrefix = Name("/ndn/ucla"); |
| 152 | state.requestId = id; |
| 153 | state.requestType = RequestType::NEW; |
| 154 | state.status = Status::PENDING; |
| 155 | state.cert = certRequest; |
| 156 | std::memcpy(state.encryptionKey.data(), key, sizeof(key)); |
| 157 | state.challengeType = "pin"; |
Zhiyi Zhang | 01efccb | 2020-12-07 11:01:00 -0800 | [diff] [blame] | 158 | auto tp = time::system_clock::now(); |
| 159 | state.challengeState = ca::ChallengeState("test", tp, 3, time::seconds(3600), JsonSection()); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 160 | auto contentBlock = challengetlv::encodeDataContent(state, Name("/ndn/ucla/a/b/c")); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 161 | |
tylerliu | 4140fe8 | 2021-01-27 15:45:44 -0800 | [diff] [blame] | 162 | requester::Request context(m_keyChain, caCache.getKnownProfiles().front(), RequestType::NEW); |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 163 | context.m_requestId = id; |
| 164 | std::memcpy(context.m_aesKey.data(), key, sizeof(key)); |
Zhiyi Zhang | 01efccb | 2020-12-07 11:01:00 -0800 | [diff] [blame] | 165 | advanceClocks(time::seconds(10)); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 166 | challengetlv::decodeDataContent(contentBlock, context); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 167 | |
Zhiyi Zhang | 6499edd | 2021-02-17 22:37:21 -0800 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(static_cast<size_t>(context.m_status), static_cast<size_t>(Status::PENDING)); |
| 169 | BOOST_CHECK_EQUAL(context.m_challengeStatus, "test"); |
| 170 | BOOST_CHECK_EQUAL(context.m_remainingTries, 3); |
| 171 | BOOST_CHECK_EQUAL(context.m_freshBefore, tp + time::seconds(3600) + time::seconds(10)); |
| 172 | BOOST_CHECK_EQUAL(context.m_issuedCertName, "/ndn/ucla/a/b/c"); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 173 | } |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 174 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestProtocolEncoding |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 176 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 177 | } // namespace ndncert::tests |