Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 21 | * |
| 22 | **/ |
| 23 | |
| 24 | #include "validator.hpp" |
| 25 | #include <ndn-cxx/util/scheduler.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
| 27 | #include <ndn-cxx/security/certificate-cache-ttl.hpp> |
| 28 | #include "boost-test.hpp" |
| 29 | |
| 30 | namespace nlsr { |
| 31 | |
| 32 | namespace test { |
| 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(TestValidator) |
| 35 | |
| 36 | struct ValidatorFixture |
| 37 | { |
| 38 | ValidatorFixture() |
| 39 | : m_face2(m_face.getIoService()) |
| 40 | , m_scheduler(m_face.getIoService()) |
| 41 | , m_certificateCache(new ndn::CertificateCacheTtl(m_face.getIoService())) |
| 42 | , m_validator(m_face2, ndn::Name("/ndn/broadcast"), m_certificateCache) |
| 43 | , m_identity("/TestValidator/NLSR") |
| 44 | { |
| 45 | ndn::Name keyPrefix("/ndn/broadcast/KEYS"); |
| 46 | m_face.setInterestFilter(keyPrefix, |
| 47 | ndn::bind(&ValidatorFixture::onKeyInterest, this, _1, _2), |
| 48 | ndn::bind(&ValidatorFixture::onKeyPrefixRegSuccess, this, _1), |
| 49 | ndn::bind(&ValidatorFixture::registrationFailed, this, _1, _2)); |
| 50 | |
| 51 | m_keyChain.createIdentity(m_identity); |
| 52 | ndn::Name certName = m_keyChain.getDefaultCertificateNameForIdentity(m_identity); |
| 53 | m_cert = m_keyChain.getCertificate(certName); |
| 54 | ndn::io::save(*m_cert, "trust-anchor.cert"); |
| 55 | |
| 56 | const std::string CONFIG = |
| 57 | "rule\n" |
| 58 | "{\n" |
| 59 | " id \"NSLR Hello Rule\"\n" |
| 60 | " for data\n" |
| 61 | " filter\n" |
| 62 | " {\n" |
| 63 | " type name\n" |
| 64 | " regex ^[^<NLSR><INFO>]*<NLSR><INFO><><>$\n" |
| 65 | " }\n" |
| 66 | " checker\n" |
| 67 | " {\n" |
| 68 | " type customized\n" |
| 69 | " sig-type rsa-sha256\n" |
| 70 | " key-locator\n" |
| 71 | " {\n" |
| 72 | " type name\n" |
| 73 | " hyper-relation\n" |
| 74 | " {\n" |
| 75 | " k-regex ^([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><ID-CERT>$\n" |
| 76 | " k-expand \\\\1\n" |
| 77 | " h-relation equal\n" |
| 78 | " p-regex ^([^<NLSR><INFO>]*)<NLSR><INFO><><>$\n" |
| 79 | " p-expand \\\\1\n" |
| 80 | " }\n" |
| 81 | " }\n" |
| 82 | " }\n" |
| 83 | "}\n" |
| 84 | "rule\n" |
| 85 | "{\n" |
| 86 | " id \"Single Rule\"\n" |
| 87 | " for data\n" |
| 88 | " filter\n" |
| 89 | " {\n" |
| 90 | " type name\n" |
| 91 | " regex ^<TestValidator><NLSR><KEY><ksk-.*><><>$\n" |
| 92 | " }\n" |
| 93 | " checker\n" |
| 94 | " {\n" |
| 95 | " type fixed-signer\n" |
| 96 | " sig-type rsa-sha256\n" |
| 97 | " signer\n" |
| 98 | " {\n" |
| 99 | " type file\n" |
| 100 | " file-name \"trust-anchor.cert\"\n" |
| 101 | " }\n" |
| 102 | " }\n" |
| 103 | "}\n"; |
| 104 | |
| 105 | const boost::filesystem::path CONFIG_PATH = |
| 106 | (boost::filesystem::current_path() / std::string("unit-test.conf")); |
| 107 | |
| 108 | m_validator.load(CONFIG, CONFIG_PATH.native()); |
| 109 | } |
| 110 | |
| 111 | ~ValidatorFixture() |
| 112 | { |
| 113 | m_keyChain.deleteIdentity(m_identity); |
| 114 | |
| 115 | const boost::filesystem::path CERT_PATH = |
| 116 | (boost::filesystem::current_path() / std::string("trust-anchor.cert")); |
| 117 | boost::filesystem::remove(CERT_PATH); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 122 | { |
| 123 | const ndn::Name& interestName = interest.getName(); |
| 124 | |
| 125 | ndn::Name certName = interestName.getSubName(name.size()); |
| 126 | |
| 127 | if (certName[-2].toUri() == "ID-CERT") |
| 128 | { |
| 129 | certName = certName.getPrefix(-1); |
| 130 | } |
| 131 | else if (certName[-1].toUri() != "ID-CERT") |
| 132 | return; //Wrong key interest. |
| 133 | |
| 134 | if (certName != m_cert->getName().getPrefix(-1)) |
| 135 | return; //No such a cert |
| 136 | |
| 137 | ndn::Data data(interestName); |
| 138 | data.setContent(m_cert->wireEncode()); |
| 139 | m_keyChain.signWithSha256(data); |
| 140 | |
| 141 | m_face.put(data); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | onKeyPrefixRegSuccess(const ndn::Name& name) |
| 146 | { |
| 147 | BOOST_REQUIRE(true); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | registrationFailed(const ndn::Name& name, const std::string& msg) |
| 152 | { |
| 153 | std::cerr << "Failure Info: " << msg << std::endl; |
| 154 | BOOST_REQUIRE(false); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | onValidated(const ndn::shared_ptr<const ndn::Data>& data) |
| 159 | { |
| 160 | BOOST_CHECK(true); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | onValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, |
| 165 | const std::string& failureInfo) |
| 166 | { |
| 167 | std::cerr << "Failure Info: " << failureInfo << std::endl; |
| 168 | BOOST_CHECK(false); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | validate(const ndn::shared_ptr<const ndn::Data>& data) |
| 173 | { |
| 174 | m_validator.validate(*data, |
| 175 | bind(&ValidatorFixture::onValidated, this, _1), |
| 176 | bind(&ValidatorFixture::onValidationFailed, this, _1, _2)); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | terminate() |
| 181 | { |
| 182 | m_face.getIoService().stop(); |
| 183 | } |
| 184 | |
| 185 | protected: |
| 186 | ndn::Face m_face; |
| 187 | ndn::Face m_face2; |
| 188 | ndn::Scheduler m_scheduler; |
| 189 | ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache; |
| 190 | nlsr::Validator m_validator; |
| 191 | |
| 192 | ndn::KeyChain m_keyChain; |
| 193 | ndn::Name m_identity; |
| 194 | ndn::shared_ptr<ndn::IdentityCertificate> m_cert; |
| 195 | }; |
| 196 | |
| 197 | BOOST_FIXTURE_TEST_CASE(InfoCertFetch, ValidatorFixture) |
| 198 | { |
| 199 | ndn::Name dataName = m_identity; |
| 200 | dataName.append("INFO").append("neighbor").append("version"); |
| 201 | ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(dataName); |
| 202 | m_keyChain.signByIdentity(*data, m_identity); |
| 203 | |
| 204 | m_scheduler.scheduleEvent(ndn::time::milliseconds(200), |
| 205 | ndn::bind(&ValidatorFixture::validate, this, data)); |
| 206 | m_scheduler.scheduleEvent(ndn::time::milliseconds(1000), |
| 207 | ndn::bind(&ValidatorFixture::terminate, this)); |
| 208 | BOOST_REQUIRE_NO_THROW(m_face.processEvents()); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_SUITE_END() |
| 212 | |
| 213 | } // namespace test |
| 214 | } // namespace nlsr |