blob: 87f915dc00d760e696574b87c4e4735aed8af720 [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 Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento288141a2024-02-13 17:30:35 -050034namespace nlsr::tests {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060035
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040036using namespace ndn;
37
38class LsaRuleFixture : public IoKeyChainFixture
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060039{
40public:
41 LsaRuleFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040042 : face(m_io, m_keyChain, {true, true})
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050043 , rootIdName("/ndn")
44 , siteIdentityName("/ndn/edu/test-site")
45 , opIdentityName("/ndn/edu/test-site/%C1.Operator/op1")
46 , routerIdName("/ndn/edu/test-site/%C1.Router/router1")
Saurab Dulal427e0122019-11-28 11:58:02 -060047 , confParam(face, m_keyChain)
Davide Pesavento1954a0c2022-09-30 15:56:04 -040048 , confProcessor(confParam, SyncProtocol::PSYNC, HYPERBOLIC_STATE_OFF,
Saurab Dulal427e0122019-11-28 11:58:02 -060049 "/ndn/", "/edu/test-site", "/%C1.Router/router1")
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070050 , lsdb(face, m_keyChain, confParam)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060051 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
52 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040053 rootId = m_keyChain.createIdentity(rootIdName);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050054 siteIdentity = addSubCertificate(siteIdentityName, rootId);
55 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
56 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060057
Saurab Dulal427e0122019-11-28 11:58:02 -060058 // Create certificate and load it to the validator
59 // previously this was done by in nlsr ctor
60 confParam.initializeKey();
61
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040062 saveIdentityCert(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060063
Junxiao Shib032fcb2022-04-28 01:28:50 +000064 for (const auto& id : {rootId, siteIdentity, opIdentity, routerId}) {
Davide Pesaventoe0ad5802023-02-20 19:42:52 -050065 confParam.loadCertToValidator(id.getDefaultKey().getDefaultCertificate());
Junxiao Shib032fcb2022-04-28 01:28:50 +000066 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060067
68 // Loading the security section's validator part into the validator
69 // See conf file processor for more details
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060070 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000071 boost::property_tree::read_info("nlsr.conf", pt);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060072
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 // Loads section and file name
74 for (const auto& tn : pt) {
75 if (tn.first == "security") {
76 auto it = tn.second.begin();
77 confParam.getValidator().load(it->second, std::string("nlsr.conf"));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060078 break;
79 }
80 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060081
Junxiao Shib032fcb2022-04-28 01:28:50 +000082 this->advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050083 face.sentInterests.clear();
84 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060085
86public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000087 ndn::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060088
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050089 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
90 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060091 ConfParameter confParam;
92 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070093 Lsdb lsdb;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060094
95 const boost::filesystem::path ROOT_CERT_PATH;
96};
97
98BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
99
100BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
101{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 ndn::Name lsaDataName = confParam.getLsaPrefix();
103 lsaDataName.append(confParam.getSiteName());
104 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600105
106 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800107 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600108
109 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700110 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600111
112 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600114
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500115 ndn::Data data(lsaDataName);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000116 data.setFreshnessPeriod(10_s);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600117
118 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600119 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600120
121 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600122 confParam.getValidator().validate(data,
123 [] (const Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000124 [] (const Data&, const ndn::security::ValidationError& e) {
125 BOOST_ERROR(e);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600126 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600127}
128
129BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
130{
131 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600132 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
133 lsaDataName.append(confParam.getSiteName());
134 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600135
136 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800137 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600138
139 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700140 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600141
142 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500143 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600144
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500145 ndn::Data data(lsaDataName);
146 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600147
148 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600149 confParam.getValidator().validate(data,
150 [] (const Data&) { BOOST_CHECK(false); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400151 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600152 BOOST_CHECK(true);
153 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600154}
155
156BOOST_AUTO_TEST_SUITE_END()
157
Davide Pesavento288141a2024-02-13 17:30:35 -0500158} // namespace nlsr::tests