blob: 8f40f4cfb1d91d47fc11fb19738e6f1645c1ef63 [file] [log] [blame]
Junxiao Shic828dfc2016-09-15 13:26:22 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang34429cc2017-01-05 17:07:28 -08002/*
Davide Pesaventoaebf5d72024-12-26 16:04:12 -05003 * Copyright (c) 2013-2025 Regents of the University of California.
Junxiao Shic828dfc2016-09-15 13:26:22 +00004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/util/dummy-client-face.hpp"
Davide Pesaventoaebf5d72024-12-26 16:04:12 -050023#include "ndn-cxx/mgmt/nfd/control-command.hpp"
Junxiao Shi0838f3d2022-03-01 01:55:05 +000024#include "ndn-cxx/mgmt/nfd/controller.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000025
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "tests/test-common.hpp"
27#include "tests/unit/io-key-chain-fixture.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Junxiao Shib55e5d32018-07-18 13:32:00 -060030
Junxiao Shic828dfc2016-09-15 13:26:22 +000031BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050032BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture)
Junxiao Shic828dfc2016-09-15 13:26:22 +000033
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080034BOOST_AUTO_TEST_CASE(ProcessEventsOverride)
Junxiao Shic828dfc2016-09-15 13:26:22 +000035{
36 bool isOverrideInvoked = false;
37 auto override = [&] (time::milliseconds timeout) {
38 isOverrideInvoked = true;
Davide Pesavento0f830802018-01-16 23:58:58 -050039 BOOST_CHECK_EQUAL(timeout, 200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000040 };
41
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050042 DummyClientFace face(m_io, {false, false, override});
Davide Pesavento0f830802018-01-16 23:58:58 -050043 face.processEvents(200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000044 BOOST_CHECK(isOverrideInvoked);
45}
46
Junxiao Shi0838f3d2022-03-01 01:55:05 +000047BOOST_AUTO_TEST_CASE(RegistrationReply)
48{
49 DummyClientFace::Options opts;
50 opts.enableRegistrationReply = true;
51 opts.registrationReplyFaceId = 3001;
52 DummyClientFace face(m_io, m_keyChain, opts);
53
54 ndn::nfd::Controller controller(face, m_keyChain);
55 ndn::nfd::ControlParameters params;
56 bool didRibRegisterSucceed = false;
57 controller.start<ndn::nfd::RibRegisterCommand>(
58 ndn::nfd::ControlParameters()
59 .setName("/Q")
60 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR)
61 .setCost(2400)
62 .setFlags(0),
63 [&] (const ndn::nfd::ControlParameters& p) {
64 BOOST_CHECK_EQUAL(p.getName(), "/Q");
65 BOOST_CHECK_EQUAL(p.getFaceId(), 3001);
66 BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR);
67 BOOST_CHECK_EQUAL(p.getCost(), 2400);
68 BOOST_CHECK_EQUAL(p.getFlags(), 0);
69 didRibRegisterSucceed = true;
70 },
71 [] (const ndn::nfd::ControlResponse& r) {
72 BOOST_TEST_FAIL("RibRegisterCommand failed " << r);
73 });
74 advanceClocks(1_ms, 2);
75 BOOST_CHECK(didRibRegisterSucceed);
76
77 bool didRibUnregisterSucceed = false;
78 controller.start<ndn::nfd::RibUnregisterCommand>(
79 ndn::nfd::ControlParameters()
80 .setName("/Q")
81 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR),
82 [&] (const ndn::nfd::ControlParameters& p) {
83 BOOST_CHECK_EQUAL(p.getName(), "/Q");
84 BOOST_CHECK_EQUAL(p.getFaceId(), 3001);
85 BOOST_CHECK_EQUAL(p.getOrigin(), ndn::nfd::ROUTE_ORIGIN_NLSR);
86 didRibUnregisterSucceed = true;
87 },
88 [] (const ndn::nfd::ControlResponse& r) {
89 BOOST_TEST_FAIL("RibUnregisterCommand failed " << r);
90 });
91 advanceClocks(1_ms, 2);
92 BOOST_CHECK(didRibUnregisterSucceed);
93}
94
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080095BOOST_AUTO_TEST_CASE(BroadcastLink)
96{
Davide Pesavento4f3d79d2024-04-19 22:44:02 -040097 DummyClientFace face1(m_io, m_keyChain, {true, true});
98 DummyClientFace face2(m_io, m_keyChain, {true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080099 face1.linkTo(face2);
100
101 int nFace1Interest = 0;
102 int nFace2Interest = 0;
103 face1.setInterestFilter("/face1",
104 [&] (const InterestFilter&, const Interest& interest) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000105 BOOST_CHECK_EQUAL(interest.getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800106 nFace1Interest++;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400107 face1.put(makeNack(interest, lp::NackReason::NO_ROUTE));
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800108 }, nullptr, nullptr);
109 face2.setInterestFilter("/face2",
110 [&] (const InterestFilter&, const Interest& interest) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000111 BOOST_CHECK_EQUAL(interest.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800112 nFace2Interest++;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400113 face2.put(*makeData("/face2/data"));
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800114 return;
115 }, nullptr, nullptr);
116
Davide Pesavento0f830802018-01-16 23:58:58 -0500117 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800118
119 int nFace1Data = 0;
120 int nFace2Nack = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600121 face1.expressInterest(*makeInterest("/face2/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800122 [&] (const Interest& i, const Data& d) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000123 BOOST_CHECK_EQUAL(d.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800124 nFace1Data++;
125 }, nullptr, nullptr);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600126 face2.expressInterest(*makeInterest("/face1/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800127 [&] (const Interest& i, const Data& d) {
128 BOOST_CHECK(false);
129 },
130 [&] (const Interest& i, const lp::Nack& n) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000131 BOOST_CHECK_EQUAL(n.getInterest().getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800132 nFace2Nack++;
133 }, nullptr);
134
Davide Pesavento0f830802018-01-16 23:58:58 -0500135 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800136
137 BOOST_CHECK_EQUAL(nFace1Data, 1);
138 BOOST_CHECK_EQUAL(nFace2Nack, 1);
139 BOOST_CHECK_EQUAL(nFace1Interest, 1);
140 BOOST_CHECK_EQUAL(nFace2Interest, 1);
141}
142
143BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
144{
Davide Pesavento4f3d79d2024-04-19 22:44:02 -0400145 DummyClientFace face1(m_io, m_keyChain, {true, true});
146 DummyClientFace face2(m_io, m_keyChain, {true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800147
148 face1.linkTo(face2);
149 face2.unlink();
150 BOOST_CHECK(face1.m_bcastLink == nullptr);
151
Davide Pesavento4f3d79d2024-04-19 22:44:02 -0400152 DummyClientFace face3(m_io, m_keyChain, {true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800153 face1.linkTo(face2);
154 face3.linkTo(face1);
155 face2.unlink();
156 BOOST_CHECK(face1.m_bcastLink != nullptr);
157}
158
159BOOST_AUTO_TEST_CASE(AlreadyLinkException)
160{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500161 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
162 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
163 DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true});
164 DummyClientFace face4(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800165
166 face1.linkTo(face2);
167 face3.linkTo(face4);
168
169 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
170}
171
Junxiao Shic828dfc2016-09-15 13:26:22 +0000172BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
173BOOST_AUTO_TEST_SUITE_END() // Util
174
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400175} // namespace ndn::tests