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 | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | inline namespace v2 { |
| 29 | namespace tests { |
| 30 | |
| 31 | const time::milliseconds ValidatorFixtureBase::s_mockPeriod{250}; |
| 32 | const int ValidatorFixtureBase::s_mockTimes = 200; |
| 33 | |
| 34 | ValidatorFixtureBase::ValidatorFixtureBase() |
| 35 | { |
| 36 | processInterest = [this] (const Interest& interest) { |
| 37 | auto cert = cache.find(interest); |
| 38 | if (cert != nullptr) { |
Junxiao Shi | 7d72868 | 2022-04-01 01:21:13 +0000 | [diff] [blame] | 39 | BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " reply " << cert->getName()); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 40 | face.receive(*cert); |
| 41 | } |
Junxiao Shi | 7d72868 | 2022-04-01 01:21:13 +0000 | [diff] [blame] | 42 | else { |
| 43 | BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " no reply"); |
| 44 | } |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 45 | }; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | ValidatorFixtureBase::mockNetworkOperations() |
| 50 | { |
| 51 | util::signal::ScopedConnection conn = face.onSendInterest.connect([this] (const Interest& interest) { |
| 52 | if (processInterest) { |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 53 | m_io.post([=] { processInterest(interest); }); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 54 | } |
| 55 | }); |
| 56 | advanceClocks(s_mockPeriod, s_mockTimes); |
| 57 | } |
| 58 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 59 | void |
| 60 | ValidatorFixtureBase::rewindClockAfterValidation() |
| 61 | { |
| 62 | m_systemClock->advance(s_mockPeriod * s_mockTimes * -1); |
| 63 | } |
| 64 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 65 | Identity |
| 66 | ValidatorFixtureBase::addSubCertificate(const Name& subIdentityName, const Identity& issuer) |
| 67 | { |
| 68 | auto subId = m_keyChain.createIdentity(subIdentityName); |
| 69 | auto cert = subId.getDefaultKey().getDefaultCertificate(); |
| 70 | cert.setName(cert.getKeyName() |
| 71 | .append("parent") |
| 72 | .appendVersion()); |
| 73 | |
| 74 | SignatureInfo info; |
| 75 | auto now = time::system_clock::now(); |
| 76 | info.setValidityPeriod(ValidityPeriod(now, now + 90_days)); |
| 77 | |
| 78 | AdditionalDescription description; |
| 79 | description.set("type", "sub-certificate"); |
| 80 | info.addCustomTlv(description.wireEncode()); |
| 81 | |
| 82 | m_keyChain.sign(cert, signingByIdentity(issuer).setSignatureInfo(info)); |
| 83 | m_keyChain.setDefaultCertificate(subId.getDefaultKey(), cert); |
| 84 | |
| 85 | return subId; |
| 86 | } |
| 87 | |
| 88 | Name |
| 89 | InterestV02Pkt::makeName(Name name, KeyChain& keyChain) |
| 90 | { |
| 91 | Interest interest(name); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 92 | SigningInfo params; |
| 93 | params.setSignedInterestFormat(SignedInterestFormat::V02); |
| 94 | keyChain.sign(interest, params); |
| 95 | return interest.getName(); |
| 96 | } |
| 97 | |
| 98 | Name |
| 99 | InterestV03Pkt::makeName(Name name, KeyChain& keyChain) |
| 100 | { |
| 101 | Interest interest(name); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 102 | SigningInfo params; |
| 103 | params.setSignedInterestFormat(SignedInterestFormat::V03); |
| 104 | keyChain.sign(interest, params); |
| 105 | return interest.getName(); |
| 106 | } |
| 107 | |
| 108 | } // namespace tests |
| 109 | } // inline namespace v2 |
| 110 | } // namespace security |
| 111 | } // namespace ndn |