blob: 10988bf25dd562f93e46c8e916d13acb23c1a9f1 [file] [log] [blame]
Saurab Dulal7526cee2018-01-31 18:14:10 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2019, 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 "update/prefix-update-processor.hpp"
Saurab Dulal7526cee2018-01-31 18:14:10 +000023#include "nlsr.hpp"
24
Junxiao Shi008c9b12019-01-13 23:15:56 +000025#include "../control-commands.hpp"
26#include "../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>
Junxiao Shi008c9b12019-01-13 23:15:56 +000030#include <ndn-cxx/security/command-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")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060050 , conf(face, testConfFile)
51 , 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 {
56 std::ifstream source("/usr/local/etc/ndn/nlsr.conf.sample", 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);
94 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
95
96 // Set the network so the LSA prefix is constructed
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 addIdentity(conf.getRouterPrefix());
Saurab Dulal7526cee2018-01-31 18:14:10 +000098
99 // Initialize NLSR so a sync socket is created
100 nlsr.initialize();
101 this->advanceClocks(ndn::time::milliseconds(10));
102 face.sentInterests.clear();
103 }
104
105 uint32_t
106 getResponseCode()
107 {
108 ndn::nfd::ControlResponse response;
109 for (const auto& data : face.sentData) {
110 response.wireDecode(data.getContent().blockFromValue());
111 }
112 return response.getCode();
113 }
114
115 bool
116 checkPrefix(const std::string prefixName)
117 {
118 counter = 0;
119 pt::ptree m_savePrefix;
120 pt::info_parser::read_info(testConfFile, m_savePrefix);
121 // counter helps to check if multiple prefix of same name exists on conf file
122 for (const auto& section : m_savePrefix.get_child("advertising")) {
123 std:: string b = section.second.get_value<std::string>();
124 if (b == prefixName) {
125 counter++;
126 }
127 }
128 if (counter > 0) {
129 return true;
130 }
131 return false;
132 }
133
134 ndn::Interest
135 advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG)
136 {
137 ndn::nfd::ControlParameters parameters;
138 parameters.setName(prefixName);
139 if (P_FLAG)
140 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600141 parameters.setFlags(update::PREFIX_FLAG);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000142 }
143 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
144 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Saurab Dulal7526cee2018-01-31 18:14:10 +0000145 ndn::security::CommandInterestSigner cis(m_keyChain);
146 // type true for advertise, else withdraw
147 if (type == "advertise") {
148 advertiseCommand.append(parameters.wireEncode());
Junxiao Shi008c9b12019-01-13 23:15:56 +0000149 return cis.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000150 }
151 else {
152 withdrawCommand.append(parameters.wireEncode());
Junxiao Shi008c9b12019-01-13 23:15:56 +0000153 return cis.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000154 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000155 }
156
157public:
158 ndn::util::DummyClientFace face;
159 ndn::Name siteIdentityName;
160 ndn::security::pib::Identity siteIdentity;
161
162 ndn::Name opIdentityName;
163 ndn::security::pib::Identity opIdentity;
164
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600165 std::string testConfFile;
166 ConfParameter conf;
167 DummyConfFileProcessor confProcessor;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000168 Nlsr nlsr;
169 const boost::filesystem::path SITE_CERT_PATH;
170 ndn::Name sessionTime;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000171 int counter;
172};
173
174BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture)
175
176BOOST_AUTO_TEST_CASE(Basic)
177{
dulalsaurab82a34c22019-02-04 17:31:21 +0000178
Saurab Dulal7526cee2018-01-31 18:14:10 +0000179 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
180 this->advanceClocks(ndn::time::milliseconds(10));
181 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
182 BOOST_CHECK_EQUAL(getResponseCode(), 200);
183 face.sentData.clear();
184
185 // trying to re-advertise
186 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
187 this->advanceClocks(ndn::time::milliseconds(10));
188 BOOST_CHECK_EQUAL(getResponseCode(), 204);
189 face.sentData.clear();
190
191 // only withdraw
192 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
193 this->advanceClocks(ndn::time::milliseconds(10));
194 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
195 BOOST_CHECK_EQUAL(getResponseCode(), 200);
196 face.sentData.clear();
197
198 // trying to re-advertise
199 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
200 this->advanceClocks(ndn::time::milliseconds(10));
201 BOOST_CHECK_EQUAL(getResponseCode(), 204);
202 face.sentData.clear();
203}
204
205BOOST_AUTO_TEST_CASE(PrefixStillSavedAfterJustWithdrawn)
206{
207 // advertise and save
208 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
209 this->advanceClocks(ndn::time::milliseconds(10));
210 face.sentData.clear();
211 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
212
213 // trying to advertise same name prefix
dulalsaurab82a34c22019-02-04 17:31:21 +0000214 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000215 this->advanceClocks(ndn::time::milliseconds(10));
216 BOOST_REQUIRE(counter == 1);
217 BOOST_CHECK_EQUAL(getResponseCode(), 406);
218 face.sentData.clear();
219
220 // only withdraw
221 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
222 this->advanceClocks(ndn::time::milliseconds(10));
223 // after withdrawing only prefix should still be there
224 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
225 BOOST_CHECK_EQUAL(getResponseCode(), 200);
226
227 // delete
228 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", true));
229 this->advanceClocks(ndn::time::milliseconds(10));
230 // after withdrawn delete prefix should be deleted from the file
231 BOOST_CHECK_EQUAL(getResponseCode(), 205);
dulalsaurab82a34c22019-02-04 17:31:21 +0000232 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000233}
234
235BOOST_AUTO_TEST_SUITE_END()
236
237} // namespace test
Saurab Dulal7526cee2018-01-31 18:14:10 +0000238} // namespace nlsr