Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 1 | /* -*- 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 Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 23 | #include "nlsr.hpp" |
| 24 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame^] | 25 | #include "../control-commands.hpp" |
| 26 | #include "../test-common.hpp" |
| 27 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame^] | 29 | #include <ndn-cxx/security/command-interest-signer.hpp> |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/security/pib/identity.hpp> |
| 31 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame^] | 32 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 33 | #include <boost/filesystem.hpp> |
| 34 | |
| 35 | namespace nlsr { |
| 36 | namespace update { |
| 37 | namespace test { |
| 38 | |
| 39 | namespace pt = boost::property_tree; |
| 40 | using namespace pt; |
| 41 | |
| 42 | class PrefixSaveDeleteFixture : public nlsr::test::UnitTestTimeFixture |
| 43 | { |
| 44 | public: |
| 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"))) |
| 49 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
| 50 | , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert")) |
| 51 | , testConfFile("/tmp/nlsr.conf.test") |
| 52 | , counter(0) |
| 53 | { |
| 54 | std::ifstream source("/usr/local/etc/ndn/nlsr.conf.sample", std::ios::binary); |
| 55 | std::ofstream destination("/tmp/nlsr.conf.test", std::ios::binary); |
| 56 | destination << source.rdbuf(); |
| 57 | source.close(); |
| 58 | destination.close(); |
| 59 | |
| 60 | nlsr.setConfFileName(testConfFile); |
| 61 | 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") { |
| 76 | auto it = section.second.begin(); |
| 77 | it++; |
| 78 | if (it != pt.end() && it->first == "prefix-update-validator") { |
| 79 | nlsr.getPrefixUpdateProcessor().loadValidator(it->second, std::string(testConfFile)); |
| 80 | } |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | inputFile.close(); |
| 85 | nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate()); |
| 86 | // Site cert |
| 87 | siteIdentity = addIdentity(siteIdentityName); |
| 88 | saveCertificate(siteIdentity, SITE_CERT_PATH.string()); |
| 89 | |
| 90 | // Operator cert |
| 91 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
| 92 | nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate()); |
| 93 | |
| 94 | // Set the network so the LSA prefix is constructed |
| 95 | nlsr.getConfParameter().setNetwork("/ndn"); |
| 96 | nlsr.getConfParameter().setSiteName("/edu/test-site"); |
| 97 | nlsr.getConfParameter().setRouterName("/%C1.Router/this-router"); |
| 98 | nlsr.getConfParameter().buildRouterPrefix(); |
| 99 | addIdentity(nlsr.getConfParameter().getRouterPrefix()); |
| 100 | |
| 101 | // Initialize NLSR so a sync socket is created |
| 102 | nlsr.initialize(); |
| 103 | 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); |
| 141 | if (P_FLAG) |
| 142 | { |
| 143 | parameters.setFlags(PREFIX_FLAG); |
| 144 | } |
| 145 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
| 146 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 147 | ndn::security::CommandInterestSigner cis(m_keyChain); |
| 148 | // type true for advertise, else withdraw |
| 149 | if (type == "advertise") { |
| 150 | advertiseCommand.append(parameters.wireEncode()); |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame^] | 151 | return cis.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 152 | } |
| 153 | else { |
| 154 | withdrawCommand.append(parameters.wireEncode()); |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame^] | 155 | return cis.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 156 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | public: |
| 160 | ndn::util::DummyClientFace face; |
| 161 | ndn::Name siteIdentityName; |
| 162 | ndn::security::pib::Identity siteIdentity; |
| 163 | |
| 164 | ndn::Name opIdentityName; |
| 165 | ndn::security::pib::Identity opIdentity; |
| 166 | |
| 167 | Nlsr nlsr; |
| 168 | const boost::filesystem::path SITE_CERT_PATH; |
| 169 | ndn::Name sessionTime; |
| 170 | std::string testConfFile; |
| 171 | int counter; |
| 172 | }; |
| 173 | |
| 174 | BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture) |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE(Basic) |
| 177 | { |
| 178 | // only advertise |
| 179 | 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 | |
| 205 | BOOST_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 |
| 214 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true)); |
| 215 | 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); |
| 232 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_SUITE_END() |
| 236 | |
| 237 | } // namespace test |
| 238 | } // namespace update |
| 239 | } // namespace nlsr |