blob: 3cfb122cf233fcc06c8ef10abda58d16bbe20ac7 [file] [log] [blame]
Yingdi Yu41546342014-11-30 23:37:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2bea5c42017-08-14 20:10:32 +00002/*
Davide Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Yingdi Yu41546342014-11-30 23:37:53 -08004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/validator-config.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050023
Alexander Afanasyev09236c22020-06-03 13:42:38 -040024#include "ndn-cxx/security/certificate-fetcher-offline.hpp"
Davide Pesavento77c5ce82021-05-07 16:12:02 -040025#include "ndn-cxx/security/interest-signer.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "ndn-cxx/util/dummy-client-face.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080027
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029#include "tests/key-chain-fixture.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040030#include "tests/unit/security/validator-config/common.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032namespace ndn::tests {
Yingdi Yu41546342014-11-30 23:37:53 -080033
Junxiao Shid5827ce2016-07-14 20:49:37 +000034BOOST_AUTO_TEST_SUITE(Security)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, KeyChainFixture)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080036
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080037// This test only for API, actual tests are in ValidationPolicyConfig and corresponding CertificateFetchers
38
39BOOST_AUTO_TEST_CASE(Construct)
40{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040041 DummyClientFace face;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080042
43 ValidatorConfig v1(face);
44 BOOST_CHECK_EQUAL(v1.m_policyConfig.m_isConfigured, false);
45
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040046 ValidatorConfig v2(make_unique<security::CertificateFetcherOffline>());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080047 BOOST_CHECK_EQUAL(v2.m_policyConfig.m_isConfigured, false);
48}
49
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050050class ValidatorConfigFixture : public KeyChainFixture
Alexander Afanasyev70244f42017-01-04 12:47:12 -080051{
52public:
53 ValidatorConfigFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050054 : path(boost::filesystem::path(UNIT_TESTS_TMPDIR) / "security" / "validator-config")
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040055 , validator(make_unique<security::CertificateFetcherOffline>())
Alexander Afanasyev70244f42017-01-04 12:47:12 -080056 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080057 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 Afanasyev70244f42017-01-04 12:47:12 -080072 }
73
74public:
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080075 const boost::filesystem::path path;
76 std::string config;
77 std::string configFile;
Alexander Afanasyev70244f42017-01-04 12:47:12 -080078 ValidatorConfig validator;
79};
80
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080081BOOST_FIXTURE_TEST_SUITE(Loads, ValidatorConfigFixture)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070082
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080083BOOST_AUTO_TEST_CASE(FromFile)
Yingdi Yu41546342014-11-30 23:37:53 -080084{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080085 validator.load(configFile);
86 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040087
88 // should reload policy
89 validator.load(configFile);
90 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -080091}
92
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080093BOOST_AUTO_TEST_CASE(FromString)
Yingdi Yu41546342014-11-30 23:37:53 -080094{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080095 validator.load(config, "config-file-from-string");
96 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040097
98 // should reload policy
99 validator.load(config, "config-file-from-string");
100 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800101}
102
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800103BOOST_AUTO_TEST_CASE(FromIstream)
Yingdi Yu41546342014-11-30 23:37:53 -0800104{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800105 std::istringstream is(config);
106 validator.load(is, "config-file-from-istream");
107 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400108
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 Yu41546342014-11-30 23:37:53 -0800113}
114
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800115BOOST_AUTO_TEST_CASE(FromSection)
Yingdi Yu41546342014-11-30 23:37:53 -0800116{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400117 validator.load(makeSection(config), "config-file-from-section");
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800118 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400119
120 // should reload policy
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400121 validator.load(makeSection(config), "config-file-from-section");
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400122 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800123}
124
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800125BOOST_AUTO_TEST_SUITE_END() // Loads
Zhiyi Zhang48becde2017-01-05 16:41:38 -0800126
Davide Pesavento0c526032024-01-31 21:14:01 -0500127BOOST_FIXTURE_TEST_CASE(ValidateCommandInterestWithDigestSha256, ValidatorConfigFixture,
128 * ut::description("test for bug #4635"))
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400129{
130 validator.load(configFile);
131
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400132 security::InterestSigner signer(m_keyChain);
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400133 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 Newberry1caa6342020-08-23 19:29:08 -0700150BOOST_FIXTURE_TEST_CASE(ValidateSignedInterest, ValidatorConfigFixture)
151{
152 validator.load(configFile);
153
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400154 security::InterestSigner signer(m_keyChain);
Eric Newberry1caa6342020-08-23 19:29:08 -0700155 Interest i1("/hello/world");
Eric Newberry1caa6342020-08-23 19:29:08 -0700156 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 Newberry1caa6342020-08-23 19:29:08 -0700168 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
174BOOST_FIXTURE_TEST_CASE(ValidateCommandInterest, ValidatorConfigFixture)
175{
176 validator.load(configFile);
177
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400178 security::InterestSigner signer(m_keyChain);
Eric Newberry1caa6342020-08-23 19:29:08 -0700179 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 Afanasyev31fd4672018-06-17 13:25:52 -0400195
Junxiao Shid5827ce2016-07-14 20:49:37 +0000196BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig
197BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu41546342014-11-30 23:37:53 -0800198
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400199} // namespace ndn::tests