Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 05ec0be | 2023-03-14 21:18:06 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library 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 Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "tests/unit/security/validator-fixture.hpp" |
| 23 | |
| 24 | #include "ndn-cxx/security/additional-description.hpp" |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/util/signal/scoped-connection.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 26 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 27 | #include <boost/asio/post.hpp> |
| 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::tests { |
| 30 | |
| 31 | using namespace ndn::security; |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 33 | ValidatorFixtureBase::ValidatorFixtureBase() |
| 34 | { |
| 35 | processInterest = [this] (const Interest& interest) { |
| 36 | auto cert = cache.find(interest); |
| 37 | if (cert != nullptr) { |
Junxiao Shi | 7d72868 | 2022-04-01 01:21:13 +0000 | [diff] [blame] | 38 | BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " reply " << cert->getName()); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 39 | face.receive(*cert); |
| 40 | } |
Junxiao Shi | 7d72868 | 2022-04-01 01:21:13 +0000 | [diff] [blame] | 41 | else { |
| 42 | BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " no reply"); |
| 43 | } |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 44 | }; |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | ValidatorFixtureBase::mockNetworkOperations() |
| 49 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 50 | signal::ScopedConnection conn = face.onSendInterest.connect([this] (const Interest& interest) { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 51 | if (processInterest) { |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 52 | boost::asio::post(m_io, [=] { processInterest(interest); }); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 53 | } |
| 54 | }); |
| 55 | advanceClocks(s_mockPeriod, s_mockTimes); |
| 56 | } |
| 57 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 58 | void |
| 59 | ValidatorFixtureBase::rewindClockAfterValidation() |
| 60 | { |
| 61 | m_systemClock->advance(s_mockPeriod * s_mockTimes * -1); |
| 62 | } |
| 63 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 64 | Identity |
| 65 | ValidatorFixtureBase::addSubCertificate(const Name& subIdentityName, const Identity& issuer) |
| 66 | { |
| 67 | auto subId = m_keyChain.createIdentity(subIdentityName); |
| 68 | auto cert = subId.getDefaultKey().getDefaultCertificate(); |
| 69 | cert.setName(cert.getKeyName() |
| 70 | .append("parent") |
| 71 | .appendVersion()); |
| 72 | |
| 73 | SignatureInfo info; |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 74 | info.setValidityPeriod(ValidityPeriod::makeRelative(0_s, 90_days)); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 75 | |
| 76 | AdditionalDescription description; |
| 77 | description.set("type", "sub-certificate"); |
| 78 | info.addCustomTlv(description.wireEncode()); |
| 79 | |
| 80 | m_keyChain.sign(cert, signingByIdentity(issuer).setSignatureInfo(info)); |
| 81 | m_keyChain.setDefaultCertificate(subId.getDefaultKey(), cert); |
| 82 | |
| 83 | return subId; |
| 84 | } |
| 85 | |
| 86 | Name |
| 87 | InterestV02Pkt::makeName(Name name, KeyChain& keyChain) |
| 88 | { |
| 89 | Interest interest(name); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 90 | SigningInfo params; |
| 91 | params.setSignedInterestFormat(SignedInterestFormat::V02); |
| 92 | keyChain.sign(interest, params); |
| 93 | return interest.getName(); |
| 94 | } |
| 95 | |
| 96 | Name |
| 97 | InterestV03Pkt::makeName(Name name, KeyChain& keyChain) |
| 98 | { |
| 99 | Interest interest(name); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 100 | SigningInfo params; |
| 101 | params.setSignedInterestFormat(SignedInterestFormat::V03); |
| 102 | keyChain.sign(interest, params); |
| 103 | return interest.getName(); |
| 104 | } |
| 105 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 106 | } // namespace ndn::tests |