Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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/>. |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 20 | */ |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 21 | |
| 22 | #include "update/prefix-update-processor.hpp" |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 23 | #include "conf-parameter.hpp" |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 24 | #include "nlsr.hpp" |
| 25 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 26 | #include "tests/io-key-chain-fixture.hpp" |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 27 | #include "tests/test-common.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> |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 30 | #include <ndn-cxx/security/interest-signer.hpp> |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/security/signing-helpers.hpp> |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 33 | #include <boost/filesystem/operations.hpp> |
| 34 | #include <boost/filesystem/path.hpp> |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 35 | #include <boost/property_tree/info_parser.hpp> |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 37 | namespace nlsr::tests { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 38 | |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 39 | namespace bpt = boost::property_tree; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 40 | |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 41 | class PrefixSaveDeleteFixture : public IoKeyChainFixture |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | PrefixSaveDeleteFixture() |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 45 | : face(m_io, m_keyChain, {true, true}) |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 46 | , siteIdentityName(ndn::Name("/edu/test-site")) |
| 47 | , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator"))) |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 48 | , testConfFile("/tmp/nlsr.conf.test") |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 49 | , conf(face, m_keyChain, testConfFile) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 50 | , confProcessor(conf) |
| 51 | , nlsr(face, m_keyChain, conf) |
| 52 | , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert")) |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 53 | , counter(0) |
| 54 | { |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame] | 55 | std::ifstream source("nlsr.conf", std::ios::binary); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 56 | std::ofstream destination(testConfFile, std::ios::binary); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 57 | destination << source.rdbuf(); |
| 58 | source.close(); |
| 59 | destination.close(); |
| 60 | |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 61 | conf.setConfFileNameDynamic(testConfFile); |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 62 | siteIdentity = m_keyChain.createIdentity(siteIdentityName); |
| 63 | saveIdentityCert(siteIdentity, SITE_CERT_PATH.string()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 64 | |
| 65 | // Operator cert |
| 66 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
| 67 | // Loading the security section's validator part into the validator |
| 68 | // See conf file processor for more details |
| 69 | std::ifstream inputFile; |
| 70 | inputFile.open(testConfFile); |
| 71 | BOOST_REQUIRE(inputFile.is_open()); |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 72 | bpt::ptree pt; |
| 73 | bpt::read_info(inputFile, pt); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 74 | // Loads section and file name |
| 75 | for (const auto& section : pt) { |
| 76 | if (section.first == "security") { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 77 | for (const auto& it : section.second) { |
| 78 | if (it.first == "prefix-update-validator") { |
| 79 | conf.getPrefixUpdateValidator().load(it.second, std::string("nlsr.conf")); |
| 80 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 81 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 84 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 85 | inputFile.close(); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 86 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 87 | // Site cert |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 88 | siteIdentity = m_keyChain.createIdentity(siteIdentityName); |
| 89 | saveIdentityCert(siteIdentity, SITE_CERT_PATH.string()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 90 | |
| 91 | // Operator cert |
| 92 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 93 | |
| 94 | // Create certificate and load it to the validator |
| 95 | conf.initializeKey(); |
| 96 | conf.loadCertToValidator(siteIdentity.getDefaultKey().getDefaultCertificate()); |
| 97 | conf.loadCertToValidator(opIdentity.getDefaultKey().getDefaultCertificate()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 98 | |
| 99 | // Set the network so the LSA prefix is constructed |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 100 | m_keyChain.createIdentity(conf.getRouterPrefix()); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 101 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 102 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 103 | face.sentInterests.clear(); |
| 104 | } |
| 105 | |
| 106 | uint32_t |
| 107 | getResponseCode() |
| 108 | { |
| 109 | ndn::nfd::ControlResponse response; |
| 110 | for (const auto& data : face.sentData) { |
| 111 | response.wireDecode(data.getContent().blockFromValue()); |
| 112 | } |
| 113 | return response.getCode(); |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | checkPrefix(const std::string prefixName) |
| 118 | { |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 119 | bpt::ptree m_savePrefix; |
| 120 | bpt::read_info(testConfFile, m_savePrefix); |
| 121 | |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 122 | // counter helps to check if multiple prefix of same name exists on conf file |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 123 | counter = 0; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 124 | for (const auto& section : m_savePrefix.get_child("advertising")) { |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 125 | auto b = section.second.get_value<std::string>(); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 126 | if (b == prefixName) { |
| 127 | counter++; |
| 128 | } |
| 129 | } |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame] | 130 | return counter > 0; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ndn::Interest |
| 134 | advertiseWithdraw(std::string prefixName, std::string type, bool P_FLAG) |
| 135 | { |
| 136 | ndn::nfd::ControlParameters parameters; |
| 137 | parameters.setName(prefixName); |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 138 | if (P_FLAG) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 139 | parameters.setFlags(update::PREFIX_FLAG); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 140 | } |
| 141 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
| 142 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 143 | ndn::security::InterestSigner signer(m_keyChain); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 144 | // type true for advertise, else withdraw |
| 145 | if (type == "advertise") { |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 146 | advertiseCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode()); |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 147 | return signer.makeCommandInterest(advertiseCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 148 | } |
| 149 | else { |
Davide Pesavento | e28d875 | 2022-03-19 03:55:25 -0400 | [diff] [blame] | 150 | withdrawCommand.append(ndn::tlv::GenericNameComponent, parameters.wireEncode()); |
Davide Pesavento | 6184c20 | 2021-05-17 02:28:03 -0400 | [diff] [blame] | 151 | return signer.makeCommandInterest(withdrawCommand, ndn::security::signingByIdentity(opIdentity)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 152 | } |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public: |
Junxiao Shi | 43f37a0 | 2023-08-09 00:09:00 +0000 | [diff] [blame] | 156 | ndn::DummyClientFace face; |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 157 | ndn::Name siteIdentityName, routerIdName; |
| 158 | ndn::security::pib::Identity siteIdentity, routerId; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 159 | |
| 160 | ndn::Name opIdentityName; |
| 161 | ndn::security::pib::Identity opIdentity; |
| 162 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 163 | std::string testConfFile; |
| 164 | ConfParameter conf; |
| 165 | DummyConfFileProcessor confProcessor; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 166 | Nlsr nlsr; |
| 167 | const boost::filesystem::path SITE_CERT_PATH; |
| 168 | ndn::Name sessionTime; |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 169 | int counter; |
| 170 | }; |
| 171 | |
| 172 | BOOST_FIXTURE_TEST_SUITE(TestAdvertiseWithdrawPrefix, PrefixSaveDeleteFixture) |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(Basic) |
| 175 | { |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 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 | |
| 202 | BOOST_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 |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 211 | face.receive(advertiseWithdraw("/prefix/to/save", "advertise", true)); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 212 | 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); |
dulalsaurab | 82a34c2 | 2019-02-04 17:31:21 +0000 | [diff] [blame] | 229 | BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false); |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_SUITE_END() |
| 233 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 234 | } // namespace nlsr::tests |