blob: 0f4f694ae74c0bfa2c2ae9ca17e5b3fb404a80d4 [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/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 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 Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/security/command-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
Yingdi Yu41546342014-11-30 23:37:53 -080032namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070033namespace security {
Yingdi Yu41546342014-11-30 23:37:53 -080034namespace tests {
35
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070036using namespace ndn::tests;
Yingdi Yu41546342014-11-30 23:37:53 -080037
Junxiao Shid5827ce2016-07-14 20:49:37 +000038BOOST_AUTO_TEST_SUITE(Security)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050039BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, KeyChainFixture)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080040
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080041// This test only for API, actual tests are in ValidationPolicyConfig and corresponding CertificateFetchers
42
43BOOST_AUTO_TEST_CASE(Construct)
44{
45 util::DummyClientFace face;
46
47 ValidatorConfig v1(face);
48 BOOST_CHECK_EQUAL(v1.m_policyConfig.m_isConfigured, false);
49
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050050 ValidatorConfig v2(make_unique<CertificateFetcherOffline>());
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080051 BOOST_CHECK_EQUAL(v2.m_policyConfig.m_isConfigured, false);
52}
53
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050054class ValidatorConfigFixture : public KeyChainFixture
Alexander Afanasyev70244f42017-01-04 12:47:12 -080055{
56public:
57 ValidatorConfigFixture()
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050058 : path(boost::filesystem::path(UNIT_TESTS_TMPDIR) / "security" / "validator-config")
59 , validator(make_unique<CertificateFetcherOffline>())
Alexander Afanasyev70244f42017-01-04 12:47:12 -080060 {
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080061 boost::filesystem::create_directories(path);
62 config = R"CONF(
63 trust-anchor
64 {
65 type any
66 }
67 )CONF";
68 configFile = (this->path / "config.conf").string();
69 std::ofstream f(configFile.c_str());
70 f << config;
71 }
72
73 ~ValidatorConfigFixture()
74 {
75 boost::filesystem::remove_all(path);
Alexander Afanasyev70244f42017-01-04 12:47:12 -080076 }
77
78public:
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080079 const boost::filesystem::path path;
80 std::string config;
81 std::string configFile;
Alexander Afanasyev70244f42017-01-04 12:47:12 -080082 ValidatorConfig validator;
83};
84
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080085BOOST_FIXTURE_TEST_SUITE(Loads, ValidatorConfigFixture)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070086
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080087BOOST_AUTO_TEST_CASE(FromFile)
Yingdi Yu41546342014-11-30 23:37:53 -080088{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080089 validator.load(configFile);
90 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -040091
92 // should reload policy
93 validator.load(configFile);
94 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -080095}
96
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080097BOOST_AUTO_TEST_CASE(FromString)
Yingdi Yu41546342014-11-30 23:37:53 -080098{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -080099 validator.load(config, "config-file-from-string");
100 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400101
102 // should reload policy
103 validator.load(config, "config-file-from-string");
104 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800105}
106
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800107BOOST_AUTO_TEST_CASE(FromIstream)
Yingdi Yu41546342014-11-30 23:37:53 -0800108{
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800109 std::istringstream is(config);
110 validator.load(is, "config-file-from-istream");
111 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400112
113 // should reload policy
114 std::istringstream is2(config);
115 validator.load(is2, "config-file-from-istream");
116 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800117}
118
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800119BOOST_AUTO_TEST_CASE(FromSection)
Yingdi Yu41546342014-11-30 23:37:53 -0800120{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500121 validator.load(validator_config::tests::makeSection(config), "config-file-from-section");
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800122 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400123
124 // should reload policy
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500125 validator.load(validator_config::tests::makeSection(config), "config-file-from-section");
Alexander Afanasyev6aff0242017-08-29 17:14:44 -0400126 BOOST_CHECK_EQUAL(validator.m_policyConfig.m_isConfigured, true);
Yingdi Yu41546342014-11-30 23:37:53 -0800127}
128
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -0800129BOOST_AUTO_TEST_SUITE_END() // Loads
Zhiyi Zhang48becde2017-01-05 16:41:38 -0800130
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400131
132BOOST_FIXTURE_TEST_CASE(ValidateCommandInterestWithDigestSha256, ValidatorConfigFixture) // Bug 4635
133{
134 validator.load(configFile);
135
Eric Newberry1caa6342020-08-23 19:29:08 -0700136 InterestSigner signer(m_keyChain);
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400137 auto i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256());
138 size_t nValidated = 0, nFailed = 0;
139
140 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
141 BOOST_CHECK_EQUAL(nValidated, 1);
142 BOOST_CHECK_EQUAL(nFailed, 0);
143
144 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
145 BOOST_CHECK_EQUAL(nValidated, 1);
146 BOOST_CHECK_EQUAL(nFailed, 1);
147
148 i = signer.makeCommandInterest("/hello/world/CMD", signingWithSha256());
149 validator.validate(i, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
150 BOOST_CHECK_EQUAL(nValidated, 2);
151 BOOST_CHECK_EQUAL(nFailed, 1);
152}
153
Eric Newberry1caa6342020-08-23 19:29:08 -0700154BOOST_FIXTURE_TEST_CASE(ValidateSignedInterest, ValidatorConfigFixture)
155{
156 validator.load(configFile);
157
158 InterestSigner signer(m_keyChain);
159 Interest i1("/hello/world");
160 i1.setCanBePrefix(false);
161 signer.makeSignedInterest(i1);
162 size_t nValidated = 0, nFailed = 0;
163
164 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
165 BOOST_CHECK_EQUAL(nValidated, 1);
166 BOOST_CHECK_EQUAL(nFailed, 0);
167
168 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
169 BOOST_CHECK_EQUAL(nValidated, 1);
170 BOOST_CHECK_EQUAL(nFailed, 1);
171
172 Interest i2("/hello/world");
173 i2.setCanBePrefix(false);
174 signer.makeSignedInterest(i2, signingWithSha256());
175 validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
176 BOOST_CHECK_EQUAL(nValidated, 2);
177 BOOST_CHECK_EQUAL(nFailed, 1);
178}
179
180BOOST_FIXTURE_TEST_CASE(ValidateCommandInterest, ValidatorConfigFixture)
181{
182 validator.load(configFile);
183
184 InterestSigner signer(m_keyChain);
185 auto i1 = signer.makeCommandInterest("/hello/world");
186 size_t nValidated = 0, nFailed = 0;
187
188 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
189 BOOST_CHECK_EQUAL(nValidated, 1);
190 BOOST_CHECK_EQUAL(nFailed, 0);
191
192 validator.validate(i1, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
193 BOOST_CHECK_EQUAL(nValidated, 1);
194 BOOST_CHECK_EQUAL(nFailed, 1);
195
196 auto i2 = signer.makeCommandInterest("/hello/world");
197 validator.validate(i2, [&] (auto&&...) { ++nValidated; }, [&] (auto&&...) { ++nFailed; });
198 BOOST_CHECK_EQUAL(nValidated, 2);
199 BOOST_CHECK_EQUAL(nFailed, 1);
200}
Alexander Afanasyev31fd4672018-06-17 13:25:52 -0400201
Junxiao Shid5827ce2016-07-14 20:49:37 +0000202BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig
203BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu41546342014-11-30 23:37:53 -0800204
205} // namespace tests
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700206} // namespace security
Yingdi Yu41546342014-11-30 23:37:53 -0800207} // namespace ndn