blob: dffe174e2435ad0fe77d0df9bca7746dda6f5aa2 [file] [log] [blame]
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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/>.
20 **/
21
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 Gawande85998a12017-12-07 22:22:13 -060053 , nlsr(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
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060093 // Initialize NLSR to initialize the keyChain
94 nlsr.initialize();
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060095
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050096 this->advanceClocks(ndn::time::milliseconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060097
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050098 face.sentInterests.clear();
99 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600100
101public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500102 ndn::util::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600103
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500104 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
105 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600106 ConfParameter confParam;
107 DummyConfFileProcessor confProcessor;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600108 Nlsr nlsr;
109
110 const boost::filesystem::path ROOT_CERT_PATH;
111};
112
113BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
114
115BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
116{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600117 ndn::Name lsaDataName = confParam.getLsaPrefix();
118 lsaDataName.append(confParam.getSiteName());
119 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600120
121 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600123
124 // This would be the sequence number of its own NameLsa
Ashlesh Gawande15052402018-12-12 20:20:00 -0600125 lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600126
127 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500128 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600129
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130 ndn::Data data(lsaDataName);
131 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600132
133 // Sign data with NLSR's key
Saurab Dulal427e0122019-11-28 11:58:02 -0600134 m_keyChain.sign(data, confParam.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600135
136 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600137 confParam.getValidator().validate(data,
138 [] (const Data&) { BOOST_CHECK(true); },
139 [] (const Data&, const ndn::security::v2::ValidationError&) {
140 BOOST_CHECK(false);
141 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600142}
143
144BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
145{
146 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
148 lsaDataName.append(confParam.getSiteName());
149 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600150
151 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500152 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600153
154 // This would be the sequence number of its own NameLsa
Ashlesh Gawande15052402018-12-12 20:20:00 -0600155 lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600156
157 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500158 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600159
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500160 ndn::Data data(lsaDataName);
161 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600162
163 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600164 confParam.getValidator().validate(data,
165 [] (const Data&) { BOOST_CHECK(false); },
166 [] (const Data&, const ndn::security::v2::ValidationError&) {
167 BOOST_CHECK(true);
168 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600169}
170
171BOOST_AUTO_TEST_SUITE_END()
172
173} // namespace test
174} // namespace nlsr