Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [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 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 22 | #ifndef NDN_CXX_TESTS_UNIT_SECURITY_VALIDATOR_FIXTURE_HPP |
| 23 | #define NDN_CXX_TESTS_UNIT_SECURITY_VALIDATOR_FIXTURE_HPP |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/validator.hpp" |
| 26 | #include "ndn-cxx/security/certificate-fetcher-from-network.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 30 | #include "tests/unit/io-key-chain-fixture.hpp" |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 31 | |
| 32 | #include <boost/lexical_cast.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 36 | inline namespace v2 { |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 37 | namespace tests { |
| 38 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 39 | class ValidatorFixtureBase : public ndn::tests::IoKeyChainFixture |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 40 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 41 | protected: |
| 42 | ValidatorFixtureBase(); |
| 43 | |
| 44 | void |
| 45 | mockNetworkOperations(); |
| 46 | |
| 47 | /** \brief undo clock advancement of mockNetworkOperations() |
| 48 | */ |
| 49 | void |
| 50 | rewindClockAfterValidation() |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 51 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 52 | m_systemClock->advance(s_mockPeriod * s_mockTimes * -1); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 55 | /** |
| 56 | * @brief Issues a certificate for @p subIdentityName signed by @p issuer |
| 57 | * |
| 58 | * If the identity does not exist, it is created. |
| 59 | * A new key is generated as the default key for the identity. |
| 60 | * A default certificate for the key is signed by the issuer using its default certificate. |
| 61 | * |
| 62 | * @return The sub-identity |
| 63 | */ |
| 64 | Identity |
| 65 | addSubCertificate(const Name& subIdentityName, const Identity& issuer); |
| 66 | |
| 67 | protected: |
| 68 | util::DummyClientFace face{m_io, {true, true}}; |
| 69 | std::function<void(const Interest&)> processInterest; |
| 70 | CertificateCache cache{100_days}; |
Davide Pesavento | 87208f9 | 2022-08-30 19:51:23 -0400 | [diff] [blame] | 71 | ValidationError lastError{ValidationError::NO_ERROR}; |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | const static time::milliseconds s_mockPeriod; |
| 75 | const static int s_mockTimes; |
| 76 | }; |
| 77 | |
| 78 | template<class ValidationPolicyT, class CertificateFetcherT = CertificateFetcherFromNetwork> |
| 79 | class ValidatorFixture : public ValidatorFixtureBase |
| 80 | { |
| 81 | protected: |
| 82 | ValidatorFixture() |
| 83 | : validator(make_unique<ValidationPolicyT>(), make_unique<CertificateFetcherT>(face)) |
| 84 | , policy(static_cast<ValidationPolicyT&>(validator.getPolicy())) |
| 85 | { |
| 86 | } |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 87 | |
| 88 | template<class Packet> |
| 89 | void |
| 90 | validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line) |
| 91 | { |
| 92 | std::string detailedInfo = msg + " on line " + to_string(line); |
| 93 | size_t nCallbacks = 0; |
| 94 | this->validator.validate(packet, |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 95 | [&] (const Packet&) { |
| 96 | ++nCallbacks; |
| 97 | BOOST_CHECK_MESSAGE(expectSuccess, |
| 98 | (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo); |
| 99 | }, |
| 100 | [&] (const Packet&, const ValidationError& error) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 101 | lastError = error; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 102 | ++nCallbacks; |
| 103 | BOOST_CHECK_MESSAGE(!expectSuccess, |
| 104 | (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo + |
| 105 | (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : "")); |
| 106 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 107 | |
| 108 | mockNetworkOperations(); |
| 109 | BOOST_CHECK_EQUAL(nCallbacks, 1); |
| 110 | } |
| 111 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 112 | protected: |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 113 | Validator validator; |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 114 | ValidationPolicyT& policy; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 117 | template<class ValidationPolicyT, class CertificateFetcherT = CertificateFetcherFromNetwork> |
| 118 | class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicyT, CertificateFetcherT> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 119 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 120 | protected: |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 121 | HierarchicalValidatorFixture() |
| 122 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 123 | identity = this->m_keyChain.createIdentity("/Security/ValidatorFixture"); |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 124 | subIdentity = this->addSubCertificate("/Security/ValidatorFixture/Sub1", identity); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 125 | subSelfSignedIdentity = this->m_keyChain.createIdentity("/Security/ValidatorFixture/Sub1/Sub2"); |
| 126 | otherIdentity = this->m_keyChain.createIdentity("/Security/OtherIdentity"); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 127 | |
| 128 | this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate())); |
| 129 | |
| 130 | this->cache.insert(identity.getDefaultKey().getDefaultCertificate()); |
| 131 | this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate()); |
| 132 | this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate()); |
| 133 | this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate()); |
| 134 | } |
| 135 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 136 | protected: |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 137 | Identity identity; |
| 138 | Identity subIdentity; |
| 139 | Identity subSelfSignedIdentity; |
| 140 | Identity otherIdentity; |
| 141 | }; |
| 142 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 143 | #define VALIDATE_SUCCESS(packet, message) this->validate(packet, message, true, __LINE__) |
| 144 | #define VALIDATE_FAILURE(packet, message) this->validate(packet, message, false, __LINE__) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 146 | class DummyValidationState : public ValidationState |
| 147 | { |
| 148 | public: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 149 | ~DummyValidationState() override |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 150 | { |
| 151 | m_outcome = false; |
| 152 | } |
| 153 | |
| 154 | void |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 155 | fail(const ValidationError&) override |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 156 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 157 | m_outcome = false; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | void |
Justin Labry | aef53b6 | 2021-03-10 06:07:27 +0000 | [diff] [blame] | 162 | verifyOriginalPacket(const optional<Certificate>&) override |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 163 | { |
| 164 | // do nothing |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | bypassValidation() override |
| 169 | { |
| 170 | // do nothing |
| 171 | } |
| 172 | }; |
| 173 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 174 | struct DataPkt |
| 175 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 176 | static constexpr uint32_t |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 177 | getType() |
| 178 | { |
| 179 | return tlv::Data; |
| 180 | } |
| 181 | |
| 182 | static Name |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 183 | makeName(Name name, KeyChain&) |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 184 | { |
| 185 | return name; |
| 186 | } |
| 187 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 188 | static Data |
| 189 | makePacket(const Name& name) |
| 190 | { |
| 191 | return Data(name); |
| 192 | } |
| 193 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 194 | static shared_ptr<ValidationState> |
| 195 | makeState() |
| 196 | { |
| 197 | return make_shared<DummyValidationState>(); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | struct InterestV02Pkt |
| 202 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 203 | static constexpr uint32_t |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 204 | getType() |
| 205 | { |
| 206 | return tlv::Interest; |
| 207 | } |
| 208 | |
| 209 | static Name |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 210 | makeName(Name name, KeyChain& keyChain); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 211 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 212 | static Interest |
| 213 | makePacket(const Name& name) |
| 214 | { |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 215 | return Interest(name); |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 218 | static shared_ptr<ValidationState> |
| 219 | makeState() |
| 220 | { |
| 221 | auto state = make_shared<DummyValidationState>(); |
| 222 | state->setTag(make_shared<SignedInterestFormatTag>(SignedInterestFormat::V02)); |
| 223 | return state; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | struct InterestV03Pkt |
| 228 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 229 | static constexpr uint32_t |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 230 | getType() |
| 231 | { |
| 232 | return tlv::Interest; |
| 233 | } |
| 234 | |
| 235 | static Name |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 236 | makeName(Name name, KeyChain& keyChain); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 237 | |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 238 | static Interest |
| 239 | makePacket(const Name& name) |
| 240 | { |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 241 | return Interest(name); |
Davide Pesavento | 1a4a7bf | 2020-12-04 22:30:46 -0500 | [diff] [blame] | 242 | } |
| 243 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 244 | static shared_ptr<ValidationState> |
| 245 | makeState() |
| 246 | { |
| 247 | auto state = make_shared<DummyValidationState>(); |
| 248 | state->setTag(make_shared<SignedInterestFormatTag>(SignedInterestFormat::V03)); |
| 249 | return state; |
| 250 | } |
| 251 | }; |
| 252 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 253 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 254 | } // inline namespace v2 |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 255 | } // namespace security |
| 256 | } // namespace ndn |
| 257 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 258 | #endif // NDN_CXX_TESTS_UNIT_SECURITY_VALIDATOR_FIXTURE_HPP |