blob: 26c8e25c0950fe85c598ab492c253ea939f77ef8 [file] [log] [blame]
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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"
24
25#include <ndn-cxx/interest.hpp>
26#include <ndn-cxx/security/key-chain.hpp>
27#include <ndn-cxx/util/dummy-client-face.hpp>
28#include <ndn-cxx/security/signing-helpers.hpp>
29#include <ndn-cxx/security/signing-info.hpp>
30
31#include <boost/filesystem.hpp>
32#include <boost/property_tree/ptree.hpp>
33#include <boost/property_tree/info_parser.hpp>
34
35using namespace ndn;
36
37namespace nlsr {
38namespace test {
39
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050040class LsaRuleFixture : public nlsr::test::UnitTestTimeFixture
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060041{
42public:
43 LsaRuleFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050044 : face(m_ioService, m_keyChain, {true, true})
45 , rootIdName("/ndn")
46 , siteIdentityName("/ndn/edu/test-site")
47 , opIdentityName("/ndn/edu/test-site/%C1.Operator/op1")
48 , routerIdName("/ndn/edu/test-site/%C1.Router/router1")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 , confParam(face)
50 , confProcessor(confParam)
51 , nlsr(face, m_keyChain, confParam)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060052 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
53 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050054 rootId = addIdentity(rootIdName);
55 siteIdentity = addSubCertificate(siteIdentityName, rootId);
56 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
57 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060058
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059 saveCertificate(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060060
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050061 auto load = [this] (const ndn::security::Identity& id) {
62 nlsr.loadCertToPublish(id.getDefaultKey().getDefaultCertificate());
63 };
64 load(rootId);
65 load(siteIdentity);
66 load(opIdentity);
67 load(routerId);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060068
69 // Loading the security section's validator part into the validator
70 // See conf file processor for more details
71 std::ifstream inputFile;
72 inputFile.open(std::string("nlsr.conf"));
73
74 BOOST_REQUIRE(inputFile.is_open());
75
76 boost::property_tree::ptree pt;
77
78 boost::property_tree::read_info(inputFile, pt);
79
Ashlesh Gawande85998a12017-12-07 22:22:13 -060080 // Loads section and file name
81 for (const auto& tn : pt) {
82 if (tn.first == "security") {
83 auto it = tn.second.begin();
84 confParam.getValidator().load(it->second, std::string("nlsr.conf"));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060085 break;
86 }
87 }
88 inputFile.close();
89
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060090 // Initialize NLSR to initialize the keyChain
91 nlsr.initialize();
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060092
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 Gawande54e726c2017-01-30 12:48:06 -0600103
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600104 ConfParameter confParam;
105 DummyConfFileProcessor confProcessor;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600106 Nlsr nlsr;
107
108 const boost::filesystem::path ROOT_CERT_PATH;
109};
110
111BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
112
113BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
114{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600115 ndn::Name lsaDataName = confParam.getLsaPrefix();
116 lsaDataName.append(confParam.getSiteName());
117 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600118
119 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500120 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600121
122 // This would be the sequence number of its own NameLsa
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123 lsaDataName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600124
125 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500126 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600127
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500128 ndn::Data data(lsaDataName);
129 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600130
131 // Sign data with NLSR's key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600132 m_keyChain.sign(data, nlsr.m_signingInfo);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600133
134 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600135 confParam.getValidator().validate(data,
136 [] (const Data&) { BOOST_CHECK(true); },
137 [] (const Data&, const ndn::security::v2::ValidationError&) {
138 BOOST_CHECK(false);
139 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600140}
141
142BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
143{
144 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600145 ndn::Name lsaDataName = confParam.getLsaPrefix().getSubName(1);
146 lsaDataName.append(confParam.getSiteName());
147 lsaDataName.append(confParam.getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600148
149 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500150 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600151
152 // This would be the sequence number of its own NameLsa
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600153 lsaDataName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600154
155 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600157
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500158 ndn::Data data(lsaDataName);
159 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600160
161 // Make NLSR validate data signed by its own key
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600162 confParam.getValidator().validate(data,
163 [] (const Data&) { BOOST_CHECK(false); },
164 [] (const Data&, const ndn::security::v2::ValidationError&) {
165 BOOST_CHECK(true);
166 });
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600167}
168
169BOOST_AUTO_TEST_SUITE_END()
170
171} // namespace test
172} // namespace nlsr