blob: 0eea2e6b1053dfe1570df907ca1728814da62de9 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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"
Junxiao Shi0838f3d2022-03-01 01:55:05 +000023#include "ndn-cxx/mgmt/nfd/controller.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000024
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/test-common.hpp"
26#include "tests/unit/io-key-chain-fixture.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Junxiao Shib55e5d32018-07-18 13:32:00 -060029
Junxiao Shic828dfc2016-09-15 13:26:22 +000030BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050031BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture)
Junxiao Shic828dfc2016-09-15 13:26:22 +000032
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080033BOOST_AUTO_TEST_CASE(ProcessEventsOverride)
Junxiao Shic828dfc2016-09-15 13:26:22 +000034{
35 bool isOverrideInvoked = false;
36 auto override = [&] (time::milliseconds timeout) {
37 isOverrideInvoked = true;
Davide Pesavento0f830802018-01-16 23:58:58 -050038 BOOST_CHECK_EQUAL(timeout, 200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000039 };
40
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041 DummyClientFace face(m_io, {false, false, override});
Davide Pesavento0f830802018-01-16 23:58:58 -050042 face.processEvents(200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000043 BOOST_CHECK(isOverrideInvoked);
44}
45
Junxiao Shi0838f3d2022-03-01 01:55:05 +000046BOOST_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 Zhang34429cc2017-01-05 17:07:28 -080094BOOST_AUTO_TEST_CASE(BroadcastLink)
95{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050096 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
97 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080098 face1.linkTo(face2);
99
100 int nFace1Interest = 0;
101 int nFace2Interest = 0;
102 face1.setInterestFilter("/face1",
103 [&] (const InterestFilter&, const Interest& interest) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000104 BOOST_CHECK_EQUAL(interest.getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800105 nFace1Interest++;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400106 face1.put(makeNack(interest, lp::NackReason::NO_ROUTE));
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800107 }, nullptr, nullptr);
108 face2.setInterestFilter("/face2",
109 [&] (const InterestFilter&, const Interest& interest) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000110 BOOST_CHECK_EQUAL(interest.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800111 nFace2Interest++;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400112 face2.put(*makeData("/face2/data"));
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800113 return;
114 }, nullptr, nullptr);
115
Davide Pesavento0f830802018-01-16 23:58:58 -0500116 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800117
118 int nFace1Data = 0;
119 int nFace2Nack = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600120 face1.expressInterest(*makeInterest("/face2/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800121 [&] (const Interest& i, const Data& d) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000122 BOOST_CHECK_EQUAL(d.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800123 nFace1Data++;
124 }, nullptr, nullptr);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600125 face2.expressInterest(*makeInterest("/face1/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800126 [&] (const Interest& i, const Data& d) {
127 BOOST_CHECK(false);
128 },
129 [&] (const Interest& i, const lp::Nack& n) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000130 BOOST_CHECK_EQUAL(n.getInterest().getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800131 nFace2Nack++;
132 }, nullptr);
133
Davide Pesavento0f830802018-01-16 23:58:58 -0500134 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800135
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
142BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
143{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500144 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
145 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800146
147 face1.linkTo(face2);
148 face2.unlink();
149 BOOST_CHECK(face1.m_bcastLink == nullptr);
150
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500151 DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800152 face1.linkTo(face2);
153 face3.linkTo(face1);
154 face2.unlink();
155 BOOST_CHECK(face1.m_bcastLink != nullptr);
156}
157
158BOOST_AUTO_TEST_CASE(AlreadyLinkException)
159{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500160 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 Zhang34429cc2017-01-05 17:07:28 -0800164
165 face1.linkTo(face2);
166 face3.linkTo(face4);
167
168 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
169}
170
Junxiao Shic828dfc2016-09-15 13:26:22 +0000171BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
172BOOST_AUTO_TEST_SUITE_END() // Util
173
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400174} // namespace ndn::tests