Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, The University of Memphis, |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 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 | |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 25 | #include "tests/control-commands.hpp" |
| 26 | #include "tests/test-common.hpp" |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 27 | #include "conf-parameter.hpp" |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 28 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/security/command-interest-signer.hpp> |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/security/pib/identity.hpp> |
| 32 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 33 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 34 | #include <boost/filesystem.hpp> |
| 35 | |
| 36 | namespace nlsr { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 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"))) |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 49 | , testConfFile("/tmp/nlsr.conf.test") |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 50 | , conf(face, m_keyChain, testConfFile) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 51 | , confProcessor(conf) |
| 52 | , nlsr(face, m_keyChain, conf) |
| 53 | , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert")) |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 54 | , counter(0) |
| 55 | { |
| 56 | std::ifstream source("/usr/local/etc/ndn/nlsr.conf.sample", std::ios::binary); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 57 | std::ofstream destination(testConfFile, std::ios::binary); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 58 | destination << source.rdbuf(); |
| 59 | source.close(); |
| 60 | destination.close(); |
| 61 | |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 62 | conf.setConfFileNameDynamic(testConfFile); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 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") { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 78 | 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 Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 82 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 85 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 86 | inputFile.close(); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 87 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 88 | // Site cert |
| 89 | siteIdentity = addIdentity(siteIdentityName); |
| 90 | saveCertificate(siteIdentity, SITE_CERT_PATH.string()); |
| 91 | |
| 92 | // Operator cert |
| 93 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 94 | |
| 95 | // Create certificate and load it to the validator |
| 96 | conf.initializeKey(); |
| 97 | conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate()); |
| 98 | conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 99 | |
| 100 | // Set the network so the LSA prefix is constructed |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 101 | addIdentity(conf.getRouterPrefix()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 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 | { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 145 | parameters.setFlags(update::PREFIX_FLAG); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 146 | } |
| 147 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
| 148 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 149 | ndn::security::CommandInterestSigner cis(m_keyChain); |
| 150 | // type true for advertise, else withdraw |
| 151 | if (type == "advertise") { |
| 152 | advertiseCommand.append(parameters.wireEncode()); |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 153 | return cis.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 154 | } |
| 155 | else { |
| 156 | withdrawCommand.append(parameters.wireEncode()); |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 157 | return cis.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 158 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public: |
| 162 | ndn::util::DummyClientFace face; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame^] | 163 | ndn::Name siteIdentityName, routerIdName; |
| 164 | ndn::security::pib::Identity siteIdentity, routerId; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 165 | |
| 166 | ndn::Name opIdentityName; |
| 167 | ndn::security::pib::Identity opIdentity; |
| 168 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 169 | std::string testConfFile; |
| 170 | ConfParameter conf; |
| 171 | DummyConfFileProcessor confProcessor; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 172 | Nlsr nlsr; |
| 173 | const boost::filesystem::path SITE_CERT_PATH; |
| 174 | ndn::Name sessionTime; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 175 | int counter; |
| 176 | }; |
| 177 | |
| 178 | BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture) |
| 179 | |
| 180 | BOOST_AUTO_TEST_CASE(Basic) |
| 181 | { |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 182 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 183 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false)); |
| 184 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 185 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false); |
| 186 | BOOST_CHECK_EQUAL(getResponseCode(), 200); |
| 187 | face.sentData.clear(); |
| 188 | |
| 189 | // trying to re-advertise |
| 190 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false)); |
| 191 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 192 | BOOST_CHECK_EQUAL(getResponseCode(), 204); |
| 193 | face.sentData.clear(); |
| 194 | |
| 195 | // only withdraw |
| 196 | face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false)); |
| 197 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 198 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false); |
| 199 | BOOST_CHECK_EQUAL(getResponseCode(), 200); |
| 200 | face.sentData.clear(); |
| 201 | |
| 202 | // trying to re-advertise |
| 203 | face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false)); |
| 204 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 205 | BOOST_CHECK_EQUAL(getResponseCode(), 204); |
| 206 | face.sentData.clear(); |
| 207 | } |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(PrefixStillSavedAfterJustWithdrawn) |
| 210 | { |
| 211 | // advertise and save |
| 212 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true)); |
| 213 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 214 | face.sentData.clear(); |
| 215 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true); |
| 216 | |
| 217 | // trying to advertise same name prefix |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 218 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 219 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 220 | BOOST_REQUIRE(counter == 1); |
| 221 | BOOST_CHECK_EQUAL(getResponseCode(), 406); |
| 222 | face.sentData.clear(); |
| 223 | |
| 224 | // only withdraw |
| 225 | face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", false)); |
| 226 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 227 | // after withdrawing only prefix should still be there |
| 228 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), true); |
| 229 | BOOST_CHECK_EQUAL(getResponseCode(), 200); |
| 230 | |
| 231 | // delete |
| 232 | face.receive(advertiseWithdraw("/prefix/to/save", "withdraw", true)); |
| 233 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 234 | // after withdrawn delete prefix should be deleted from the file |
| 235 | BOOST_CHECK_EQUAL(getResponseCode(), 205); |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() |
| 240 | |
| 241 | } // namespace test |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 242 | } // namespace nlsr |