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