blob: e6d445831e4356d7c763d353761151a0ee0058ab [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/*
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07003 * Copyright (c) 2014-2021, 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>
28#include <ndn-cxx/util/dummy-client-face.hpp>
29#include <ndn-cxx/security/signing-helpers.hpp>
30#include <ndn-cxx/security/signing-info.hpp>
31
32#include <boost/filesystem.hpp>
33#include <boost/property_tree/ptree.hpp>
34#include <boost/property_tree/info_parser.hpp>
35
36using namespace ndn;
37
38namespace nlsr {
39namespace test {
40
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050041class LsaRuleFixture : public nlsr::test::UnitTestTimeFixture
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060042{
43public:
44 LsaRuleFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050045 : face(m_ioService, m_keyChain, {true, true})
46 , rootIdName("/ndn")
47 , siteIdentityName("/ndn/edu/test-site")
48 , opIdentityName("/ndn/edu/test-site/%C1.Operator/op1")
49 , routerIdName("/ndn/edu/test-site/%C1.Router/router1")
Saurab Dulal427e0122019-11-28 11:58:02 -060050 , confParam(face, m_keyChain)
51 , confProcessor(confParam, SYNC_PROTOCOL_PSYNC, HYPERBOLIC_STATE_OFF,
52 "/ndn/", "/edu/test-site", "/%C1.Router/router1")
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070053 , lsdb(face, m_keyChain, confParam)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060054 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
55 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050056 rootId = addIdentity(rootIdName);
57 siteIdentity = addSubCertificate(siteIdentityName, rootId);
58 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
59 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060060
Saurab Dulal427e0122019-11-28 11:58:02 -060061 // Create certificate and load it to the validator
62 // previously this was done by in nlsr ctor
63 confParam.initializeKey();
64
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 saveCertificate(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060066
Saurab Dulal427e0122019-11-28 11:58:02 -060067 confParam.loadCertToValidator(rootId.getDefaultKey().getDefaultCertificate());
68 confParam.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
69 confParam.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
70 confParam.loadCertToValidator(routerId.getDefaultKey().getDefaultCertificate());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060071
72 // Loading the security section's validator part into the validator
73 // See conf file processor for more details
74 std::ifstream inputFile;
75 inputFile.open(std::string("nlsr.conf"));
76
77 BOOST_REQUIRE(inputFile.is_open());
78
79 boost::property_tree::ptree pt;
80
81 boost::property_tree::read_info(inputFile, pt);
82
Ashlesh Gawande85998a12017-12-07 22:22:13 -060083 // Loads section and file name
84 for (const auto& tn : pt) {
85 if (tn.first == "security") {
86 auto it = tn.second.begin();
87 confParam.getValidator().load(it->second, std::string("nlsr.conf"));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060088 break;
89 }
90 }
91 inputFile.close();
92
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050093 this->advanceClocks(ndn::time::milliseconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060094
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050095 face.sentInterests.clear();
96 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060097
98public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050099 ndn::util::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600100
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500101 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
102 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600103 ConfParameter confParam;
104 DummyConfFileProcessor confProcessor;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700105 Lsdb lsdb;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600106
107 const boost::filesystem::path ROOT_CERT_PATH;
108};
109
110BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
111
112BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
113{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600114 ndn::Name lsaDataName = confParam.getLsaPrefix();
115 lsaDataName.append(confParam.getSiteName());
116 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600117
118 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800119 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600120
121 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700122 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600123
124 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500125 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600126
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500127 ndn::Data data(lsaDataName);
128 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600129
130 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600131 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600132
133 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600134 confParam.getValidator().validate(data,
135 [] (const Data&) { BOOST_CHECK(true); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400136 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600137 BOOST_CHECK(false);
138 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600139}
140
141BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
142{
143 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600144 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
145 lsaDataName.append(confParam.getSiteName());
146 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600147
148 // Append LSA type
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800149 lsaDataName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600150
151 // This would be the sequence number of its own NameLsa
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700152 lsaDataName.appendNumber(lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600153
154 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500155 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600156
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500157 ndn::Data data(lsaDataName);
158 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600159
160 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600161 confParam.getValidator().validate(data,
162 [] (const Data&) { BOOST_CHECK(false); },
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400163 [] (const Data&, const ndn::security::ValidationError&) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 BOOST_CHECK(true);
165 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600166}
167
168BOOST_AUTO_TEST_SUITE_END()
169
170} // namespace test
171} // namespace nlsr