Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2bea5c4 | 2017-08-14 20:10:32 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -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 | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/validator-config.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 23 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 24 | #include "ndn-cxx/security/certificate-fetcher-offline.hpp" |
Davide Pesavento | 77c5ce8 | 2021-05-07 16:12:02 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/interest-signer.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 29 | #include "tests/key-chain-fixture.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 30 | #include "tests/unit/security/validator-config/common.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | namespace ndn::tests { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 33 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Security) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, KeyChainFixture) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 37 | // This test only for API, actual tests are in ValidationPolicyConfig and corresponding CertificateFetchers |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE(Construct) |
| 40 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 41 | DummyClientFace face; |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 42 | |
| 43 | ValidatorConfig v1(face); |
| 44 | BOOST_CHECK_EQUAL(v1.m_policyConfig.m_isConfigured, false); |
| 45 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 46 | ValidatorConfig v2(make_unique<security::CertificateFetcherOffline>()); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(v2.m_policyConfig.m_isConfigured, false); |
| 48 | } |
| 49 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 50 | class ValidatorConfigFixture : public KeyChainFixture |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 51 | { |
| 52 | public: |
| 53 | ValidatorConfigFixture() |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 54 | : path(boost::filesystem::path(UNIT_TESTS_TMPDIR) / "security" / "validator-config") |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 55 | , validator(make_unique<security::CertificateFetcherOffline>()) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 56 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 57 | boost::filesystem::create_directories(path); |
| 58 | config = R"CONF( |
| 59 | trust-anchor |
| 60 | { |
| 61 | type any |
| 62 | } |
| 63 | )CONF"; |
| 64 | configFile = (this->path / "config.conf").string(); |
| 65 | std::ofstream f(configFile.c_str()); |
| 66 | f << config; |
| 67 | } |
| 68 | |
| 69 | ~ValidatorConfigFixture() |
| 70 | { |
| 71 | boost::filesystem::remove_all(path); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public: |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 75 | const boost::filesystem::path path; |
| 76 | std::string config; |
| 77 | std::string configFile; |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 78 | ValidatorConfig validator; |
| 79 | }; |
| 80 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 81 | BOOST_FIXTURE_TEST_SUITE(Loads, ValidatorConfigFixture) |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(FromFile) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 84 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 85 | validator.load(configFile); |
| 86 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 87 | |
| 88 | // should reload policy |
| 89 | validator.load(configFile); |
| 90 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(FromString) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 95 | validator.load(config, "config-file-from-string"); |
| 96 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 97 | |
| 98 | // should reload policy |
| 99 | validator.load(config, "config-file-from-string"); |
| 100 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(FromIstream) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 104 | { |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 105 | std::istringstream is(config); |
| 106 | validator.load(is, "config-file-from-istream"); |
| 107 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 108 | |
| 109 | // should reload policy |
| 110 | std::istringstream is2(config); |
| 111 | validator.load(is2, "config-file-from-istream"); |
| 112 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(FromSection) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 116 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 117 | validator.load(makeSection(config), "config-file-from-section"); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 119 | |
| 120 | // should reload policy |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 121 | validator.load(makeSection(config), "config-file-from-section"); |
Alexander Afanasyev | 6aff024 | 2017-08-29 17:14:44 -0400 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 125 | BOOST_AUTO_TEST_SUITE_END() // Loads |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 127 | |
| 128 | BOOST_FIXTURE_TEST_CASE(ValidateCommandInterestWithDigestSha256, ValidatorConfigFixture) // Bug 4635 |
| 129 | { |
| 130 | validator.load(configFile); |
| 131 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 132 | security::InterestSigner signer(m_keyChain); |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 133 | auto i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256()); |
| 134 | size_t nValidated = 0, nFailed = 0; |
| 135 | |
| 136 | validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 137 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 138 | BOOST_CHECK_EQUAL(nFailed, 0); |
| 139 | |
| 140 | validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 141 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 142 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 143 | |
| 144 | i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256()); |
| 145 | validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 146 | BOOST_CHECK_EQUAL(nValidated, 2); |
| 147 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 148 | } |
| 149 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 150 | BOOST_FIXTURE_TEST_CASE(ValidateSignedInterest, ValidatorConfigFixture) |
| 151 | { |
| 152 | validator.load(configFile); |
| 153 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 154 | security::InterestSigner signer(m_keyChain); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 155 | Interest i1("/hello/world"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 156 | signer.makeSignedInterest(i1); |
| 157 | size_t nValidated = 0, nFailed = 0; |
| 158 | |
| 159 | validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 160 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 161 | BOOST_CHECK_EQUAL(nFailed, 0); |
| 162 | |
| 163 | validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 164 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 165 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 166 | |
| 167 | Interest i2("/hello/world"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 168 | signer.makeSignedInterest(i2, signingWithSha256()); |
| 169 | validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 170 | BOOST_CHECK_EQUAL(nValidated, 2); |
| 171 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 172 | } |
| 173 | |
| 174 | BOOST_FIXTURE_TEST_CASE(ValidateCommandInterest, ValidatorConfigFixture) |
| 175 | { |
| 176 | validator.load(configFile); |
| 177 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 178 | security::InterestSigner signer(m_keyChain); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 179 | auto i1 = signer.makeCommandInterest("/hello/world"); |
| 180 | size_t nValidated = 0, nFailed = 0; |
| 181 | |
| 182 | validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 183 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 184 | BOOST_CHECK_EQUAL(nFailed, 0); |
| 185 | |
| 186 | validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 187 | BOOST_CHECK_EQUAL(nValidated, 1); |
| 188 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 189 | |
| 190 | auto i2 = signer.makeCommandInterest("/hello/world"); |
| 191 | validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; }); |
| 192 | BOOST_CHECK_EQUAL(nValidated, 2); |
| 193 | BOOST_CHECK_EQUAL(nFailed, 1); |
| 194 | } |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 195 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 196 | BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig |
| 197 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 198 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 199 | } // namespace ndn::tests |