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