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 | /* |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #ifndef NDN_TESTS_UNIT_SECURITY_VALIDATOR_FIXTURE_HPP |
| 23 | #define NDN_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" |
| 30 | #include "tests/unit/identity-management-time-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 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 39 | template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 40 | class ValidatorFixture : public ndn::tests::IdentityManagementTimeFixture |
| 41 | { |
| 42 | public: |
| 43 | ValidatorFixture() |
| 44 | : face(io, {true, true}) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 45 | , validator(make_unique<ValidationPolicy>(), make_unique<CertificateFetcher>(face)) |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 46 | , policy(static_cast<ValidationPolicy&>(validator.getPolicy())) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 47 | , cache(100_days) |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 48 | , lastError(ValidationError::Code::NO_ERROR) |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 49 | { |
| 50 | processInterest = [this] (const Interest& interest) { |
| 51 | auto cert = cache.find(interest); |
| 52 | if (cert != nullptr) { |
| 53 | face.receive(*cert); |
| 54 | } |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | virtual |
| 59 | ~ValidatorFixture() = default; |
| 60 | |
| 61 | template<class Packet> |
| 62 | void |
| 63 | validate(const Packet& packet, const std::string& msg, bool expectSuccess, int line) |
| 64 | { |
| 65 | std::string detailedInfo = msg + " on line " + to_string(line); |
| 66 | size_t nCallbacks = 0; |
| 67 | this->validator.validate(packet, |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 68 | [&] (const Packet&) { |
| 69 | ++nCallbacks; |
| 70 | BOOST_CHECK_MESSAGE(expectSuccess, |
| 71 | (expectSuccess ? "OK: " : "FAILED: ") + detailedInfo); |
| 72 | }, |
| 73 | [&] (const Packet&, const ValidationError& error) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 74 | lastError = error; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 75 | ++nCallbacks; |
| 76 | BOOST_CHECK_MESSAGE(!expectSuccess, |
| 77 | (!expectSuccess ? "OK: " : "FAILED: ") + detailedInfo + |
| 78 | (expectSuccess ? " (" + boost::lexical_cast<std::string>(error) + ")" : "")); |
| 79 | }); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 80 | |
| 81 | mockNetworkOperations(); |
| 82 | BOOST_CHECK_EQUAL(nCallbacks, 1); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | mockNetworkOperations() |
| 87 | { |
| 88 | util::signal::ScopedConnection connection = face.onSendInterest.connect([this] (const Interest& interest) { |
| 89 | if (processInterest != nullptr) { |
| 90 | io.post(bind(processInterest, interest)); |
| 91 | } |
| 92 | }); |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 93 | advanceClocks(time::milliseconds(s_mockPeriod), s_mockTimes); |
| 94 | } |
| 95 | |
| 96 | /** \brief undo clock advancement of mockNetworkOperations |
| 97 | */ |
| 98 | void |
| 99 | rewindClockAfterValidation() |
| 100 | { |
| 101 | this->systemClock->advance(time::milliseconds(s_mockPeriod * s_mockTimes * -1)); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public: |
| 105 | util::DummyClientFace face; |
| 106 | std::function<void(const Interest& interest)> processInterest; |
| 107 | Validator validator; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 108 | ValidationPolicy& policy; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 109 | |
| 110 | CertificateCache cache; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 111 | ValidationError lastError; |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | const static int s_mockPeriod; |
| 115 | const static int s_mockTimes; |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
Alexander Afanasyev | 9333887 | 2017-01-30 22:37:00 -0800 | [diff] [blame] | 118 | template<class ValidationPolicy, class CertificateFetcher> |
| 119 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockPeriod = 250; |
| 120 | |
| 121 | template<class ValidationPolicy, class CertificateFetcher> |
| 122 | const int ValidatorFixture<ValidationPolicy, CertificateFetcher>::s_mockTimes = 200; |
| 123 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 124 | template<class ValidationPolicy, class CertificateFetcher = CertificateFetcherFromNetwork> |
| 125 | class HierarchicalValidatorFixture : public ValidatorFixture<ValidationPolicy, CertificateFetcher> |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 126 | { |
| 127 | public: |
| 128 | HierarchicalValidatorFixture() |
| 129 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 130 | identity = this->addIdentity("/Security/ValidatorFixture"); |
| 131 | subIdentity = this->addSubCertificate("/Security/ValidatorFixture/Sub1", identity); |
| 132 | subSelfSignedIdentity = this->addIdentity("/Security/ValidatorFixture/Sub1/Sub2"); |
| 133 | otherIdentity = this->addIdentity("/Security/OtherIdentity"); |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 134 | |
| 135 | this->validator.loadAnchor("", Certificate(identity.getDefaultKey().getDefaultCertificate())); |
| 136 | |
| 137 | this->cache.insert(identity.getDefaultKey().getDefaultCertificate()); |
| 138 | this->cache.insert(subIdentity.getDefaultKey().getDefaultCertificate()); |
| 139 | this->cache.insert(subSelfSignedIdentity.getDefaultKey().getDefaultCertificate()); |
| 140 | this->cache.insert(otherIdentity.getDefaultKey().getDefaultCertificate()); |
| 141 | } |
| 142 | |
| 143 | public: |
| 144 | Identity identity; |
| 145 | Identity subIdentity; |
| 146 | Identity subSelfSignedIdentity; |
| 147 | Identity otherIdentity; |
| 148 | }; |
| 149 | |
| 150 | #define VALIDATE_SUCCESS(packet, message) this->template validate(packet, message, true, __LINE__) |
| 151 | #define VALIDATE_FAILURE(packet, message) this->template validate(packet, message, false, __LINE__) |
| 152 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 153 | class DummyValidationState : public ValidationState |
| 154 | { |
| 155 | public: |
| 156 | ~DummyValidationState() |
| 157 | { |
| 158 | m_outcome = false; |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | fail(const ValidationError& error) override |
| 163 | { |
| 164 | // BOOST_TEST_MESSAGE(error); |
| 165 | m_outcome = false; |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | void |
| 170 | verifyOriginalPacket(const Certificate& trustedCert) override |
| 171 | { |
| 172 | // do nothing |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | bypassValidation() override |
| 177 | { |
| 178 | // do nothing |
| 179 | } |
| 180 | }; |
| 181 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame^] | 182 | |
| 183 | struct DataPkt |
| 184 | { |
| 185 | static uint32_t |
| 186 | getType() |
| 187 | { |
| 188 | return tlv::Data; |
| 189 | } |
| 190 | |
| 191 | static Name |
| 192 | makeName(Name name, KeyChain& keyChain) |
| 193 | { |
| 194 | return name; |
| 195 | } |
| 196 | |
| 197 | static shared_ptr<ValidationState> |
| 198 | makeState() |
| 199 | { |
| 200 | return make_shared<DummyValidationState>(); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | struct InterestV02Pkt |
| 205 | { |
| 206 | static uint32_t |
| 207 | getType() |
| 208 | { |
| 209 | return tlv::Interest; |
| 210 | } |
| 211 | |
| 212 | static Name |
| 213 | makeName(Name name, KeyChain& keyChain) |
| 214 | { |
| 215 | Interest interest(name); |
| 216 | interest.setCanBePrefix(false); |
| 217 | SigningInfo params; |
| 218 | params.setSignedInterestFormat(SignedInterestFormat::V02); |
| 219 | keyChain.sign(interest, params); |
| 220 | return interest.getName(); |
| 221 | } |
| 222 | |
| 223 | static shared_ptr<ValidationState> |
| 224 | makeState() |
| 225 | { |
| 226 | auto state = make_shared<DummyValidationState>(); |
| 227 | state->setTag(make_shared<SignedInterestFormatTag>(SignedInterestFormat::V02)); |
| 228 | return state; |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | struct InterestV03Pkt |
| 233 | { |
| 234 | static uint32_t |
| 235 | getType() |
| 236 | { |
| 237 | return tlv::Interest; |
| 238 | } |
| 239 | |
| 240 | static Name |
| 241 | makeName(Name name, KeyChain& keyChain) |
| 242 | { |
| 243 | Interest interest(name); |
| 244 | interest.setCanBePrefix(false); |
| 245 | SigningInfo params; |
| 246 | params.setSignedInterestFormat(SignedInterestFormat::V03); |
| 247 | keyChain.sign(interest, params); |
| 248 | return interest.getName(); |
| 249 | } |
| 250 | |
| 251 | static shared_ptr<ValidationState> |
| 252 | makeState() |
| 253 | { |
| 254 | auto state = make_shared<DummyValidationState>(); |
| 255 | state->setTag(make_shared<SignedInterestFormatTag>(SignedInterestFormat::V03)); |
| 256 | return state; |
| 257 | } |
| 258 | }; |
| 259 | |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 260 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 261 | } // inline namespace v2 |
Alexander Afanasyev | 7e72141 | 2017-01-11 13:36:08 -0800 | [diff] [blame] | 262 | } // namespace security |
| 263 | } // namespace ndn |
| 264 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 265 | #endif // NDN_TESTS_UNIT_SECURITY_VALIDATOR_FIXTURE_HPP |