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