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 | /* |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | namespace tests { |
| 31 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 32 | using namespace ndn::tests; |
| 33 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Util) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 36 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(ProcessEventsOverride) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 38 | { |
| 39 | bool isOverrideInvoked = false; |
| 40 | auto override = [&] (time::milliseconds timeout) { |
| 41 | isOverrideInvoked = true; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(timeout, 200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 45 | DummyClientFace face(m_io, {false, false, override}); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 46 | face.processEvents(200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 47 | BOOST_CHECK(isOverrideInvoked); |
| 48 | } |
| 49 | |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(RegistrationReply) |
| 51 | { |
| 52 | DummyClientFace::Options opts; |
| 53 | opts.enableRegistrationReply = true; |
| 54 | opts.registrationReplyFaceId = 3001; |
| 55 | DummyClientFace face(m_io, m_keyChain, opts); |
| 56 | |
| 57 | ndn::nfd::Controller controller(face, m_keyChain); |
| 58 | ndn::nfd::ControlParameters params; |
| 59 | bool didRibRegisterSucceed = false; |
| 60 | controller.start<ndn::nfd::RibRegisterCommand>( |
| 61 | ndn::nfd::ControlParameters() |
| 62 | .setName("/Q") |
| 63 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR) |
| 64 | .setCost(2400) |
| 65 | .setFlags(0), |
| 66 | [&] (const ndn::nfd::ControlParameters& p) { |
| 67 | BOOST_CHECK_EQUAL(p.getName(), "/Q"); |
| 68 | BOOST_CHECK_EQUAL(p.getFaceId(), 3001); |
| 69 | BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR); |
| 70 | BOOST_CHECK_EQUAL(p.getCost(), 2400); |
| 71 | BOOST_CHECK_EQUAL(p.getFlags(), 0); |
| 72 | didRibRegisterSucceed = true; |
| 73 | }, |
| 74 | [] (const ndn::nfd::ControlResponse& r) { |
| 75 | BOOST_TEST_FAIL("RibRegisterCommand failed " << r); |
| 76 | }); |
| 77 | advanceClocks(1_ms, 2); |
| 78 | BOOST_CHECK(didRibRegisterSucceed); |
| 79 | |
| 80 | bool didRibUnregisterSucceed = false; |
| 81 | controller.start<ndn::nfd::RibUnregisterCommand>( |
| 82 | ndn::nfd::ControlParameters() |
| 83 | .setName("/Q") |
| 84 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR), |
| 85 | [&] (const ndn::nfd::ControlParameters& p) { |
| 86 | BOOST_CHECK_EQUAL(p.getName(), "/Q"); |
| 87 | BOOST_CHECK_EQUAL(p.getFaceId(), 3001); |
| 88 | BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR); |
| 89 | didRibUnregisterSucceed = true; |
| 90 | }, |
| 91 | [] (const ndn::nfd::ControlResponse& r) { |
| 92 | BOOST_TEST_FAIL("RibUnregisterCommand failed " << r); |
| 93 | }); |
| 94 | advanceClocks(1_ms, 2); |
| 95 | BOOST_CHECK(didRibUnregisterSucceed); |
| 96 | } |
| 97 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(BroadcastLink) |
| 99 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 100 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 101 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 102 | face1.linkTo(face2); |
| 103 | |
| 104 | int nFace1Interest = 0; |
| 105 | int nFace2Interest = 0; |
| 106 | face1.setInterestFilter("/face1", |
| 107 | [&] (const InterestFilter&, const Interest& interest) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(interest.getName(), "/face1/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 109 | nFace1Interest++; |
| 110 | face1.put(ndn::tests::makeNack(interest, lp::NackReason::NO_ROUTE)); |
| 111 | }, nullptr, nullptr); |
| 112 | face2.setInterestFilter("/face2", |
| 113 | [&] (const InterestFilter&, const Interest& interest) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(interest.getName(), "/face2/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 115 | nFace2Interest++; |
| 116 | face2.put(*ndn::tests::makeData("/face2/data")); |
| 117 | return; |
| 118 | }, nullptr, nullptr); |
| 119 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 120 | advanceClocks(25_ms, 4); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 121 | |
| 122 | int nFace1Data = 0; |
| 123 | int nFace2Nack = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 124 | face1.expressInterest(*makeInterest("/face2/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 125 | [&] (const Interest& i, const Data& d) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(d.getName(), "/face2/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 127 | nFace1Data++; |
| 128 | }, nullptr, nullptr); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 129 | face2.expressInterest(*makeInterest("/face1/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 130 | [&] (const Interest& i, const Data& d) { |
| 131 | BOOST_CHECK(false); |
| 132 | }, |
| 133 | [&] (const Interest& i, const lp::Nack& n) { |
Junxiao Shi | 0838f3d | 2022-03-01 01:55:05 +0000 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(n.getInterest().getName(), "/face1/data"); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 135 | nFace2Nack++; |
| 136 | }, nullptr); |
| 137 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 138 | advanceClocks(10_ms, 100); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 139 | |
| 140 | BOOST_CHECK_EQUAL(nFace1Data, 1); |
| 141 | BOOST_CHECK_EQUAL(nFace2Nack, 1); |
| 142 | BOOST_CHECK_EQUAL(nFace1Interest, 1); |
| 143 | BOOST_CHECK_EQUAL(nFace2Interest, 1); |
| 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy) |
| 147 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 148 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 149 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 150 | |
| 151 | face1.linkTo(face2); |
| 152 | face2.unlink(); |
| 153 | BOOST_CHECK(face1.m_bcastLink == nullptr); |
| 154 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 155 | DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 156 | face1.linkTo(face2); |
| 157 | face3.linkTo(face1); |
| 158 | face2.unlink(); |
| 159 | BOOST_CHECK(face1.m_bcastLink != nullptr); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(AlreadyLinkException) |
| 163 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 164 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 165 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 166 | DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 167 | DummyClientFace face4(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 168 | |
| 169 | face1.linkTo(face2); |
| 170 | face3.linkTo(face4); |
| 171 | |
| 172 | BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError); |
| 173 | } |
| 174 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace |
| 176 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 177 | |
| 178 | } // namespace tests |
| 179 | } // namespace util |
| 180 | } // namespace ndn |