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