Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -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 | |
| 22 | #include "security/v2/certificate-bundle-fetcher.hpp" |
| 23 | #include "security/v2/validation-policy-simple-hierarchy.hpp" |
| 24 | #include "util/regex/regex-pattern-list-matcher.hpp" |
| 25 | #include "lp/nack.hpp" |
| 26 | |
| 27 | #include "boost-test.hpp" |
| 28 | #include "validator-fixture.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace v2 { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace ndn::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
| 38 | BOOST_AUTO_TEST_SUITE(V2) |
| 39 | BOOST_AUTO_TEST_SUITE(TestCertificateBundleFetcher) |
| 40 | |
| 41 | class CertificateBundleFetcherWrapper : public CertificateBundleFetcher |
| 42 | { |
| 43 | public: |
| 44 | CertificateBundleFetcherWrapper(Face& face) |
| 45 | : CertificateBundleFetcher(make_unique<CertificateFetcherFromNetwork>(face), face) |
| 46 | { |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | class Bundle |
| 51 | { |
| 52 | }; |
| 53 | |
| 54 | class Cert |
| 55 | { |
| 56 | }; |
| 57 | |
| 58 | class Timeout |
| 59 | { |
| 60 | }; |
| 61 | |
| 62 | class Nack |
| 63 | { |
| 64 | }; |
| 65 | |
| 66 | template<class Response> |
| 67 | class CertificateBundleFetcherFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 68 | CertificateBundleFetcherWrapper> |
| 69 | { |
| 70 | public: |
| 71 | CertificateBundleFetcherFixture() |
| 72 | : data("/Security/V2/ValidatorFixture/Sub1/Sub3/Data") |
| 73 | { |
| 74 | subSubIdentity = addSubCertificate("/Security/V2/ValidatorFixture/Sub1/Sub3", subIdentity); |
| 75 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 76 | |
| 77 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 78 | bundleRegexMatcher = make_shared<RegexPatternListMatcher>("<>*<_BUNDLE><>*", nullptr); |
| 79 | processInterest = [this] (const Interest& interest) { |
| 80 | // check if the interest is for Bundle or individual certificates |
| 81 | if (bundleRegexMatcher->match(interest.getName(), 0, interest.getName().size())) { |
| 82 | makeResponse(interest); |
| 83 | } |
| 84 | else { |
| 85 | auto cert = cache.find(interest); |
| 86 | if (cert == nullptr) { |
| 87 | return; |
| 88 | } |
| 89 | face.receive(*cert); |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | makeResponse(const Interest& interest); |
| 96 | |
| 97 | public: |
| 98 | Data data; |
| 99 | Identity subSubIdentity; |
| 100 | shared_ptr<RegexPatternListMatcher> bundleRegexMatcher; |
| 101 | }; |
| 102 | |
| 103 | template<> |
| 104 | void |
| 105 | CertificateBundleFetcherFixture<Bundle>::makeResponse(const Interest& interest) |
| 106 | { |
| 107 | Block certList = Block(tlv::Content); |
| 108 | Name bundleName(interest.getName()); |
| 109 | |
| 110 | if (!bundleName.get(-1).isSegment() || bundleName.get(-1).toSegment() == 0) { |
| 111 | Block subSubCert = subSubIdentity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 112 | certList.push_back(subSubCert); |
| 113 | |
| 114 | if (!bundleName.get(-1).isSegment()) { |
| 115 | bundleName |
| 116 | .appendVersion() |
| 117 | .appendSegment(0); |
| 118 | } |
| 119 | } |
| 120 | else { |
| 121 | Block subCert = subIdentity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 122 | Block anchor = identity.getDefaultKey().getDefaultCertificate().wireEncode(); |
| 123 | certList.push_back(subCert); |
| 124 | certList.push_back(anchor); |
| 125 | } |
| 126 | |
| 127 | shared_ptr<Data> certBundle = make_shared<Data>(); |
| 128 | certBundle->setName(bundleName); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 129 | certBundle->setFreshnessPeriod(100_s); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 130 | certBundle->setContent(certList); |
| 131 | certBundle->setFinalBlockId(name::Component::fromSegment(1)); |
| 132 | |
| 133 | m_keyChain.sign(*certBundle, signingWithSha256()); |
| 134 | |
| 135 | face.receive(*certBundle); |
| 136 | } |
| 137 | |
| 138 | template<> |
| 139 | void |
| 140 | CertificateBundleFetcherFixture<Timeout>::makeResponse(const Interest& interest) |
| 141 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 142 | this->advanceClocks(200_s); |
Manika Mittal | 95c8070 | 2017-01-27 09:54:41 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template<> |
| 146 | void |
| 147 | CertificateBundleFetcherFixture<Nack>::makeResponse(const Interest& interest) |
| 148 | { |
| 149 | lp::Nack nack(interest); |
| 150 | nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 151 | face.receive(nack); |
| 152 | } |
| 153 | |
| 154 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessWithBundle, CertificateBundleFetcherFixture<Bundle>) |
| 155 | { |
| 156 | VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the bundle segments"); |
| 157 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); // produced bundle has 2 segments |
| 158 | |
| 159 | for (const auto& sentInterest : this->face.sentInterests) { |
| 160 | BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | using SuccessWithoutBundle = boost::mpl::vector<Nack, Timeout>; |
| 165 | |
| 166 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithoutBundle, T, SuccessWithoutBundle, CertificateBundleFetcherFixture<T>) |
| 167 | { |
| 168 | VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the certs"); |
| 169 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); // since interest for Bundle fails, each cert is retrieved |
| 170 | |
| 171 | bool toggle = true; |
| 172 | for (const auto& sentInterest : this->face.sentInterests) { |
| 173 | if (toggle) { |
| 174 | // every alternate interest is going to be that of a bundle |
| 175 | BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 176 | } |
| 177 | else { |
| 178 | BOOST_CHECK(!this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size())); |
| 179 | } |
| 180 | toggle = !toggle; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateBundleFetcher |
| 185 | BOOST_AUTO_TEST_SUITE_END() // V2 |
| 186 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 187 | |
| 188 | } // namespace tests |
| 189 | } // namespace v2 |
| 190 | } // namespace security |
| 191 | } // namespace ndn |