Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, The University of Memphis, |
| 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "test-common.hpp" |
| 23 | #include "nlsr.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/interest.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
| 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 28 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 29 | #include <ndn-cxx/security/signing-info.hpp> |
| 30 | |
| 31 | #include <boost/filesystem.hpp> |
| 32 | #include <boost/property_tree/ptree.hpp> |
| 33 | #include <boost/property_tree/info_parser.hpp> |
| 34 | |
| 35 | using namespace ndn; |
| 36 | |
| 37 | namespace nlsr { |
| 38 | namespace test { |
| 39 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 40 | class LsaRuleFixture : public nlsr::test::UnitTestTimeFixture |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | LsaRuleFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 44 | : face(m_ioService, m_keyChain, {true, true}) |
| 45 | , rootIdName("/ndn") |
| 46 | , siteIdentityName("/ndn/edu/test-site") |
| 47 | , opIdentityName("/ndn/edu/test-site/%C1.Operator/op1") |
| 48 | , routerIdName("/ndn/edu/test-site/%C1.Router/router1") |
| 49 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 50 | , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert")) |
| 51 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 52 | rootId = addIdentity(rootIdName); |
| 53 | siteIdentity = addSubCertificate(siteIdentityName, rootId); |
| 54 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
| 55 | routerId = addSubCertificate(routerIdName, opIdentity); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 56 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 57 | saveCertificate(rootId, ROOT_CERT_PATH.string()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 58 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 59 | auto load = [this] (const ndn::security::Identity& id) { |
| 60 | nlsr.loadCertToPublish(id.getDefaultKey().getDefaultCertificate()); |
| 61 | }; |
| 62 | load(rootId); |
| 63 | load(siteIdentity); |
| 64 | load(opIdentity); |
| 65 | load(routerId); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 66 | |
| 67 | // Loading the security section's validator part into the validator |
| 68 | // See conf file processor for more details |
| 69 | std::ifstream inputFile; |
| 70 | inputFile.open(std::string("nlsr.conf")); |
| 71 | |
| 72 | BOOST_REQUIRE(inputFile.is_open()); |
| 73 | |
| 74 | boost::property_tree::ptree pt; |
| 75 | |
| 76 | boost::property_tree::read_info(inputFile, pt); |
| 77 | |
| 78 | //Loads section and file name |
| 79 | for (auto tn = pt.begin(); tn != pt.end(); ++tn) { |
| 80 | if (tn->first == "security") { |
| 81 | auto it = tn->second.begin(); |
| 82 | nlsr.loadValidator(it->second, std::string("nlsr.conf")); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | inputFile.close(); |
| 87 | |
| 88 | // Set the network so the LSA prefix is constructed |
| 89 | // Set all so that buildRouterPrefix is set |
| 90 | nlsr.getConfParameter().setNetwork("/ndn"); |
| 91 | nlsr.getConfParameter().setSiteName("/edu/test-site"); |
| 92 | nlsr.getConfParameter().setRouterName("/%C1.Router/router1"); |
| 93 | |
| 94 | // Initialize NLSR to initialize the keyChain |
| 95 | nlsr.initialize(); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 96 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 97 | this->advanceClocks(ndn::time::milliseconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 98 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 99 | face.sentInterests.clear(); |
| 100 | } |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 101 | |
| 102 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 103 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 104 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 105 | ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName; |
| 106 | ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 107 | |
| 108 | Nlsr nlsr; |
| 109 | |
| 110 | const boost::filesystem::path ROOT_CERT_PATH; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 111 | |
| 112 | //std::function<void(const ndn::Interest& interest)> processInterest; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture) |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(ValidateCorrectLSA) |
| 118 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 119 | ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix(); |
| 120 | lsaDataName.append(nlsr.getConfParameter().getSiteName()); |
| 121 | lsaDataName.append(nlsr.getConfParameter().getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 122 | |
| 123 | // Append LSA type |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 124 | lsaDataName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 125 | |
| 126 | // This would be the sequence number of its own NameLsa |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 127 | lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 128 | |
| 129 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 130 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 131 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 132 | ndn::Data data(lsaDataName); |
| 133 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 134 | |
| 135 | // Sign data with NLSR's key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 136 | nlsr.getKeyChain().sign(data, nlsr.getSigningInfo()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 137 | |
| 138 | // Make NLSR validate data signed by its own key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 139 | nlsr.getValidator().validate(data, |
| 140 | [] (const Data&) { BOOST_CHECK(true); }, |
| 141 | [] (const Data&, const ndn::security::v2::ValidationError&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 142 | BOOST_CHECK(false); |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA) |
| 147 | { |
| 148 | // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 149 | ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix().getSubName(1); |
| 150 | lsaDataName.append(nlsr.getConfParameter().getSiteName()); |
| 151 | lsaDataName.append(nlsr.getConfParameter().getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 152 | |
| 153 | // Append LSA type |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 154 | lsaDataName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 155 | |
| 156 | // This would be the sequence number of its own NameLsa |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 157 | lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 158 | |
| 159 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 160 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 161 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 162 | ndn::Data data(lsaDataName); |
| 163 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 164 | |
| 165 | // Make NLSR validate data signed by its own key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 166 | nlsr.getValidator().validate(data, |
| 167 | [] (const Data&) { BOOST_CHECK(false); }, |
| 168 | [] (const Data&, const ndn::security::v2::ValidationError&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 169 | BOOST_CHECK(true); |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | BOOST_AUTO_TEST_SUITE_END() |
| 174 | |
| 175 | } // namespace test |
| 176 | } // namespace nlsr |