blob: 7f93e9d4bcb97edcb1169a9e4b3cf4588d19d0c3 [file] [log] [blame]
Saurab Dulal7526cee2018-01-31 18:14:10 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07002/*
3 * Copyright (c) 2014-2021, The University of Memphis,
Saurab Dulal7526cee2018-01-31 18:14:10 +00004 * 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/>.
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070020 */
Saurab Dulal7526cee2018-01-31 18:14:10 +000021
22#include "update/prefix-update-processor.hpp"
Saurab Dulal7526cee2018-01-31 18:14:10 +000023#include "nlsr.hpp"
24
Saurab Dulal427e0122019-11-28 11:58:02 -060025#include "tests/control-commands.hpp"
26#include "tests/test-common.hpp"
dulalsaurab82a34c22019-02-04 17:31:21 +000027#include "conf-parameter.hpp"
Junxiao Shi008c9b12019-01-13 23:15:56 +000028
Saurab Dulal7526cee2018-01-31 18:14:10 +000029#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Davide Pesavento6184c202021-05-17 02:28:03 -040030#include <ndn-cxx/security/interest-signer.hpp>
Saurab Dulal7526cee2018-01-31 18:14:10 +000031#include <ndn-cxx/security/pib/identity.hpp>
32#include <ndn-cxx/security/signing-helpers.hpp>
Junxiao Shi008c9b12019-01-13 23:15:56 +000033
Saurab Dulal7526cee2018-01-31 18:14:10 +000034#include <boost/filesystem.hpp>
35
36namespace nlsr {
Saurab Dulal7526cee2018-01-31 18:14:10 +000037namespace test {
38
39namespace pt = boost::property_tree;
40using namespace pt;
41
42class PrefixSaveDeleteFixture : public nlsr::test::UnitTestTimeFixture
43{
44public:
45 PrefixSaveDeleteFixture()
46 : face(m_ioService, m_keyChain, {true, true})
47 , siteIdentityName(ndn::Name("/edu/test-site"))
48 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
Saurab Dulal7526cee2018-01-31 18:14:10 +000049 , testConfFile("/tmp/nlsr.conf.test")
Saurab Dulal427e0122019-11-28 11:58:02 -060050 , conf(face, m_keyChain, testConfFile)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060051 , confProcessor(conf)
52 , nlsr(face, m_keyChain, conf)
53 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
Saurab Dulal7526cee2018-01-31 18:14:10 +000054 , counter(0)
55 {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070056 std::ifstream source("nlsr.conf", std::ios::binary);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060057 std::ofstream destination(testConfFile, std::ios::binary);
Saurab Dulal7526cee2018-01-31 18:14:10 +000058 destination << source.rdbuf();
59 source.close();
60 destination.close();
61
dulalsaurab82a34c22019-02-04 17:31:21 +000062 conf.setConfFileNameDynamic(testConfFile);
Saurab Dulal7526cee2018-01-31 18:14:10 +000063 siteIdentity = addIdentity(siteIdentityName);
64 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
65
66 // Operator cert
67 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
68 // Loading the security section's validator part into the validator
69 // See conf file processor for more details
70 std::ifstream inputFile;
71 inputFile.open(testConfFile);
72 BOOST_REQUIRE(inputFile.is_open());
73 pt::ptree pt;
74 boost::property_tree::read_info(inputFile, pt);
75 // Loads section and file name
76 for (const auto& section : pt) {
77 if (section.first == "security") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060078 for (const auto& it : section.second) {
79 if (it.first == "prefix-update-validator") {
80 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
81 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000082 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000083 }
84 }
dulalsaurab82a34c22019-02-04 17:31:21 +000085
Saurab Dulal7526cee2018-01-31 18:14:10 +000086 inputFile.close();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087
Saurab Dulal7526cee2018-01-31 18:14:10 +000088 // Site cert
89 siteIdentity = addIdentity(siteIdentityName);
90 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
91
92 // Operator cert
93 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
Saurab Dulal427e0122019-11-28 11:58:02 -060094
95 // Create certificate and load it to the validator
96 conf.initializeKey();
97 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
98 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
Saurab Dulal7526cee2018-01-31 18:14:10 +000099
100 // Set the network so the LSA prefix is constructed
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600101 addIdentity(conf.getRouterPrefix());
Saurab Dulal7526cee2018-01-31 18:14:10 +0000102
Saurab Dulal7526cee2018-01-31 18:14:10 +0000103 this->advanceClocks(ndn::time::milliseconds(10));
104 face.sentInterests.clear();
105 }
106
107 uint32_t
108 getResponseCode()
109 {
110 ndn::nfd::ControlResponse response;
111 for (const auto& data : face.sentData) {
112 response.wireDecode(data.getContent().blockFromValue());
113 }
114 return response.getCode();
115 }
116
117 bool
118 checkPrefix(const std::string prefixName)
119 {
120 counter = 0;
121 pt::ptree m_savePrefix;
122 pt::info_parser::read_info(testConfFile, m_savePrefix);
123 // counter helps to check if multiple prefix of same name exists on conf file
124 for (const auto& section : m_savePrefix.get_child("advertising")) {
125 std:: string b = section.second.get_value<std::string>();
126 if (b == prefixName) {
127 counter++;
128 }
129 }
130 if (counter > 0) {
131 return true;
132 }
133 return false;
134 }
135
136 ndn::Interest
137 advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG)
138 {
139 ndn::nfd::ControlParameters parameters;
140 parameters.setName(prefixName);
Davide Pesavento6184c202021-05-17 02:28:03 -0400141 if (P_FLAG) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142 parameters.setFlags(update::PREFIX_FLAG);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000143 }
144 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
145 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Davide Pesavento6184c202021-05-17 02:28:03 -0400146 ndn::security::InterestSigner signer(m_keyChain);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000147 // type true for advertise, else withdraw
148 if (type == "advertise") {
149 advertiseCommand.append(parameters.wireEncode());
Davide Pesavento6184c202021-05-17 02:28:03 -0400150 return signer.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000151 }
152 else {
153 withdrawCommand.append(parameters.wireEncode());
Davide Pesavento6184c202021-05-17 02:28:03 -0400154 return signer.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000155 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000156 }
157
158public:
159 ndn::util::DummyClientFace face;
Saurab Dulal427e0122019-11-28 11:58:02 -0600160 ndn::Name siteIdentityName, routerIdName;
161 ndn::security::pib::Identity siteIdentity, routerId;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000162
163 ndn::Name opIdentityName;
164 ndn::security::pib::Identity opIdentity;
165
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600166 std::string testConfFile;
167 ConfParameter conf;
168 DummyConfFileProcessor confProcessor;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000169 Nlsr nlsr;
170 const boost::filesystem::path SITE_CERT_PATH;
171 ndn::Name sessionTime;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000172 int counter;
173};
174
175BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture)
176
177BOOST_AUTO_TEST_CASE(Basic)
178{
dulalsaurab82a34c22019-02-04 17:31:21 +0000179
Saurab Dulal7526cee2018-01-31 18:14:10 +0000180 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
181 this->advanceClocks(ndn::time::milliseconds(10));
182 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
183 BOOST_CHECK_EQUAL(getResponseCode(), 200);
184 face.sentData.clear();
185
186 // trying to re-advertise
187 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
188 this->advanceClocks(ndn::time::milliseconds(10));
189 BOOST_CHECK_EQUAL(getResponseCode(), 204);
190 face.sentData.clear();
191
192 // only withdraw
193 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
194 this->advanceClocks(ndn::time::milliseconds(10));
195 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
196 BOOST_CHECK_EQUAL(getResponseCode(), 200);
197 face.sentData.clear();
198
199 // trying to re-advertise
200 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
201 this->advanceClocks(ndn::time::milliseconds(10));
202 BOOST_CHECK_EQUAL(getResponseCode(), 204);
203 face.sentData.clear();
204}
205
206BOOST_AUTO_TEST_CASE(PrefixStillSavedAfterJustWithdrawn)
207{
208 // advertise and save
209 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
210 this->advanceClocks(ndn::time::milliseconds(10));
211 face.sentData.clear();
212 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
213
214 // trying to advertise same name prefix
dulalsaurab82a34c22019-02-04 17:31:21 +0000215 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000216 this->advanceClocks(ndn::time::milliseconds(10));
217 BOOST_REQUIRE(counter == 1);
218 BOOST_CHECK_EQUAL(getResponseCode(), 406);
219 face.sentData.clear();
220
221 // only withdraw
222 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
223 this->advanceClocks(ndn::time::milliseconds(10));
224 // after withdrawing only prefix should still be there
225 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
226 BOOST_CHECK_EQUAL(getResponseCode(), 200);
227
228 // delete
229 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", true));
230 this->advanceClocks(ndn::time::milliseconds(10));
231 // after withdrawn delete prefix should be deleted from the file
232 BOOST_CHECK_EQUAL(getResponseCode(), 205);
dulalsaurab82a34c22019-02-04 17:31:21 +0000233 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000234}
235
236BOOST_AUTO_TEST_SUITE_END()
237
238} // namespace test
Saurab Dulal7526cee2018-01-31 18:14:10 +0000239} // namespace nlsr