Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [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 | ba2cf39 | 2017-01-13 19:05:23 -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 | #include "ndn-cxx/security/certificate-fetcher-direct-fetch.hpp" |
| 23 | #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/lp/nack.hpp" |
| 25 | #include "ndn-cxx/lp/tags.hpp" |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 28 | #include "tests/unit/security/validator-fixture.hpp" |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 30 | #include <boost/range/adaptor/sliced.hpp> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 31 | #include <boost/range/adaptor/strided.hpp> |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 35 | inline namespace v2 { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 36 | namespace tests { |
| 37 | |
| 38 | using namespace ndn::tests; |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch) |
| 42 | |
| 43 | class Cert |
| 44 | { |
| 45 | }; |
| 46 | |
| 47 | class Timeout |
| 48 | { |
| 49 | }; |
| 50 | |
| 51 | class Nack |
| 52 | { |
| 53 | }; |
| 54 | |
| 55 | template<class Response> |
| 56 | class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 57 | CertificateFetcherDirectFetch> |
| 58 | { |
| 59 | public: |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 60 | enum class ResponseType { |
| 61 | INFRASTRUCTURE, |
| 62 | DIRECT, |
| 63 | BOTH |
| 64 | }; |
| 65 | |
| 66 | public: |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 67 | CertificateFetcherDirectFetchFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 68 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
| 69 | , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest") |
| 70 | , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2") |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 71 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 72 | Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 73 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 74 | |
| 75 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 76 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 77 | m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity)); |
| 78 | |
| 79 | data.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 80 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 81 | |
| 82 | processInterest = [this] (const Interest& interest) { |
| 83 | auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>(); |
| 84 | if (nextHopFaceIdTag == nullptr) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 85 | if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) { |
| 86 | makeResponse(interest); |
| 87 | } |
| 88 | } |
| 89 | else { |
| 90 | if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) { |
| 91 | makeResponse(interest); |
| 92 | } |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | makeResponse(const Interest& interest); |
| 99 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 100 | void |
| 101 | setResponseType(ResponseType type) |
| 102 | { |
| 103 | responseType = type; |
| 104 | } |
| 105 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 106 | public: |
| 107 | Data data; |
| 108 | Interest interest; |
| 109 | Interest interestNoTag; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 110 | ResponseType responseType = ResponseType::INFRASTRUCTURE; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | template<> |
| 114 | void |
| 115 | CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest) |
| 116 | { |
| 117 | auto cert = cache.find(interest); |
| 118 | if (cert == nullptr) { |
| 119 | return; |
| 120 | } |
| 121 | face.receive(*cert); |
| 122 | } |
| 123 | |
| 124 | template<> |
| 125 | void |
| 126 | CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest) |
| 127 | { |
| 128 | // do nothing |
| 129 | } |
| 130 | |
| 131 | template<> |
| 132 | void |
| 133 | CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest) |
| 134 | { |
| 135 | lp::Nack nack(interest); |
| 136 | nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 137 | face.receive(nack); |
| 138 | } |
| 139 | |
| 140 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 141 | |
| 142 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>) |
| 143 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 144 | VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs"); |
| 145 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 146 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 147 | // odd interests |
| 148 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 149 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 150 | } |
| 151 | |
| 152 | // even interests |
| 153 | for (const auto& sentInterest : this->face.sentInterests | |
| 154 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 155 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 156 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 157 | } |
| 158 | } |
| 159 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 160 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>) |
| 161 | { |
| 162 | setResponseType(ResponseType::DIRECT); |
| 163 | static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true); |
| 164 | |
| 165 | VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs"); |
| 166 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 167 | |
| 168 | for (const auto& sentInterest : this->face.sentInterests) { |
| 169 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 170 | } |
| 171 | } |
| 172 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 173 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 174 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 175 | VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout"); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 176 | // Direct fetcher sends two interests each time - to network and face |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 177 | // 3 retries on nack or timeout (2 * (1 + 3) = 8) |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 179 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 180 | // odd interests |
| 181 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 182 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 183 | } |
| 184 | |
| 185 | // even interests |
| 186 | for (const auto& sentInterest : this->face.sentInterests | |
| 187 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 188 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 189 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 190 | } |
| 191 | } |
| 192 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 193 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 194 | { |
| 195 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 196 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 197 | |
| 198 | VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout"); |
| 199 | // Direct fetcher sends two interests each time - to network and face |
| 200 | // 3 retries on nack or timeout (1 + 3 = 4) |
| 201 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 202 | |
| 203 | for (const auto& sentInterest : this->face.sentInterests) { |
| 204 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 209 | { |
| 210 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 211 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 212 | |
| 213 | this->data.template removeTag<lp::IncomingFaceIdTag>(); |
| 214 | this->interest.template removeTag<lp::IncomingFaceIdTag>(); |
| 215 | |
| 216 | VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected"); |
| 217 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0); |
| 218 | BOOST_CHECK(this->lastError.getCode() != ValidationError::Code::IMPLEMENTATION_ERROR); |
| 219 | } |
| 220 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 221 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 222 | { |
| 223 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 224 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 225 | |
| 226 | // odd interests |
| 227 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 228 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 229 | } |
| 230 | |
| 231 | // even interests |
| 232 | for (const auto& sentInterest : this->face.sentInterests | |
| 233 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 234 | boost::adaptors::strided(2)) { |
| 235 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 240 | { |
| 241 | VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout"); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 242 | // Direct fetcher sends two interests each time - to network and face |
| 243 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
| 244 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 245 | |
| 246 | // odd interests |
| 247 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 248 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 249 | } |
| 250 | |
| 251 | // even interests |
| 252 | for (const auto& sentInterest : this->face.sentInterests | |
| 253 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 254 | boost::adaptors::strided(2)) { |
| 255 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 260 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 261 | |
| 262 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 263 | } // inline namespace v2 |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 264 | } // namespace security |
| 265 | } // namespace ndn |