blob: 7bbe06e4e17c435a46ded6f5f50463a8991e32b1 [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"
23#include "../control-commands.hpp"
24#include "../test-common.hpp"
25#include "nlsr.hpp"
26
27#include <ndn-cxx/interest.hpp>
28#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
29#include <ndn-cxx/mgmt/nfd/control-response.hpp>
30#include <ndn-cxx/security/key-chain.hpp>
31#include <ndn-cxx/security/pib/identity.hpp>
32#include <ndn-cxx/security/signing-helpers.hpp>
33#include <ndn-cxx/security/command-interest-signer.hpp>
34#include <ndn-cxx/util/dummy-client-face.hpp>
35#include <boost/filesystem.hpp>
36
37namespace nlsr {
38namespace update {
39namespace test {
40
41namespace pt = boost::property_tree;
42using namespace pt;
43
44class PrefixSaveDeleteFixture : public nlsr::test::UnitTestTimeFixture
45{
46public:
47 PrefixSaveDeleteFixture()
48 : face(m_ioService, m_keyChain, {true, true})
49 , siteIdentityName(ndn::Name("/edu/test-site"))
50 , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
51 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
52 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
53 , testConfFile("/tmp/nlsr.conf.test")
54 , counter(0)
55 {
56 std::ifstream source("/usr/local/etc/ndn/nlsr.conf.sample", std::ios::binary);
57 std::ofstream destination("/tmp/nlsr.conf.test", std::ios::binary);
58 destination << source.rdbuf();
59 source.close();
60 destination.close();
61
62 nlsr.setConfFileName(testConfFile);
63 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") {
78 auto it = section.second.begin();
79 it++;
80 if (it != pt.end() && it->first == "prefix-update-validator") {
81 nlsr.getPrefixUpdateProcessor().loadValidator(it->second, std::string(testConfFile));
82 }
83 break;
84 }
85 }
86 inputFile.close();
87 nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
88 // 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
97 nlsr.getConfParameter().setNetwork("/ndn");
98 nlsr.getConfParameter().setSiteName("/edu/test-site");
99 nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
100 nlsr.getConfParameter().buildRouterPrefix();
101 addIdentity(nlsr.getConfParameter().getRouterPrefix());
102
103 // Initialize NLSR so a sync socket is created
104 nlsr.initialize();
105 this->advanceClocks(ndn::time::milliseconds(10));
106 face.sentInterests.clear();
107 }
108
109 uint32_t
110 getResponseCode()
111 {
112 ndn::nfd::ControlResponse response;
113 for (const auto& data : face.sentData) {
114 response.wireDecode(data.getContent().blockFromValue());
115 }
116 return response.getCode();
117 }
118
119 bool
120 checkPrefix(const std::string prefixName)
121 {
122 counter = 0;
123 pt::ptree m_savePrefix;
124 pt::info_parser::read_info(testConfFile, m_savePrefix);
125 // counter helps to check if multiple prefix of same name exists on conf file
126 for (const auto& section : m_savePrefix.get_child("advertising")) {
127 std:: string b = section.second.get_value<std::string>();
128 if (b == prefixName) {
129 counter++;
130 }
131 }
132 if (counter > 0) {
133 return true;
134 }
135 return false;
136 }
137
138 ndn::Interest
139 advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG)
140 {
141 ndn::nfd::ControlParameters parameters;
142 parameters.setName(prefixName);
143 if (P_FLAG)
144 {
145 parameters.setFlags(PREFIX_FLAG);
146 }
147 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
148 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
149 ndn:: Interest interestName;
150 ndn::security::CommandInterestSigner cis(m_keyChain);
151 // type true for advertise, else withdraw
152 if (type == "advertise") {
153 advertiseCommand.append(parameters.wireEncode());
154 interestName = cis.makeCommandInterest(advertiseCommand,
155 ndn::security::signingByIdentity(opIdentity));
156 }
157 else {
158 withdrawCommand.append(parameters.wireEncode());
159 interestName = cis.makeCommandInterest(withdrawCommand,
160 ndn::security::signingByIdentity(opIdentity));
161 }
162 return interestName;
163 }
164
165public:
166 ndn::util::DummyClientFace face;
167 ndn::Name siteIdentityName;
168 ndn::security::pib::Identity siteIdentity;
169
170 ndn::Name opIdentityName;
171 ndn::security::pib::Identity opIdentity;
172
173 Nlsr nlsr;
174 const boost::filesystem::path SITE_CERT_PATH;
175 ndn::Name sessionTime;
176 std::string testConfFile;
177 int counter;
178};
179
180BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture)
181
182BOOST_AUTO_TEST_CASE(Basic)
183{
184 // only advertise
185 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
186 this->advanceClocks(ndn::time::milliseconds(10));
187 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
188 BOOST_CHECK_EQUAL(getResponseCode(), 200);
189 face.sentData.clear();
190
191 // trying to re-advertise
192 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
193 this->advanceClocks(ndn::time::milliseconds(10));
194 BOOST_CHECK_EQUAL(getResponseCode(), 204);
195 face.sentData.clear();
196
197 // only withdraw
198 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
199 this->advanceClocks(ndn::time::milliseconds(10));
200 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
201 BOOST_CHECK_EQUAL(getResponseCode(), 200);
202 face.sentData.clear();
203
204 // trying to re-advertise
205 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
206 this->advanceClocks(ndn::time::milliseconds(10));
207 BOOST_CHECK_EQUAL(getResponseCode(), 204);
208 face.sentData.clear();
209}
210
211BOOST_AUTO_TEST_CASE(PrefixStillSavedAfterJustWithdrawn)
212{
213 // advertise and save
214 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
215 this->advanceClocks(ndn::time::milliseconds(10));
216 face.sentData.clear();
217 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
218
219 // trying to advertise same name prefix
220 face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true));
221 this->advanceClocks(ndn::time::milliseconds(10));
222 BOOST_REQUIRE(counter == 1);
223 BOOST_CHECK_EQUAL(getResponseCode(), 406);
224 face.sentData.clear();
225
226 // only withdraw
227 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false));
228 this->advanceClocks(ndn::time::milliseconds(10));
229 // after withdrawing only prefix should still be there
230 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true);
231 BOOST_CHECK_EQUAL(getResponseCode(), 200);
232
233 // delete
234 face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", true));
235 this->advanceClocks(ndn::time::milliseconds(10));
236 // after withdrawn delete prefix should be deleted from the file
237 BOOST_CHECK_EQUAL(getResponseCode(), 205);
238 BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);
239}
240
241BOOST_AUTO_TEST_SUITE_END()
242
243} // namespace test
244} // namespace update
245} // namespace nlsr