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() |
| 52 | : face(nullptr, m_keyChain) |
| 53 | , validator(face) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | public: |
| 58 | Face face; |
| 59 | ValidatorConfig validator; |
| 60 | }; |
| 61 | |
| 62 | BOOST_FIXTURE_TEST_SUITE(TestValidatorConfig, ValidatorConfigFixture) |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 63 | |
| 64 | BOOST_AUTO_TEST_CASE(NameFilter) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 65 | { |
| 66 | Name identity("/TestValidatorConfig/NameFilter"); |
| 67 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 68 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 69 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 70 | |
| 71 | Name dataName1("/simple/equal"); |
| 72 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 73 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 74 | |
| 75 | Name dataName2("/simple/different"); |
| 76 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 77 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 78 | |
| 79 | std::string CONFIG_1 = |
| 80 | "rule\n" |
| 81 | "{\n" |
| 82 | " id \"Simple Rule\"\n" |
| 83 | " for data\n" |
| 84 | " filter" |
| 85 | " {\n" |
| 86 | " type name\n" |
| 87 | " name /simple/equal\n" |
| 88 | " relation equal\n" |
| 89 | " }\n" |
| 90 | " checker\n" |
| 91 | " {\n" |
| 92 | " type customized\n" |
| 93 | " sig-type rsa-sha256\n" |
| 94 | " key-locator\n" |
| 95 | " {\n" |
| 96 | " type name\n" |
| 97 | " name "; |
| 98 | |
| 99 | std::string CONFIG_2 = |
| 100 | "\n" |
| 101 | " relation equal\n" |
| 102 | " }\n" |
| 103 | " }\n" |
| 104 | "}\n" |
| 105 | "trust-anchor\n" |
| 106 | "{\n" |
| 107 | " type file\n" |
| 108 | " file-name \"trust-anchor-1.cert\"\n" |
| 109 | "}\n"; |
| 110 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 111 | |
| 112 | const boost::filesystem::path CONFIG_PATH = |
| 113 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 114 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 115 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 116 | |
| 117 | validator.validate(*data1, |
| 118 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 119 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 120 | |
| 121 | validator.validate(*data2, |
| 122 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 123 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(NameFilter2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 127 | { |
| 128 | Name identity("/TestValidatorConfig/NameFilter2"); |
| 129 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 130 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 131 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 132 | |
| 133 | Name dataName1("/simple/isPrefixOf"); |
| 134 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 135 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 136 | |
| 137 | Name dataName2("/simple/notPrefixOf"); |
| 138 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 139 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 140 | |
| 141 | Name dataName3("/simple/isPrefixOf/anotherLevel"); |
| 142 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 143 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 144 | |
| 145 | std::string CONFIG_1 = |
| 146 | "rule\n" |
| 147 | "{\n" |
| 148 | " id \"Simple2 Rule\"\n" |
| 149 | " for data\n" |
| 150 | " filter" |
| 151 | " {\n" |
| 152 | " type name\n" |
| 153 | " name /simple/isPrefixOf\n" |
| 154 | " relation is-prefix-of\n" |
| 155 | " }\n" |
| 156 | " checker\n" |
| 157 | " {\n" |
| 158 | " type customized\n" |
| 159 | " sig-type rsa-sha256\n" |
| 160 | " key-locator\n" |
| 161 | " {\n" |
| 162 | " type name\n" |
| 163 | " name "; |
| 164 | |
| 165 | std::string CONFIG_2 = |
| 166 | "\n" |
| 167 | " relation equal\n" |
| 168 | " }\n" |
| 169 | " }\n" |
| 170 | "}\n" |
| 171 | "trust-anchor\n" |
| 172 | "{\n" |
| 173 | " type file\n" |
| 174 | " file-name \"trust-anchor-2.cert\"\n" |
| 175 | "}\n"; |
| 176 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 177 | |
| 178 | const boost::filesystem::path CONFIG_PATH = |
| 179 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 180 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 181 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 182 | |
| 183 | validator.validate(*data1, |
| 184 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 185 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 186 | |
| 187 | validator.validate(*data2, |
| 188 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 189 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 190 | |
| 191 | validator.validate(*data3, |
| 192 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 193 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 196 | BOOST_AUTO_TEST_CASE(NameFilter3) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 197 | { |
| 198 | Name identity("/TestValidatorConfig/NameFilter3"); |
| 199 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 200 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 201 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 202 | |
| 203 | Name dataName1("/simple/isStrictPrefixOf"); |
| 204 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 205 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 206 | |
| 207 | Name dataName2("/simple"); |
| 208 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 209 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 210 | |
| 211 | Name dataName3("/simple/isStrictPrefixOf/anotherLevel"); |
| 212 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 213 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 214 | |
| 215 | std::string CONFIG_1 = |
| 216 | "rule\n" |
| 217 | "{\n" |
| 218 | " id \"Simple3 Rule\"\n" |
| 219 | " for data\n" |
| 220 | " filter" |
| 221 | " {\n" |
| 222 | " type name\n" |
| 223 | " name /simple/isStrictPrefixOf\n" |
| 224 | " relation is-strict-prefix-of\n" |
| 225 | " }\n" |
| 226 | " checker\n" |
| 227 | " {\n" |
| 228 | " type customized\n" |
| 229 | " sig-type rsa-sha256\n" |
| 230 | " key-locator\n" |
| 231 | " {\n" |
| 232 | " type name\n" |
| 233 | " name "; |
| 234 | |
| 235 | std::string CONFIG_2 = |
| 236 | "\n" |
| 237 | " relation equal\n" |
| 238 | " }\n" |
| 239 | " }\n" |
| 240 | "}\n" |
| 241 | "trust-anchor\n" |
| 242 | "{\n" |
| 243 | " type file\n" |
| 244 | " file-name \"trust-anchor-3.cert\"\n" |
| 245 | "}\n"; |
| 246 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 247 | |
| 248 | const boost::filesystem::path CONFIG_PATH = |
| 249 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 250 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 251 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 252 | |
| 253 | validator.validate(*data1, |
| 254 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 255 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 256 | |
| 257 | validator.validate(*data2, |
| 258 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 259 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 260 | |
| 261 | validator.validate(*data3, |
| 262 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 263 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 266 | BOOST_AUTO_TEST_CASE(NameFilter4) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 267 | { |
| 268 | Name identity("/TestValidatorConfig/NameFilter4"); |
| 269 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 270 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-4.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 271 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 272 | |
| 273 | Name dataName1("/simple/regex"); |
| 274 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 275 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 276 | |
| 277 | Name dataName2("/simple/regex-wrong"); |
| 278 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 279 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 280 | |
| 281 | Name dataName3("/simple/regex/correct"); |
| 282 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 283 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 284 | |
| 285 | std::string CONFIG_1 = |
| 286 | "rule\n" |
| 287 | "{\n" |
| 288 | " id \"Simple3 Rule\"\n" |
| 289 | " for data\n" |
| 290 | " filter" |
| 291 | " {\n" |
| 292 | " type name\n" |
| 293 | " regex ^<simple><regex>\n" |
| 294 | " }\n" |
| 295 | " checker\n" |
| 296 | " {\n" |
| 297 | " type customized\n" |
| 298 | " sig-type rsa-sha256\n" |
| 299 | " key-locator\n" |
| 300 | " {\n" |
| 301 | " type name\n" |
| 302 | " name "; |
| 303 | |
| 304 | std::string CONFIG_2 = |
| 305 | "\n" |
| 306 | " relation equal\n" |
| 307 | " }\n" |
| 308 | " }\n" |
| 309 | "}\n" |
| 310 | "trust-anchor\n" |
| 311 | "{\n" |
| 312 | " type file\n" |
| 313 | " file-name \"trust-anchor-4.cert\"\n" |
| 314 | "}\n"; |
| 315 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 316 | |
| 317 | const boost::filesystem::path CONFIG_PATH = |
| 318 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 319 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 320 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 321 | |
| 322 | validator.validate(*data1, |
| 323 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 324 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 325 | |
| 326 | validator.validate(*data2, |
| 327 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 328 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 329 | |
| 330 | validator.validate(*data3, |
| 331 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 332 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 335 | BOOST_AUTO_TEST_CASE(KeyLocatorNameChecker1) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 336 | { |
| 337 | Name identity("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 338 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 339 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity), "trust-anchor-5.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 340 | |
| 341 | Name dataName1 = identity; |
| 342 | dataName1.append("1"); |
| 343 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 344 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 345 | |
| 346 | Name dataName2 = identity; |
| 347 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 348 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 349 | |
| 350 | Name dataName3("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 351 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 352 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 353 | |
| 354 | const std::string CONFIG = |
| 355 | "rule\n" |
| 356 | "{\n" |
| 357 | " id \"Simple3 Rule\"\n" |
| 358 | " for data\n" |
| 359 | " checker\n" |
| 360 | " {\n" |
| 361 | " type customized\n" |
| 362 | " sig-type rsa-sha256\n" |
| 363 | " key-locator\n" |
| 364 | " {\n" |
| 365 | " type name\n" |
| 366 | " hyper-relation\n" |
| 367 | " {\n" |
| 368 | " k-regex ^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$\n" |
| 369 | " k-expand \\\\1\\\\2\n" |
| 370 | " h-relation is-strict-prefix-of\n" |
| 371 | " p-regex ^(<>*)$\n" |
| 372 | " p-expand \\\\1\n" |
| 373 | " }\n" |
| 374 | " }\n" |
| 375 | " }\n" |
| 376 | "}\n" |
| 377 | "trust-anchor\n" |
| 378 | "{\n" |
| 379 | " type file\n" |
| 380 | " file-name \"trust-anchor-5.cert\"\n" |
| 381 | "}\n"; |
| 382 | const boost::filesystem::path CONFIG_PATH = |
| 383 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 384 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 385 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 386 | |
| 387 | validator.validate(*data1, |
| 388 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 389 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 390 | |
| 391 | validator.validate(*data2, |
| 392 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 393 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 394 | |
| 395 | validator.validate(*data3, |
| 396 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 397 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(FixedSignerChecker) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 401 | { |
| 402 | Name identity("/TestValidatorConfig/FixedSignerChecker"); |
| 403 | |
| 404 | Name identity1 = identity; |
| 405 | identity1.append("1").appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 406 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-7.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 407 | |
| 408 | Name identity2 = identity; |
| 409 | identity2.append("2").appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 410 | addIdentity(identity2); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 411 | |
| 412 | Name dataName1 = identity; |
| 413 | dataName1.append("data").appendVersion(); |
| 414 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 415 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 416 | |
| 417 | Name dataName2 = identity; |
| 418 | dataName2.append("data").appendVersion(); |
| 419 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 420 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 421 | |
| 422 | Name interestName("/TestValidatorConfig/FixedSignerChecker/fakeSigInfo/fakeSigValue"); |
| 423 | shared_ptr<Interest> interest = make_shared<Interest>(interestName); |
| 424 | |
| 425 | const std::string CONFIG = |
| 426 | "rule\n" |
| 427 | "{\n" |
| 428 | " id \"FixedSignerChecker Data Rule\"\n" |
| 429 | " for data\n" |
| 430 | " filter" |
| 431 | " {\n" |
| 432 | " type name\n" |
| 433 | " name /TestValidatorConfig/FixedSignerChecker\n" |
| 434 | " relation is-strict-prefix-of\n" |
| 435 | " }\n" |
| 436 | " checker\n" |
| 437 | " {\n" |
| 438 | " type fixed-signer\n" |
| 439 | " sig-type rsa-sha256\n" |
| 440 | " signer\n" |
| 441 | " {\n" |
| 442 | " type file\n" |
| 443 | " file-name \"trust-anchor-7.cert\"\n" |
| 444 | " }\n" |
| 445 | " }\n" |
| 446 | "}\n" |
| 447 | "rule\n" |
| 448 | "{\n" |
| 449 | " id \"FixedSignerChecker Interest Rule\"\n" |
| 450 | " for interest\n" |
| 451 | " filter" |
| 452 | " {\n" |
| 453 | " type name\n" |
| 454 | " name /TestValidatorConfig/FixedSignerChecker\n" |
| 455 | " relation is-strict-prefix-of\n" |
| 456 | " }\n" |
| 457 | " checker\n" |
| 458 | " {\n" |
| 459 | " type fixed-signer\n" |
| 460 | " sig-type rsa-sha256\n" |
| 461 | " signer\n" |
| 462 | " {\n" |
| 463 | " type file\n" |
| 464 | " file-name \"trust-anchor-7.cert\"\n" |
| 465 | " }\n" |
| 466 | " }\n" |
| 467 | "}\n"; |
| 468 | const boost::filesystem::path CONFIG_PATH = |
| 469 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 470 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 471 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 472 | |
| 473 | validator.validate(*data1, |
| 474 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 475 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 476 | |
| 477 | validator.validate(*data2, |
| 478 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 479 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 480 | |
| 481 | validator.validate(*interest, |
| 482 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 483 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 486 | BOOST_AUTO_TEST_CASE(MultiCheckers) |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 487 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 488 | Name identity1("/TestValidatorConfig/MultiCheckers/"); |
| 489 | identity1.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 490 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-multi-1.cert")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 491 | |
| 492 | Name identity2("/TestValidatorConfig/"); |
| 493 | identity2.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 494 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-multi-2.cert")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 495 | |
| 496 | const std::string CONFIG = |
| 497 | "rule\n" |
| 498 | "{\n" |
| 499 | " id \"Simple Rule01\"\n" |
| 500 | " for data\n" |
| 501 | " filter" |
| 502 | " {\n" |
| 503 | " type name\n" |
| 504 | " name /\n" |
| 505 | " relation is-prefix-of\n" |
| 506 | " }\n" |
| 507 | " checker\n" // checker 1, signer should have prefix /TestValidatorConfig/MultiCheckers |
| 508 | " {\n" |
| 509 | " type customized\n" |
| 510 | " sig-type rsa-sha256\n" |
| 511 | " key-locator\n" |
| 512 | " {\n" |
| 513 | " type name\n" |
| 514 | " name /TestValidatorConfig/MultiCheckers/\n" |
| 515 | " relation is-prefix-of\n" |
| 516 | " }\n" |
| 517 | " }\n" |
| 518 | " checker\n" // checker 2, data should have same prefix of its signer |
| 519 | " {\n" |
| 520 | " type hierarchical\n" |
| 521 | " sig-type rsa-sha256\n" |
| 522 | " }\n" |
| 523 | " checker\n" // checker 3, the signer should be identity1 |
| 524 | " {\n" |
| 525 | " type fixed-signer\n" |
| 526 | " sig-type rsa-sha256\n" |
| 527 | " signer\n" |
| 528 | " {\n" |
| 529 | " type file\n" |
| 530 | " file-name \"trust-anchor-multi-1.cert\"\n" |
| 531 | " }\n" |
| 532 | " }\n" |
| 533 | "}\n"; |
| 534 | |
| 535 | const boost::filesystem::path CONFIG_PATH = |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 536 | (boost::filesystem::current_path() / std::string("unit-test-multicheckers.conf")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 537 | |
| 538 | ValidatorConfig validator; |
| 539 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
| 540 | conf::Checker& checker0 = *validator.m_dataRules.front()->m_checkers[0]; |
| 541 | conf::Checker& checker1 = *validator.m_dataRules.front()->m_checkers[1]; |
| 542 | conf::Checker& checker2 = *validator.m_dataRules.front()->m_checkers[2]; |
| 543 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 544 | auto data1 = makeData(Name(identity1).append("Test")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 545 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity1))); |
| 546 | BOOST_CHECK_EQUAL(checker0.check(*data1), 0); |
| 547 | BOOST_CHECK_EQUAL(checker1.check(*data1), 0); |
| 548 | BOOST_CHECK_EQUAL(checker2.check(*data1), 1); |
| 549 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 550 | auto data2 = makeData(Name(identity2).append("Data2")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 551 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(identity2))); |
| 552 | BOOST_CHECK_EQUAL(checker0.check(*data2), -1); |
| 553 | BOOST_CHECK_EQUAL(checker1.check(*data2), 0); |
| 554 | BOOST_CHECK_EQUAL(checker2.check(*data2), -1); |
| 555 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 556 | auto data3 = makeData(Name(identity2).append("Data3")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 557 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data3, security::signingByIdentity(identity1))); |
| 558 | BOOST_CHECK_EQUAL(checker0.check(*data3), 0); |
| 559 | BOOST_CHECK_EQUAL(checker1.check(*data3), -1); |
| 560 | BOOST_CHECK_EQUAL(checker2.check(*data3), 1); |
| 561 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 562 | auto data4 = makeData("/Data4"); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 563 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data4, security::signingByIdentity(identity2))); |
| 564 | BOOST_CHECK_EQUAL(checker0.check(*data4), -1); |
| 565 | BOOST_CHECK_EQUAL(checker1.check(*data4), -1); |
| 566 | BOOST_CHECK_EQUAL(checker2.check(*data4), -1); |
| 567 | |
| 568 | int count = 0; |
| 569 | validator.validate(*data1, |
| 570 | [&] (const shared_ptr<const Data>&) { |
| 571 | BOOST_CHECK(true); |
| 572 | count++; |
| 573 | }, |
| 574 | [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); }); |
| 575 | |
| 576 | validator.validate(*data2, |
| 577 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
| 578 | [&] (const shared_ptr<const Data>&, const std::string& str) { |
| 579 | BOOST_CHECK(true); |
| 580 | count++; |
| 581 | }); |
| 582 | |
| 583 | validator.validate(*data3, |
| 584 | [&] (const shared_ptr<const Data>&) { |
| 585 | BOOST_CHECK(true); |
| 586 | count++; |
| 587 | }, |
| 588 | [] (const shared_ptr<const Data>&, const std::string& str) { BOOST_CHECK(false); }); |
| 589 | |
| 590 | validator.validate(*data4, |
| 591 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
| 592 | [&] (const shared_ptr<const Data>&, const std::string& str) { |
| 593 | BOOST_CHECK(true); |
| 594 | count++; |
| 595 | }); |
| 596 | |
| 597 | BOOST_CHECK_EQUAL(count, 4); |
| 598 | } |
| 599 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 600 | BOOST_AUTO_TEST_CASE(Reset) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 601 | { |
| 602 | Name root("/TestValidatorConfig/Reload"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 603 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 604 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 605 | const std::string CONFIG = |
| 606 | "rule\n" |
| 607 | "{\n" |
| 608 | " id \"NRD Prefix Registration Command Rule\"\n" |
| 609 | " for interest\n" |
| 610 | " filter\n" |
| 611 | " {\n" |
| 612 | " type name\n" |
| 613 | " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n" |
| 614 | " }\n" |
| 615 | " checker\n" |
| 616 | " {\n" |
| 617 | " type customized\n" |
| 618 | " sig-type rsa-sha256\n" |
| 619 | " key-locator\n" |
| 620 | " {\n" |
| 621 | " type name\n" |
| 622 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n" |
| 623 | " }\n" |
| 624 | " }\n" |
| 625 | "}\n" |
| 626 | "rule\n" |
| 627 | "{\n" |
| 628 | " id \"Testbed Hierarchy Rule\"\n" |
| 629 | " for data\n" |
| 630 | " filter\n" |
| 631 | " {\n" |
| 632 | " type name\n" |
| 633 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n" |
| 634 | " }\n" |
| 635 | " checker\n" |
| 636 | " {\n" |
| 637 | " type hierarchical\n" |
| 638 | " sig-type rsa-sha256\n" |
| 639 | " }\n" |
| 640 | "}\n" |
| 641 | "trust-anchor\n" |
| 642 | "{\n" |
| 643 | " type file\n" |
| 644 | " file-name \"trust-anchor-8.cert\"\n" |
| 645 | "}\n"; |
| 646 | const boost::filesystem::path CONFIG_PATH = |
| 647 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 648 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 649 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
| 650 | BOOST_CHECK_EQUAL(validator.isEmpty(), false); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 651 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 652 | validator.reset(); |
| 653 | BOOST_CHECK(validator.isEmpty()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 656 | BOOST_AUTO_TEST_CASE(TrustAnchorWildcard) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 657 | { |
| 658 | Name identity("/TestValidatorConfig/Wildcard"); |
| 659 | identity.appendVersion(); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 660 | addIdentity(identity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 661 | |
| 662 | Name dataName1("/any/data"); |
| 663 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 664 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(identity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 665 | |
| 666 | std::string CONFIG = |
| 667 | "trust-anchor\n" |
| 668 | "{\n" |
| 669 | " type any\n" |
| 670 | "}\n"; |
| 671 | |
| 672 | const boost::filesystem::path CONFIG_PATH = |
| 673 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 674 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 675 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 676 | |
| 677 | validator.validate(*data1, |
| 678 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 679 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 682 | BOOST_AUTO_TEST_CASE(SignedInterestTest) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 683 | { |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 684 | Name certName = addIdentity("/TestValidatorConfig/SignedInterestTest"); |
| 685 | BOOST_REQUIRE(saveIdentityCertificate(certName, "trust-anchor-9.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 686 | |
| 687 | Name interestName("/TestValidatorConfig/SignedInterestTest"); |
| 688 | Name interestName1 = interestName; |
| 689 | interestName1.append("1"); |
| 690 | shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1); |
| 691 | Name interestName2 = interestName; |
| 692 | interestName2.append("2"); |
| 693 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
| 694 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 695 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByCertificate(certName))); |
| 696 | advanceClocks(time::milliseconds(10)); |
| 697 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByCertificate(certName))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 698 | |
| 699 | const std::string CONFIG = |
| 700 | "rule\n" |
| 701 | "{\n" |
| 702 | " id \"FixedSignerChecker Interest Rule\"\n" |
| 703 | " for interest\n" |
| 704 | " filter" |
| 705 | " {\n" |
| 706 | " type name\n" |
| 707 | " name /TestValidatorConfig/SignedInterestTest\n" |
| 708 | " relation is-strict-prefix-of\n" |
| 709 | " }\n" |
| 710 | " checker\n" |
| 711 | " {\n" |
| 712 | " type fixed-signer\n" |
| 713 | " sig-type rsa-sha256\n" |
| 714 | " signer\n" |
| 715 | " {\n" |
| 716 | " type file\n" |
| 717 | " file-name \"trust-anchor-9.cert\"\n" |
| 718 | " }\n" |
| 719 | " }\n" |
| 720 | "}\n"; |
| 721 | const boost::filesystem::path CONFIG_PATH = |
| 722 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 723 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 724 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 725 | |
| 726 | validator.validate(*interest1, |
| 727 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 728 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 729 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 730 | |
| 731 | validator.validate(*interest2, |
| 732 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 733 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 734 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 735 | |
| 736 | validator.validate(*interest1, |
| 737 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 738 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 739 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 742 | BOOST_AUTO_TEST_CASE(MaxKeyTest) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 743 | { |
| 744 | Name identity("/TestValidatorConfig/MaxKeyTest"); |
| 745 | |
| 746 | Name identity1 = identity; |
| 747 | identity1.append("Key1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 748 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 749 | |
| 750 | Name identity2 = identity; |
| 751 | identity2.append("Key2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 752 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 753 | |
| 754 | Name identity3 = identity; |
| 755 | identity3.append("Key3"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 756 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 757 | |
| 758 | |
| 759 | Name interestName("/TestValidatorConfig/MaxKeyTest"); |
| 760 | Name interestName1 = interestName; |
| 761 | interestName1.append("1"); |
| 762 | shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1); |
| 763 | Name interestName2 = interestName; |
| 764 | interestName2.append("2"); |
| 765 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
| 766 | Name interestName3 = interestName; |
| 767 | interestName3.append("3"); |
| 768 | shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3); |
| 769 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 770 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 771 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 772 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2))); |
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(*interest3, security::signingByIdentity(identity3))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 775 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 776 | |
| 777 | const std::string CONFIG = |
| 778 | "rule\n" |
| 779 | "{\n" |
| 780 | " id \"FixedSignerChecker Interest Rule\"\n" |
| 781 | " for interest\n" |
| 782 | " filter" |
| 783 | " {\n" |
| 784 | " type name\n" |
| 785 | " name /TestValidatorConfig/MaxKeyTest\n" |
| 786 | " relation is-strict-prefix-of\n" |
| 787 | " }\n" |
| 788 | " checker\n" |
| 789 | " {\n" |
| 790 | " type fixed-signer\n" |
| 791 | " sig-type rsa-sha256\n" |
| 792 | " signer\n" |
| 793 | " {\n" |
| 794 | " type file\n" |
| 795 | " file-name \"trust-anchor-10-1.cert\"\n" |
| 796 | " }\n" |
| 797 | " signer\n" |
| 798 | " {\n" |
| 799 | " type file\n" |
| 800 | " file-name \"trust-anchor-10-2.cert\"\n" |
| 801 | " }\n" |
| 802 | " signer\n" |
| 803 | " {\n" |
| 804 | " type file\n" |
| 805 | " file-name \"trust-anchor-10-3.cert\"\n" |
| 806 | " }\n" |
| 807 | " }\n" |
| 808 | "}\n"; |
| 809 | const boost::filesystem::path CONFIG_PATH = |
| 810 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 811 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 812 | ValidatorConfig validator(face, |
| 813 | ValidatorConfig::DEFAULT_CERTIFICATE_CACHE, |
| 814 | ValidatorConfig::DEFAULT_GRACE_INTERVAL, |
| 815 | 10, |
| 816 | 2, // Two keys can be tracked |
| 817 | time::seconds(1)); // TTL is set to 1 sec |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 818 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 819 | |
| 820 | validator.validate(*interest1, |
| 821 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 822 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 823 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 824 | |
| 825 | validator.validate(*interest2, |
| 826 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 827 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 828 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 829 | |
| 830 | validator.validate(*interest1, |
| 831 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 832 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 833 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 834 | |
| 835 | validator.validate(*interest3, |
| 836 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 837 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 838 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 839 | |
| 840 | // Should succeed because identity1's key has been cleaned up due to space limit. |
| 841 | validator.validate(*interest1, |
| 842 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 843 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 844 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 847 | BOOST_AUTO_TEST_CASE(MaxKeyTest2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 848 | { |
| 849 | Name identity("/TestValidatorConfig/MaxKeyTest"); |
| 850 | |
| 851 | Name identity1 = identity; |
| 852 | identity1.append("Key1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 853 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity1), "trust-anchor-10-1.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 854 | |
| 855 | Name identity2 = identity; |
| 856 | identity2.append("Key2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 857 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity2), "trust-anchor-10-2.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 858 | |
| 859 | Name identity3 = identity; |
| 860 | identity3.append("Key3"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 861 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity3), "trust-anchor-10-3.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 862 | |
| 863 | Name identity4 = identity; |
| 864 | identity4.append("Key4"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 865 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(identity4), "trust-anchor-10-4.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 866 | |
| 867 | |
| 868 | Name interestName("/TestValidatorConfig/MaxKeyTest"); |
| 869 | Name interestName1 = interestName; |
| 870 | interestName1.append("1"); |
| 871 | shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1); |
| 872 | Name interestName2 = interestName; |
| 873 | interestName2.append("2"); |
| 874 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
| 875 | Name interestName3 = interestName; |
| 876 | interestName3.append("3"); |
| 877 | shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3); |
| 878 | Name interestName4 = interestName; |
| 879 | interestName4.append("4"); |
| 880 | shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4); |
| 881 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 882 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(identity1))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 883 | advanceClocks(time::milliseconds(10)); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 884 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(identity2))); |
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(*interest3, security::signingByIdentity(identity3))); |
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(*interest4, security::signingByIdentity(identity4))); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 889 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 890 | |
| 891 | const std::string CONFIG = |
| 892 | "rule\n" |
| 893 | "{\n" |
| 894 | " id \"FixedSignerChecker Interest Rule\"\n" |
| 895 | " for interest\n" |
| 896 | " filter" |
| 897 | " {\n" |
| 898 | " type name\n" |
| 899 | " name /TestValidatorConfig/MaxKeyTest\n" |
| 900 | " relation is-strict-prefix-of\n" |
| 901 | " }\n" |
| 902 | " checker\n" |
| 903 | " {\n" |
| 904 | " type fixed-signer\n" |
| 905 | " sig-type rsa-sha256\n" |
| 906 | " signer\n" |
| 907 | " {\n" |
| 908 | " type file\n" |
| 909 | " file-name \"trust-anchor-10-1.cert\"\n" |
| 910 | " }\n" |
| 911 | " signer\n" |
| 912 | " {\n" |
| 913 | " type file\n" |
| 914 | " file-name \"trust-anchor-10-2.cert\"\n" |
| 915 | " }\n" |
| 916 | " signer\n" |
| 917 | " {\n" |
| 918 | " type file\n" |
| 919 | " file-name \"trust-anchor-10-3.cert\"\n" |
| 920 | " }\n" |
| 921 | " signer\n" |
| 922 | " {\n" |
| 923 | " type file\n" |
| 924 | " file-name \"trust-anchor-10-4.cert\"\n" |
| 925 | " }\n" |
| 926 | " }\n" |
| 927 | "}\n"; |
| 928 | const boost::filesystem::path CONFIG_PATH = |
| 929 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 930 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 931 | ValidatorConfig validator(face, |
| 932 | ValidatorConfig::DEFAULT_CERTIFICATE_CACHE, |
| 933 | ValidatorConfig::DEFAULT_GRACE_INTERVAL, |
| 934 | 10, |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 935 | 3, // Three keys can be tracked |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 936 | time::seconds(1)); // TTL is set to 1 sec |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 937 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 938 | |
| 939 | validator.validate(*interest1, |
| 940 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 941 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 942 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 943 | |
| 944 | validator.validate(*interest2, |
| 945 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 946 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 947 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 948 | |
| 949 | validator.validate(*interest3, |
| 950 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 951 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 952 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 953 | |
| 954 | validator.validate(*interest1, |
| 955 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 956 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 957 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 958 | |
| 959 | validator.validate(*interest2, |
| 960 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 961 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 962 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 963 | |
| 964 | validator.validate(*interest3, |
| 965 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 966 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 967 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 968 | advanceClocks(time::seconds(2)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 969 | |
| 970 | validator.validate(*interest4, |
| 971 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 972 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 973 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 974 | |
| 975 | // Should succeed because identity1 and identity2's key has been cleaned up due to ttl limit. |
| 976 | validator.validate(*interest1, |
| 977 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 978 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 979 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 980 | |
| 981 | validator.validate(*interest2, |
| 982 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 983 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 984 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 985 | |
| 986 | validator.validate(*interest3, |
| 987 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 988 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 989 | advanceClocks(time::milliseconds(10)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 992 | BOOST_AUTO_TEST_CASE(FixedSignerChecker2) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 993 | { |
| 994 | Name rsaIdentity("/TestValidatorConfig/FixedSignerChecker2/Rsa"); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 995 | addIdentity(rsaIdentity, RsaKeyParams()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 996 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 997 | Name ecIdentity("/TestValidatorConfig/FixedSignerChecker2/Ec"); |
| 998 | auto identity = addIdentity(ecIdentity, EcKeyParams()); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 999 | BOOST_REQUIRE(saveIdentityCertificate(identity, "trust-anchor-11.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1000 | |
| 1001 | Name dataName("/TestValidatorConfig/FixedSignerChecker2"); |
| 1002 | shared_ptr<Data> dataRsa = make_shared<Data>(dataName); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1003 | m_keyChain.sign(*dataRsa, security::signingByIdentity(rsaIdentity)); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1004 | shared_ptr<Data> dataEc = make_shared<Data>(dataName); |
| 1005 | m_keyChain.sign(*dataEc, security::signingByIdentity(ecIdentity)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1006 | |
| 1007 | shared_ptr<Interest> interestRsa = make_shared<Interest>(dataName); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1008 | m_keyChain.sign(*interestRsa, security::signingByIdentity(rsaIdentity)); |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1009 | shared_ptr<Interest> interestEc = make_shared<Interest>(dataName); |
| 1010 | m_keyChain.sign(*interestEc, security::signingByIdentity(ecIdentity)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1011 | |
| 1012 | const std::string CONFIG = |
| 1013 | "rule\n" |
| 1014 | "{\n" |
| 1015 | " id \"FixedSignerChecker Data Rule\"\n" |
| 1016 | " for data\n" |
| 1017 | " filter" |
| 1018 | " {\n" |
| 1019 | " type name\n" |
| 1020 | " name /TestValidatorConfig/FixedSignerChecker2\n" |
| 1021 | " relation equal\n" |
| 1022 | " }\n" |
| 1023 | " checker\n" |
| 1024 | " {\n" |
| 1025 | " type fixed-signer\n" |
| 1026 | " sig-type ecdsa-sha256\n" |
| 1027 | " signer\n" |
| 1028 | " {\n" |
| 1029 | " type file\n" |
| 1030 | " file-name \"trust-anchor-11.cert\"\n" |
| 1031 | " }\n" |
| 1032 | " }\n" |
| 1033 | "}\n" |
| 1034 | "rule\n" |
| 1035 | "{\n" |
| 1036 | " id \"FixedSignerChecker Interest Rule\"\n" |
| 1037 | " for interest\n" |
| 1038 | " filter" |
| 1039 | " {\n" |
| 1040 | " type name\n" |
| 1041 | " name /TestValidatorConfig/FixedSignerChecker2\n" |
| 1042 | " relation equal\n" |
| 1043 | " }\n" |
| 1044 | " checker\n" |
| 1045 | " {\n" |
| 1046 | " type fixed-signer\n" |
| 1047 | " sig-type ecdsa-sha256\n" |
| 1048 | " signer\n" |
| 1049 | " {\n" |
| 1050 | " type file\n" |
| 1051 | " file-name \"trust-anchor-11.cert\"\n" |
| 1052 | " }\n" |
| 1053 | " }\n" |
| 1054 | "}\n"; |
| 1055 | const boost::filesystem::path CONFIG_PATH = |
| 1056 | (boost::filesystem::current_path() / std::string("unit-test.conf")); |
| 1057 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1058 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1059 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1060 | validator.validate(*dataEc, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1061 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1062 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1063 | |
| 1064 | validator.validate(*dataRsa, |
| 1065 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1066 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1067 | |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 1068 | validator.validate(*interestEc, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1069 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1070 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1071 | |
| 1072 | validator.validate(*interestRsa, |
| 1073 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1074 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1078 | struct FacesFixture : public ValidatorConfigFixture |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1079 | { |
| 1080 | FacesFixture() |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1081 | : face1(io, m_keyChain, {true, true}) |
| 1082 | , face2(io, m_keyChain, {true, true}) |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1083 | , readInterestOffset1(0) |
| 1084 | , readDataOffset1(0) |
| 1085 | , readInterestOffset2(0) |
| 1086 | , readDataOffset2(0) |
| 1087 | { |
| 1088 | } |
| 1089 | |
| 1090 | bool |
| 1091 | passPacket() |
| 1092 | { |
| 1093 | bool hasPassed = false; |
| 1094 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1095 | checkFace(face1.sentInterests, readInterestOffset1, face2, hasPassed); |
| 1096 | checkFace(face1.sentData, readDataOffset1, face2, hasPassed); |
| 1097 | checkFace(face2.sentInterests, readInterestOffset2, face1, hasPassed); |
Zhiyi Zhang | 99832d4 | 2016-11-18 10:03:49 -0800 | [diff] [blame] | 1098 | checkFace(face2.sentData, readDataOffset2, face1, hasPassed); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1099 | |
| 1100 | return hasPassed; |
| 1101 | } |
| 1102 | |
| 1103 | template<typename Packet> |
| 1104 | void |
| 1105 | checkFace(std::vector<Packet>& receivedPackets, |
| 1106 | size_t& readPacketOffset, |
| 1107 | util::DummyClientFace& receiver, |
| 1108 | bool& hasPassed) |
| 1109 | { |
| 1110 | while (receivedPackets.size() > readPacketOffset) { |
| 1111 | receiver.receive(receivedPackets[readPacketOffset]); |
| 1112 | readPacketOffset++; |
| 1113 | hasPassed = true; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1117 | public: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1118 | util::DummyClientFace face1; |
| 1119 | util::DummyClientFace face2; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1120 | |
| 1121 | size_t readInterestOffset1; |
| 1122 | size_t readDataOffset1; |
| 1123 | size_t readInterestOffset2; |
| 1124 | size_t readDataOffset2; |
| 1125 | }; |
| 1126 | |
| 1127 | BOOST_FIXTURE_TEST_CASE(HierarchicalChecker, FacesFixture) |
| 1128 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1129 | std::vector<v1::CertificateSubjectDescription> subjectDescription; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1130 | |
| 1131 | Name root("/TestValidatorConfig"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1132 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-6.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1133 | |
| 1134 | |
| 1135 | Name sld("/TestValidatorConfig/HierarchicalChecker"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1136 | addIdentity(sld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1137 | advanceClocks(time::milliseconds(100)); |
| 1138 | Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1139 | shared_ptr<v1::IdentityCertificate> sldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1140 | m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 1141 | root, |
| 1142 | time::system_clock::now(), |
| 1143 | time::system_clock::now() + time::days(7300), |
| 1144 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1145 | m_keyChain.sign(*sldCert, security::signingByIdentity(root)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1146 | m_keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 1147 | |
| 1148 | Name nld("/TestValidatorConfig/HierarchicalChecker/NextLevel"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1149 | addIdentity(nld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1150 | advanceClocks(time::milliseconds(100)); |
| 1151 | Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1152 | shared_ptr<v1::IdentityCertificate> nldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1153 | m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 1154 | sld, |
| 1155 | time::system_clock::now(), |
| 1156 | time::system_clock::now() + time::days(7300), |
| 1157 | subjectDescription); |
Zhiyi Zhang | 433686c | 2016-11-18 10:18:30 -0800 | [diff] [blame] | 1158 | m_keyChain.sign(*nldCert, security::signingByIdentity(sld)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1159 | m_keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 1160 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1161 | face1.setInterestFilter(sldCert->getName().getPrefix(-1), |
| 1162 | [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1163 | RegisterPrefixSuccessCallback(), |
| 1164 | [] (const Name&, const std::string&) {}); |
| 1165 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1166 | face1.setInterestFilter(nldCert->getName().getPrefix(-1), |
| 1167 | [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1168 | RegisterPrefixSuccessCallback(), |
| 1169 | [] (const Name&, const std::string&) {}); |
| 1170 | |
| 1171 | Name dataName1 = nld; |
| 1172 | dataName1.append("data1"); |
| 1173 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1174 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1175 | |
| 1176 | Name dataName2("/ConfValidatorTest"); |
| 1177 | dataName2.append("data1"); |
| 1178 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1179 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1180 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1181 | const std::string CONFIG = |
| 1182 | "rule\n" |
| 1183 | "{\n" |
| 1184 | " id \"Simple3 Rule\"\n" |
| 1185 | " for data\n" |
| 1186 | " checker\n" |
| 1187 | " {\n" |
| 1188 | " type hierarchical\n" |
| 1189 | " sig-type rsa-sha256\n" |
| 1190 | " }\n" |
| 1191 | "}\n" |
| 1192 | "trust-anchor\n" |
| 1193 | "{\n" |
| 1194 | " type file\n" |
| 1195 | " file-name \"trust-anchor-6.cert\"\n" |
| 1196 | "}\n"; |
| 1197 | const boost::filesystem::path CONFIG_PATH = |
| 1198 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 1199 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1200 | auto validator = make_shared<ValidatorConfig>(&face2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1201 | validator->load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1202 | |
| 1203 | advanceClocks(time::milliseconds(2), 100); |
| 1204 | validator->validate(*data1, |
| 1205 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1206 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1207 | |
| 1208 | do { |
| 1209 | advanceClocks(time::milliseconds(2), 10); |
| 1210 | } while (passPacket()); |
| 1211 | |
| 1212 | validator->validate(*data2, |
| 1213 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1214 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1215 | |
| 1216 | do { |
| 1217 | advanceClocks(time::milliseconds(2), 10); |
| 1218 | } while (passPacket()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | BOOST_FIXTURE_TEST_CASE(Nrd, FacesFixture) |
| 1222 | { |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 1223 | advanceClocks(time::nanoseconds(1)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1224 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1225 | std::vector<v1::CertificateSubjectDescription> subjectDescription; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1226 | |
| 1227 | Name root("/TestValidatorConfig"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1228 | BOOST_REQUIRE(saveIdentityCertificate(addIdentity(root), "trust-anchor-8.cert")); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1229 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1230 | Name sld("/TestValidatorConfig/Nrd-1"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1231 | addIdentity(sld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1232 | advanceClocks(time::milliseconds(100)); |
| 1233 | Name sldKeyName = m_keyChain.generateRsaKeyPairAsDefault(sld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1234 | shared_ptr<v1::IdentityCertificate> sldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1235 | m_keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 1236 | root, |
| 1237 | time::system_clock::now(), |
| 1238 | time::system_clock::now() + time::days(7300), |
| 1239 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1240 | m_keyChain.sign(*sldCert, security::signingByIdentity(root)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1241 | m_keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 1242 | |
| 1243 | Name nld("/TestValidatorConfig/Nrd-1/Nrd-2"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1244 | addIdentity(nld); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1245 | advanceClocks(time::milliseconds(100)); |
| 1246 | Name nldKeyName = m_keyChain.generateRsaKeyPairAsDefault(nld, true); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1247 | shared_ptr<v1::IdentityCertificate> nldCert = |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1248 | m_keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 1249 | sld, |
| 1250 | time::system_clock::now(), |
| 1251 | time::system_clock::now() + time::days(7300), |
| 1252 | subjectDescription); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1253 | m_keyChain.sign(*nldCert, security::signingByIdentity(sld)); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1254 | m_keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 1255 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1256 | face1.setInterestFilter(sldCert->getName().getPrefix(-1), |
| 1257 | [&] (const InterestFilter&, const Interest&) { face1.put(*sldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1258 | RegisterPrefixSuccessCallback(), |
| 1259 | [] (const Name&, const std::string&) {}); |
| 1260 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1261 | face1.setInterestFilter(nldCert->getName().getPrefix(-1), |
| 1262 | [&] (const InterestFilter&, const Interest&) { face1.put(*nldCert); }, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1263 | RegisterPrefixSuccessCallback(), |
| 1264 | [] (const Name&, const std::string&) {}); |
| 1265 | |
| 1266 | advanceClocks(time::milliseconds(10)); |
| 1267 | Name interestName1("/localhost/nrd/register/option"); |
| 1268 | shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1269 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest1, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1270 | |
| 1271 | advanceClocks(time::milliseconds(10)); |
| 1272 | Name interestName2("/localhost/nrd/non-register"); |
| 1273 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1274 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest2, security::signingByIdentity(nld))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1275 | |
| 1276 | advanceClocks(time::milliseconds(10)); |
| 1277 | Name interestName3("/localhost/nrd/register/option"); |
| 1278 | shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1279 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest3, security::signingByIdentity(root))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1280 | |
| 1281 | advanceClocks(time::milliseconds(10)); |
| 1282 | Name interestName4("/localhost/nrd/register/option/timestamp/nonce/fakeSigInfo/fakeSigValue"); |
| 1283 | shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4); |
| 1284 | |
| 1285 | const std::string CONFIG = |
| 1286 | "rule\n" |
| 1287 | "{\n" |
| 1288 | " id \"NRD Prefix Registration Command Rule\"\n" |
| 1289 | " for interest\n" |
| 1290 | " filter\n" |
| 1291 | " {\n" |
| 1292 | " type name\n" |
| 1293 | " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>$\n" |
| 1294 | " }\n" |
| 1295 | " checker\n" |
| 1296 | " {\n" |
| 1297 | " type customized\n" |
| 1298 | " sig-type rsa-sha256\n" |
| 1299 | " key-locator\n" |
| 1300 | " {\n" |
| 1301 | " type name\n" |
| 1302 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n" |
| 1303 | " }\n" |
| 1304 | " }\n" |
| 1305 | "}\n" |
| 1306 | "rule\n" |
| 1307 | "{\n" |
| 1308 | " id \"Testbed Hierarchy Rule\"\n" |
| 1309 | " for data\n" |
| 1310 | " filter\n" |
| 1311 | " {\n" |
| 1312 | " type name\n" |
| 1313 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n" |
| 1314 | " }\n" |
| 1315 | " checker\n" |
| 1316 | " {\n" |
| 1317 | " type hierarchical\n" |
| 1318 | " sig-type rsa-sha256\n" |
| 1319 | " }\n" |
| 1320 | "}\n" |
| 1321 | "trust-anchor\n" |
| 1322 | "{\n" |
| 1323 | " type file\n" |
| 1324 | " file-name \"trust-anchor-8.cert\"\n" |
| 1325 | "}\n"; |
| 1326 | const boost::filesystem::path CONFIG_PATH = |
| 1327 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 1328 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 1329 | auto validator = make_shared<ValidatorConfig>(&face2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1330 | validator->load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1331 | |
| 1332 | advanceClocks(time::milliseconds(2), 100); |
| 1333 | |
| 1334 | // should succeed |
| 1335 | validator->validate(*interest1, |
| 1336 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1337 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1338 | |
| 1339 | do { |
| 1340 | advanceClocks(time::milliseconds(2), 10); |
| 1341 | } while (passPacket()); |
| 1342 | |
| 1343 | // should fail |
| 1344 | validator->validate(*interest2, |
| 1345 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1346 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1347 | |
| 1348 | do { |
| 1349 | advanceClocks(time::milliseconds(2), 10); |
| 1350 | } while (passPacket()); |
| 1351 | |
| 1352 | // should succeed |
| 1353 | validator->validate(*interest3, |
| 1354 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1355 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1356 | |
| 1357 | do { |
| 1358 | advanceClocks(time::milliseconds(2), 10); |
| 1359 | } while (passPacket()); |
| 1360 | |
| 1361 | // should fail |
| 1362 | validator->validate(*interest4, |
| 1363 | [] (const shared_ptr<const Interest>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1364 | [] (const shared_ptr<const Interest>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1365 | |
| 1366 | do { |
| 1367 | advanceClocks(time::milliseconds(2), 10); |
| 1368 | } while (passPacket()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1371 | struct DirTestFixture : public ValidatorConfigFixture |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1372 | { |
| 1373 | DirTestFixture() |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1374 | : validator(&face, ValidatorConfig::DEFAULT_CERTIFICATE_CACHE, |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1375 | ValidatorConfig::DEFAULT_GRACE_INTERVAL, 0) |
| 1376 | { |
| 1377 | certDirPath = (boost::filesystem::current_path() / std::string("test-cert-dir")); |
| 1378 | boost::filesystem::create_directory(certDirPath); |
| 1379 | |
| 1380 | firstCertPath = (boost::filesystem::current_path() / |
| 1381 | std::string("test-cert-dir") / |
| 1382 | std::string("trust-anchor-1.cert")); |
| 1383 | |
| 1384 | secondCertPath = (boost::filesystem::current_path() / |
| 1385 | std::string("test-cert-dir") / |
| 1386 | std::string("trust-anchor-2.cert")); |
| 1387 | |
| 1388 | firstIdentity = Name("/TestValidatorConfig/Dir/First"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1389 | addIdentity(firstIdentity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1390 | Name firstCertName = m_keyChain.getDefaultCertificateNameForIdentity(firstIdentity); |
| 1391 | firstCert = m_keyChain.getCertificate(firstCertName); |
| 1392 | io::save(*firstCert, firstCertPath.string()); |
| 1393 | |
| 1394 | secondIdentity = Name("/TestValidatorConfig/Dir/Second"); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1395 | addIdentity(secondIdentity); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1396 | Name secondCertName = m_keyChain.getDefaultCertificateNameForIdentity(secondIdentity); |
| 1397 | secondCert = m_keyChain.getCertificate(secondCertName); |
| 1398 | } |
| 1399 | |
| 1400 | ~DirTestFixture() |
| 1401 | { |
| 1402 | boost::filesystem::remove_all(certDirPath); |
| 1403 | } |
| 1404 | |
| 1405 | public: |
| 1406 | boost::filesystem::path certDirPath; |
| 1407 | boost::filesystem::path firstCertPath; |
| 1408 | boost::filesystem::path secondCertPath; |
| 1409 | |
| 1410 | Name firstIdentity; |
| 1411 | Name secondIdentity; |
| 1412 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 1413 | shared_ptr<v1::IdentityCertificate> firstCert; |
| 1414 | shared_ptr<v1::IdentityCertificate> secondCert; |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1415 | |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1416 | ValidatorConfig validator; |
| 1417 | }; |
| 1418 | |
| 1419 | BOOST_FIXTURE_TEST_CASE(TrustAnchorDir, DirTestFixture) |
| 1420 | { |
| 1421 | advanceClocks(time::milliseconds(10)); |
| 1422 | |
| 1423 | Name dataName1("/any/data/1"); |
| 1424 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1425 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data1, security::signingByIdentity(firstIdentity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1426 | |
| 1427 | Name dataName2("/any/data/2"); |
| 1428 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1429 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*data2, security::signingByIdentity(secondIdentity))); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1430 | |
| 1431 | std::string CONFIG = |
| 1432 | "rule\n" |
| 1433 | "{\n" |
| 1434 | " id \"Any Rule\"\n" |
| 1435 | " for data\n" |
| 1436 | " filter\n" |
| 1437 | " {\n" |
| 1438 | " type name\n" |
| 1439 | " regex ^<>*$\n" |
| 1440 | " }\n" |
| 1441 | " checker\n" |
| 1442 | " {\n" |
| 1443 | " type customized\n" |
| 1444 | " sig-type rsa-sha256\n" |
| 1445 | " key-locator\n" |
| 1446 | " {\n" |
| 1447 | " type name\n" |
| 1448 | " regex ^<>*$\n" |
| 1449 | " }\n" |
| 1450 | " }\n" |
| 1451 | "}\n" |
| 1452 | "trust-anchor\n" |
| 1453 | "{\n" |
| 1454 | " type dir\n" |
| 1455 | " dir test-cert-dir\n" |
| 1456 | " refresh 1s\n" |
| 1457 | "}\n"; |
| 1458 | |
| 1459 | const boost::filesystem::path CONFIG_PATH = |
| 1460 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 1461 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 1462 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1463 | |
| 1464 | advanceClocks(time::milliseconds(10), 20); |
| 1465 | validator.validate(*data1, |
| 1466 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1467 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1468 | advanceClocks(time::milliseconds(10), 20); |
| 1469 | |
| 1470 | validator.validate(*data2, |
| 1471 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1472 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(true); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1473 | advanceClocks(time::milliseconds(10), 20); |
| 1474 | |
| 1475 | io::save(*secondCert, secondCertPath.string()); |
| 1476 | advanceClocks(time::milliseconds(10), 200); |
| 1477 | |
| 1478 | validator.validate(*data1, |
| 1479 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1480 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1481 | advanceClocks(time::milliseconds(10), 20); |
| 1482 | |
| 1483 | validator.validate(*data2, |
| 1484 | [] (const shared_ptr<const Data>&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1485 | [] (const shared_ptr<const Data>&, const std::string&) { BOOST_CHECK(false); }); |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1486 | advanceClocks(time::milliseconds(10), 20); |
| 1487 | } |
| 1488 | |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 1489 | class DirectCertFetchFixture : public IdentityManagementV1TimeFixture |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 1490 | { |
| 1491 | public: |
| 1492 | DirectCertFetchFixture() |
| 1493 | : clientFace(io, m_keyChain, {true, true}) |
| 1494 | , validationResult(boost::logic::indeterminate) |
| 1495 | { |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 1496 | auto certName = addIdentity(ca); |
| 1497 | saveIdentityCertificate(certName, "trust-anchor-1.cert"); |
| 1498 | addSubCertificate(user, ca); |
Zhiyi Zhang | 48becde | 2017-01-05 16:41:38 -0800 | [diff] [blame] | 1499 | |
| 1500 | userCertName = m_keyChain.getDefaultCertificateNameForIdentity(user); |
| 1501 | userCert = m_keyChain.getCertificate(userCertName); |
| 1502 | } |
| 1503 | |
| 1504 | protected: |
| 1505 | void |
| 1506 | runTest(const std::function<void(const Interest&, const Interest&)> respond) |
| 1507 | { |
| 1508 | optional<Interest> directInterest; |
| 1509 | optional<Interest> infrastructureInterest; |
| 1510 | clientFace.onSendInterest.connect([&] (const Interest& interest) { |
| 1511 | const Name& interestName = interest.getName(); |
| 1512 | if (interestName == userCert->getName().getPrefix(-1)) { |
| 1513 | auto nextHopFaceIdTag = interest.getTag<lp::NextHopFaceIdTag>(); |
| 1514 | if (nextHopFaceIdTag != nullptr) { |
| 1515 | BOOST_CHECK(!directInterest); |
| 1516 | directInterest = interest; |
| 1517 | } |
| 1518 | else { |
| 1519 | BOOST_CHECK(!infrastructureInterest); |
| 1520 | infrastructureInterest = interest; |
| 1521 | } |
| 1522 | if (static_cast<bool>(directInterest) && static_cast<bool>(infrastructureInterest)) { |
| 1523 | io.post([directInterest, infrastructureInterest, respond] { |
| 1524 | respond(directInterest.value(), infrastructureInterest.value()); |
| 1525 | }); |
| 1526 | directInterest = nullopt; |
| 1527 | infrastructureInterest = nullopt; |
| 1528 | } |
| 1529 | } |
| 1530 | }); |
| 1531 | |
| 1532 | const boost::filesystem::path CONFIG_PATH = |
| 1533 | (boost::filesystem::current_path() / std::string("unit-test-direct.conf")); |
| 1534 | ValidatorConfig validator(&clientFace); |
| 1535 | validator.load(CONFIG, CONFIG_PATH.c_str()); |
| 1536 | validator.setDirectCertFetchEnabled(true); |
| 1537 | |
| 1538 | shared_ptr<Interest> interest = make_shared<Interest>(interestName); |
| 1539 | interest->setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 1540 | BOOST_CHECK_NO_THROW(m_keyChain.sign(*interest, security::signingByIdentity(user))); |
| 1541 | |
| 1542 | validator.validate(*interest, |
| 1543 | [&] (const shared_ptr<const Interest>&) { |
| 1544 | BOOST_CHECK(boost::logic::indeterminate(validationResult)); |
| 1545 | validationResult = true; |
| 1546 | }, |
| 1547 | [&] (const shared_ptr<const Interest>&, const std::string& s) { |
| 1548 | BOOST_CHECK(boost::logic::indeterminate(validationResult)); |
| 1549 | validationResult = false; |
| 1550 | }); |
| 1551 | |
| 1552 | advanceClocks(time::milliseconds(200), 60); |
| 1553 | |
| 1554 | BOOST_CHECK(!boost::logic::indeterminate(validationResult)); |
| 1555 | } |
| 1556 | |
| 1557 | public: |
| 1558 | util::DummyClientFace clientFace; |
| 1559 | const Name ca = Name("/DirectCertFetch"); |
| 1560 | const Name user = Name("/DirectCertFetch/user"); |
| 1561 | const Name interestName = Name("/DirectCertFetch/user/tag-interest"); |
| 1562 | const std::string CONFIG = R"_TEXT_( |
| 1563 | rule |
| 1564 | { |
| 1565 | id "RuleForInterest" |
| 1566 | for interest |
| 1567 | checker |
| 1568 | { |
| 1569 | type hierarchical |
| 1570 | sig-type rsa-sha256 |
| 1571 | } |
| 1572 | } |
| 1573 | rule |
| 1574 | { |
| 1575 | id "RuleForData" |
| 1576 | for data |
| 1577 | filter |
| 1578 | { |
| 1579 | type name |
| 1580 | regex ^<>*$ |
| 1581 | } |
| 1582 | checker |
| 1583 | { |
| 1584 | type customized |
| 1585 | sig-type rsa-sha256 |
| 1586 | key-locator |
| 1587 | { |
| 1588 | type name |
| 1589 | regex ^<>*$ |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | trust-anchor |
| 1594 | { |
| 1595 | type file |
| 1596 | file-name "trust-anchor-1.cert" |
| 1597 | } |
| 1598 | )_TEXT_"; |
| 1599 | boost::logic::tribool validationResult; |
| 1600 | Name userCertName; |
| 1601 | shared_ptr<v1::IdentityCertificate> userCert; |
| 1602 | }; |
| 1603 | |
| 1604 | BOOST_FIXTURE_TEST_SUITE(DirectCertFetch, DirectCertFetchFixture) |
| 1605 | |
| 1606 | BOOST_AUTO_TEST_CASE(CertFetchSuccess) |
| 1607 | { |
| 1608 | runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) { |
| 1609 | this->clientFace.receive(*userCert); |
| 1610 | }); |
| 1611 | |
| 1612 | BOOST_CHECK_EQUAL(validationResult, true); |
| 1613 | } |
| 1614 | |
| 1615 | BOOST_AUTO_TEST_CASE(CertFetchTimeout) |
| 1616 | { |
| 1617 | // In this test case, certificate request would time out |
| 1618 | runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) { }); |
| 1619 | |
| 1620 | BOOST_CHECK_EQUAL(validationResult, false); |
| 1621 | } |
| 1622 | |
| 1623 | BOOST_AUTO_TEST_CASE(CertFetchNack) |
| 1624 | { |
| 1625 | runTest([this] (const Interest& directInterest, const Interest& infrastructureInterest) { |
| 1626 | lp::Nack nackInfrastructureInterest(infrastructureInterest); |
| 1627 | nackInfrastructureInterest.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 1628 | this->clientFace.receive(nackInfrastructureInterest); |
| 1629 | }); |
| 1630 | |
| 1631 | BOOST_CHECK_EQUAL(validationResult, false); |
| 1632 | } |
| 1633 | |
| 1634 | BOOST_AUTO_TEST_SUITE_END() // DirectCertFetch |
Junxiao Shi | d5827ce | 2016-07-14 20:49:37 +0000 | [diff] [blame] | 1635 | BOOST_AUTO_TEST_SUITE_END() // TestValidatorConfig |
| 1636 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1637 | |
| 1638 | } // namespace tests |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 1639 | } // namespace security |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 1640 | } // namespace ndn |