blob: e77694229487255a76e02c971d0face44679d0a0 [file] [log] [blame]
Ashlesh Gawande54e726c2017-01-30 12:48:06 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, The University of Memphis,
4 * 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")
49 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060050 , ROOT_CERT_PATH(boost::filesystem::current_path() / std::string("root.cert"))
51 {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050052 rootId = addIdentity(rootIdName);
53 siteIdentity = addSubCertificate(siteIdentityName, rootId);
54 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
55 routerId = addSubCertificate(routerIdName, opIdentity);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060056
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 saveCertificate(rootId, ROOT_CERT_PATH.string());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060058
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050059 auto load = [this] (const ndn::security::Identity& id) {
60 nlsr.loadCertToPublish(id.getDefaultKey().getDefaultCertificate());
61 };
62 load(rootId);
63 load(siteIdentity);
64 load(opIdentity);
65 load(routerId);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060066
67 // Loading the security section's validator part into the validator
68 // See conf file processor for more details
69 std::ifstream inputFile;
70 inputFile.open(std::string("nlsr.conf"));
71
72 BOOST_REQUIRE(inputFile.is_open());
73
74 boost::property_tree::ptree pt;
75
76 boost::property_tree::read_info(inputFile, pt);
77
78 //Loads section and file name
79 for (auto tn = pt.begin(); tn != pt.end(); ++tn) {
80 if (tn->first == "security") {
81 auto it = tn->second.begin();
82 nlsr.loadValidator(it->second, std::string("nlsr.conf"));
83 break;
84 }
85 }
86 inputFile.close();
87
88 // Set the network so the LSA prefix is constructed
89 // Set all so that buildRouterPrefix is set
90 nlsr.getConfParameter().setNetwork("/ndn");
91 nlsr.getConfParameter().setSiteName("/edu/test-site");
92 nlsr.getConfParameter().setRouterName("/%C1.Router/router1");
93
94 // Initialize NLSR to initialize the keyChain
95 nlsr.initialize();
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060096
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050097 this->advanceClocks(ndn::time::milliseconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -060098
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050099 face.sentInterests.clear();
100 }
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600101
102public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500103 ndn::util::DummyClientFace face;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600104
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500105 ndn::Name rootIdName, siteIdentityName, opIdentityName, routerIdName;
106 ndn::security::pib::Identity rootId, siteIdentity, opIdentity, routerId;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600107
108 Nlsr nlsr;
109
110 const boost::filesystem::path ROOT_CERT_PATH;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500111
112 //std::function<void(const ndn::Interest& interest)> processInterest;
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600113};
114
115BOOST_FIXTURE_TEST_SUITE(TestLsaDataValidation, LsaRuleFixture)
116
117BOOST_AUTO_TEST_CASE(ValidateCorrectLSA)
118{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500119 ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix();
120 lsaDataName.append(nlsr.getConfParameter().getSiteName());
121 lsaDataName.append(nlsr.getConfParameter().getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600122
123 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500124 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600125
126 // This would be the sequence number of its own NameLsa
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500127 lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600128
129 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600131
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500132 ndn::Data data(lsaDataName);
133 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600134
135 // Sign data with NLSR's key
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500136 nlsr.getKeyChain().sign(data, nlsr.getSigningInfo());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600137
138 // Make NLSR validate data signed by its own key
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500139 nlsr.getValidator().validate(data,
140 [] (const Data&) { BOOST_CHECK(true); },
141 [] (const Data&, const ndn::security::v2::ValidationError&) {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600142 BOOST_CHECK(false);
143 });
144}
145
146BOOST_AUTO_TEST_CASE(DoNotValidateIncorrectLSA)
147{
148 // getSubName removes the /localhop compnonent from /localhop/ndn/NLSR/LSA
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500149 ndn::Name lsaDataName = nlsr.getConfParameter().getLsaPrefix().getSubName(1);
150 lsaDataName.append(nlsr.getConfParameter().getSiteName());
151 lsaDataName.append(nlsr.getConfParameter().getRouterName());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600152
153 // Append LSA type
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154 lsaDataName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600155
156 // This would be the sequence number of its own NameLsa
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500157 lsaDataName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600158
159 // Append version, segmentNo
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500160 lsaDataName.appendNumber(1).appendNumber(1);
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600161
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500162 ndn::Data data(lsaDataName);
163 data.setFreshnessPeriod(ndn::time::seconds(10));
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600164
165 // Make NLSR validate data signed by its own key
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 nlsr.getValidator().validate(data,
167 [] (const Data&) { BOOST_CHECK(false); },
168 [] (const Data&, const ndn::security::v2::ValidationError&) {
Ashlesh Gawande54e726c2017-01-30 12:48:06 -0600169 BOOST_CHECK(true);
170 });
171}
172
173BOOST_AUTO_TEST_SUITE_END()
174
175} // namespace test
176} // namespace nlsr