Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4f3d79d | 2024-04-19 22:44:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 23 | #include "ndn-cxx/mgmt/nfd/controller.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/test-common.hpp" |
| 26 | #include "tests/unit/io-key-chain-fixture.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 29 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Util) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 31 | BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 32 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(ProcessEventsOverride) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 34 | { |
| 35 | bool isOverrideInvoked = false; |
| 36 | auto override = [&] (time::milliseconds timeout) { |
| 37 | isOverrideInvoked = true; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(timeout, 200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 41 | DummyClientFace face(m_io, {false, false, override}); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 42 | face.processEvents(200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 43 | BOOST_CHECK(isOverrideInvoked); |
| 44 | } |
| 45 | |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 46 | BOOST_AUTO_TEST_CASE(RegistrationReply) |
| 47 | { |
| 48 | DummyClientFace::Options opts; |
| 49 | opts.enableRegistrationReply = true; |
| 50 | opts.registrationReplyFaceId = 3001; |
| 51 | DummyClientFace face(m_io, m_keyChain, opts); |
| 52 | |
| 53 | ndn::nfd::Controller controller(face, m_keyChain); |
| 54 | ndn::nfd::ControlParameters params; |
| 55 | bool didRibRegisterSucceed = false; |
| 56 | controller.start<ndn::nfd::RibRegisterCommand>( |
| 57 | ndn::nfd::ControlParameters() |
| 58 | .setName("/Q") |
| 59 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR) |
| 60 | .setCost(2400) |
| 61 | .setFlags(0), |
| 62 | [&] (const ndn::nfd::ControlParameters& p) { |
| 63 | BOOST_CHECK_EQUAL(p.getName(), "/Q"); |
| 64 | BOOST_CHECK_EQUAL(p.getFaceId(), 3001); |
| 65 | BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR); |
| 66 | BOOST_CHECK_EQUAL(p.getCost(), 2400); |
| 67 | BOOST_CHECK_EQUAL(p.getFlags(), 0); |
| 68 | didRibRegisterSucceed = true; |
| 69 | }, |
| 70 | [] (const ndn::nfd::ControlResponse& r) { |
| 71 | BOOST_TEST_FAIL("RibRegisterCommand failed " << r); |
| 72 | }); |
| 73 | advanceClocks(1_ms, 2); |
| 74 | BOOST_CHECK(didRibRegisterSucceed); |
| 75 | |
| 76 | bool didRibUnregisterSucceed = false; |
| 77 | controller.start<ndn::nfd::RibUnregisterCommand>( |
| 78 | ndn::nfd::ControlParameters() |
| 79 | .setName("/Q") |
| 80 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR), |
| 81 | [&] (const ndn::nfd::ControlParameters& p) { |
| 82 | BOOST_CHECK_EQUAL(p.getName(), "/Q"); |
| 83 | BOOST_CHECK_EQUAL(p.getFaceId(), 3001); |
| 84 | BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR); |
| 85 | didRibUnregisterSucceed = true; |
| 86 | }, |
| 87 | [] (const ndn::nfd::ControlResponse& r) { |
| 88 | BOOST_TEST_FAIL("RibUnregisterCommand failed " << r); |
| 89 | }); |
| 90 | advanceClocks(1_ms, 2); |
| 91 | BOOST_CHECK(didRibUnregisterSucceed); |
| 92 | } |
| 93 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(BroadcastLink) |
| 95 | { |
Davide Pesavento | 4f3d79d | 2024-04-19 22:44:02 -0400 | [diff] [blame] | 96 | DummyClientFace face1(m_io, m_keyChain, {true, true}); |
| 97 | DummyClientFace face2(m_io, m_keyChain, {true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 98 | face1.linkTo(face2); |
| 99 | |
| 100 | int nFace1Interest = 0; |
| 101 | int nFace2Interest = 0; |
| 102 | face1.setInterestFilter("/face1", |
| 103 | [&] (const InterestFilter&, const Interest& interest) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(interest.getName(), "/face1/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 105 | nFace1Interest++; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 106 | face1.put(makeNack(interest, lp::NackReason::NO_ROUTE)); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 107 | }, nullptr, nullptr); |
| 108 | face2.setInterestFilter("/face2", |
| 109 | [&] (const InterestFilter&, const Interest& interest) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(interest.getName(), "/face2/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 111 | nFace2Interest++; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 112 | face2.put(*makeData("/face2/data")); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 113 | return; |
| 114 | }, nullptr, nullptr); |
| 115 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 116 | advanceClocks(25_ms, 4); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 117 | |
| 118 | int nFace1Data = 0; |
| 119 | int nFace2Nack = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 120 | face1.expressInterest(*makeInterest("/face2/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 121 | [&] (const Interest& i, const Data& d) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(d.getName(), "/face2/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 123 | nFace1Data++; |
| 124 | }, nullptr, nullptr); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 125 | face2.expressInterest(*makeInterest("/face1/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 126 | [&] (const Interest& i, const Data& d) { |
| 127 | BOOST_CHECK(false); |
| 128 | }, |
| 129 | [&] (const Interest& i, const lp::Nack& n) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(n.getInterest().getName(), "/face1/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 131 | nFace2Nack++; |
| 132 | }, nullptr); |
| 133 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 134 | advanceClocks(10_ms, 100); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 135 | |
| 136 | BOOST_CHECK_EQUAL(nFace1Data, 1); |
| 137 | BOOST_CHECK_EQUAL(nFace2Nack, 1); |
| 138 | BOOST_CHECK_EQUAL(nFace1Interest, 1); |
| 139 | BOOST_CHECK_EQUAL(nFace2Interest, 1); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy) |
| 143 | { |
Davide Pesavento | 4f3d79d | 2024-04-19 22:44:02 -0400 | [diff] [blame] | 144 | DummyClientFace face1(m_io, m_keyChain, {true, true}); |
| 145 | DummyClientFace face2(m_io, m_keyChain, {true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 146 | |
| 147 | face1.linkTo(face2); |
| 148 | face2.unlink(); |
| 149 | BOOST_CHECK(face1.m_bcastLink == nullptr); |
| 150 | |
Davide Pesavento | 4f3d79d | 2024-04-19 22:44:02 -0400 | [diff] [blame] | 151 | DummyClientFace face3(m_io, m_keyChain, {true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 152 | face1.linkTo(face2); |
| 153 | face3.linkTo(face1); |
| 154 | face2.unlink(); |
| 155 | BOOST_CHECK(face1.m_bcastLink != nullptr); |
| 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(AlreadyLinkException) |
| 159 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 160 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 161 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 162 | DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 163 | DummyClientFace face4(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 164 | |
| 165 | face1.linkTo(face2); |
| 166 | face3.linkTo(face4); |
| 167 | |
| 168 | BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError); |
| 169 | } |
| 170 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 171 | BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace |
| 172 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 173 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 174 | } // namespace ndn::tests |