blob: eaebf1200c8704a17ee88b45ef658b93595a2bb6 [file] [log] [blame]
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Davide Pesaventoe0ad5802023-02-20 19:42:52 -05003 * Copyright (c) 2014-2023, The University of Memphis,
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06004 * 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 Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060021
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060022#include "nlsr.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060023#include "security/certificate-store.hpp"
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060024
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040025#include "tests/io-key-chain-fixture.hpp"
26#include "tests/test-common.hpp"
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060027
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040028#include <boost/filesystem/operations.hpp>
29#include <boost/filesystem/path.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030#include <boost/lexical_cast.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060031#include <boost/property_tree/info_parser.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070032#include <boost/property_tree/ptree.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060033
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060034namespace nlsr {
35namespace test {
36
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040037using namespace ndn;
38
39class LsaRuleFixture : public IoKeyChainFixture
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060040{
41public:
42 LsaRuleFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040043 : face(m_io, m_keyChain, {true, true})
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 , 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 Dulal427e0122019-11-28 11:58:02 -060048 , confParam(face, m_keyChain)
Davide Pesavento1954a0c2022-09-30 15:56:04 -040049 , confProcessor(confParam, SyncProtocol::PSYNC, HYPERBOLIC_STATE_OFF,
Saurab Dulal427e0122019-11-28 11:58:02 -060050 "/ndn/", "/edu/test-site", "/%C1.Router/router1")
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070051 , lsdb(face, m_keyChain, confParam)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060052 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
53 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040054 rootId = m_keyChain.createIdentity(rootIdName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050055 siteIdentity = addSubCertificate(siteIdentityName, rootId);
56 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
57 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060058
Saurab Dulal427e0122019-11-28 11:58:02 -060059 // Create certificate and load it to the validator
60 // previously this was done by in nlsr ctor
61 confParam.initializeKey();
62
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040063 saveIdentityCert(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060064
Junxiao Shib032fcb2022-04-28 01:28:50 +000065 for (const auto& id : {rootId, siteIdentity, opIdentity, routerId}) {
Davide Pesaventoe0ad5802023-02-20 19:42:52 -050066 confParam.loadCertToValidator(id.getDefaultKey().getDefaultCertificate());
Junxiao Shib032fcb2022-04-28 01:28:50 +000067 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060068
69 // Loading the security section's validator part into the validator
70 // See conf file processor for more details
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060071 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000072 boost::property_tree::read_info("nlsr.conf", pt);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060073
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 // 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 Gawande54e726c2017-01-30 12:48:06 -060079 break;
80 }
81 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060082
Junxiao Shib032fcb2022-04-28 01:28:50 +000083 this->advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050084 face.sentInterests.clear();
85 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060086
87public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000088 ndn::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060089
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050090 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
91 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 ConfParameter confParam;
93 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070094 Lsdb lsdb;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060095
96 const boost::filesystem::path ROOT_CERT_PATH;
97};
98
99BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
100
101BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
102{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600103 ndn::Name lsaDataName = confParam.getLsaPrefix();
104 lsaDataName.append(confParam.getSiteName());
105 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600106
107 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800108 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600109
110 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700111 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600112
113 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500114 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600115
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500116 ndn::Data data(lsaDataName);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000117 data.setFreshnessPeriod(10_s);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600118
119 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600120 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600121
122 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123 confParam.getValidator().validate(data,
124 [] (const Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000125 [] (const Data&, const ndn::security::ValidationError& e) {
126 BOOST_ERROR(e);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600128}
129
130BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
131{
132 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600133 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
134 lsaDataName.append(confParam.getSiteName());
135 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600136
137 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800138 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600139
140 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700141 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600142
143 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500144 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600145
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500146 ndn::Data data(lsaDataName);
147 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600148
149 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600150 confParam.getValidator().validate(data,
151 [] (const Data&) { BOOST_CHECK(false); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400152 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600153 BOOST_CHECK(true);
154 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600155}
156
157BOOST_AUTO_TEST_SUITE_END()
158
159} // namespace test
160} // namespace nlsr