blob: a2d048180faeb54379ab665d249d596dabf8a413 [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
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)
49 , confProcessor(confParam, SYNC_PROTOCOL_PSYNC, HYPERBOLIC_STATE_OFF,
50 "/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}) {
66 const auto& cert = id.getDefaultKey().getDefaultCertificate();
67 confParam.loadCertToValidator(cert);
68 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060069
70 // Loading the security section's validator part into the validator
71 // See conf file processor for more details
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060072 boost::property_tree::ptree pt;
Junxiao Shib032fcb2022-04-28 01:28:50 +000073 boost::property_tree::read_info("nlsr.conf", pt);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060074
Ashlesh Gawande85998a12017-12-07 22:22:13 -060075 // Loads section and file name
76 for (const auto& tn : pt) {
77 if (tn.first == "security") {
78 auto it = tn.second.begin();
79 confParam.getValidator().load(it->second, std::string("nlsr.conf"));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060080 break;
81 }
82 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060083
Junxiao Shib032fcb2022-04-28 01:28:50 +000084 this->advanceClocks(10_ms);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050085 face.sentInterests.clear();
86 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060087
88public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050089 ndn::util::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060090
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050091 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
92 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093 ConfParameter confParam;
94 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070095 Lsdb lsdb;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060096
97 const boost::filesystem::path ROOT_CERT_PATH;
98};
99
100BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
101
102BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
103{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600104 ndn::Name lsaDataName = confParam.getLsaPrefix();
105 lsaDataName.append(confParam.getSiteName());
106 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600107
108 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800109 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600110
111 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700112 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600113
114 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500115 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600116
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117 ndn::Data data(lsaDataName);
Junxiao Shib032fcb2022-04-28 01:28:50 +0000118 data.setFreshnessPeriod(10_s);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600119
120 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600121 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600122
123 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600124 confParam.getValidator().validate(data,
125 [] (const Data&) { BOOST_CHECK(true); },
Junxiao Shib032fcb2022-04-28 01:28:50 +0000126 [] (const Data&, const ndn::security::ValidationError& e) {
127 BOOST_ERROR(e);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600128 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600129}
130
131BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
132{
133 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600134 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
135 lsaDataName.append(confParam.getSiteName());
136 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600137
138 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800139 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600140
141 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700142 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600143
144 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500145 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600146
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147 ndn::Data data(lsaDataName);
148 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600149
150 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600151 confParam.getValidator().validate(data,
152 [] (const Data&) { BOOST_CHECK(false); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400153 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600154 BOOST_CHECK(true);
155 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600156}
157
158BOOST_AUTO_TEST_SUITE_END()
159
160} // namespace test
161} // namespace nlsr