blob: 557b540ca700613cf08a8392241e5212f3148244 [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 Pesaventod1f1df82022-03-12 16:40:37 -05003 * Copyright (c) 2014-2022, 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
22#include "test-common.hpp"
23#include "nlsr.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060024#include "security/certificate-store.hpp"
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060025
26#include <ndn-cxx/interest.hpp>
27#include <ndn-cxx/security/key-chain.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060028#include <ndn-cxx/security/signing-helpers.hpp>
29#include <ndn-cxx/security/signing-info.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030#include <ndn-cxx/util/dummy-client-face.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060031
32#include <boost/filesystem.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070033#include <boost/lexical_cast.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060034#include <boost/property_tree/info_parser.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070035#include <boost/property_tree/ptree.hpp>
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060036
37using namespace ndn;
38
39namespace nlsr {
40namespace test {
41
Davide Pesaventod1f1df82022-03-12 16:40:37 -050042class LsaRuleFixture : public UnitTestTimeFixture
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060043{
44public:
45 LsaRuleFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050046 : 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 Dulal427e0122019-11-28 11:58:02 -060051 , confParam(face, m_keyChain)
52 , confProcessor(confParam, SYNC_PROTOCOL_PSYNC, HYPERBOLIC_STATE_OFF,
53 "/ndn/", "/edu/test-site", "/%C1.Router/router1")
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070054 , lsdb(face, m_keyChain, confParam)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060055 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
56 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 rootId = addIdentity(rootIdName);
58 siteIdentity = addSubCertificate(siteIdentityName, rootId);
59 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
60 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060061
Saurab Dulal427e0122019-11-28 11:58:02 -060062 // Create certificate and load it to the validator
63 // previously this was done by in nlsr ctor
64 confParam.initializeKey();
65
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050066 saveCertificate(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060067
Junxiao Shib032fcb2022-04-28 01:28:50 +000068 for (const auto& id : {rootId, siteIdentity, opIdentity, routerId}) {
69 const auto& cert = id.getDefaultKey().getDefaultCertificate();
70 confParam.loadCertToValidator(cert);
71 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060072
73 // Loading the security section's validator part into the validator
74 // See conf file processor for more details
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060075 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000076 boost::property_tree::read_info("nlsr.conf", pt);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060077
Ashlesh Gawande85998a12017-12-07 22:22:13 -060078 // Loads section and file name
79 for (const auto& tn : pt) {
80 if (tn.first == "security") {
81 auto it = tn.second.begin();
82 confParam.getValidator().load(it->second, std::string("nlsr.conf"));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060083 break;
84 }
85 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060086
Junxiao Shib032fcb2022-04-28 01:28:50 +000087 this->advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050088 face.sentInterests.clear();
89 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060090
91public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050092 ndn::util::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060093
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050094 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
95 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096 ConfParameter confParam;
97 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070098 Lsdb lsdb;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060099
100 const boost::filesystem::path ROOT_CERT_PATH;
101};
102
103BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
104
105BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
106{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600107 ndn::Name lsaDataName = confParam.getLsaPrefix();
108 lsaDataName.append(confParam.getSiteName());
109 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600110
111 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800112 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600113
114 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700115 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600116
117 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500118 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600119
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500120 ndn::Data data(lsaDataName);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000121 data.setFreshnessPeriod(10_s);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600122
123 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600124 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600125
126 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600127 confParam.getValidator().validate(data,
128 [] (const Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000129 [] (const Data&, const ndn::security::ValidationError& e) {
130 BOOST_ERROR(e);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600131 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600132}
133
134BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
135{
136 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600137 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
138 lsaDataName.append(confParam.getSiteName());
139 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600140
141 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800142 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600143
144 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700145 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600146
147 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500148 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600149
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150 ndn::Data data(lsaDataName);
151 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600152
153 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 confParam.getValidator().validate(data,
155 [] (const Data&) { BOOST_CHECK(false); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400156 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600157 BOOST_CHECK(true);
158 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600159}
160
161BOOST_AUTO_TEST_SUITE_END()
162
163} // namespace test
164} // namespace nlsr