blob: ad0c3bfa204770247e34265c32963475cc9ed97f [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/*
Junxiao Shi0838f3d2022-03-01 01:55:05 +00003 * Copyright (c) 2013-2022 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
28namespace ndn {
29namespace util {
30namespace tests {
31
Junxiao Shib55e5d32018-07-18 13:32:00 -060032using namespace ndn::tests;
33
Junxiao Shic828dfc2016-09-15 13:26:22 +000034BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050035BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture)
Junxiao Shic828dfc2016-09-15 13:26:22 +000036
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080037BOOST_AUTO_TEST_CASE(ProcessEventsOverride)
Junxiao Shic828dfc2016-09-15 13:26:22 +000038{
39 bool isOverrideInvoked = false;
40 auto override = [&] (time::milliseconds timeout) {
41 isOverrideInvoked = true;
Davide Pesavento0f830802018-01-16 23:58:58 -050042 BOOST_CHECK_EQUAL(timeout, 200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000043 };
44
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050045 DummyClientFace face(m_io, {false, false, override});
Davide Pesavento0f830802018-01-16 23:58:58 -050046 face.processEvents(200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000047 BOOST_CHECK(isOverrideInvoked);
48}
49
Junxiao Shi0838f3d2022-03-01 01:55:05 +000050BOOST_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 Zhang34429cc2017-01-05 17:07:28 -080098BOOST_AUTO_TEST_CASE(BroadcastLink)
99{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500100 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
101 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800102 face1.linkTo(face2);
103
104 int nFace1Interest = 0;
105 int nFace2Interest = 0;
106 face1.setInterestFilter("/face1",
107 [&] (const InterestFilter&, const Interest& interest) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000108 BOOST_CHECK_EQUAL(interest.getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800109 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 Shi0838f3d2022-03-01 01:55:05 +0000114 BOOST_CHECK_EQUAL(interest.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800115 nFace2Interest++;
116 face2.put(*ndn::tests::makeData("/face2/data"));
117 return;
118 }, nullptr, nullptr);
119
Davide Pesavento0f830802018-01-16 23:58:58 -0500120 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800121
122 int nFace1Data = 0;
123 int nFace2Nack = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600124 face1.expressInterest(*makeInterest("/face2/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800125 [&] (const Interest& i, const Data& d) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000126 BOOST_CHECK_EQUAL(d.getName(), "/face2/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800127 nFace1Data++;
128 }, nullptr, nullptr);
Junxiao Shib55e5d32018-07-18 13:32:00 -0600129 face2.expressInterest(*makeInterest("/face1/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800130 [&] (const Interest& i, const Data& d) {
131 BOOST_CHECK(false);
132 },
133 [&] (const Interest& i, const lp::Nack& n) {
Junxiao Shi0838f3d2022-03-01 01:55:05 +0000134 BOOST_CHECK_EQUAL(n.getInterest().getName(), "/face1/data");
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800135 nFace2Nack++;
136 }, nullptr);
137
Davide Pesavento0f830802018-01-16 23:58:58 -0500138 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800139
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
146BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
147{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500148 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
149 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800150
151 face1.linkTo(face2);
152 face2.unlink();
153 BOOST_CHECK(face1.m_bcastLink == nullptr);
154
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500155 DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800156 face1.linkTo(face2);
157 face3.linkTo(face1);
158 face2.unlink();
159 BOOST_CHECK(face1.m_bcastLink != nullptr);
160}
161
162BOOST_AUTO_TEST_CASE(AlreadyLinkException)
163{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500164 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 Zhang34429cc2017-01-05 17:07:28 -0800168
169 face1.linkTo(face2);
170 face3.linkTo(face4);
171
172 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
173}
174
Junxiao Shic828dfc2016-09-15 13:26:22 +0000175BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
176BOOST_AUTO_TEST_SUITE_END() // Util
177
178} // namespace tests
179} // namespace util
180} // namespace ndn