blob: 8ca42453616895cbeb09e14e1173278b0fef5ca5 [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 Pesavento51974f62024-12-21 20:42:45 -050054 : path(std::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 {
Davide Pesavento51974f62024-12-21 20:42:45 -050057 std::filesystem::create_directories(path);
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080058 config = R"CONF(
59 trust-anchor
60 {
61 type any
62 }
63 )CONF";
Davide Pesavento51974f62024-12-21 20:42:45 -050064 configFile = path / "config.conf";
65 std::ofstream(configFile) << config;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080066 }
67
68 ~ValidatorConfigFixture()
69 {
Davide Pesavento51974f62024-12-21 20:42:45 -050070 std::filesystem::remove_all(path);
Alexander Afanasyev70244f42017-01-04 12:47:12 -080071 }
72
73public:
Davide Pesavento51974f62024-12-21 20:42:45 -050074 const std::filesystem::path path;
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080075 std::string config;
76 std::string configFile;
Alexander Afanasyev70244f42017-01-04 12:47:12 -080077 ValidatorConfig validator;
78};
79
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080080BOOST_FIXTURE_TEST_SUITE(Loads, ValidatorConfigFixture)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070081
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080082BOOST_AUTO_TEST_CASE(FromFile)
Yingdi Yu41546342014-11-30 23:37:53 -080083{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080084 validator.load(configFile);
85 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040086
87 // should reload policy
88 validator.load(configFile);
89 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -080090}
91
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080092BOOST_AUTO_TEST_CASE(FromString)
Yingdi Yu41546342014-11-30 23:37:53 -080093{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080094 validator.load(config, "config-file-from-string");
95 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040096
97 // should reload policy
98 validator.load(config, "config-file-from-string");
99 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800100}
101
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800102BOOST_AUTO_TEST_CASE(FromIstream)
Yingdi Yu41546342014-11-30 23:37:53 -0800103{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800104 std::istringstream is(config);
105 validator.load(is, "config-file-from-istream");
106 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400107
108 // should reload policy
109 std::istringstream is2(config);
110 validator.load(is2, "config-file-from-istream");
111 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800112}
113
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800114BOOST_AUTO_TEST_CASE(FromSection)
Yingdi Yu41546342014-11-30 23:37:53 -0800115{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400116 validator.load(makeSection(config), "config-file-from-section");
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800117 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400118
119 // should reload policy
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400120 validator.load(makeSection(config), "config-file-from-section");
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400121 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800122}
123
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800124BOOST_AUTO_TEST_SUITE_END() // Loads
Zhiyi Zhang48becde2017-01-05 16:41:38 -0800125
Davide Pesavento0c526032024-01-31 21:14:01 -0500126BOOST_FIXTURE_TEST_CASE(ValidateCommandInterestWithDigestSha256, ValidatorConfigFixture,
127 * ut::description("test for bug #4635"))
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400128{
129 validator.load(configFile);
130
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400131 security::InterestSigner signer(m_keyChain);
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400132 auto i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256());
133 size_t nValidated = 0, nFailed = 0;
134
135 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
136 BOOST_CHECK_EQUAL(nValidated, 1);
137 BOOST_CHECK_EQUAL(nFailed, 0);
138
139 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
140 BOOST_CHECK_EQUAL(nValidated, 1);
141 BOOST_CHECK_EQUAL(nFailed, 1);
142
143 i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256());
144 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
145 BOOST_CHECK_EQUAL(nValidated, 2);
146 BOOST_CHECK_EQUAL(nFailed, 1);
147}
148
Eric Newberry1caa6342020-08-23 19:29:08 -0700149BOOST_FIXTURE_TEST_CASE(ValidateSignedInterest, ValidatorConfigFixture)
150{
151 validator.load(configFile);
152
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400153 security::InterestSigner signer(m_keyChain);
Eric Newberry1caa6342020-08-23 19:29:08 -0700154 Interest i1("/hello/world");
Eric Newberry1caa6342020-08-23 19:29:08 -0700155 signer.makeSignedInterest(i1);
156 size_t nValidated = 0, nFailed = 0;
157
158 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
159 BOOST_CHECK_EQUAL(nValidated, 1);
160 BOOST_CHECK_EQUAL(nFailed, 0);
161
162 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
163 BOOST_CHECK_EQUAL(nValidated, 1);
164 BOOST_CHECK_EQUAL(nFailed, 1);
165
166 Interest i2("/hello/world");
Eric Newberry1caa6342020-08-23 19:29:08 -0700167 signer.makeSignedInterest(i2, signingWithSha256());
168 validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
169 BOOST_CHECK_EQUAL(nValidated, 2);
170 BOOST_CHECK_EQUAL(nFailed, 1);
171}
172
173BOOST_FIXTURE_TEST_CASE(ValidateCommandInterest, ValidatorConfigFixture)
174{
175 validator.load(configFile);
176
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400177 security::InterestSigner signer(m_keyChain);
Eric Newberry1caa6342020-08-23 19:29:08 -0700178 auto i1 = signer.makeCommandInterest("/hello/world");
179 size_t nValidated = 0, nFailed = 0;
180
181 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
182 BOOST_CHECK_EQUAL(nValidated, 1);
183 BOOST_CHECK_EQUAL(nFailed, 0);
184
185 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
186 BOOST_CHECK_EQUAL(nValidated, 1);
187 BOOST_CHECK_EQUAL(nFailed, 1);
188
189 auto i2 = signer.makeCommandInterest("/hello/world");
190 validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
191 BOOST_CHECK_EQUAL(nValidated, 2);
192 BOOST_CHECK_EQUAL(nFailed, 1);
193}
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400194
Junxiao Shid5827ce2016-07-14 20:49:37 +0000195BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig
196BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu41546342014-11-30 23:37:53 -0800197
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400198} // namespace ndn::tests