blob: e2fbda180ce5e3b93032a85701623cf8ae0b214a [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/*
Zhiyi Zhang48becde2017-01-05 16:41:38 -08003 * Copyright (c) 2013-2017 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
22#include "security/validator-config.hpp"
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080023#include "security/signing-helpers.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000024#include "util/dummy-client-face.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080025#include "util/io.hpp"
26#include "util/scheduler.hpp"
Zhiyi Zhang48becde2017-01-05 16:41:38 -080027#include "lp/nack.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000028#include "lp/tags.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080029
Yingdi Yu41546342014-11-30 23:37:53 -080030#include "boost-test.hpp"
Junxiao Shi2bea5c42017-08-14 20:10:32 +000031#include "make-interest-data.hpp"
32#include "../identity-management-time-fixture.hpp"
33
34#include <boost/logic/tribool.hpp>
Yingdi Yu41546342014-11-30 23:37:53 -080035
Yingdi Yu41546342014-11-30 23:37:53 -080036namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070037namespace security {
Yingdi Yu41546342014-11-30 23:37:53 -080038namespace tests {
39
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040using namespace ndn::tests;
Yingdi Yu41546342014-11-30 23:37:53 -080041
Junxiao Shid5827ce2016-07-14 20:49:37 +000042BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080043
44// Needed to create Face instance
45class ValidatorConfigFixture : public IdentityManagementV1TimeFixture
46{
47public:
48 ValidatorConfigFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -080049 : m_v2KeyChain("pib-memory:", "tpm-memory:")
50 , face(nullptr, m_v2KeyChain)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080051 , validator(face)
52 {
53 }
54
55public:
Alexander Afanasyev80782e02017-01-04 13:16:54 -080056 v2::KeyChain m_v2KeyChain;
Alexander Afanasyev70244f42017-01-04 12:47:12 -080057 Face face;
58 ValidatorConfig validator;
59};
60
61BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, ValidatorConfigFixture)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070062
63BOOST_AUTO_TEST_CASE(NameFilter)
Yingdi Yu41546342014-11-30 23:37:53 -080064{
65 Name identity("/TestValidatorConfig/NameFilter");
66 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080067 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -080068 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -080069
70 Name dataName1("/simple/equal");
71 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070072 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -080073
74 Name dataName2("/simple/different");
75 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070076 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -080077
78 std::string CONFIG_1 =
79 "rule\n"
80 "{\n"
81 " id \"Simple Rule\"\n"
82 " for data\n"
83 " filter"
84 " {\n"
85 " type name\n"
86 " name /simple/equal\n"
87 " relation equal\n"
88 " }\n"
89 " checker\n"
90 " {\n"
91 " type customized\n"
92 " sig-type rsa-sha256\n"
93 " key-locator\n"
94 " {\n"
95 " type name\n"
96 " name ";
97
98 std::string CONFIG_2 =
99 "\n"
100 " relation equal\n"
101 " }\n"
102 " }\n"
103 "}\n"
104 "trust-anchor\n"
105 "{\n"
106 " type file\n"
107 " file-name \"trust-anchor-1.cert\"\n"
108 "}\n";
109 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
110
111 const boost::filesystem::path CONFIG_PATH =
112 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
113
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700114 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800115
116 validator.validate(*data1,
117 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700118 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800119
120 validator.validate(*data2,
121 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700122 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800123}
124
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700125BOOST_AUTO_TEST_CASE(NameFilter2)
Yingdi Yu41546342014-11-30 23:37:53 -0800126{
127 Name identity("/TestValidatorConfig/NameFilter2");
128 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800129 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800130 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800131
132 Name dataName1("/simple/isPrefixOf");
133 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700134 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800135
136 Name dataName2("/simple/notPrefixOf");
137 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700138 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800139
140 Name dataName3("/simple/isPrefixOf/anotherLevel");
141 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700142 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800143
144 std::string CONFIG_1 =
145 "rule\n"
146 "{\n"
147 " id \"Simple2 Rule\"\n"
148 " for data\n"
149 " filter"
150 " {\n"
151 " type name\n"
152 " name /simple/isPrefixOf\n"
153 " relation is-prefix-of\n"
154 " }\n"
155 " checker\n"
156 " {\n"
157 " type customized\n"
158 " sig-type rsa-sha256\n"
159 " key-locator\n"
160 " {\n"
161 " type name\n"
162 " name ";
163
164 std::string CONFIG_2 =
165 "\n"
166 " relation equal\n"
167 " }\n"
168 " }\n"
169 "}\n"
170 "trust-anchor\n"
171 "{\n"
172 " type file\n"
173 " file-name \"trust-anchor-2.cert\"\n"
174 "}\n";
175 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
176
177 const boost::filesystem::path CONFIG_PATH =
178 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
179
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700180 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800181
182 validator.validate(*data1,
183 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700184 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800185
186 validator.validate(*data2,
187 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700188 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800189
190 validator.validate(*data3,
191 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700192 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800193}
194
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700195BOOST_AUTO_TEST_CASE(NameFilter3)
Yingdi Yu41546342014-11-30 23:37:53 -0800196{
197 Name identity("/TestValidatorConfig/NameFilter3");
198 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800199 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800200 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800201
202 Name dataName1("/simple/isStrictPrefixOf");
203 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700204 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800205
206 Name dataName2("/simple");
207 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700208 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800209
210 Name dataName3("/simple/isStrictPrefixOf/anotherLevel");
211 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700212 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800213
214 std::string CONFIG_1 =
215 "rule\n"
216 "{\n"
217 " id \"Simple3 Rule\"\n"
218 " for data\n"
219 " filter"
220 " {\n"
221 " type name\n"
222 " name /simple/isStrictPrefixOf\n"
223 " relation is-strict-prefix-of\n"
224 " }\n"
225 " checker\n"
226 " {\n"
227 " type customized\n"
228 " sig-type rsa-sha256\n"
229 " key-locator\n"
230 " {\n"
231 " type name\n"
232 " name ";
233
234 std::string CONFIG_2 =
235 "\n"
236 " relation equal\n"
237 " }\n"
238 " }\n"
239 "}\n"
240 "trust-anchor\n"
241 "{\n"
242 " type file\n"
243 " file-name \"trust-anchor-3.cert\"\n"
244 "}\n";
245 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
246
247 const boost::filesystem::path CONFIG_PATH =
248 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
249
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700250 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800251
252 validator.validate(*data1,
253 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700254 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800255
256 validator.validate(*data2,
257 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700258 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800259
260 validator.validate(*data3,
261 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700262 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800263}
264
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700265BOOST_AUTO_TEST_CASE(NameFilter4)
Yingdi Yu41546342014-11-30 23:37:53 -0800266{
267 Name identity("/TestValidatorConfig/NameFilter4");
268 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800269 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-4.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800270 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800271
272 Name dataName1("/simple/regex");
273 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700274 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800275
276 Name dataName2("/simple/regex-wrong");
277 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700278 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800279
280 Name dataName3("/simple/regex/correct");
281 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700282 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800283
284 std::string CONFIG_1 =
285 "rule\n"
286 "{\n"
287 " id \"Simple3 Rule\"\n"
288 " for data\n"
289 " filter"
290 " {\n"
291 " type name\n"
292 " regex ^<simple><regex>\n"
293 " }\n"
294 " checker\n"
295 " {\n"
296 " type customized\n"
297 " sig-type rsa-sha256\n"
298 " key-locator\n"
299 " {\n"
300 " type name\n"
301 " name ";
302
303 std::string CONFIG_2 =
304 "\n"
305 " relation equal\n"
306 " }\n"
307 " }\n"
308 "}\n"
309 "trust-anchor\n"
310 "{\n"
311 " type file\n"
312 " file-name \"trust-anchor-4.cert\"\n"
313 "}\n";
314 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
315
316 const boost::filesystem::path CONFIG_PATH =
317 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
318
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700319 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800320
321 validator.validate(*data1,
322 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700323 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800324
325 validator.validate(*data2,
326 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700327 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800328
329 validator.validate(*data3,
330 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700331 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800332}
333
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700334BOOST_AUTO_TEST_CASE(KeyLocatorNameChecker1)
Yingdi Yu41546342014-11-30 23:37:53 -0800335{
336 Name identity("/TestValidatorConfig/KeyLocatorNameChecker1");
337 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800338 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-5.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800339
340 Name dataName1 = identity;
341 dataName1.append("1");
342 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700343 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800344
345 Name dataName2 = identity;
346 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700347 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800348
349 Name dataName3("/TestValidatorConfig/KeyLocatorNameChecker1");
350 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700351 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800352
353 const std::string CONFIG =
354 "rule\n"
355 "{\n"
356 " id \"Simple3 Rule\"\n"
357 " for data\n"
358 " checker\n"
359 " {\n"
360 " type customized\n"
361 " sig-type rsa-sha256\n"
362 " key-locator\n"
363 " {\n"
364 " type name\n"
365 " hyper-relation\n"
366 " {\n"
367 " k-regex ^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$\n"
368 " k-expand \\\\1\\\\2\n"
369 " h-relation is-strict-prefix-of\n"
370 " p-regex ^(<>*)$\n"
371 " p-expand \\\\1\n"
372 " }\n"
373 " }\n"
374 " }\n"
375 "}\n"
376 "trust-anchor\n"
377 "{\n"
378 " type file\n"
379 " file-name \"trust-anchor-5.cert\"\n"
380 "}\n";
381 const boost::filesystem::path CONFIG_PATH =
382 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
383
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700384 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800385
386 validator.validate(*data1,
387 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700388 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800389
390 validator.validate(*data2,
391 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700392 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800393
394 validator.validate(*data3,
395 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700396 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800397}
398
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700399BOOST_AUTO_TEST_CASE(FixedSignerChecker)
Yingdi Yu41546342014-11-30 23:37:53 -0800400{
401 Name identity("/TestValidatorConfig/FixedSignerChecker");
402
403 Name identity1 = identity;
404 identity1.append("1").appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800405 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-7.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800406
407 Name identity2 = identity;
408 identity2.append("2").appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800409 addIdentity(identity2);
Yingdi Yu41546342014-11-30 23:37:53 -0800410
411 Name dataName1 = identity;
412 dataName1.append("data").appendVersion();
413 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700414 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1)));
Yingdi Yu41546342014-11-30 23:37:53 -0800415
416 Name dataName2 = identity;
417 dataName2.append("data").appendVersion();
418 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700419 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2)));
Yingdi Yu41546342014-11-30 23:37:53 -0800420
421 Name interestName("/TestValidatorConfig/FixedSignerChecker/fakeSigInfo/fakeSigValue");
422 shared_ptr<Interest> interest = make_shared<Interest>(interestName);
423
424 const std::string CONFIG =
425 "rule\n"
426 "{\n"
427 " id \"FixedSignerChecker Data Rule\"\n"
428 " for data\n"
429 " filter"
430 " {\n"
431 " type name\n"
432 " name /TestValidatorConfig/FixedSignerChecker\n"
433 " relation is-strict-prefix-of\n"
434 " }\n"
435 " checker\n"
436 " {\n"
437 " type fixed-signer\n"
438 " sig-type rsa-sha256\n"
439 " signer\n"
440 " {\n"
441 " type file\n"
442 " file-name \"trust-anchor-7.cert\"\n"
443 " }\n"
444 " }\n"
445 "}\n"
446 "rule\n"
447 "{\n"
448 " id \"FixedSignerChecker Interest Rule\"\n"
449 " for interest\n"
450 " filter"
451 " {\n"
452 " type name\n"
453 " name /TestValidatorConfig/FixedSignerChecker\n"
454 " relation is-strict-prefix-of\n"
455 " }\n"
456 " checker\n"
457 " {\n"
458 " type fixed-signer\n"
459 " sig-type rsa-sha256\n"
460 " signer\n"
461 " {\n"
462 " type file\n"
463 " file-name \"trust-anchor-7.cert\"\n"
464 " }\n"
465 " }\n"
466 "}\n";
467 const boost::filesystem::path CONFIG_PATH =
468 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
469
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700470 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800471
472 validator.validate(*data1,
473 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700474 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800475
476 validator.validate(*data2,
477 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700478 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800479
480 validator.validate(*interest,
481 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700482 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800483}
484
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800485BOOST_AUTO_TEST_CASE(MultiCheckers)
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700486{
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700487 Name identity1("/TestValidatorConfig/MultiCheckers/");
488 identity1.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800489 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-multi-1.cert"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700490
491 Name identity2("/TestValidatorConfig/");
492 identity2.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800493 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-multi-2.cert"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700494
495 const std::string CONFIG =
496 "rule\n"
497 "{\n"
498 " id \"Simple Rule01\"\n"
499 " for data\n"
500 " filter"
501 " {\n"
502 " type name\n"
503 " name /\n"
504 " relation is-prefix-of\n"
505 " }\n"
506 " checker\n" // checker 1, signer should have prefix /TestValidatorConfig/MultiCheckers
507 " {\n"
508 " type customized\n"
509 " sig-type rsa-sha256\n"
510 " key-locator\n"
511 " {\n"
512 " type name\n"
513 " name /TestValidatorConfig/MultiCheckers/\n"
514 " relation is-prefix-of\n"
515 " }\n"
516 " }\n"
517 " checker\n" // checker 2, data should have same prefix of its signer
518 " {\n"
519 " type hierarchical\n"
520 " sig-type rsa-sha256\n"
521 " }\n"
522 " checker\n" // checker 3, the signer should be identity1
523 " {\n"
524 " type fixed-signer\n"
525 " sig-type rsa-sha256\n"
526 " signer\n"
527 " {\n"
528 " type file\n"
529 " file-name \"trust-anchor-multi-1.cert\"\n"
530 " }\n"
531 " }\n"
532 "}\n";
533
534 const boost::filesystem::path CONFIG_PATH =
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800535 (boost::filesystem::current_path() / std::string("unit-test-multicheckers.conf"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700536
537 ValidatorConfig validator;
538 validator.load(CONFIG, CONFIG_PATH.c_str());
539 conf::Checker& checker0 = *validator.m_dataRules.front()->m_checkers[0];
540 conf::Checker& checker1 = *validator.m_dataRules.front()->m_checkers[1];
541 conf::Checker& checker2 = *validator.m_dataRules.front()->m_checkers[2];
542
Junxiao Shi85d90832016-08-04 03:19:46 +0000543 auto data1 = makeData(Name(identity1).append("Test"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700544 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1)));
545 BOOST_CHECK_EQUAL(checker0.check(*data1), 0);
546 BOOST_CHECK_EQUAL(checker1.check(*data1), 0);
547 BOOST_CHECK_EQUAL(checker2.check(*data1), 1);
548
Junxiao Shi85d90832016-08-04 03:19:46 +0000549 auto data2 = makeData(Name(identity2).append("Data2"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700550 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2)));
551 BOOST_CHECK_EQUAL(checker0.check(*data2), -1);
552 BOOST_CHECK_EQUAL(checker1.check(*data2), 0);
553 BOOST_CHECK_EQUAL(checker2.check(*data2), -1);
554
Junxiao Shi85d90832016-08-04 03:19:46 +0000555 auto data3 = makeData(Name(identity2).append("Data3"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700556 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity1)));
557 BOOST_CHECK_EQUAL(checker0.check(*data3), 0);
558 BOOST_CHECK_EQUAL(checker1.check(*data3), -1);
559 BOOST_CHECK_EQUAL(checker2.check(*data3), 1);
560
Junxiao Shi85d90832016-08-04 03:19:46 +0000561 auto data4 = makeData("/Data4");
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700562 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data4, security::signingByIdentity(identity2)));
563 BOOST_CHECK_EQUAL(checker0.check(*data4), -1);
564 BOOST_CHECK_EQUAL(checker1.check(*data4), -1);
565 BOOST_CHECK_EQUAL(checker2.check(*data4), -1);
566
567 int count = 0;
568 validator.validate(*data1,
569 [&] (const shared_ptr<const Data>&) {
570 BOOST_CHECK(true);
571 count++;
572 },
573 [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); });
574
575 validator.validate(*data2,
576 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
577 [&] (const shared_ptr<const Data>&, const std::string& str) {
578 BOOST_CHECK(true);
579 count++;
580 });
581
582 validator.validate(*data3,
583 [&] (const shared_ptr<const Data>&) {
584 BOOST_CHECK(true);
585 count++;
586 },
587 [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); });
588
589 validator.validate(*data4,
590 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
591 [&] (const shared_ptr<const Data>&, const std::string& str) {
592 BOOST_CHECK(true);
593 count++;
594 });
595
596 BOOST_CHECK_EQUAL(count, 4);
597}
598
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700599BOOST_AUTO_TEST_CASE(Reset)
Yingdi Yu41546342014-11-30 23:37:53 -0800600{
601 Name root("/TestValidatorConfig/Reload");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800602 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800603
Yingdi Yu41546342014-11-30 23:37:53 -0800604 const std::string CONFIG =
605 "rule\n"
606 "{\n"
607 " id \"NRD Prefix Registration Command Rule\"\n"
608 " for interest\n"
609 " filter\n"
610 " {\n"
611 " type name\n"
612 " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n"
613 " }\n"
614 " checker\n"
615 " {\n"
616 " type customized\n"
617 " sig-type rsa-sha256\n"
618 " key-locator\n"
619 " {\n"
620 " type name\n"
621 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n"
622 " }\n"
623 " }\n"
624 "}\n"
625 "rule\n"
626 "{\n"
627 " id \"Testbed Hierarchy Rule\"\n"
628 " for data\n"
629 " filter\n"
630 " {\n"
631 " type name\n"
632 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n"
633 " }\n"
634 " checker\n"
635 " {\n"
636 " type hierarchical\n"
637 " sig-type rsa-sha256\n"
638 " }\n"
639 "}\n"
640 "trust-anchor\n"
641 "{\n"
642 " type file\n"
643 " file-name \"trust-anchor-8.cert\"\n"
644 "}\n";
645 const boost::filesystem::path CONFIG_PATH =
646 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
647
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800648 validator.load(CONFIG, CONFIG_PATH.c_str());
649 BOOST_CHECK_EQUAL(validator.isEmpty(), false);
Yingdi Yu41546342014-11-30 23:37:53 -0800650
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800651 validator.reset();
652 BOOST_CHECK(validator.isEmpty());
Yingdi Yu41546342014-11-30 23:37:53 -0800653}
654
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700655BOOST_AUTO_TEST_CASE(TrustAnchorWildcard)
Yingdi Yu41546342014-11-30 23:37:53 -0800656{
657 Name identity("/TestValidatorConfig/Wildcard");
658 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800659 addIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800660
661 Name dataName1("/any/data");
662 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700663 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800664
665 std::string CONFIG =
666 "trust-anchor\n"
667 "{\n"
668 " type any\n"
669 "}\n";
670
671 const boost::filesystem::path CONFIG_PATH =
672 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
673
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700674 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800675
676 validator.validate(*data1,
677 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700678 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800679}
680
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700681BOOST_AUTO_TEST_CASE(SignedInterestTest)
Yingdi Yu41546342014-11-30 23:37:53 -0800682{
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800683 Name certName = addIdentity("/TestValidatorConfig/SignedInterestTest");
684 BOOST_REQUIRE(saveIdentityCertificate(certName, "trust-anchor-9.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800685
686 Name interestName("/TestValidatorConfig/SignedInterestTest");
687 Name interestName1 = interestName;
688 interestName1.append("1");
689 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
690 Name interestName2 = interestName;
691 interestName2.append("2");
692 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
693
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800694 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByCertificate(certName)));
695 advanceClocks(time::milliseconds(10));
696 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByCertificate(certName)));
Yingdi Yu41546342014-11-30 23:37:53 -0800697
698 const std::string CONFIG =
699 "rule\n"
700 "{\n"
701 " id \"FixedSignerChecker Interest Rule\"\n"
702 " for interest\n"
703 " filter"
704 " {\n"
705 " type name\n"
706 " name /TestValidatorConfig/SignedInterestTest\n"
707 " relation is-strict-prefix-of\n"
708 " }\n"
709 " checker\n"
710 " {\n"
711 " type fixed-signer\n"
712 " sig-type rsa-sha256\n"
713 " signer\n"
714 " {\n"
715 " type file\n"
716 " file-name \"trust-anchor-9.cert\"\n"
717 " }\n"
718 " }\n"
719 "}\n";
720 const boost::filesystem::path CONFIG_PATH =
721 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
722
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700723 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800724
725 validator.validate(*interest1,
726 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700727 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800728 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800729
730 validator.validate(*interest2,
731 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700732 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800733 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800734
735 validator.validate(*interest1,
736 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700737 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800738 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800739}
740
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700741BOOST_AUTO_TEST_CASE(MaxKeyTest)
Yingdi Yu41546342014-11-30 23:37:53 -0800742{
743 Name identity("/TestValidatorConfig/MaxKeyTest");
744
745 Name identity1 = identity;
746 identity1.append("Key1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800747 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800748
749 Name identity2 = identity;
750 identity2.append("Key2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800751 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800752
753 Name identity3 = identity;
754 identity3.append("Key3");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800755 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800756
757
758 Name interestName("/TestValidatorConfig/MaxKeyTest");
759 Name interestName1 = interestName;
760 interestName1.append("1");
761 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
762 Name interestName2 = interestName;
763 interestName2.append("2");
764 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
765 Name interestName3 = interestName;
766 interestName3.append("3");
767 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
768
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700769 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800770 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700771 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800772 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700773 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800774 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800775
776 const std::string CONFIG =
777 "rule\n"
778 "{\n"
779 " id \"FixedSignerChecker Interest Rule\"\n"
780 " for interest\n"
781 " filter"
782 " {\n"
783 " type name\n"
784 " name /TestValidatorConfig/MaxKeyTest\n"
785 " relation is-strict-prefix-of\n"
786 " }\n"
787 " checker\n"
788 " {\n"
789 " type fixed-signer\n"
790 " sig-type rsa-sha256\n"
791 " signer\n"
792 " {\n"
793 " type file\n"
794 " file-name \"trust-anchor-10-1.cert\"\n"
795 " }\n"
796 " signer\n"
797 " {\n"
798 " type file\n"
799 " file-name \"trust-anchor-10-2.cert\"\n"
800 " }\n"
801 " signer\n"
802 " {\n"
803 " type file\n"
804 " file-name \"trust-anchor-10-3.cert\"\n"
805 " }\n"
806 " }\n"
807 "}\n";
808 const boost::filesystem::path CONFIG_PATH =
809 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
810
Yingdi Yu41546342014-11-30 23:37:53 -0800811 ValidatorConfig validator(face,
812 ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
813 ValidatorConfig::DEFAULT_GRACE_INTERVAL,
814 10,
815 2, // Two keys can be tracked
816 time::seconds(1)); // TTL is set to 1 sec
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700817 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800818
819 validator.validate(*interest1,
820 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700821 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800822 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800823
824 validator.validate(*interest2,
825 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700826 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800827 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800828
829 validator.validate(*interest1,
830 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700831 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800832 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800833
834 validator.validate(*interest3,
835 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700836 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800837 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800838
839 // Should succeed because identity1's key has been cleaned up due to space limit.
840 validator.validate(*interest1,
841 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700842 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800843 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800844}
845
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700846BOOST_AUTO_TEST_CASE(MaxKeyTest2)
Yingdi Yu41546342014-11-30 23:37:53 -0800847{
848 Name identity("/TestValidatorConfig/MaxKeyTest");
849
850 Name identity1 = identity;
851 identity1.append("Key1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800852 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800853
854 Name identity2 = identity;
855 identity2.append("Key2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800856 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800857
858 Name identity3 = identity;
859 identity3.append("Key3");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800860 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800861
862 Name identity4 = identity;
863 identity4.append("Key4");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800864 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity4), "trust-anchor-10-4.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800865
866
867 Name interestName("/TestValidatorConfig/MaxKeyTest");
868 Name interestName1 = interestName;
869 interestName1.append("1");
870 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
871 Name interestName2 = interestName;
872 interestName2.append("2");
873 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
874 Name interestName3 = interestName;
875 interestName3.append("3");
876 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
877 Name interestName4 = interestName;
878 interestName4.append("4");
879 shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4);
880
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700881 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800882 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700883 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800884 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700885 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800886 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700887 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest4, security::signingByIdentity(identity4)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800888 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800889
890 const std::string CONFIG =
891 "rule\n"
892 "{\n"
893 " id \"FixedSignerChecker Interest Rule\"\n"
894 " for interest\n"
895 " filter"
896 " {\n"
897 " type name\n"
898 " name /TestValidatorConfig/MaxKeyTest\n"
899 " relation is-strict-prefix-of\n"
900 " }\n"
901 " checker\n"
902 " {\n"
903 " type fixed-signer\n"
904 " sig-type rsa-sha256\n"
905 " signer\n"
906 " {\n"
907 " type file\n"
908 " file-name \"trust-anchor-10-1.cert\"\n"
909 " }\n"
910 " signer\n"
911 " {\n"
912 " type file\n"
913 " file-name \"trust-anchor-10-2.cert\"\n"
914 " }\n"
915 " signer\n"
916 " {\n"
917 " type file\n"
918 " file-name \"trust-anchor-10-3.cert\"\n"
919 " }\n"
920 " signer\n"
921 " {\n"
922 " type file\n"
923 " file-name \"trust-anchor-10-4.cert\"\n"
924 " }\n"
925 " }\n"
926 "}\n";
927 const boost::filesystem::path CONFIG_PATH =
928 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
929
Yingdi Yu41546342014-11-30 23:37:53 -0800930 ValidatorConfig validator(face,
931 ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
932 ValidatorConfig::DEFAULT_GRACE_INTERVAL,
933 10,
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800934 3, // Three keys can be tracked
Yingdi Yu41546342014-11-30 23:37:53 -0800935 time::seconds(1)); // TTL is set to 1 sec
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700936 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800937
938 validator.validate(*interest1,
939 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700940 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800941 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800942
943 validator.validate(*interest2,
944 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700945 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800946 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800947
948 validator.validate(*interest3,
949 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700950 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800951 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800952
953 validator.validate(*interest1,
954 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700955 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800956 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800957
958 validator.validate(*interest2,
959 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700960 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800961 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800962
963 validator.validate(*interest3,
964 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700965 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800966
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800967 advanceClocks(time::seconds(2));
Yingdi Yu41546342014-11-30 23:37:53 -0800968
969 validator.validate(*interest4,
970 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700971 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800972 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800973
974 // Should succeed because identity1 and identity2's key has been cleaned up due to ttl limit.
975 validator.validate(*interest1,
976 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700977 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800978 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800979
980 validator.validate(*interest2,
981 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700982 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800983 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800984
985 validator.validate(*interest3,
986 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700987 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800988 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800989}
990
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700991BOOST_AUTO_TEST_CASE(FixedSignerChecker2)
Yingdi Yu41546342014-11-30 23:37:53 -0800992{
993 Name rsaIdentity("/TestValidatorConfig/FixedSignerChecker2/Rsa");
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800994 addIdentity(rsaIdentity, RsaKeyParams());
Yingdi Yu41546342014-11-30 23:37:53 -0800995
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700996 Name ecIdentity("/TestValidatorConfig/FixedSignerChecker2/Ec");
997 auto identity = addIdentity(ecIdentity, EcKeyParams());
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800998 BOOST_REQUIRE(saveIdentityCertificate(identity, "trust-anchor-11.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800999
1000 Name dataName("/TestValidatorConfig/FixedSignerChecker2");
1001 shared_ptr<Data> dataRsa = make_shared<Data>(dataName);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001002 m_keyChain.sign(*dataRsa, security::signingByIdentity(rsaIdentity));
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001003 shared_ptr<Data> dataEc = make_shared<Data>(dataName);
1004 m_keyChain.sign(*dataEc, security::signingByIdentity(ecIdentity));
Yingdi Yu41546342014-11-30 23:37:53 -08001005
1006 shared_ptr<Interest> interestRsa = make_shared<Interest>(dataName);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001007 m_keyChain.sign(*interestRsa, security::signingByIdentity(rsaIdentity));
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001008 shared_ptr<Interest> interestEc = make_shared<Interest>(dataName);
1009 m_keyChain.sign(*interestEc, security::signingByIdentity(ecIdentity));
Yingdi Yu41546342014-11-30 23:37:53 -08001010
1011 const std::string CONFIG =
1012 "rule\n"
1013 "{\n"
1014 " id \"FixedSignerChecker Data Rule\"\n"
1015 " for data\n"
1016 " filter"
1017 " {\n"
1018 " type name\n"
1019 " name /TestValidatorConfig/FixedSignerChecker2\n"
1020 " relation equal\n"
1021 " }\n"
1022 " checker\n"
1023 " {\n"
1024 " type fixed-signer\n"
1025 " sig-type ecdsa-sha256\n"
1026 " signer\n"
1027 " {\n"
1028 " type file\n"
1029 " file-name \"trust-anchor-11.cert\"\n"
1030 " }\n"
1031 " }\n"
1032 "}\n"
1033 "rule\n"
1034 "{\n"
1035 " id \"FixedSignerChecker Interest Rule\"\n"
1036 " for interest\n"
1037 " filter"
1038 " {\n"
1039 " type name\n"
1040 " name /TestValidatorConfig/FixedSignerChecker2\n"
1041 " relation equal\n"
1042 " }\n"
1043 " checker\n"
1044 " {\n"
1045 " type fixed-signer\n"
1046 " sig-type ecdsa-sha256\n"
1047 " signer\n"
1048 " {\n"
1049 " type file\n"
1050 " file-name \"trust-anchor-11.cert\"\n"
1051 " }\n"
1052 " }\n"
1053 "}\n";
1054 const boost::filesystem::path CONFIG_PATH =
1055 (boost::filesystem::current_path() / std::string("unit-test.conf"));
1056
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001057 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001058
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001059 validator.validate(*dataEc,
Yingdi Yu41546342014-11-30 23:37:53 -08001060 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001061 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001062
1063 validator.validate(*dataRsa,
1064 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001065 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001066
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001067 validator.validate(*interestEc,
Yingdi Yu41546342014-11-30 23:37:53 -08001068 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001069 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001070
1071 validator.validate(*interestRsa,
1072 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001073 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001074}
1075
1076
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001077struct FacesFixture : public ValidatorConfigFixture
Yingdi Yu41546342014-11-30 23:37:53 -08001078{
1079 FacesFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001080 : face1(io, m_v2KeyChain, {true, true})
1081 , face2(io, m_v2KeyChain, {true, true})
Yingdi Yu41546342014-11-30 23:37:53 -08001082 , readInterestOffset1(0)
1083 , readDataOffset1(0)
1084 , readInterestOffset2(0)
1085 , readDataOffset2(0)
1086 {
1087 }
1088
1089 bool
1090 passPacket()
1091 {
1092 bool hasPassed = false;
1093
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001094 checkFace(face1.sentInterests, readInterestOffset1, face2, hasPassed);
1095 checkFace(face1.sentData, readDataOffset1, face2, hasPassed);
1096 checkFace(face2.sentInterests, readInterestOffset2, face1, hasPassed);
Zhiyi Zhang99832d42016-11-18 10:03:49 -08001097 checkFace(face2.sentData, readDataOffset2, face1, hasPassed);
Yingdi Yu41546342014-11-30 23:37:53 -08001098
1099 return hasPassed;
1100 }
1101
1102 template<typename Packet>
1103 void
1104 checkFace(std::vector<Packet>& receivedPackets,
1105 size_t& readPacketOffset,
1106 util::DummyClientFace& receiver,
1107 bool& hasPassed)
1108 {
1109 while (receivedPackets.size() > readPacketOffset) {
1110 receiver.receive(receivedPackets[readPacketOffset]);
1111 readPacketOffset++;
1112 hasPassed = true;
1113 }
1114 }
1115
Yingdi Yu41546342014-11-30 23:37:53 -08001116public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001117 util::DummyClientFace face1;
1118 util::DummyClientFace face2;
Yingdi Yu41546342014-11-30 23:37:53 -08001119
1120 size_t readInterestOffset1;
1121 size_t readDataOffset1;
1122 size_t readInterestOffset2;
1123 size_t readDataOffset2;
1124};
1125
1126BOOST_FIXTURE_TEST_CASE(HierarchicalChecker, FacesFixture)
1127{
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001128 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yu41546342014-11-30 23:37:53 -08001129
1130 Name root("/TestValidatorConfig");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001131 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-6.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -08001132
1133
1134 Name sld("/TestValidatorConfig/HierarchicalChecker");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001135 addIdentity(sld);
Yingdi Yu41546342014-11-30 23:37:53 -08001136 advanceClocks(time::milliseconds(100));
1137 Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001138 shared_ptr<v1::IdentityCertificate> sldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001139 m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName,
1140 root,
1141 time::system_clock::now(),
1142 time::system_clock::now() + time::days(7300),
1143 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001144 m_keyChain.sign(*sldCert, security::signingByIdentity(root));
Yingdi Yu41546342014-11-30 23:37:53 -08001145 m_keyChain.addCertificateAsIdentityDefault(*sldCert);
1146
1147 Name nld("/TestValidatorConfig/HierarchicalChecker/NextLevel");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001148 addIdentity(nld);
Yingdi Yu41546342014-11-30 23:37:53 -08001149 advanceClocks(time::milliseconds(100));
1150 Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001151 shared_ptr<v1::IdentityCertificate> nldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001152 m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName,
1153 sld,
1154 time::system_clock::now(),
1155 time::system_clock::now() + time::days(7300),
1156 subjectDescription);
Zhiyi Zhang433686c2016-11-18 10:18:30 -08001157 m_keyChain.sign(*nldCert, security::signingByIdentity(sld));
Yingdi Yu41546342014-11-30 23:37:53 -08001158 m_keyChain.addCertificateAsIdentityDefault(*nldCert);
1159
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001160 face1.setInterestFilter(sldCert->getName().getPrefix(-1),
1161 [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001162 RegisterPrefixSuccessCallback(),
1163 [] (const Name&, const std::string&) {});
1164
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001165 face1.setInterestFilter(nldCert->getName().getPrefix(-1),
1166 [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001167 RegisterPrefixSuccessCallback(),
1168 [] (const Name&, const std::string&) {});
1169
1170 Name dataName1 = nld;
1171 dataName1.append("data1");
1172 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001173 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001174
1175 Name dataName2("/ConfValidatorTest");
1176 dataName2.append("data1");
1177 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001178 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001179
Yingdi Yu41546342014-11-30 23:37:53 -08001180 const std::string CONFIG =
1181 "rule\n"
1182 "{\n"
1183 " id \"Simple3 Rule\"\n"
1184 " for data\n"
1185 " checker\n"
1186 " {\n"
1187 " type hierarchical\n"
1188 " sig-type rsa-sha256\n"
1189 " }\n"
1190 "}\n"
1191 "trust-anchor\n"
1192 "{\n"
1193 " type file\n"
1194 " file-name \"trust-anchor-6.cert\"\n"
1195 "}\n";
1196 const boost::filesystem::path CONFIG_PATH =
1197 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1198
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001199 auto validator = make_shared<ValidatorConfig>(&face2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001200 validator->load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001201
1202 advanceClocks(time::milliseconds(2), 100);
1203 validator->validate(*data1,
1204 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001205 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001206
1207 do {
1208 advanceClocks(time::milliseconds(2), 10);
1209 } while (passPacket());
1210
1211 validator->validate(*data2,
1212 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001213 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001214
1215 do {
1216 advanceClocks(time::milliseconds(2), 10);
1217 } while (passPacket());
Yingdi Yu41546342014-11-30 23:37:53 -08001218}
1219
1220BOOST_FIXTURE_TEST_CASE(Nrd, FacesFixture)
1221{
Junxiao Shid5827ce2016-07-14 20:49:37 +00001222 advanceClocks(time::nanoseconds(1));
Yingdi Yu41546342014-11-30 23:37:53 -08001223
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001224 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yu41546342014-11-30 23:37:53 -08001225
1226 Name root("/TestValidatorConfig");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001227 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -08001228
Yingdi Yu41546342014-11-30 23:37:53 -08001229 Name sld("/TestValidatorConfig/Nrd-1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001230 addIdentity(sld);
Yingdi Yu41546342014-11-30 23:37:53 -08001231 advanceClocks(time::milliseconds(100));
1232 Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001233 shared_ptr<v1::IdentityCertificate> sldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001234 m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName,
1235 root,
1236 time::system_clock::now(),
1237 time::system_clock::now() + time::days(7300),
1238 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001239 m_keyChain.sign(*sldCert, security::signingByIdentity(root));
Yingdi Yu41546342014-11-30 23:37:53 -08001240 m_keyChain.addCertificateAsIdentityDefault(*sldCert);
1241
1242 Name nld("/TestValidatorConfig/Nrd-1/Nrd-2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001243 addIdentity(nld);
Yingdi Yu41546342014-11-30 23:37:53 -08001244 advanceClocks(time::milliseconds(100));
1245 Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001246 shared_ptr<v1::IdentityCertificate> nldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001247 m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName,
1248 sld,
1249 time::system_clock::now(),
1250 time::system_clock::now() + time::days(7300),
1251 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001252 m_keyChain.sign(*nldCert, security::signingByIdentity(sld));
Yingdi Yu41546342014-11-30 23:37:53 -08001253 m_keyChain.addCertificateAsIdentityDefault(*nldCert);
1254
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001255 face1.setInterestFilter(sldCert->getName().getPrefix(-1),
1256 [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001257 RegisterPrefixSuccessCallback(),
1258 [] (const Name&, const std::string&) {});
1259
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001260 face1.setInterestFilter(nldCert->getName().getPrefix(-1),
1261 [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001262 RegisterPrefixSuccessCallback(),
1263 [] (const Name&, const std::string&) {});
1264
1265 advanceClocks(time::milliseconds(10));
1266 Name interestName1("/localhost/nrd/register/option");
1267 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001268 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001269
1270 advanceClocks(time::milliseconds(10));
1271 Name interestName2("/localhost/nrd/non-register");
1272 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001273 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001274
1275 advanceClocks(time::milliseconds(10));
1276 Name interestName3("/localhost/nrd/register/option");
1277 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001278 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(root)));
Yingdi Yu41546342014-11-30 23:37:53 -08001279
1280 advanceClocks(time::milliseconds(10));
1281 Name interestName4("/localhost/nrd/register/option/timestamp/nonce/fakeSigInfo/fakeSigValue");
1282 shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4);
1283
1284 const std::string CONFIG =
1285 "rule\n"
1286 "{\n"
1287 " id \"NRD Prefix Registration Command Rule\"\n"
1288 " for interest\n"
1289 " filter\n"
1290 " {\n"
1291 " type name\n"
1292 " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n"
1293 " }\n"
1294 " checker\n"
1295 " {\n"
1296 " type customized\n"
1297 " sig-type rsa-sha256\n"
1298 " key-locator\n"
1299 " {\n"
1300 " type name\n"
1301 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n"
1302 " }\n"
1303 " }\n"
1304 "}\n"
1305 "rule\n"
1306 "{\n"
1307 " id \"Testbed Hierarchy Rule\"\n"
1308 " for data\n"
1309 " filter\n"
1310 " {\n"
1311 " type name\n"
1312 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n"
1313 " }\n"
1314 " checker\n"
1315 " {\n"
1316 " type hierarchical\n"
1317 " sig-type rsa-sha256\n"
1318 " }\n"
1319 "}\n"
1320 "trust-anchor\n"
1321 "{\n"
1322 " type file\n"
1323 " file-name \"trust-anchor-8.cert\"\n"
1324 "}\n";
1325 const boost::filesystem::path CONFIG_PATH =
1326 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1327
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001328 auto validator = make_shared<ValidatorConfig>(&face2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001329 validator->load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001330
1331 advanceClocks(time::milliseconds(2), 100);
1332
1333 // should succeed
1334 validator->validate(*interest1,
1335 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001336 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001337
1338 do {
1339 advanceClocks(time::milliseconds(2), 10);
1340 } while (passPacket());
1341
1342 // should fail
1343 validator->validate(*interest2,
1344 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001345 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001346
1347 do {
1348 advanceClocks(time::milliseconds(2), 10);
1349 } while (passPacket());
1350
1351 // should succeed
1352 validator->validate(*interest3,
1353 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001354 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001355
1356 do {
1357 advanceClocks(time::milliseconds(2), 10);
1358 } while (passPacket());
1359
1360 // should fail
1361 validator->validate(*interest4,
1362 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001363 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001364
1365 do {
1366 advanceClocks(time::milliseconds(2), 10);
1367 } while (passPacket());
Yingdi Yu41546342014-11-30 23:37:53 -08001368}
1369
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001370struct DirTestFixture : public ValidatorConfigFixture
Yingdi Yu41546342014-11-30 23:37:53 -08001371{
1372 DirTestFixture()
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001373 : validator(&face, ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
Yingdi Yu41546342014-11-30 23:37:53 -08001374 ValidatorConfig::DEFAULT_GRACE_INTERVAL, 0)
1375 {
1376 certDirPath = (boost::filesystem::current_path() / std::string("test-cert-dir"));
1377 boost::filesystem::create_directory(certDirPath);
1378
1379 firstCertPath = (boost::filesystem::current_path() /
1380 std::string("test-cert-dir") /
1381 std::string("trust-anchor-1.cert"));
1382
1383 secondCertPath = (boost::filesystem::current_path() /
1384 std::string("test-cert-dir") /
1385 std::string("trust-anchor-2.cert"));
1386
1387 firstIdentity = Name("/TestValidatorConfig/Dir/First");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001388 addIdentity(firstIdentity);
Yingdi Yu41546342014-11-30 23:37:53 -08001389 Name firstCertName = m_keyChain.getDefaultCertificateNameForIdentity(firstIdentity);
1390 firstCert = m_keyChain.getCertificate(firstCertName);
1391 io::save(*firstCert, firstCertPath.string());
1392
1393 secondIdentity = Name("/TestValidatorConfig/Dir/Second");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001394 addIdentity(secondIdentity);
Yingdi Yu41546342014-11-30 23:37:53 -08001395 Name secondCertName = m_keyChain.getDefaultCertificateNameForIdentity(secondIdentity);
1396 secondCert = m_keyChain.getCertificate(secondCertName);
1397 }
1398
1399 ~DirTestFixture()
1400 {
1401 boost::filesystem::remove_all(certDirPath);
1402 }
1403
1404public:
1405 boost::filesystem::path certDirPath;
1406 boost::filesystem::path firstCertPath;
1407 boost::filesystem::path secondCertPath;
1408
1409 Name firstIdentity;
1410 Name secondIdentity;
1411
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001412 shared_ptr<v1::IdentityCertificate> firstCert;
1413 shared_ptr<v1::IdentityCertificate> secondCert;
Yingdi Yu41546342014-11-30 23:37:53 -08001414
Yingdi Yu41546342014-11-30 23:37:53 -08001415 ValidatorConfig validator;
1416};
1417
1418BOOST_FIXTURE_TEST_CASE(TrustAnchorDir, DirTestFixture)
1419{
1420 advanceClocks(time::milliseconds(10));
1421
1422 Name dataName1("/any/data/1");
1423 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001424 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(firstIdentity)));
Yingdi Yu41546342014-11-30 23:37:53 -08001425
1426 Name dataName2("/any/data/2");
1427 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001428 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(secondIdentity)));
Yingdi Yu41546342014-11-30 23:37:53 -08001429
1430 std::string CONFIG =
1431 "rule\n"
1432 "{\n"
1433 " id \"Any Rule\"\n"
1434 " for data\n"
1435 " filter\n"
1436 " {\n"
1437 " type name\n"
1438 " regex ^<>*$\n"
1439 " }\n"
1440 " checker\n"
1441 " {\n"
1442 " type customized\n"
1443 " sig-type rsa-sha256\n"
1444 " key-locator\n"
1445 " {\n"
1446 " type name\n"
1447 " regex ^<>*$\n"
1448 " }\n"
1449 " }\n"
1450 "}\n"
1451 "trust-anchor\n"
1452 "{\n"
1453 " type dir\n"
1454 " dir test-cert-dir\n"
1455 " refresh 1s\n"
1456 "}\n";
1457
1458 const boost::filesystem::path CONFIG_PATH =
1459 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1460
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001461 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001462
1463 advanceClocks(time::milliseconds(10), 20);
1464 validator.validate(*data1,
1465 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001466 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001467 advanceClocks(time::milliseconds(10), 20);
1468
1469 validator.validate(*data2,
1470 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001471 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001472 advanceClocks(time::milliseconds(10), 20);
1473
1474 io::save(*secondCert, secondCertPath.string());
1475 advanceClocks(time::milliseconds(10), 200);
1476
1477 validator.validate(*data1,
1478 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001479 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001480 advanceClocks(time::milliseconds(10), 20);
1481
1482 validator.validate(*data2,
1483 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001484 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001485 advanceClocks(time::milliseconds(10), 20);
1486}
1487
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001488class DirectCertFetchFixture : public ValidatorConfigFixture
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001489{
1490public:
1491 DirectCertFetchFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001492 : clientFace(io, m_v2KeyChain, {true, true})
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001493 , validationResult(boost::logic::indeterminate)
1494 {
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001495 auto certName = addIdentity(ca);
1496 saveIdentityCertificate(certName, "trust-anchor-1.cert");
1497 addSubCertificate(user, ca);
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001498
1499 userCertName = m_keyChain.getDefaultCertificateNameForIdentity(user);
1500 userCert = m_keyChain.getCertificate(userCertName);
1501 }
1502
1503protected:
1504 void
1505 runTest(const std::function<void(const Interest&, const Interest&)> respond)
1506 {
1507 optional<Interest> directInterest;
1508 optional<Interest> infrastructureInterest;
1509 clientFace.onSendInterest.connect([&] (const Interest& interest) {
1510 const Name& interestName = interest.getName();
1511 if (interestName == userCert->getName().getPrefix(-1)) {
1512 auto nextHopFaceIdTag = interest.getTag<lp::NextHopFaceIdTag>();
1513 if (nextHopFaceIdTag != nullptr) {
1514 BOOST_CHECK(!directInterest);
1515 directInterest = interest;
1516 }
1517 else {
1518 BOOST_CHECK(!infrastructureInterest);
1519 infrastructureInterest = interest;
1520 }
1521 if (static_cast<bool>(directInterest) && static_cast<bool>(infrastructureInterest)) {
1522 io.post([directInterest, infrastructureInterest, respond] {
1523 respond(directInterest.value(), infrastructureInterest.value());
1524 });
1525 directInterest = nullopt;
1526 infrastructureInterest = nullopt;
1527 }
1528 }
1529 });
1530
1531 const boost::filesystem::path CONFIG_PATH =
1532 (boost::filesystem::current_path() / std::string("unit-test-direct.conf"));
1533 ValidatorConfig validator(&clientFace);
1534 validator.load(CONFIG, CONFIG_PATH.c_str());
1535 validator.setDirectCertFetchEnabled(true);
1536
1537 shared_ptr<Interest> interest = make_shared<Interest>(interestName);
1538 interest->setTag(make_shared<lp::IncomingFaceIdTag>(123));
1539 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest, security::signingByIdentity(user)));
1540
1541 validator.validate(*interest,
1542 [&] (const shared_ptr<const Interest>&) {
1543 BOOST_CHECK(boost::logic::indeterminate(validationResult));
1544 validationResult = true;
1545 },
1546 [&] (const shared_ptr<const Interest>&, const std::string& s) {
1547 BOOST_CHECK(boost::logic::indeterminate(validationResult));
1548 validationResult = false;
1549 });
1550
1551 advanceClocks(time::milliseconds(200), 60);
1552
1553 BOOST_CHECK(!boost::logic::indeterminate(validationResult));
1554 }
1555
1556public:
1557 util::DummyClientFace clientFace;
1558 const Name ca = Name("/DirectCertFetch");
1559 const Name user = Name("/DirectCertFetch/user");
1560 const Name interestName = Name("/DirectCertFetch/user/tag-interest");
1561 const std::string CONFIG = R"_TEXT_(
1562 rule
1563 {
1564 id "RuleForInterest"
1565 for interest
1566 checker
1567 {
1568 type hierarchical
1569 sig-type rsa-sha256
1570 }
1571 }
1572 rule
1573 {
1574 id "RuleForData"
1575 for data
1576 filter
1577 {
1578 type name
1579 regex ^<>*$
1580 }
1581 checker
1582 {
1583 type customized
1584 sig-type rsa-sha256
1585 key-locator
1586 {
1587 type name
1588 regex ^<>*$
1589 }
1590 }
1591 }
1592 trust-anchor
1593 {
1594 type file
1595 file-name "trust-anchor-1.cert"
1596 }
1597 )_TEXT_";
1598 boost::logic::tribool validationResult;
1599 Name userCertName;
1600 shared_ptr<v1::IdentityCertificate> userCert;
1601};
1602
1603BOOST_FIXTURE_TEST_SUITE(DirectCertFetch, DirectCertFetchFixture)
1604
1605BOOST_AUTO_TEST_CASE(CertFetchSuccess)
1606{
1607 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) {
1608 this->clientFace.receive(*userCert);
1609 });
1610
1611 BOOST_CHECK_EQUAL(validationResult, true);
1612}
1613
1614BOOST_AUTO_TEST_CASE(CertFetchTimeout)
1615{
1616 // In this test case, certificate request would time out
1617 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) { });
1618
1619 BOOST_CHECK_EQUAL(validationResult, false);
1620}
1621
1622BOOST_AUTO_TEST_CASE(CertFetchNack)
1623{
1624 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) {
1625 lp::Nack nackInfrastructureInterest(infrastructureInterest);
1626 nackInfrastructureInterest.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
1627 this->clientFace.receive(nackInfrastructureInterest);
1628 });
1629
1630 BOOST_CHECK_EQUAL(validationResult, false);
1631}
1632
1633BOOST_AUTO_TEST_SUITE_END() // DirectCertFetch
Junxiao Shid5827ce2016-07-14 20:49:37 +00001634BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig
1635BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu41546342014-11-30 23:37:53 -08001636
1637} // namespace tests
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001638} // namespace security
Yingdi Yu41546342014-11-30 23:37:53 -08001639} // namespace ndn