Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 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"); |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame] | 93 | // Otherwise code coverage node fails with default 60 seconds lifetime |
| 94 | nlsr.getConfParameter().setSyncInterestLifetime(1000); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 95 | |
| 96 | // Initialize NLSR to initialize the keyChain |
| 97 | nlsr.initialize(); |
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 | this->advanceClocks(ndn::time::milliseconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 100 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 101 | face.sentInterests.clear(); |
| 102 | } |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 103 | |
| 104 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 105 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 106 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 107 | ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName; |
| 108 | ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 109 | |
| 110 | Nlsr nlsr; |
| 111 | |
| 112 | const boost::filesystem::path ROOT_CERT_PATH; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 113 | |
| 114 | //std::function<void(const ndn::Interest& interest)> processInterest; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture) |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(ValidateCorrectLSA) |
| 120 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 121 | ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix(); |
| 122 | lsaDataName.append(nlsr.getConfParameter().getSiteName()); |
| 123 | lsaDataName.append(nlsr.getConfParameter().getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 124 | |
| 125 | // Append LSA type |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 126 | lsaDataName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 127 | |
| 128 | // This would be the sequence number of its own NameLsa |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 129 | lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 130 | |
| 131 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 132 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 133 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 134 | ndn::Data data(lsaDataName); |
| 135 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 136 | |
| 137 | // Sign data with NLSR's key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 138 | nlsr.getKeyChain().sign(data, nlsr.getSigningInfo()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 139 | |
| 140 | // Make NLSR validate data signed by its own key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 141 | nlsr.getValidator().validate(data, |
| 142 | [] (const Data&) { BOOST_CHECK(true); }, |
| 143 | [] (const Data&, const ndn::security::v2::ValidationError&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 144 | BOOST_CHECK(false); |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA) |
| 149 | { |
| 150 | // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 151 | ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix().getSubName(1); |
| 152 | lsaDataName.append(nlsr.getConfParameter().getSiteName()); |
| 153 | lsaDataName.append(nlsr.getConfParameter().getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 154 | |
| 155 | // Append LSA type |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 156 | lsaDataName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 157 | |
| 158 | // This would be the sequence number of its own NameLsa |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 159 | lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 160 | |
| 161 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 162 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 163 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 164 | ndn::Data data(lsaDataName); |
| 165 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 166 | |
| 167 | // Make NLSR validate data signed by its own key |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 168 | nlsr.getValidator().validate(data, |
| 169 | [] (const Data&) { BOOST_CHECK(false); }, |
| 170 | [] (const Data&, const ndn::security::v2::ValidationError&) { |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 171 | BOOST_CHECK(true); |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_SUITE_END() |
| 176 | |
| 177 | } // namespace test |
| 178 | } // namespace nlsr |