Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 24 | #include "security/signing-helpers.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 25 | #include "util/io.hpp" |
| 26 | #include "util/scheduler.hpp" |
| 27 | #include "util/dummy-client-face.hpp" |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 28 | #include "lp/tags.hpp" |
| 29 | #include "lp/nack.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 30 | |
| 31 | #include <boost/asio.hpp> |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 32 | #include <boost/logic/tribool.hpp> |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 33 | |
| 34 | #include "identity-management-fixture.hpp" |
| 35 | #include "../identity-management-time-fixture.hpp" |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 36 | #include "../make-interest-data.hpp" |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 37 | #include "boost-test.hpp" |
| 38 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 39 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 40 | namespace security { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 41 | namespace tests { |
| 42 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 43 | using namespace ndn::tests; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 44 | |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 45 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 46 | |
| 47 | // Needed to create Face instance |
| 48 | class ValidatorConfigFixture : public IdentityManagementV1TimeFixture |
| 49 | { |
| 50 | public: |
| 51 | ValidatorConfigFixture() |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame^] | 52 | : m_v2KeyChain("pib-memory:", "tpm-memory:") |
| 53 | , face(nullptr, m_v2KeyChain) |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 54 | , validator(face) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | public: |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame^] | 59 | v2::KeyChain m_v2KeyChain; |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 60 | Face face; |
| 61 | ValidatorConfig validator; |
| 62 | }; |
| 63 | |
| 64 | BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, ValidatorConfigFixture) |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 65 | |
| 66 | BOOST_AUTO_TEST_CASE(NameFilter) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 67 | { |
| 68 | Name identity("/TestValidatorConfig/NameFilter"); |
| 69 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 70 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 71 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 72 | |
| 73 | Name dataName1("/simple/equal"); |
| 74 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 75 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 76 | |
| 77 | Name dataName2("/simple/different"); |
| 78 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 79 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 80 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 117 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 118 | |
| 119 | validator.validate(*data1, |
| 120 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 121 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 122 | |
| 123 | validator.validate(*data2, |
| 124 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 125 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(NameFilter2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 129 | { |
| 130 | Name identity("/TestValidatorConfig/NameFilter2"); |
| 131 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 132 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 133 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 134 | |
| 135 | Name dataName1("/simple/isPrefixOf"); |
| 136 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 137 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 138 | |
| 139 | Name dataName2("/simple/notPrefixOf"); |
| 140 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 141 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 142 | |
| 143 | Name dataName3("/simple/isPrefixOf/anotherLevel"); |
| 144 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 145 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 146 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 183 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 184 | |
| 185 | validator.validate(*data1, |
| 186 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 187 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 188 | |
| 189 | validator.validate(*data2, |
| 190 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 191 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 192 | |
| 193 | validator.validate(*data3, |
| 194 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 195 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(NameFilter3) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 199 | { |
| 200 | Name identity("/TestValidatorConfig/NameFilter3"); |
| 201 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 202 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 203 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 204 | |
| 205 | Name dataName1("/simple/isStrictPrefixOf"); |
| 206 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 207 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 208 | |
| 209 | Name dataName2("/simple"); |
| 210 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 211 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 212 | |
| 213 | Name dataName3("/simple/isStrictPrefixOf/anotherLevel"); |
| 214 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 215 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 216 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 253 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 254 | |
| 255 | validator.validate(*data1, |
| 256 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 257 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 258 | |
| 259 | validator.validate(*data2, |
| 260 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 261 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 262 | |
| 263 | validator.validate(*data3, |
| 264 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 265 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 268 | BOOST_AUTO_TEST_CASE(NameFilter4) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 269 | { |
| 270 | Name identity("/TestValidatorConfig/NameFilter4"); |
| 271 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 272 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-4.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 273 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 274 | |
| 275 | Name dataName1("/simple/regex"); |
| 276 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 277 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 278 | |
| 279 | Name dataName2("/simple/regex-wrong"); |
| 280 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 281 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 282 | |
| 283 | Name dataName3("/simple/regex/correct"); |
| 284 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 285 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 286 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 322 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 323 | |
| 324 | validator.validate(*data1, |
| 325 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 326 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 327 | |
| 328 | validator.validate(*data2, |
| 329 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 330 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 331 | |
| 332 | validator.validate(*data3, |
| 333 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 334 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 337 | BOOST_AUTO_TEST_CASE(KeyLocatorNameChecker1) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 338 | { |
| 339 | Name identity("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 340 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 341 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-5.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 342 | |
| 343 | Name dataName1 = identity; |
| 344 | dataName1.append("1"); |
| 345 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 346 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 347 | |
| 348 | Name dataName2 = identity; |
| 349 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 350 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 351 | |
| 352 | Name dataName3("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 353 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 354 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 355 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 387 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 388 | |
| 389 | validator.validate(*data1, |
| 390 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 391 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 392 | |
| 393 | validator.validate(*data2, |
| 394 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 395 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 396 | |
| 397 | validator.validate(*data3, |
| 398 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 399 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 402 | BOOST_AUTO_TEST_CASE(FixedSignerChecker) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 403 | { |
| 404 | Name identity("/TestValidatorConfig/FixedSignerChecker"); |
| 405 | |
| 406 | Name identity1 = identity; |
| 407 | identity1.append("1").appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 408 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-7.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 409 | |
| 410 | Name identity2 = identity; |
| 411 | identity2.append("2").appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 412 | addIdentity(identity2); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 413 | |
| 414 | Name dataName1 = identity; |
| 415 | dataName1.append("data").appendVersion(); |
| 416 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 417 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 418 | |
| 419 | Name dataName2 = identity; |
| 420 | dataName2.append("data").appendVersion(); |
| 421 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 422 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 423 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 473 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 474 | |
| 475 | validator.validate(*data1, |
| 476 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 477 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 478 | |
| 479 | validator.validate(*data2, |
| 480 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 481 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 482 | |
| 483 | validator.validate(*interest, |
| 484 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 485 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 488 | BOOST_AUTO_TEST_CASE(MultiCheckers) |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 489 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 490 | Name identity1("/TestValidatorConfig/MultiCheckers/"); |
| 491 | identity1.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 492 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-multi-1.cert")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 493 | |
| 494 | Name identity2("/TestValidatorConfig/"); |
| 495 | identity2.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 496 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-multi-2.cert")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 497 | |
| 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 Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 538 | (boost::filesystem::current_path() / std::string("unit-test-multicheckers.conf")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 539 | |
| 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 Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 546 | auto data1 = makeData(Name(identity1).append("Test")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 547 | 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 Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 552 | auto data2 = makeData(Name(identity2).append("Data2")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 553 | 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 Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 558 | auto data3 = makeData(Name(identity2).append("Data3")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 559 | 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 Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 564 | auto data4 = makeData("/Data4"); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 565 | 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 602 | BOOST_AUTO_TEST_CASE(Reset) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 603 | { |
| 604 | Name root("/TestValidatorConfig/Reload"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 605 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 606 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 607 | 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 Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 651 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
| 652 | BOOST_CHECK_EQUAL(validator.isEmpty(), false); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 653 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 654 | validator.reset(); |
| 655 | BOOST_CHECK(validator.isEmpty()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 658 | BOOST_AUTO_TEST_CASE(TrustAnchorWildcard) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 659 | { |
| 660 | Name identity("/TestValidatorConfig/Wildcard"); |
| 661 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 662 | addIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 663 | |
| 664 | Name dataName1("/any/data"); |
| 665 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 666 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 667 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 677 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 678 | |
| 679 | validator.validate(*data1, |
| 680 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 681 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 684 | BOOST_AUTO_TEST_CASE(SignedInterestTest) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 685 | { |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 686 | Name certName = addIdentity("/TestValidatorConfig/SignedInterestTest"); |
| 687 | BOOST_REQUIRE(saveIdentityCertificate(certName, "trust-anchor-9.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 688 | |
| 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 Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 697 | 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 Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 700 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 726 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 727 | |
| 728 | validator.validate(*interest1, |
| 729 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 730 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 731 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 732 | |
| 733 | validator.validate(*interest2, |
| 734 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 735 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 736 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 737 | |
| 738 | validator.validate(*interest1, |
| 739 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 740 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 741 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 744 | BOOST_AUTO_TEST_CASE(MaxKeyTest) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 745 | { |
| 746 | Name identity("/TestValidatorConfig/MaxKeyTest"); |
| 747 | |
| 748 | Name identity1 = identity; |
| 749 | identity1.append("Key1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 750 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 751 | |
| 752 | Name identity2 = identity; |
| 753 | identity2.append("Key2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 754 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 755 | |
| 756 | Name identity3 = identity; |
| 757 | identity3.append("Key3"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 758 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 759 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 772 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 773 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 774 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 775 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 776 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 777 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 778 | |
| 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 Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 814 | 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 820 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 821 | |
| 822 | validator.validate(*interest1, |
| 823 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 824 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 825 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 826 | |
| 827 | validator.validate(*interest2, |
| 828 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 829 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 830 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 831 | |
| 832 | validator.validate(*interest1, |
| 833 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 834 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 835 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 836 | |
| 837 | validator.validate(*interest3, |
| 838 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 839 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 840 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 841 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 845 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 846 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 849 | BOOST_AUTO_TEST_CASE(MaxKeyTest2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 850 | { |
| 851 | Name identity("/TestValidatorConfig/MaxKeyTest"); |
| 852 | |
| 853 | Name identity1 = identity; |
| 854 | identity1.append("Key1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 855 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 856 | |
| 857 | Name identity2 = identity; |
| 858 | identity2.append("Key2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 859 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 860 | |
| 861 | Name identity3 = identity; |
| 862 | identity3.append("Key3"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 863 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 864 | |
| 865 | Name identity4 = identity; |
| 866 | identity4.append("Key4"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 867 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity4), "trust-anchor-10-4.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 868 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 884 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 885 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 886 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 887 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 888 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(identity3))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 889 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 890 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest4, security::signingByIdentity(identity4))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 891 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 892 | |
| 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 Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 933 | ValidatorConfig validator(face, |
| 934 | ValidatorConfig::DEFAULT_CERTIFICATE_CACHE, |
| 935 | ValidatorConfig::DEFAULT_GRACE_INTERVAL, |
| 936 | 10, |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 937 | 3, // Three keys can be tracked |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 938 | time::seconds(1)); // TTL is set to 1 sec |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 939 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 940 | |
| 941 | validator.validate(*interest1, |
| 942 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 943 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 944 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 945 | |
| 946 | validator.validate(*interest2, |
| 947 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 948 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 949 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 950 | |
| 951 | validator.validate(*interest3, |
| 952 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 953 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 954 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 955 | |
| 956 | validator.validate(*interest1, |
| 957 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 958 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 959 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 960 | |
| 961 | validator.validate(*interest2, |
| 962 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 963 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 964 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 965 | |
| 966 | validator.validate(*interest3, |
| 967 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 968 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 969 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 970 | advanceClocks(time::seconds(2)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 971 | |
| 972 | validator.validate(*interest4, |
| 973 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 974 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 975 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 976 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 980 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 981 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 982 | |
| 983 | validator.validate(*interest2, |
| 984 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 985 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 986 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 987 | |
| 988 | validator.validate(*interest3, |
| 989 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 990 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 991 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 992 | } |
| 993 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 994 | BOOST_AUTO_TEST_CASE(FixedSignerChecker2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 995 | { |
| 996 | Name rsaIdentity("/TestValidatorConfig/FixedSignerChecker2/Rsa"); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 997 | addIdentity(rsaIdentity, RsaKeyParams()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 998 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 999 | Name ecIdentity("/TestValidatorConfig/FixedSignerChecker2/Ec"); |
| 1000 | auto identity = addIdentity(ecIdentity, EcKeyParams()); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1001 | BOOST_REQUIRE(saveIdentityCertificate(identity, "trust-anchor-11.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1002 | |
| 1003 | Name dataName("/TestValidatorConfig/FixedSignerChecker2"); |
| 1004 | shared_ptr<Data> dataRsa = make_shared<Data>(dataName); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1005 | m_keyChain.sign(*dataRsa, security::signingByIdentity(rsaIdentity)); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1006 | shared_ptr<Data> dataEc = make_shared<Data>(dataName); |
| 1007 | m_keyChain.sign(*dataEc, security::signingByIdentity(ecIdentity)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1008 | |
| 1009 | shared_ptr<Interest> interestRsa = make_shared<Interest>(dataName); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1010 | m_keyChain.sign(*interestRsa, security::signingByIdentity(rsaIdentity)); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1011 | shared_ptr<Interest> interestEc = make_shared<Interest>(dataName); |
| 1012 | m_keyChain.sign(*interestEc, security::signingByIdentity(ecIdentity)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1013 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1060 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1061 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1062 | validator.validate(*dataEc, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1063 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1064 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1065 | |
| 1066 | validator.validate(*dataRsa, |
| 1067 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1068 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1069 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1070 | validator.validate(*interestEc, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1071 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1072 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1073 | |
| 1074 | validator.validate(*interestRsa, |
| 1075 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1076 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1080 | struct FacesFixture : public ValidatorConfigFixture |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1081 | { |
| 1082 | FacesFixture() |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame^] | 1083 | : face1(io, m_v2KeyChain, {true, true}) |
| 1084 | , face2(io, m_v2KeyChain, {true, true}) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1085 | , 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 Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1097 | checkFace(face1.sentInterests, readInterestOffset1, face2, hasPassed); |
| 1098 | checkFace(face1.sentData, readDataOffset1, face2, hasPassed); |
| 1099 | checkFace(face2.sentInterests, readInterestOffset2, face1, hasPassed); |
Zhiyi Zhang | 99832d4 | 2016-11-18 10:03:49 -0800 | [diff] [blame] | 1100 | checkFace(face2.sentData, readDataOffset2, face1, hasPassed); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1101 | |
| 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 Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1119 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1120 | util::DummyClientFace face1; |
| 1121 | util::DummyClientFace face2; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1122 | |
| 1123 | size_t readInterestOffset1; |
| 1124 | size_t readDataOffset1; |
| 1125 | size_t readInterestOffset2; |
| 1126 | size_t readDataOffset2; |
| 1127 | }; |
| 1128 | |
| 1129 | BOOST_FIXTURE_TEST_CASE(HierarchicalChecker, FacesFixture) |
| 1130 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1131 | std::vector<v1::CertificateSubjectDescription> subjectDescription; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1132 | |
| 1133 | Name root("/TestValidatorConfig"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1134 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-6.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1135 | |
| 1136 | |
| 1137 | Name sld("/TestValidatorConfig/HierarchicalChecker"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1138 | addIdentity(sld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1139 | advanceClocks(time::milliseconds(100)); |
| 1140 | Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1141 | shared_ptr<v1::IdentityCertificate> sldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1142 | m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 1143 | root, |
| 1144 | time::system_clock::now(), |
| 1145 | time::system_clock::now() + time::days(7300), |
| 1146 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1147 | m_keyChain.sign(*sldCert, security::signingByIdentity(root)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1148 | m_keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 1149 | |
| 1150 | Name nld("/TestValidatorConfig/HierarchicalChecker/NextLevel"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1151 | addIdentity(nld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1152 | advanceClocks(time::milliseconds(100)); |
| 1153 | Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1154 | shared_ptr<v1::IdentityCertificate> nldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1155 | m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 1156 | sld, |
| 1157 | time::system_clock::now(), |
| 1158 | time::system_clock::now() + time::days(7300), |
| 1159 | subjectDescription); |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 1160 | m_keyChain.sign(*nldCert, security::signingByIdentity(sld)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1161 | m_keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 1162 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1163 | face1.setInterestFilter(sldCert->getName().getPrefix(-1), |
| 1164 | [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1165 | RegisterPrefixSuccessCallback(), |
| 1166 | [] (const Name&, const std::string&) {}); |
| 1167 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1168 | face1.setInterestFilter(nldCert->getName().getPrefix(-1), |
| 1169 | [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1170 | 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1176 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1177 | |
| 1178 | Name dataName2("/ConfValidatorTest"); |
| 1179 | dataName2.append("data1"); |
| 1180 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1181 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1182 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1183 | 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 Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1202 | auto validator = make_shared<ValidatorConfig>(&face2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1203 | validator->load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1204 | |
| 1205 | advanceClocks(time::milliseconds(2), 100); |
| 1206 | validator->validate(*data1, |
| 1207 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1208 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1209 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1216 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1217 | |
| 1218 | do { |
| 1219 | advanceClocks(time::milliseconds(2), 10); |
| 1220 | } while (passPacket()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | BOOST_FIXTURE_TEST_CASE(Nrd, FacesFixture) |
| 1224 | { |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 1225 | advanceClocks(time::nanoseconds(1)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1226 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1227 | std::vector<v1::CertificateSubjectDescription> subjectDescription; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1228 | |
| 1229 | Name root("/TestValidatorConfig"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1230 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1231 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1232 | Name sld("/TestValidatorConfig/Nrd-1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1233 | addIdentity(sld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1234 | advanceClocks(time::milliseconds(100)); |
| 1235 | Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1236 | shared_ptr<v1::IdentityCertificate> sldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1237 | m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 1238 | root, |
| 1239 | time::system_clock::now(), |
| 1240 | time::system_clock::now() + time::days(7300), |
| 1241 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1242 | m_keyChain.sign(*sldCert, security::signingByIdentity(root)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1243 | m_keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 1244 | |
| 1245 | Name nld("/TestValidatorConfig/Nrd-1/Nrd-2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1246 | addIdentity(nld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1247 | advanceClocks(time::milliseconds(100)); |
| 1248 | Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1249 | shared_ptr<v1::IdentityCertificate> nldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1250 | m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 1251 | sld, |
| 1252 | time::system_clock::now(), |
| 1253 | time::system_clock::now() + time::days(7300), |
| 1254 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1255 | m_keyChain.sign(*nldCert, security::signingByIdentity(sld)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1256 | m_keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 1257 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1258 | face1.setInterestFilter(sldCert->getName().getPrefix(-1), |
| 1259 | [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1260 | RegisterPrefixSuccessCallback(), |
| 1261 | [] (const Name&, const std::string&) {}); |
| 1262 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1263 | face1.setInterestFilter(nldCert->getName().getPrefix(-1), |
| 1264 | [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1265 | 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1271 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1272 | |
| 1273 | advanceClocks(time::milliseconds(10)); |
| 1274 | Name interestName2("/localhost/nrd/non-register"); |
| 1275 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1276 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1277 | |
| 1278 | advanceClocks(time::milliseconds(10)); |
| 1279 | Name interestName3("/localhost/nrd/register/option"); |
| 1280 | shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1281 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(root))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1282 | |
| 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 Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1331 | auto validator = make_shared<ValidatorConfig>(&face2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1332 | validator->load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1333 | |
| 1334 | advanceClocks(time::milliseconds(2), 100); |
| 1335 | |
| 1336 | // should succeed |
| 1337 | validator->validate(*interest1, |
| 1338 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1339 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1340 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1348 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1349 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1357 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1358 | |
| 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1366 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1367 | |
| 1368 | do { |
| 1369 | advanceClocks(time::milliseconds(2), 10); |
| 1370 | } while (passPacket()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1373 | struct DirTestFixture : public ValidatorConfigFixture |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1374 | { |
| 1375 | DirTestFixture() |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1376 | : validator(&face, ValidatorConfig::DEFAULT_CERTIFICATE_CACHE, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1377 | 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 Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1391 | addIdentity(firstIdentity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1392 | 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 Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1397 | addIdentity(secondIdentity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1398 | 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 | |
| 1407 | public: |
| 1408 | boost::filesystem::path certDirPath; |
| 1409 | boost::filesystem::path firstCertPath; |
| 1410 | boost::filesystem::path secondCertPath; |
| 1411 | |
| 1412 | Name firstIdentity; |
| 1413 | Name secondIdentity; |
| 1414 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1415 | shared_ptr<v1::IdentityCertificate> firstCert; |
| 1416 | shared_ptr<v1::IdentityCertificate> secondCert; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1417 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1418 | ValidatorConfig validator; |
| 1419 | }; |
| 1420 | |
| 1421 | BOOST_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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1427 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(firstIdentity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1428 | |
| 1429 | Name dataName2("/any/data/2"); |
| 1430 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1431 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(secondIdentity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1432 | |
| 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 Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1464 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1465 | |
| 1466 | advanceClocks(time::milliseconds(10), 20); |
| 1467 | validator.validate(*data1, |
| 1468 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1469 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1470 | advanceClocks(time::milliseconds(10), 20); |
| 1471 | |
| 1472 | validator.validate(*data2, |
| 1473 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1474 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1475 | 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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1482 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1483 | advanceClocks(time::milliseconds(10), 20); |
| 1484 | |
| 1485 | validator.validate(*data2, |
| 1486 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1487 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1488 | advanceClocks(time::milliseconds(10), 20); |
| 1489 | } |
| 1490 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame^] | 1491 | class DirectCertFetchFixture : public ValidatorConfigFixture |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 1492 | { |
| 1493 | public: |
| 1494 | DirectCertFetchFixture() |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame^] | 1495 | : clientFace(io, m_v2KeyChain, {true, true}) |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 1496 | , validationResult(boost::logic::indeterminate) |
| 1497 | { |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1498 | auto certName = addIdentity(ca); |
| 1499 | saveIdentityCertificate(certName, "trust-anchor-1.cert"); |
| 1500 | addSubCertificate(user, ca); |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 1501 | |
| 1502 | userCertName = m_keyChain.getDefaultCertificateNameForIdentity(user); |
| 1503 | userCert = m_keyChain.getCertificate(userCertName); |
| 1504 | } |
| 1505 | |
| 1506 | protected: |
| 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 | |
| 1559 | public: |
| 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 | |
| 1606 | BOOST_FIXTURE_TEST_SUITE(DirectCertFetch, DirectCertFetchFixture) |
| 1607 | |
| 1608 | BOOST_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 | |
| 1617 | BOOST_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 | |
| 1625 | BOOST_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 | |
| 1636 | BOOST_AUTO_TEST_SUITE_END() // DirectCertFetch |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 1637 | BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig |
| 1638 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1639 | |
| 1640 | } // namespace tests |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1641 | } // namespace security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1642 | } // namespace ndn |