Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame^] | 2 | /* |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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/>. |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame^] | 20 | */ |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 21 | |
| 22 | #include "test-common.hpp" |
| 23 | #include "nlsr.hpp" |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 24 | #include "security/certificate-store.hpp" |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 25 | |
| 26 | #include <ndn-cxx/interest.hpp> |
| 27 | #include <ndn-cxx/security/key-chain.hpp> |
| 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 30 | #include <ndn-cxx/security/signing-info.hpp> |
| 31 | |
| 32 | #include <boost/filesystem.hpp> |
| 33 | #include <boost/property_tree/ptree.hpp> |
| 34 | #include <boost/property_tree/info_parser.hpp> |
| 35 | |
| 36 | using namespace ndn; |
| 37 | |
| 38 | namespace nlsr { |
| 39 | namespace test { |
| 40 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 41 | class LsaRuleFixture : public nlsr::test::UnitTestTimeFixture |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | LsaRuleFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 45 | : face(m_ioService, m_keyChain, {true, true}) |
| 46 | , rootIdName("/ndn") |
| 47 | , siteIdentityName("/ndn/edu/test-site") |
| 48 | , opIdentityName("/ndn/edu/test-site/%C1.Operator/op1") |
| 49 | , routerIdName("/ndn/edu/test-site/%C1.Router/router1") |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 50 | , confParam(face, m_keyChain) |
| 51 | , confProcessor(confParam, SYNC_PROTOCOL_PSYNC, HYPERBOLIC_STATE_OFF, |
| 52 | "/ndn/", "/edu/test-site", "/%C1.Router/router1") |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 53 | , nlsr(face, m_keyChain, confParam) |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 54 | , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert")) |
| 55 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 56 | rootId = addIdentity(rootIdName); |
| 57 | siteIdentity = addSubCertificate(siteIdentityName, rootId); |
| 58 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
| 59 | routerId = addSubCertificate(routerIdName, opIdentity); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 60 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 61 | // Create certificate and load it to the validator |
| 62 | // previously this was done by in nlsr ctor |
| 63 | confParam.initializeKey(); |
| 64 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 65 | saveCertificate(rootId, ROOT_CERT_PATH.string()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 66 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 67 | confParam.loadCertToValidator(rootId.getDefaultKey().getDefaultCertificate()); |
| 68 | confParam.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate()); |
| 69 | confParam.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate()); |
| 70 | confParam.loadCertToValidator(routerId.getDefaultKey().getDefaultCertificate()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 71 | |
| 72 | // Loading the security section's validator part into the validator |
| 73 | // See conf file processor for more details |
| 74 | std::ifstream inputFile; |
| 75 | inputFile.open(std::string("nlsr.conf")); |
| 76 | |
| 77 | BOOST_REQUIRE(inputFile.is_open()); |
| 78 | |
| 79 | boost::property_tree::ptree pt; |
| 80 | |
| 81 | boost::property_tree::read_info(inputFile, pt); |
| 82 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 83 | // Loads section and file name |
| 84 | for (const auto& tn : pt) { |
| 85 | if (tn.first == "security") { |
| 86 | auto it = tn.second.begin(); |
| 87 | confParam.getValidator().load(it->second, std::string("nlsr.conf")); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | } |
| 91 | inputFile.close(); |
| 92 | |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 93 | // Initialize NLSR to initialize the keyChain |
| 94 | nlsr.initialize(); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 95 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 96 | this->advanceClocks(ndn::time::milliseconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 97 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 98 | face.sentInterests.clear(); |
| 99 | } |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 100 | |
| 101 | public: |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 102 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 103 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 104 | ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName; |
| 105 | ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 106 | ConfParameter confParam; |
| 107 | DummyConfFileProcessor confProcessor; |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 108 | Nlsr nlsr; |
| 109 | |
| 110 | const boost::filesystem::path ROOT_CERT_PATH; |
| 111 | }; |
| 112 | |
| 113 | BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture) |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(ValidateCorrectLSA) |
| 116 | { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 117 | ndn::Name lsaDataName = confParam.getLsaPrefix(); |
| 118 | lsaDataName.append(confParam.getSiteName()); |
| 119 | lsaDataName.append(confParam.getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 120 | |
| 121 | // Append LSA type |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 122 | lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 123 | |
| 124 | // This would be the sequence number of its own NameLsa |
Ashlesh Gawande | 1505240 | 2018-12-12 20:20:00 -0600 | [diff] [blame] | 125 | lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 126 | |
| 127 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 128 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 129 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 130 | ndn::Data data(lsaDataName); |
| 131 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 132 | |
| 133 | // Sign data with NLSR's key |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 134 | m_keyChain.sign(data, confParam.getSigningInfo()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 135 | |
| 136 | // Make NLSR validate data signed by its own key |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 137 | confParam.getValidator().validate(data, |
| 138 | [] (const Data&) { BOOST_CHECK(true); }, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame^] | 139 | [] (const Data&, const ndn::security::ValidationError&) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 140 | BOOST_CHECK(false); |
| 141 | }); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA) |
| 145 | { |
| 146 | // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 147 | ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1); |
| 148 | lsaDataName.append(confParam.getSiteName()); |
| 149 | lsaDataName.append(confParam.getRouterName()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 150 | |
| 151 | // Append LSA type |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 152 | lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 153 | |
| 154 | // This would be the sequence number of its own NameLsa |
Ashlesh Gawande | 1505240 | 2018-12-12 20:20:00 -0600 | [diff] [blame] | 155 | lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq()); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 156 | |
| 157 | // Append version, segmentNo |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 158 | lsaDataName.appendNumber(1).appendNumber(1); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 159 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 160 | ndn::Data data(lsaDataName); |
| 161 | data.setFreshnessPeriod(ndn::time::seconds(10)); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 162 | |
| 163 | // Make NLSR validate data signed by its own key |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 164 | confParam.getValidator().validate(data, |
| 165 | [] (const Data&) { BOOST_CHECK(false); }, |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame^] | 166 | [] (const Data&, const ndn::security::ValidationError&) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 167 | BOOST_CHECK(true); |
| 168 | }); |
Ashlesh Gawande | 54e726c | 2017-01-30 12:48:06 -0600 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_SUITE_END() |
| 172 | |
| 173 | } // namespace test |
| 174 | } // namespace nlsr |