blob: ef38cefd4773a6638c44b0898c9f3e624728601c [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/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, 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"
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040023#include "conf-parameter.hpp"
Saurab Dulal7526cee2018-01-31 18:14:10 +000024#include "nlsr.hpp"
25
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040026#include "tests/io-key-chain-fixture.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060027#include "tests/test-common.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/signing-helpers.hpp>
Junxiao Shi008c9b12019-01-13 23:15:56 +000032
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040033#include <boost/filesystem/operations.hpp>
34#include <boost/filesystem/path.hpp>
Davide Pesavento7bc3d432021-10-25 21:08:04 -040035#include <boost/property_tree/info_parser.hpp>
Saurab Dulal7526cee2018-01-31 18:14:10 +000036
Davide Pesavento288141a2024-02-13 17:30:35 -050037namespace nlsr::tests {
Saurab Dulal7526cee2018-01-31 18:14:10 +000038
Davide Pesavento7bc3d432021-10-25 21:08:04 -040039namespace bpt = boost::property_tree;
Saurab Dulal7526cee2018-01-31 18:14:10 +000040
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040041class PrefixSaveDeleteFixture : public IoKeyChainFixture
Saurab Dulal7526cee2018-01-31 18:14:10 +000042{
43public:
44 PrefixSaveDeleteFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040045 : face(m_io, m_keyChain, {true, true})
Saurab Dulal7526cee2018-01-31 18:14:10 +000046 , siteIdentityName(ndn::Name("/edu/test-site"))
47 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
Saurab Dulal7526cee2018-01-31 18:14:10 +000048 , testConfFile("/tmp/nlsr.conf.test")
Saurab Dulal427e0122019-11-28 11:58:02 -060049 , conf(face, m_keyChain, testConfFile)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060050 , confProcessor(conf)
51 , nlsr(face, m_keyChain, conf)
52 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
Saurab Dulal7526cee2018-01-31 18:14:10 +000053 , counter(0)
54 {
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070055 std::ifstream source("nlsr.conf", std::ios::binary);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060056 std::ofstream destination(testConfFile, std::ios::binary);
Saurab Dulal7526cee2018-01-31 18:14:10 +000057 destination << source.rdbuf();
58 source.close();
59 destination.close();
60
dulalsaurab82a34c22019-02-04 17:31:21 +000061 conf.setConfFileNameDynamic(testConfFile);
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040062 siteIdentity = m_keyChain.createIdentity(siteIdentityName);
63 saveIdentityCert(siteIdentity, SITE_CERT_PATH.string());
Saurab Dulal7526cee2018-01-31 18:14:10 +000064
65 // Operator cert
66 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
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(testConfFile);
71 BOOST_REQUIRE(inputFile.is_open());
Davide Pesavento7bc3d432021-10-25 21:08:04 -040072 bpt::ptree pt;
73 bpt::read_info(inputFile, pt);
Saurab Dulal7526cee2018-01-31 18:14:10 +000074 // Loads section and file name
75 for (const auto& section : pt) {
76 if (section.first == "security") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060077 for (const auto& it : section.second) {
78 if (it.first == "prefix-update-validator") {
79 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
80 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000081 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000082 }
83 }
dulalsaurab82a34c22019-02-04 17:31:21 +000084
Saurab Dulal7526cee2018-01-31 18:14:10 +000085 inputFile.close();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086
Saurab Dulal7526cee2018-01-31 18:14:10 +000087 // Site cert
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040088 siteIdentity = m_keyChain.createIdentity(siteIdentityName);
89 saveIdentityCert(siteIdentity, SITE_CERT_PATH.string());
Saurab Dulal7526cee2018-01-31 18:14:10 +000090
91 // Operator cert
92 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
Saurab Dulal427e0122019-11-28 11:58:02 -060093
94 // Create certificate and load it to the validator
95 conf.initializeKey();
96 conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate());
97 conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate());
Saurab Dulal7526cee2018-01-31 18:14:10 +000098
99 // Set the network so the LSA prefix is constructed
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400100 m_keyChain.createIdentity(conf.getRouterPrefix());
Saurab Dulal7526cee2018-01-31 18:14:10 +0000101
Saurab Dulal7526cee2018-01-31 18:14:10 +0000102 this->advanceClocks(ndn::time::milliseconds(10));
103 face.sentInterests.clear();
104 }
105
106 uint32_t
107 getResponseCode()
108 {
109 ndn::nfd::ControlResponse response;
110 for (const auto& data : face.sentData) {
111 response.wireDecode(data.getContent().blockFromValue());
112 }
113 return response.getCode();
114 }
115
116 bool
117 checkPrefix(const std::string prefixName)
118 {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400119 bpt::ptree m_savePrefix;
120 bpt::read_info(testConfFile, m_savePrefix);
121
Saurab Dulal7526cee2018-01-31 18:14:10 +0000122 // counter helps to check if multiple prefix of same name exists on conf file
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400123 counter = 0;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000124 for (const auto& section : m_savePrefix.get_child("advertising")) {
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400125 auto b = section.second.get_value<std::string>();
Saurab Dulal7526cee2018-01-31 18:14:10 +0000126 if (b == prefixName) {
127 counter++;
128 }
129 }
Davide Pesavento7bc3d432021-10-25 21:08:04 -0400130 return counter > 0;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000131 }
132
133 ndn::Interest
134 advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG)
135 {
136 ndn::nfd::ControlParameters parameters;
137 parameters.setName(prefixName);
Davide Pesavento6184c202021-05-17 02:28:03 -0400138 if (P_FLAG) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139 parameters.setFlags(update::PREFIX_FLAG);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000140 }
141 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
142 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Davide Pesavento6184c202021-05-17 02:28:03 -0400143 ndn::security::InterestSigner signer(m_keyChain);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000144 // type true for advertise, else withdraw
145 if (type == "advertise") {
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400146 advertiseCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
Davide Pesavento6184c202021-05-17 02:28:03 -0400147 return signer.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000148 }
149 else {
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400150 withdrawCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode());
Davide Pesavento6184c202021-05-17 02:28:03 -0400151 return signer.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000152 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000153 }
154
155public:
Junxiao Shi43f37a02023-08-09 00:09:00 +0000156 ndn::DummyClientFace face;
Saurab Dulal427e0122019-11-28 11:58:02 -0600157 ndn::Name siteIdentityName, routerIdName;
158 ndn::security::pib::Identity siteIdentity, routerId;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000159
160 ndn::Name opIdentityName;
161 ndn::security::pib::Identity opIdentity;
162
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600163 std::string testConfFile;
164 ConfParameter conf;
165 DummyConfFileProcessor confProcessor;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000166 Nlsr nlsr;
167 const boost::filesystem::path SITE_CERT_PATH;
168 ndn::Name sessionTime;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000169 int counter;
170};
171
172BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture)
173
174BOOST_AUTO_TEST_CASE(Basic)
175{
Saurab Dulal7526cee2018-01-31 18:14:10 +0000176 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
177 this->advanceClocks(ndn::time::milliseconds(10));
178 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
179 BOOST_CHECK_EQUAL(getResponseCode(), 200);
180 face.sentData.clear();
181
182 // trying to re-advertise
183 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
184 this->advanceClocks(ndn::time::milliseconds(10));
185 BOOST_CHECK_EQUAL(getResponseCode(), 204);
186 face.sentData.clear();
187
188 // only withdraw
189 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
190 this->advanceClocks(ndn::time::milliseconds(10));
191 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
192 BOOST_CHECK_EQUAL(getResponseCode(), 200);
193 face.sentData.clear();
194
195 // trying to re-advertise
196 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
197 this->advanceClocks(ndn::time::milliseconds(10));
198 BOOST_CHECK_EQUAL(getResponseCode(), 204);
199 face.sentData.clear();
200}
201
202BOOST_AUTO_TEST_CASE(PrefixStillSavedAfterJustWithdrawn)
203{
204 // advertise and save
205 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
206 this->advanceClocks(ndn::time::milliseconds(10));
207 face.sentData.clear();
208 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
209
210 // trying to advertise same name prefix
dulalsaurab82a34c22019-02-04 17:31:21 +0000211 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000212 this->advanceClocks(ndn::time::milliseconds(10));
213 BOOST_REQUIRE(counter == 1);
214 BOOST_CHECK_EQUAL(getResponseCode(), 406);
215 face.sentData.clear();
216
217 // only withdraw
218 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
219 this->advanceClocks(ndn::time::milliseconds(10));
220 // after withdrawing only prefix should still be there
221 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
222 BOOST_CHECK_EQUAL(getResponseCode(), 200);
223
224 // delete
225 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", true));
226 this->advanceClocks(ndn::time::milliseconds(10));
227 // after withdrawn delete prefix should be deleted from the file
228 BOOST_CHECK_EQUAL(getResponseCode(), 205);
dulalsaurab82a34c22019-02-04 17:31:21 +0000229 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000230}
231
232BOOST_AUTO_TEST_SUITE_END()
233
Davide Pesavento288141a2024-02-13 17:30:35 -0500234} // namespace nlsr::tests