blob: ab6556441761ef2331bc0791d5b46308cc17da68 [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"
27
Saurab Dulal7526cee2018-01-31 18:14:10 +000028#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Junxiao Shi008c9b12019-01-13 23:15:56 +000029#include <ndn-cxx/security/command-interest-signer.hpp>
Saurab Dulal7526cee2018-01-31 18:14:10 +000030#include <ndn-cxx/security/pib/identity.hpp>
31#include <ndn-cxx/security/signing-helpers.hpp>
Junxiao Shi008c9b12019-01-13 23:15:56 +000032
Saurab Dulal7526cee2018-01-31 18:14:10 +000033#include <boost/filesystem.hpp>
34
35namespace nlsr {
Saurab Dulal7526cee2018-01-31 18:14:10 +000036namespace test {
37
38namespace pt = boost::property_tree;
39using namespace pt;
40
41class PrefixSaveDeleteFixture : public nlsr::test::UnitTestTimeFixture
42{
43public:
44 PrefixSaveDeleteFixture()
45 : face(m_ioService, m_keyChain, {true, true})
46 , 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")
Ashlesh Gawande85998a12017-12-07 22:22:13 -060049 , conf(face, testConfFile)
50 , 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 {
55 std::ifstream source("/usr/local/etc/ndn/nlsr.conf.sample", 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
Saurab Dulal7526cee2018-01-31 18:14:10 +000061 siteIdentity = addIdentity(siteIdentityName);
62 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
63
64 // Operator cert
65 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
66 // Loading the security section's validator part into the validator
67 // See conf file processor for more details
68 std::ifstream inputFile;
69 inputFile.open(testConfFile);
70 BOOST_REQUIRE(inputFile.is_open());
71 pt::ptree pt;
72 boost::property_tree::read_info(inputFile, pt);
73 // Loads section and file name
74 for (const auto& section : pt) {
75 if (section.first == "security") {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060076 for (const auto& it : section.second) {
77 if (it.first == "prefix-update-validator") {
78 conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf"));
79 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000080 }
Saurab Dulal7526cee2018-01-31 18:14:10 +000081 }
82 }
83 inputFile.close();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060084
Saurab Dulal7526cee2018-01-31 18:14:10 +000085 // Site cert
86 siteIdentity = addIdentity(siteIdentityName);
87 saveCertificate(siteIdentity, SITE_CERT_PATH.string());
88
89 // Operator cert
90 opIdentity = addSubCertificate(opIdentityName, siteIdentity);
91 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
92
93 // Set the network so the LSA prefix is constructed
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094 addIdentity(conf.getRouterPrefix());
Saurab Dulal7526cee2018-01-31 18:14:10 +000095
96 // Initialize NLSR so a sync socket is created
97 nlsr.initialize();
98 this->advanceClocks(ndn::time::milliseconds(10));
99 face.sentInterests.clear();
100 }
101
102 uint32_t
103 getResponseCode()
104 {
105 ndn::nfd::ControlResponse response;
106 for (const auto& data : face.sentData) {
107 response.wireDecode(data.getContent().blockFromValue());
108 }
109 return response.getCode();
110 }
111
112 bool
113 checkPrefix(const std::string prefixName)
114 {
115 counter = 0;
116 pt::ptree m_savePrefix;
117 pt::info_parser::read_info(testConfFile, m_savePrefix);
118 // counter helps to check if multiple prefix of same name exists on conf file
119 for (const auto& section : m_savePrefix.get_child("advertising")) {
120 std:: string b = section.second.get_value<std::string>();
121 if (b == prefixName) {
122 counter++;
123 }
124 }
125 if (counter > 0) {
126 return true;
127 }
128 return false;
129 }
130
131 ndn::Interest
132 advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG)
133 {
134 ndn::nfd::ControlParameters parameters;
135 parameters.setName(prefixName);
136 if (P_FLAG)
137 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600138 parameters.setFlags(update::PREFIX_FLAG);
Saurab Dulal7526cee2018-01-31 18:14:10 +0000139 }
140 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
141 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
Saurab Dulal7526cee2018-01-31 18:14:10 +0000142 ndn::security::CommandInterestSigner cis(m_keyChain);
143 // type true for advertise, else withdraw
144 if (type == "advertise") {
145 advertiseCommand.append(parameters.wireEncode());
Junxiao Shi008c9b12019-01-13 23:15:56 +0000146 return cis.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000147 }
148 else {
149 withdrawCommand.append(parameters.wireEncode());
Junxiao Shi008c9b12019-01-13 23:15:56 +0000150 return cis.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity));
Saurab Dulal7526cee2018-01-31 18:14:10 +0000151 }
Saurab Dulal7526cee2018-01-31 18:14:10 +0000152 }
153
154public:
155 ndn::util::DummyClientFace face;
156 ndn::Name siteIdentityName;
157 ndn::security::pib::Identity siteIdentity;
158
159 ndn::Name opIdentityName;
160 ndn::security::pib::Identity opIdentity;
161
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600162 std::string testConfFile;
163 ConfParameter conf;
164 DummyConfFileProcessor confProcessor;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000165 Nlsr nlsr;
166 const boost::filesystem::path SITE_CERT_PATH;
167 ndn::Name sessionTime;
Saurab Dulal7526cee2018-01-31 18:14:10 +0000168 int counter;
169};
170
171BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture)
172
173BOOST_AUTO_TEST_CASE(Basic)
174{
175 // only advertise
176 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
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600211 /*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);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);*/
Saurab Dulal7526cee2018-01-31 18:14:10 +0000230}
231
232BOOST_AUTO_TEST_SUITE_END()
233
234} // namespace test
Saurab Dulal7526cee2018-01-31 18:14:10 +0000235} // namespace nlsr