blob: ff198a307c9e0180bb2d33e5826eb2a590d380d2 [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento05ec0be2023-03-14 21:18:06 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05004 *
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 Pesavento47ce2ee2023-05-09 01:33:33 -040025#include "ndn-cxx/util/signal/scoped-connection.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026
Davide Pesavento2f46d652023-11-09 23:40:01 -050027#include <boost/asio/post.hpp>
28
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::security;
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050032
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050033ValidatorFixtureBase::ValidatorFixtureBase()
34{
35 processInterest = [this] (const Interest& interest) {
36 auto cert = cache.find(interest);
37 if (cert != nullptr) {
Junxiao Shi7d728682022-04-01 01:21:13 +000038 BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " reply " << cert->getName());
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050039 face.receive(*cert);
40 }
Junxiao Shi7d728682022-04-01 01:21:13 +000041 else {
42 BOOST_TEST_MESSAGE("ValidatorFixture processInterest " << interest << " no reply");
43 }
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044 };
45}
46
47void
48ValidatorFixtureBase::mockNetworkOperations()
49{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040050 signal::ScopedConnection conn = face.onSendInterest.connect([this] (const Interest& interest) {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050051 if (processInterest) {
Davide Pesavento2f46d652023-11-09 23:40:01 -050052 boost::asio::post(m_io, [=] { processInterest(interest); });
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050053 }
54 });
55 advanceClocks(s_mockPeriod, s_mockTimes);
56}
57
Davide Pesavento2acce252022-09-08 22:03:03 -040058void
59ValidatorFixtureBase::rewindClockAfterValidation()
60{
61 m_systemClock->advance(s_mockPeriod * s_mockTimes * -1);
62}
63
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050064Identity
65ValidatorFixtureBase::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 Pesavento2f46d652023-11-09 23:40:01 -050074 info.setValidityPeriod(ValidityPeriod::makeRelative(0_s, 90_days));
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050075
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
86Name
87InterestV02Pkt::makeName(Name name, KeyChain& keyChain)
88{
89 Interest interest(name);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050090 SigningInfo params;
91 params.setSignedInterestFormat(SignedInterestFormat::V02);
92 keyChain.sign(interest, params);
93 return interest.getName();
94}
95
96Name
97InterestV03Pkt::makeName(Name name, KeyChain& keyChain)
98{
99 Interest interest(name);
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500100 SigningInfo params;
101 params.setSignedInterestFormat(SignedInterestFormat::V03);
102 keyChain.sign(interest, params);
103 return interest.getName();
104}
105
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400106} // namespace ndn::tests