blob: 74c29aecd5fc3816f6e0b9db535bb304ea109070 [file] [log] [blame]
Yingdi Yu41546342014-11-30 23:37:53 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
23
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080024#include "security/signing-helpers.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080025#include "util/io.hpp"
26#include "util/scheduler.hpp"
27#include "util/dummy-client-face.hpp"
Zhiyi Zhang48becde2017-01-05 16:41:38 -080028#include "lp/tags.hpp"
29#include "lp/nack.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080030
31#include <boost/asio.hpp>
Zhiyi Zhang48becde2017-01-05 16:41:38 -080032#include <boost/logic/tribool.hpp>
Yingdi Yu41546342014-11-30 23:37:53 -080033
34#include "identity-management-fixture.hpp"
35#include "../identity-management-time-fixture.hpp"
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070036#include "../make-interest-data.hpp"
Yingdi Yu41546342014-11-30 23:37:53 -080037#include "boost-test.hpp"
38
Yingdi Yu41546342014-11-30 23:37:53 -080039namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040namespace security {
Yingdi Yu41546342014-11-30 23:37:53 -080041namespace tests {
42
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070043using namespace ndn::tests;
Yingdi Yu41546342014-11-30 23:37:53 -080044
Junxiao Shid5827ce2016-07-14 20:49:37 +000045BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080046
47// Needed to create Face instance
48class ValidatorConfigFixture : public IdentityManagementV1TimeFixture
49{
50public:
51 ValidatorConfigFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -080052 : m_v2KeyChain("pib-memory:", "tpm-memory:")
53 , face(nullptr, m_v2KeyChain)
Alexander Afanasyev70244f42017-01-04 12:47:12 -080054 , validator(face)
55 {
56 }
57
58public:
Alexander Afanasyev80782e02017-01-04 13:16:54 -080059 v2::KeyChain m_v2KeyChain;
Alexander Afanasyev70244f42017-01-04 12:47:12 -080060 Face face;
61 ValidatorConfig validator;
62};
63
64BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, ValidatorConfigFixture)
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070065
66BOOST_AUTO_TEST_CASE(NameFilter)
Yingdi Yu41546342014-11-30 23:37:53 -080067{
68 Name identity("/TestValidatorConfig/NameFilter");
69 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080070 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -080071 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -080072
73 Name dataName1("/simple/equal");
74 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070075 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -080076
77 Name dataName2("/simple/different");
78 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070079 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -080080
81 std::string CONFIG_1 =
82 "rule\n"
83 "{\n"
84 " id \"Simple Rule\"\n"
85 " for data\n"
86 " filter"
87 " {\n"
88 " type name\n"
89 " name /simple/equal\n"
90 " relation equal\n"
91 " }\n"
92 " checker\n"
93 " {\n"
94 " type customized\n"
95 " sig-type rsa-sha256\n"
96 " key-locator\n"
97 " {\n"
98 " type name\n"
99 " name ";
100
101 std::string CONFIG_2 =
102 "\n"
103 " relation equal\n"
104 " }\n"
105 " }\n"
106 "}\n"
107 "trust-anchor\n"
108 "{\n"
109 " type file\n"
110 " file-name \"trust-anchor-1.cert\"\n"
111 "}\n";
112 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
113
114 const boost::filesystem::path CONFIG_PATH =
115 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
116
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700117 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800118
119 validator.validate(*data1,
120 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700121 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800122
123 validator.validate(*data2,
124 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700125 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800126}
127
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700128BOOST_AUTO_TEST_CASE(NameFilter2)
Yingdi Yu41546342014-11-30 23:37:53 -0800129{
130 Name identity("/TestValidatorConfig/NameFilter2");
131 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800132 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800133 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800134
135 Name dataName1("/simple/isPrefixOf");
136 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700137 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800138
139 Name dataName2("/simple/notPrefixOf");
140 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700141 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800142
143 Name dataName3("/simple/isPrefixOf/anotherLevel");
144 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700145 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800146
147 std::string CONFIG_1 =
148 "rule\n"
149 "{\n"
150 " id \"Simple2 Rule\"\n"
151 " for data\n"
152 " filter"
153 " {\n"
154 " type name\n"
155 " name /simple/isPrefixOf\n"
156 " relation is-prefix-of\n"
157 " }\n"
158 " checker\n"
159 " {\n"
160 " type customized\n"
161 " sig-type rsa-sha256\n"
162 " key-locator\n"
163 " {\n"
164 " type name\n"
165 " name ";
166
167 std::string CONFIG_2 =
168 "\n"
169 " relation equal\n"
170 " }\n"
171 " }\n"
172 "}\n"
173 "trust-anchor\n"
174 "{\n"
175 " type file\n"
176 " file-name \"trust-anchor-2.cert\"\n"
177 "}\n";
178 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
179
180 const boost::filesystem::path CONFIG_PATH =
181 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
182
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700183 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800184
185 validator.validate(*data1,
186 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700187 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800188
189 validator.validate(*data2,
190 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700191 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800192
193 validator.validate(*data3,
194 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700195 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800196}
197
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700198BOOST_AUTO_TEST_CASE(NameFilter3)
Yingdi Yu41546342014-11-30 23:37:53 -0800199{
200 Name identity("/TestValidatorConfig/NameFilter3");
201 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800202 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800203 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800204
205 Name dataName1("/simple/isStrictPrefixOf");
206 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700207 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800208
209 Name dataName2("/simple");
210 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700211 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800212
213 Name dataName3("/simple/isStrictPrefixOf/anotherLevel");
214 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700215 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800216
217 std::string CONFIG_1 =
218 "rule\n"
219 "{\n"
220 " id \"Simple3 Rule\"\n"
221 " for data\n"
222 " filter"
223 " {\n"
224 " type name\n"
225 " name /simple/isStrictPrefixOf\n"
226 " relation is-strict-prefix-of\n"
227 " }\n"
228 " checker\n"
229 " {\n"
230 " type customized\n"
231 " sig-type rsa-sha256\n"
232 " key-locator\n"
233 " {\n"
234 " type name\n"
235 " name ";
236
237 std::string CONFIG_2 =
238 "\n"
239 " relation equal\n"
240 " }\n"
241 " }\n"
242 "}\n"
243 "trust-anchor\n"
244 "{\n"
245 " type file\n"
246 " file-name \"trust-anchor-3.cert\"\n"
247 "}\n";
248 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
249
250 const boost::filesystem::path CONFIG_PATH =
251 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
252
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700253 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800254
255 validator.validate(*data1,
256 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700257 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800258
259 validator.validate(*data2,
260 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700261 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800262
263 validator.validate(*data3,
264 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700265 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800266}
267
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700268BOOST_AUTO_TEST_CASE(NameFilter4)
Yingdi Yu41546342014-11-30 23:37:53 -0800269{
270 Name identity("/TestValidatorConfig/NameFilter4");
271 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800272 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-4.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800273 Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800274
275 Name dataName1("/simple/regex");
276 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700277 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800278
279 Name dataName2("/simple/regex-wrong");
280 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700281 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800282
283 Name dataName3("/simple/regex/correct");
284 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700285 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800286
287 std::string CONFIG_1 =
288 "rule\n"
289 "{\n"
290 " id \"Simple3 Rule\"\n"
291 " for data\n"
292 " filter"
293 " {\n"
294 " type name\n"
295 " regex ^<simple><regex>\n"
296 " }\n"
297 " checker\n"
298 " {\n"
299 " type customized\n"
300 " sig-type rsa-sha256\n"
301 " key-locator\n"
302 " {\n"
303 " type name\n"
304 " name ";
305
306 std::string CONFIG_2 =
307 "\n"
308 " relation equal\n"
309 " }\n"
310 " }\n"
311 "}\n"
312 "trust-anchor\n"
313 "{\n"
314 " type file\n"
315 " file-name \"trust-anchor-4.cert\"\n"
316 "}\n";
317 const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2;
318
319 const boost::filesystem::path CONFIG_PATH =
320 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
321
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700322 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800323
324 validator.validate(*data1,
325 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700326 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800327
328 validator.validate(*data2,
329 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700330 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800331
332 validator.validate(*data3,
333 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700334 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800335}
336
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700337BOOST_AUTO_TEST_CASE(KeyLocatorNameChecker1)
Yingdi Yu41546342014-11-30 23:37:53 -0800338{
339 Name identity("/TestValidatorConfig/KeyLocatorNameChecker1");
340 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800341 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-5.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800342
343 Name dataName1 = identity;
344 dataName1.append("1");
345 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700346 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800347
348 Name dataName2 = identity;
349 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700350 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800351
352 Name dataName3("/TestValidatorConfig/KeyLocatorNameChecker1");
353 shared_ptr<Data> data3 = make_shared<Data>(dataName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700354 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800355
356 const std::string CONFIG =
357 "rule\n"
358 "{\n"
359 " id \"Simple3 Rule\"\n"
360 " for data\n"
361 " checker\n"
362 " {\n"
363 " type customized\n"
364 " sig-type rsa-sha256\n"
365 " key-locator\n"
366 " {\n"
367 " type name\n"
368 " hyper-relation\n"
369 " {\n"
370 " k-regex ^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$\n"
371 " k-expand \\\\1\\\\2\n"
372 " h-relation is-strict-prefix-of\n"
373 " p-regex ^(<>*)$\n"
374 " p-expand \\\\1\n"
375 " }\n"
376 " }\n"
377 " }\n"
378 "}\n"
379 "trust-anchor\n"
380 "{\n"
381 " type file\n"
382 " file-name \"trust-anchor-5.cert\"\n"
383 "}\n";
384 const boost::filesystem::path CONFIG_PATH =
385 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
386
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700387 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800388
389 validator.validate(*data1,
390 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700391 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800392
393 validator.validate(*data2,
394 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700395 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800396
397 validator.validate(*data3,
398 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700399 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800400}
401
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700402BOOST_AUTO_TEST_CASE(FixedSignerChecker)
Yingdi Yu41546342014-11-30 23:37:53 -0800403{
404 Name identity("/TestValidatorConfig/FixedSignerChecker");
405
406 Name identity1 = identity;
407 identity1.append("1").appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800408 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-7.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800409
410 Name identity2 = identity;
411 identity2.append("2").appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800412 addIdentity(identity2);
Yingdi Yu41546342014-11-30 23:37:53 -0800413
414 Name dataName1 = identity;
415 dataName1.append("data").appendVersion();
416 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700417 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1)));
Yingdi Yu41546342014-11-30 23:37:53 -0800418
419 Name dataName2 = identity;
420 dataName2.append("data").appendVersion();
421 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700422 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2)));
Yingdi Yu41546342014-11-30 23:37:53 -0800423
424 Name interestName("/TestValidatorConfig/FixedSignerChecker/fakeSigInfo/fakeSigValue");
425 shared_ptr<Interest> interest = make_shared<Interest>(interestName);
426
427 const std::string CONFIG =
428 "rule\n"
429 "{\n"
430 " id \"FixedSignerChecker Data Rule\"\n"
431 " for data\n"
432 " filter"
433 " {\n"
434 " type name\n"
435 " name /TestValidatorConfig/FixedSignerChecker\n"
436 " relation is-strict-prefix-of\n"
437 " }\n"
438 " checker\n"
439 " {\n"
440 " type fixed-signer\n"
441 " sig-type rsa-sha256\n"
442 " signer\n"
443 " {\n"
444 " type file\n"
445 " file-name \"trust-anchor-7.cert\"\n"
446 " }\n"
447 " }\n"
448 "}\n"
449 "rule\n"
450 "{\n"
451 " id \"FixedSignerChecker Interest Rule\"\n"
452 " for interest\n"
453 " filter"
454 " {\n"
455 " type name\n"
456 " name /TestValidatorConfig/FixedSignerChecker\n"
457 " relation is-strict-prefix-of\n"
458 " }\n"
459 " checker\n"
460 " {\n"
461 " type fixed-signer\n"
462 " sig-type rsa-sha256\n"
463 " signer\n"
464 " {\n"
465 " type file\n"
466 " file-name \"trust-anchor-7.cert\"\n"
467 " }\n"
468 " }\n"
469 "}\n";
470 const boost::filesystem::path CONFIG_PATH =
471 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
472
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700473 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800474
475 validator.validate(*data1,
476 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700477 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800478
479 validator.validate(*data2,
480 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700481 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800482
483 validator.validate(*interest,
484 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700485 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800486}
487
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800488BOOST_AUTO_TEST_CASE(MultiCheckers)
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700489{
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700490 Name identity1("/TestValidatorConfig/MultiCheckers/");
491 identity1.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800492 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-multi-1.cert"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700493
494 Name identity2("/TestValidatorConfig/");
495 identity2.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800496 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-multi-2.cert"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700497
498 const std::string CONFIG =
499 "rule\n"
500 "{\n"
501 " id \"Simple Rule01\"\n"
502 " for data\n"
503 " filter"
504 " {\n"
505 " type name\n"
506 " name /\n"
507 " relation is-prefix-of\n"
508 " }\n"
509 " checker\n" // checker 1, signer should have prefix /TestValidatorConfig/MultiCheckers
510 " {\n"
511 " type customized\n"
512 " sig-type rsa-sha256\n"
513 " key-locator\n"
514 " {\n"
515 " type name\n"
516 " name /TestValidatorConfig/MultiCheckers/\n"
517 " relation is-prefix-of\n"
518 " }\n"
519 " }\n"
520 " checker\n" // checker 2, data should have same prefix of its signer
521 " {\n"
522 " type hierarchical\n"
523 " sig-type rsa-sha256\n"
524 " }\n"
525 " checker\n" // checker 3, the signer should be identity1
526 " {\n"
527 " type fixed-signer\n"
528 " sig-type rsa-sha256\n"
529 " signer\n"
530 " {\n"
531 " type file\n"
532 " file-name \"trust-anchor-multi-1.cert\"\n"
533 " }\n"
534 " }\n"
535 "}\n";
536
537 const boost::filesystem::path CONFIG_PATH =
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800538 (boost::filesystem::current_path() / std::string("unit-test-multicheckers.conf"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700539
540 ValidatorConfig validator;
541 validator.load(CONFIG, CONFIG_PATH.c_str());
542 conf::Checker& checker0 = *validator.m_dataRules.front()->m_checkers[0];
543 conf::Checker& checker1 = *validator.m_dataRules.front()->m_checkers[1];
544 conf::Checker& checker2 = *validator.m_dataRules.front()->m_checkers[2];
545
Junxiao Shi85d90832016-08-04 03:19:46 +0000546 auto data1 = makeData(Name(identity1).append("Test"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700547 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1)));
548 BOOST_CHECK_EQUAL(checker0.check(*data1), 0);
549 BOOST_CHECK_EQUAL(checker1.check(*data1), 0);
550 BOOST_CHECK_EQUAL(checker2.check(*data1), 1);
551
Junxiao Shi85d90832016-08-04 03:19:46 +0000552 auto data2 = makeData(Name(identity2).append("Data2"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700553 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2)));
554 BOOST_CHECK_EQUAL(checker0.check(*data2), -1);
555 BOOST_CHECK_EQUAL(checker1.check(*data2), 0);
556 BOOST_CHECK_EQUAL(checker2.check(*data2), -1);
557
Junxiao Shi85d90832016-08-04 03:19:46 +0000558 auto data3 = makeData(Name(identity2).append("Data3"));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700559 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity1)));
560 BOOST_CHECK_EQUAL(checker0.check(*data3), 0);
561 BOOST_CHECK_EQUAL(checker1.check(*data3), -1);
562 BOOST_CHECK_EQUAL(checker2.check(*data3), 1);
563
Junxiao Shi85d90832016-08-04 03:19:46 +0000564 auto data4 = makeData("/Data4");
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700565 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data4, security::signingByIdentity(identity2)));
566 BOOST_CHECK_EQUAL(checker0.check(*data4), -1);
567 BOOST_CHECK_EQUAL(checker1.check(*data4), -1);
568 BOOST_CHECK_EQUAL(checker2.check(*data4), -1);
569
570 int count = 0;
571 validator.validate(*data1,
572 [&] (const shared_ptr<const Data>&) {
573 BOOST_CHECK(true);
574 count++;
575 },
576 [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); });
577
578 validator.validate(*data2,
579 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
580 [&] (const shared_ptr<const Data>&, const std::string& str) {
581 BOOST_CHECK(true);
582 count++;
583 });
584
585 validator.validate(*data3,
586 [&] (const shared_ptr<const Data>&) {
587 BOOST_CHECK(true);
588 count++;
589 },
590 [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); });
591
592 validator.validate(*data4,
593 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
594 [&] (const shared_ptr<const Data>&, const std::string& str) {
595 BOOST_CHECK(true);
596 count++;
597 });
598
599 BOOST_CHECK_EQUAL(count, 4);
600}
601
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700602BOOST_AUTO_TEST_CASE(Reset)
Yingdi Yu41546342014-11-30 23:37:53 -0800603{
604 Name root("/TestValidatorConfig/Reload");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800605 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800606
Yingdi Yu41546342014-11-30 23:37:53 -0800607 const std::string CONFIG =
608 "rule\n"
609 "{\n"
610 " id \"NRD Prefix Registration Command Rule\"\n"
611 " for interest\n"
612 " filter\n"
613 " {\n"
614 " type name\n"
615 " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n"
616 " }\n"
617 " checker\n"
618 " {\n"
619 " type customized\n"
620 " sig-type rsa-sha256\n"
621 " key-locator\n"
622 " {\n"
623 " type name\n"
624 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n"
625 " }\n"
626 " }\n"
627 "}\n"
628 "rule\n"
629 "{\n"
630 " id \"Testbed Hierarchy Rule\"\n"
631 " for data\n"
632 " filter\n"
633 " {\n"
634 " type name\n"
635 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n"
636 " }\n"
637 " checker\n"
638 " {\n"
639 " type hierarchical\n"
640 " sig-type rsa-sha256\n"
641 " }\n"
642 "}\n"
643 "trust-anchor\n"
644 "{\n"
645 " type file\n"
646 " file-name \"trust-anchor-8.cert\"\n"
647 "}\n";
648 const boost::filesystem::path CONFIG_PATH =
649 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
650
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800651 validator.load(CONFIG, CONFIG_PATH.c_str());
652 BOOST_CHECK_EQUAL(validator.isEmpty(), false);
Yingdi Yu41546342014-11-30 23:37:53 -0800653
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800654 validator.reset();
655 BOOST_CHECK(validator.isEmpty());
Yingdi Yu41546342014-11-30 23:37:53 -0800656}
657
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700658BOOST_AUTO_TEST_CASE(TrustAnchorWildcard)
Yingdi Yu41546342014-11-30 23:37:53 -0800659{
660 Name identity("/TestValidatorConfig/Wildcard");
661 identity.appendVersion();
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800662 addIdentity(identity);
Yingdi Yu41546342014-11-30 23:37:53 -0800663
664 Name dataName1("/any/data");
665 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700666 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity)));
Yingdi Yu41546342014-11-30 23:37:53 -0800667
668 std::string CONFIG =
669 "trust-anchor\n"
670 "{\n"
671 " type any\n"
672 "}\n";
673
674 const boost::filesystem::path CONFIG_PATH =
675 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
676
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700677 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800678
679 validator.validate(*data1,
680 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700681 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -0800682}
683
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700684BOOST_AUTO_TEST_CASE(SignedInterestTest)
Yingdi Yu41546342014-11-30 23:37:53 -0800685{
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800686 Name certName = addIdentity("/TestValidatorConfig/SignedInterestTest");
687 BOOST_REQUIRE(saveIdentityCertificate(certName, "trust-anchor-9.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800688
689 Name interestName("/TestValidatorConfig/SignedInterestTest");
690 Name interestName1 = interestName;
691 interestName1.append("1");
692 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
693 Name interestName2 = interestName;
694 interestName2.append("2");
695 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
696
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800697 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByCertificate(certName)));
698 advanceClocks(time::milliseconds(10));
699 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByCertificate(certName)));
Yingdi Yu41546342014-11-30 23:37:53 -0800700
701 const std::string CONFIG =
702 "rule\n"
703 "{\n"
704 " id \"FixedSignerChecker Interest Rule\"\n"
705 " for interest\n"
706 " filter"
707 " {\n"
708 " type name\n"
709 " name /TestValidatorConfig/SignedInterestTest\n"
710 " relation is-strict-prefix-of\n"
711 " }\n"
712 " checker\n"
713 " {\n"
714 " type fixed-signer\n"
715 " sig-type rsa-sha256\n"
716 " signer\n"
717 " {\n"
718 " type file\n"
719 " file-name \"trust-anchor-9.cert\"\n"
720 " }\n"
721 " }\n"
722 "}\n";
723 const boost::filesystem::path CONFIG_PATH =
724 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
725
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700726 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800727
728 validator.validate(*interest1,
729 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700730 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800731 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800732
733 validator.validate(*interest2,
734 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700735 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800736 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800737
738 validator.validate(*interest1,
739 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700740 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800741 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800742}
743
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700744BOOST_AUTO_TEST_CASE(MaxKeyTest)
Yingdi Yu41546342014-11-30 23:37:53 -0800745{
746 Name identity("/TestValidatorConfig/MaxKeyTest");
747
748 Name identity1 = identity;
749 identity1.append("Key1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800750 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800751
752 Name identity2 = identity;
753 identity2.append("Key2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800754 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800755
756 Name identity3 = identity;
757 identity3.append("Key3");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800758 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800759
760
761 Name interestName("/TestValidatorConfig/MaxKeyTest");
762 Name interestName1 = interestName;
763 interestName1.append("1");
764 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
765 Name interestName2 = interestName;
766 interestName2.append("2");
767 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
768 Name interestName3 = interestName;
769 interestName3.append("3");
770 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
771
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700772 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800773 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700774 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800775 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700776 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800777 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800778
779 const std::string CONFIG =
780 "rule\n"
781 "{\n"
782 " id \"FixedSignerChecker Interest Rule\"\n"
783 " for interest\n"
784 " filter"
785 " {\n"
786 " type name\n"
787 " name /TestValidatorConfig/MaxKeyTest\n"
788 " relation is-strict-prefix-of\n"
789 " }\n"
790 " checker\n"
791 " {\n"
792 " type fixed-signer\n"
793 " sig-type rsa-sha256\n"
794 " signer\n"
795 " {\n"
796 " type file\n"
797 " file-name \"trust-anchor-10-1.cert\"\n"
798 " }\n"
799 " signer\n"
800 " {\n"
801 " type file\n"
802 " file-name \"trust-anchor-10-2.cert\"\n"
803 " }\n"
804 " signer\n"
805 " {\n"
806 " type file\n"
807 " file-name \"trust-anchor-10-3.cert\"\n"
808 " }\n"
809 " }\n"
810 "}\n";
811 const boost::filesystem::path CONFIG_PATH =
812 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
813
Yingdi Yu41546342014-11-30 23:37:53 -0800814 ValidatorConfig validator(face,
815 ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
816 ValidatorConfig::DEFAULT_GRACE_INTERVAL,
817 10,
818 2, // Two keys can be tracked
819 time::seconds(1)); // TTL is set to 1 sec
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700820 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800821
822 validator.validate(*interest1,
823 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700824 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800825 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800826
827 validator.validate(*interest2,
828 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700829 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800830 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800831
832 validator.validate(*interest1,
833 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700834 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800835 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800836
837 validator.validate(*interest3,
838 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700839 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800840 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800841
842 // Should succeed because identity1's key has been cleaned up due to space limit.
843 validator.validate(*interest1,
844 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700845 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800846 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800847}
848
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700849BOOST_AUTO_TEST_CASE(MaxKeyTest2)
Yingdi Yu41546342014-11-30 23:37:53 -0800850{
851 Name identity("/TestValidatorConfig/MaxKeyTest");
852
853 Name identity1 = identity;
854 identity1.append("Key1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800855 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800856
857 Name identity2 = identity;
858 identity2.append("Key2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800859 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800860
861 Name identity3 = identity;
862 identity3.append("Key3");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800863 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800864
865 Name identity4 = identity;
866 identity4.append("Key4");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -0800867 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity4), "trust-anchor-10-4.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -0800868
869
870 Name interestName("/TestValidatorConfig/MaxKeyTest");
871 Name interestName1 = interestName;
872 interestName1.append("1");
873 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
874 Name interestName2 = interestName;
875 interestName2.append("2");
876 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
877 Name interestName3 = interestName;
878 interestName3.append("3");
879 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
880 Name interestName4 = interestName;
881 interestName4.append("4");
882 shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4);
883
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700884 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800885 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700886 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800887 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700888 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800889 advanceClocks(time::milliseconds(10));
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700890 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest4, security::signingByIdentity(identity4)));
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800891 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800892
893 const std::string CONFIG =
894 "rule\n"
895 "{\n"
896 " id \"FixedSignerChecker Interest Rule\"\n"
897 " for interest\n"
898 " filter"
899 " {\n"
900 " type name\n"
901 " name /TestValidatorConfig/MaxKeyTest\n"
902 " relation is-strict-prefix-of\n"
903 " }\n"
904 " checker\n"
905 " {\n"
906 " type fixed-signer\n"
907 " sig-type rsa-sha256\n"
908 " signer\n"
909 " {\n"
910 " type file\n"
911 " file-name \"trust-anchor-10-1.cert\"\n"
912 " }\n"
913 " signer\n"
914 " {\n"
915 " type file\n"
916 " file-name \"trust-anchor-10-2.cert\"\n"
917 " }\n"
918 " signer\n"
919 " {\n"
920 " type file\n"
921 " file-name \"trust-anchor-10-3.cert\"\n"
922 " }\n"
923 " signer\n"
924 " {\n"
925 " type file\n"
926 " file-name \"trust-anchor-10-4.cert\"\n"
927 " }\n"
928 " }\n"
929 "}\n";
930 const boost::filesystem::path CONFIG_PATH =
931 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
932
Yingdi Yu41546342014-11-30 23:37:53 -0800933 ValidatorConfig validator(face,
934 ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
935 ValidatorConfig::DEFAULT_GRACE_INTERVAL,
936 10,
Zhiyi Zhang433686c2016-11-18 10:18:30 -0800937 3, // Three keys can be tracked
Yingdi Yu41546342014-11-30 23:37:53 -0800938 time::seconds(1)); // TTL is set to 1 sec
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700939 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -0800940
941 validator.validate(*interest1,
942 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700943 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800944 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800945
946 validator.validate(*interest2,
947 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700948 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800949 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800950
951 validator.validate(*interest3,
952 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700953 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800954 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800955
956 validator.validate(*interest1,
957 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700958 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800959 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800960
961 validator.validate(*interest2,
962 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700963 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800964 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800965
966 validator.validate(*interest3,
967 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700968 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -0800969
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800970 advanceClocks(time::seconds(2));
Yingdi Yu41546342014-11-30 23:37:53 -0800971
972 validator.validate(*interest4,
973 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700974 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800975 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800976
977 // Should succeed because identity1 and identity2's key has been cleaned up due to ttl limit.
978 validator.validate(*interest1,
979 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700980 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800981 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800982
983 validator.validate(*interest2,
984 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700985 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800986 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800987
988 validator.validate(*interest3,
989 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700990 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800991 advanceClocks(time::milliseconds(10));
Yingdi Yu41546342014-11-30 23:37:53 -0800992}
993
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700994BOOST_AUTO_TEST_CASE(FixedSignerChecker2)
Yingdi Yu41546342014-11-30 23:37:53 -0800995{
996 Name rsaIdentity("/TestValidatorConfig/FixedSignerChecker2/Rsa");
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800997 addIdentity(rsaIdentity, RsaKeyParams());
Yingdi Yu41546342014-11-30 23:37:53 -0800998
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -0700999 Name ecIdentity("/TestValidatorConfig/FixedSignerChecker2/Ec");
1000 auto identity = addIdentity(ecIdentity, EcKeyParams());
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001001 BOOST_REQUIRE(saveIdentityCertificate(identity, "trust-anchor-11.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -08001002
1003 Name dataName("/TestValidatorConfig/FixedSignerChecker2");
1004 shared_ptr<Data> dataRsa = make_shared<Data>(dataName);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001005 m_keyChain.sign(*dataRsa, security::signingByIdentity(rsaIdentity));
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001006 shared_ptr<Data> dataEc = make_shared<Data>(dataName);
1007 m_keyChain.sign(*dataEc, security::signingByIdentity(ecIdentity));
Yingdi Yu41546342014-11-30 23:37:53 -08001008
1009 shared_ptr<Interest> interestRsa = make_shared<Interest>(dataName);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001010 m_keyChain.sign(*interestRsa, security::signingByIdentity(rsaIdentity));
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001011 shared_ptr<Interest> interestEc = make_shared<Interest>(dataName);
1012 m_keyChain.sign(*interestEc, security::signingByIdentity(ecIdentity));
Yingdi Yu41546342014-11-30 23:37:53 -08001013
1014 const std::string CONFIG =
1015 "rule\n"
1016 "{\n"
1017 " id \"FixedSignerChecker Data Rule\"\n"
1018 " for data\n"
1019 " filter"
1020 " {\n"
1021 " type name\n"
1022 " name /TestValidatorConfig/FixedSignerChecker2\n"
1023 " relation equal\n"
1024 " }\n"
1025 " checker\n"
1026 " {\n"
1027 " type fixed-signer\n"
1028 " sig-type ecdsa-sha256\n"
1029 " signer\n"
1030 " {\n"
1031 " type file\n"
1032 " file-name \"trust-anchor-11.cert\"\n"
1033 " }\n"
1034 " }\n"
1035 "}\n"
1036 "rule\n"
1037 "{\n"
1038 " id \"FixedSignerChecker Interest Rule\"\n"
1039 " for interest\n"
1040 " filter"
1041 " {\n"
1042 " type name\n"
1043 " name /TestValidatorConfig/FixedSignerChecker2\n"
1044 " relation equal\n"
1045 " }\n"
1046 " checker\n"
1047 " {\n"
1048 " type fixed-signer\n"
1049 " sig-type ecdsa-sha256\n"
1050 " signer\n"
1051 " {\n"
1052 " type file\n"
1053 " file-name \"trust-anchor-11.cert\"\n"
1054 " }\n"
1055 " }\n"
1056 "}\n";
1057 const boost::filesystem::path CONFIG_PATH =
1058 (boost::filesystem::current_path() / std::string("unit-test.conf"));
1059
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001060 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001061
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001062 validator.validate(*dataEc,
Yingdi Yu41546342014-11-30 23:37:53 -08001063 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001064 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001065
1066 validator.validate(*dataRsa,
1067 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001068 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001069
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -07001070 validator.validate(*interestEc,
Yingdi Yu41546342014-11-30 23:37:53 -08001071 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001072 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001073
1074 validator.validate(*interestRsa,
1075 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001076 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001077}
1078
1079
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001080struct FacesFixture : public ValidatorConfigFixture
Yingdi Yu41546342014-11-30 23:37:53 -08001081{
1082 FacesFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001083 : face1(io, m_v2KeyChain, {true, true})
1084 , face2(io, m_v2KeyChain, {true, true})
Yingdi Yu41546342014-11-30 23:37:53 -08001085 , readInterestOffset1(0)
1086 , readDataOffset1(0)
1087 , readInterestOffset2(0)
1088 , readDataOffset2(0)
1089 {
1090 }
1091
1092 bool
1093 passPacket()
1094 {
1095 bool hasPassed = false;
1096
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001097 checkFace(face1.sentInterests, readInterestOffset1, face2, hasPassed);
1098 checkFace(face1.sentData, readDataOffset1, face2, hasPassed);
1099 checkFace(face2.sentInterests, readInterestOffset2, face1, hasPassed);
Zhiyi Zhang99832d42016-11-18 10:03:49 -08001100 checkFace(face2.sentData, readDataOffset2, face1, hasPassed);
Yingdi Yu41546342014-11-30 23:37:53 -08001101
1102 return hasPassed;
1103 }
1104
1105 template<typename Packet>
1106 void
1107 checkFace(std::vector<Packet>& receivedPackets,
1108 size_t& readPacketOffset,
1109 util::DummyClientFace& receiver,
1110 bool& hasPassed)
1111 {
1112 while (receivedPackets.size() > readPacketOffset) {
1113 receiver.receive(receivedPackets[readPacketOffset]);
1114 readPacketOffset++;
1115 hasPassed = true;
1116 }
1117 }
1118
Yingdi Yu41546342014-11-30 23:37:53 -08001119public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001120 util::DummyClientFace face1;
1121 util::DummyClientFace face2;
Yingdi Yu41546342014-11-30 23:37:53 -08001122
1123 size_t readInterestOffset1;
1124 size_t readDataOffset1;
1125 size_t readInterestOffset2;
1126 size_t readDataOffset2;
1127};
1128
1129BOOST_FIXTURE_TEST_CASE(HierarchicalChecker, FacesFixture)
1130{
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001131 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yu41546342014-11-30 23:37:53 -08001132
1133 Name root("/TestValidatorConfig");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001134 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-6.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -08001135
1136
1137 Name sld("/TestValidatorConfig/HierarchicalChecker");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001138 addIdentity(sld);
Yingdi Yu41546342014-11-30 23:37:53 -08001139 advanceClocks(time::milliseconds(100));
1140 Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001141 shared_ptr<v1::IdentityCertificate> sldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001142 m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName,
1143 root,
1144 time::system_clock::now(),
1145 time::system_clock::now() + time::days(7300),
1146 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001147 m_keyChain.sign(*sldCert, security::signingByIdentity(root));
Yingdi Yu41546342014-11-30 23:37:53 -08001148 m_keyChain.addCertificateAsIdentityDefault(*sldCert);
1149
1150 Name nld("/TestValidatorConfig/HierarchicalChecker/NextLevel");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001151 addIdentity(nld);
Yingdi Yu41546342014-11-30 23:37:53 -08001152 advanceClocks(time::milliseconds(100));
1153 Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001154 shared_ptr<v1::IdentityCertificate> nldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001155 m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName,
1156 sld,
1157 time::system_clock::now(),
1158 time::system_clock::now() + time::days(7300),
1159 subjectDescription);
Zhiyi Zhang433686c2016-11-18 10:18:30 -08001160 m_keyChain.sign(*nldCert, security::signingByIdentity(sld));
Yingdi Yu41546342014-11-30 23:37:53 -08001161 m_keyChain.addCertificateAsIdentityDefault(*nldCert);
1162
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001163 face1.setInterestFilter(sldCert->getName().getPrefix(-1),
1164 [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001165 RegisterPrefixSuccessCallback(),
1166 [] (const Name&, const std::string&) {});
1167
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001168 face1.setInterestFilter(nldCert->getName().getPrefix(-1),
1169 [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001170 RegisterPrefixSuccessCallback(),
1171 [] (const Name&, const std::string&) {});
1172
1173 Name dataName1 = nld;
1174 dataName1.append("data1");
1175 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001176 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001177
1178 Name dataName2("/ConfValidatorTest");
1179 dataName2.append("data1");
1180 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001181 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001182
Yingdi Yu41546342014-11-30 23:37:53 -08001183 const std::string CONFIG =
1184 "rule\n"
1185 "{\n"
1186 " id \"Simple3 Rule\"\n"
1187 " for data\n"
1188 " checker\n"
1189 " {\n"
1190 " type hierarchical\n"
1191 " sig-type rsa-sha256\n"
1192 " }\n"
1193 "}\n"
1194 "trust-anchor\n"
1195 "{\n"
1196 " type file\n"
1197 " file-name \"trust-anchor-6.cert\"\n"
1198 "}\n";
1199 const boost::filesystem::path CONFIG_PATH =
1200 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1201
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001202 auto validator = make_shared<ValidatorConfig>(&face2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001203 validator->load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001204
1205 advanceClocks(time::milliseconds(2), 100);
1206 validator->validate(*data1,
1207 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001208 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001209
1210 do {
1211 advanceClocks(time::milliseconds(2), 10);
1212 } while (passPacket());
1213
1214 validator->validate(*data2,
1215 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001216 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001217
1218 do {
1219 advanceClocks(time::milliseconds(2), 10);
1220 } while (passPacket());
Yingdi Yu41546342014-11-30 23:37:53 -08001221}
1222
1223BOOST_FIXTURE_TEST_CASE(Nrd, FacesFixture)
1224{
Junxiao Shid5827ce2016-07-14 20:49:37 +00001225 advanceClocks(time::nanoseconds(1));
Yingdi Yu41546342014-11-30 23:37:53 -08001226
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001227 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yu41546342014-11-30 23:37:53 -08001228
1229 Name root("/TestValidatorConfig");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001230 BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert"));
Yingdi Yu41546342014-11-30 23:37:53 -08001231
Yingdi Yu41546342014-11-30 23:37:53 -08001232 Name sld("/TestValidatorConfig/Nrd-1");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001233 addIdentity(sld);
Yingdi Yu41546342014-11-30 23:37:53 -08001234 advanceClocks(time::milliseconds(100));
1235 Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001236 shared_ptr<v1::IdentityCertificate> sldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001237 m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName,
1238 root,
1239 time::system_clock::now(),
1240 time::system_clock::now() + time::days(7300),
1241 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001242 m_keyChain.sign(*sldCert, security::signingByIdentity(root));
Yingdi Yu41546342014-11-30 23:37:53 -08001243 m_keyChain.addCertificateAsIdentityDefault(*sldCert);
1244
1245 Name nld("/TestValidatorConfig/Nrd-1/Nrd-2");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001246 addIdentity(nld);
Yingdi Yu41546342014-11-30 23:37:53 -08001247 advanceClocks(time::milliseconds(100));
1248 Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001249 shared_ptr<v1::IdentityCertificate> nldCert =
Yingdi Yu41546342014-11-30 23:37:53 -08001250 m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName,
1251 sld,
1252 time::system_clock::now(),
1253 time::system_clock::now() + time::days(7300),
1254 subjectDescription);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001255 m_keyChain.sign(*nldCert, security::signingByIdentity(sld));
Yingdi Yu41546342014-11-30 23:37:53 -08001256 m_keyChain.addCertificateAsIdentityDefault(*nldCert);
1257
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001258 face1.setInterestFilter(sldCert->getName().getPrefix(-1),
1259 [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001260 RegisterPrefixSuccessCallback(),
1261 [] (const Name&, const std::string&) {});
1262
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001263 face1.setInterestFilter(nldCert->getName().getPrefix(-1),
1264 [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); },
Yingdi Yu41546342014-11-30 23:37:53 -08001265 RegisterPrefixSuccessCallback(),
1266 [] (const Name&, const std::string&) {});
1267
1268 advanceClocks(time::milliseconds(10));
1269 Name interestName1("/localhost/nrd/register/option");
1270 shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001271 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001272
1273 advanceClocks(time::milliseconds(10));
1274 Name interestName2("/localhost/nrd/non-register");
1275 shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001276 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(nld)));
Yingdi Yu41546342014-11-30 23:37:53 -08001277
1278 advanceClocks(time::milliseconds(10));
1279 Name interestName3("/localhost/nrd/register/option");
1280 shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001281 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(root)));
Yingdi Yu41546342014-11-30 23:37:53 -08001282
1283 advanceClocks(time::milliseconds(10));
1284 Name interestName4("/localhost/nrd/register/option/timestamp/nonce/fakeSigInfo/fakeSigValue");
1285 shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4);
1286
1287 const std::string CONFIG =
1288 "rule\n"
1289 "{\n"
1290 " id \"NRD Prefix Registration Command Rule\"\n"
1291 " for interest\n"
1292 " filter\n"
1293 " {\n"
1294 " type name\n"
1295 " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n"
1296 " }\n"
1297 " checker\n"
1298 " {\n"
1299 " type customized\n"
1300 " sig-type rsa-sha256\n"
1301 " key-locator\n"
1302 " {\n"
1303 " type name\n"
1304 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n"
1305 " }\n"
1306 " }\n"
1307 "}\n"
1308 "rule\n"
1309 "{\n"
1310 " id \"Testbed Hierarchy Rule\"\n"
1311 " for data\n"
1312 " filter\n"
1313 " {\n"
1314 " type name\n"
1315 " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n"
1316 " }\n"
1317 " checker\n"
1318 " {\n"
1319 " type hierarchical\n"
1320 " sig-type rsa-sha256\n"
1321 " }\n"
1322 "}\n"
1323 "trust-anchor\n"
1324 "{\n"
1325 " type file\n"
1326 " file-name \"trust-anchor-8.cert\"\n"
1327 "}\n";
1328 const boost::filesystem::path CONFIG_PATH =
1329 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1330
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -08001331 auto validator = make_shared<ValidatorConfig>(&face2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001332 validator->load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001333
1334 advanceClocks(time::milliseconds(2), 100);
1335
1336 // should succeed
1337 validator->validate(*interest1,
1338 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001339 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001340
1341 do {
1342 advanceClocks(time::milliseconds(2), 10);
1343 } while (passPacket());
1344
1345 // should fail
1346 validator->validate(*interest2,
1347 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001348 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001349
1350 do {
1351 advanceClocks(time::milliseconds(2), 10);
1352 } while (passPacket());
1353
1354 // should succeed
1355 validator->validate(*interest3,
1356 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001357 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001358
1359 do {
1360 advanceClocks(time::milliseconds(2), 10);
1361 } while (passPacket());
1362
1363 // should fail
1364 validator->validate(*interest4,
1365 [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001366 [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001367
1368 do {
1369 advanceClocks(time::milliseconds(2), 10);
1370 } while (passPacket());
Yingdi Yu41546342014-11-30 23:37:53 -08001371}
1372
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001373struct DirTestFixture : public ValidatorConfigFixture
Yingdi Yu41546342014-11-30 23:37:53 -08001374{
1375 DirTestFixture()
Alexander Afanasyev70244f42017-01-04 12:47:12 -08001376 : validator(&face, ValidatorConfig::DEFAULT_CERTIFICATE_CACHE,
Yingdi Yu41546342014-11-30 23:37:53 -08001377 ValidatorConfig::DEFAULT_GRACE_INTERVAL, 0)
1378 {
1379 certDirPath = (boost::filesystem::current_path() / std::string("test-cert-dir"));
1380 boost::filesystem::create_directory(certDirPath);
1381
1382 firstCertPath = (boost::filesystem::current_path() /
1383 std::string("test-cert-dir") /
1384 std::string("trust-anchor-1.cert"));
1385
1386 secondCertPath = (boost::filesystem::current_path() /
1387 std::string("test-cert-dir") /
1388 std::string("trust-anchor-2.cert"));
1389
1390 firstIdentity = Name("/TestValidatorConfig/Dir/First");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001391 addIdentity(firstIdentity);
Yingdi Yu41546342014-11-30 23:37:53 -08001392 Name firstCertName = m_keyChain.getDefaultCertificateNameForIdentity(firstIdentity);
1393 firstCert = m_keyChain.getCertificate(firstCertName);
1394 io::save(*firstCert, firstCertPath.string());
1395
1396 secondIdentity = Name("/TestValidatorConfig/Dir/Second");
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001397 addIdentity(secondIdentity);
Yingdi Yu41546342014-11-30 23:37:53 -08001398 Name secondCertName = m_keyChain.getDefaultCertificateNameForIdentity(secondIdentity);
1399 secondCert = m_keyChain.getCertificate(secondCertName);
1400 }
1401
1402 ~DirTestFixture()
1403 {
1404 boost::filesystem::remove_all(certDirPath);
1405 }
1406
1407public:
1408 boost::filesystem::path certDirPath;
1409 boost::filesystem::path firstCertPath;
1410 boost::filesystem::path secondCertPath;
1411
1412 Name firstIdentity;
1413 Name secondIdentity;
1414
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07001415 shared_ptr<v1::IdentityCertificate> firstCert;
1416 shared_ptr<v1::IdentityCertificate> secondCert;
Yingdi Yu41546342014-11-30 23:37:53 -08001417
Yingdi Yu41546342014-11-30 23:37:53 -08001418 ValidatorConfig validator;
1419};
1420
1421BOOST_FIXTURE_TEST_CASE(TrustAnchorDir, DirTestFixture)
1422{
1423 advanceClocks(time::milliseconds(10));
1424
1425 Name dataName1("/any/data/1");
1426 shared_ptr<Data> data1 = make_shared<Data>(dataName1);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001427 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(firstIdentity)));
Yingdi Yu41546342014-11-30 23:37:53 -08001428
1429 Name dataName2("/any/data/2");
1430 shared_ptr<Data> data2 = make_shared<Data>(dataName2);
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001431 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(secondIdentity)));
Yingdi Yu41546342014-11-30 23:37:53 -08001432
1433 std::string CONFIG =
1434 "rule\n"
1435 "{\n"
1436 " id \"Any Rule\"\n"
1437 " for data\n"
1438 " filter\n"
1439 " {\n"
1440 " type name\n"
1441 " regex ^<>*$\n"
1442 " }\n"
1443 " checker\n"
1444 " {\n"
1445 " type customized\n"
1446 " sig-type rsa-sha256\n"
1447 " key-locator\n"
1448 " {\n"
1449 " type name\n"
1450 " regex ^<>*$\n"
1451 " }\n"
1452 " }\n"
1453 "}\n"
1454 "trust-anchor\n"
1455 "{\n"
1456 " type dir\n"
1457 " dir test-cert-dir\n"
1458 " refresh 1s\n"
1459 "}\n";
1460
1461 const boost::filesystem::path CONFIG_PATH =
1462 (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
1463
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07001464 validator.load(CONFIG, CONFIG_PATH.c_str());
Yingdi Yu41546342014-11-30 23:37:53 -08001465
1466 advanceClocks(time::milliseconds(10), 20);
1467 validator.validate(*data1,
1468 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001469 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001470 advanceClocks(time::milliseconds(10), 20);
1471
1472 validator.validate(*data2,
1473 [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001474 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); });
Yingdi Yu41546342014-11-30 23:37:53 -08001475 advanceClocks(time::milliseconds(10), 20);
1476
1477 io::save(*secondCert, secondCertPath.string());
1478 advanceClocks(time::milliseconds(10), 200);
1479
1480 validator.validate(*data1,
1481 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001482 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001483 advanceClocks(time::milliseconds(10), 20);
1484
1485 validator.validate(*data2,
1486 [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); },
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001487 [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); });
Yingdi Yu41546342014-11-30 23:37:53 -08001488 advanceClocks(time::milliseconds(10), 20);
1489}
1490
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001491class DirectCertFetchFixture : public ValidatorConfigFixture
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001492{
1493public:
1494 DirectCertFetchFixture()
Alexander Afanasyev80782e02017-01-04 13:16:54 -08001495 : clientFace(io, m_v2KeyChain, {true, true})
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001496 , validationResult(boost::logic::indeterminate)
1497 {
Alexander Afanasyevfc99b512017-01-04 11:10:36 -08001498 auto certName = addIdentity(ca);
1499 saveIdentityCertificate(certName, "trust-anchor-1.cert");
1500 addSubCertificate(user, ca);
Zhiyi Zhang48becde2017-01-05 16:41:38 -08001501
1502 userCertName = m_keyChain.getDefaultCertificateNameForIdentity(user);
1503 userCert = m_keyChain.getCertificate(userCertName);
1504 }
1505
1506protected:
1507 void
1508 runTest(const std::function<void(const Interest&, const Interest&)> respond)
1509 {
1510 optional<Interest> directInterest;
1511 optional<Interest> infrastructureInterest;
1512 clientFace.onSendInterest.connect([&] (const Interest& interest) {
1513 const Name& interestName = interest.getName();
1514 if (interestName == userCert->getName().getPrefix(-1)) {
1515 auto nextHopFaceIdTag = interest.getTag<lp::NextHopFaceIdTag>();
1516 if (nextHopFaceIdTag != nullptr) {
1517 BOOST_CHECK(!directInterest);
1518 directInterest = interest;
1519 }
1520 else {
1521 BOOST_CHECK(!infrastructureInterest);
1522 infrastructureInterest = interest;
1523 }
1524 if (static_cast<bool>(directInterest) && static_cast<bool>(infrastructureInterest)) {
1525 io.post([directInterest, infrastructureInterest, respond] {
1526 respond(directInterest.value(), infrastructureInterest.value());
1527 });
1528 directInterest = nullopt;
1529 infrastructureInterest = nullopt;
1530 }
1531 }
1532 });
1533
1534 const boost::filesystem::path CONFIG_PATH =
1535 (boost::filesystem::current_path() / std::string("unit-test-direct.conf"));
1536 ValidatorConfig validator(&clientFace);
1537 validator.load(CONFIG, CONFIG_PATH.c_str());
1538 validator.setDirectCertFetchEnabled(true);
1539
1540 shared_ptr<Interest> interest = make_shared<Interest>(interestName);
1541 interest->setTag(make_shared<lp::IncomingFaceIdTag>(123));
1542 BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest, security::signingByIdentity(user)));
1543
1544 validator.validate(*interest,
1545 [&] (const shared_ptr<const Interest>&) {
1546 BOOST_CHECK(boost::logic::indeterminate(validationResult));
1547 validationResult = true;
1548 },
1549 [&] (const shared_ptr<const Interest>&, const std::string& s) {
1550 BOOST_CHECK(boost::logic::indeterminate(validationResult));
1551 validationResult = false;
1552 });
1553
1554 advanceClocks(time::milliseconds(200), 60);
1555
1556 BOOST_CHECK(!boost::logic::indeterminate(validationResult));
1557 }
1558
1559public:
1560 util::DummyClientFace clientFace;
1561 const Name ca = Name("/DirectCertFetch");
1562 const Name user = Name("/DirectCertFetch/user");
1563 const Name interestName = Name("/DirectCertFetch/user/tag-interest");
1564 const std::string CONFIG = R"_TEXT_(
1565 rule
1566 {
1567 id "RuleForInterest"
1568 for interest
1569 checker
1570 {
1571 type hierarchical
1572 sig-type rsa-sha256
1573 }
1574 }
1575 rule
1576 {
1577 id "RuleForData"
1578 for data
1579 filter
1580 {
1581 type name
1582 regex ^<>*$
1583 }
1584 checker
1585 {
1586 type customized
1587 sig-type rsa-sha256
1588 key-locator
1589 {
1590 type name
1591 regex ^<>*$
1592 }
1593 }
1594 }
1595 trust-anchor
1596 {
1597 type file
1598 file-name "trust-anchor-1.cert"
1599 }
1600 )_TEXT_";
1601 boost::logic::tribool validationResult;
1602 Name userCertName;
1603 shared_ptr<v1::IdentityCertificate> userCert;
1604};
1605
1606BOOST_FIXTURE_TEST_SUITE(DirectCertFetch, DirectCertFetchFixture)
1607
1608BOOST_AUTO_TEST_CASE(CertFetchSuccess)
1609{
1610 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) {
1611 this->clientFace.receive(*userCert);
1612 });
1613
1614 BOOST_CHECK_EQUAL(validationResult, true);
1615}
1616
1617BOOST_AUTO_TEST_CASE(CertFetchTimeout)
1618{
1619 // In this test case, certificate request would time out
1620 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) { });
1621
1622 BOOST_CHECK_EQUAL(validationResult, false);
1623}
1624
1625BOOST_AUTO_TEST_CASE(CertFetchNack)
1626{
1627 runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) {
1628 lp::Nack nackInfrastructureInterest(infrastructureInterest);
1629 nackInfrastructureInterest.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
1630 this->clientFace.receive(nackInfrastructureInterest);
1631 });
1632
1633 BOOST_CHECK_EQUAL(validationResult, false);
1634}
1635
1636BOOST_AUTO_TEST_SUITE_END() // DirectCertFetch
Junxiao Shid5827ce2016-07-14 20:49:37 +00001637BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig
1638BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu41546342014-11-30 23:37:53 -08001639
1640} // namespace tests
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -07001641} // namespace security
Yingdi Yu41546342014-11-30 23:37:53 -08001642} // namespace ndn