blob: b823be2d716f638766cf3684730701d45e57faec [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 Pesavento4c1ad4c2020-11-16 21:12:02 -05003 * Copyright (c) 2013-2020 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 Shic828dfc2016-09-15 13:26:22 +000023
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050024#include "tests/test-common.hpp"
25#include "tests/unit/io-key-chain-fixture.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000026
27namespace ndn {
28namespace util {
29namespace tests {
30
Junxiao Shib55e5d32018-07-18 13:32:00 -060031using namespace ndn::tests;
32
Junxiao Shic828dfc2016-09-15 13:26:22 +000033BOOST_AUTO_TEST_SUITE(Util)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050034BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture)
Junxiao Shic828dfc2016-09-15 13:26:22 +000035
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080036BOOST_AUTO_TEST_CASE(ProcessEventsOverride)
Junxiao Shic828dfc2016-09-15 13:26:22 +000037{
38 bool isOverrideInvoked = false;
39 auto override = [&] (time::milliseconds timeout) {
40 isOverrideInvoked = true;
Davide Pesavento0f830802018-01-16 23:58:58 -050041 BOOST_CHECK_EQUAL(timeout, 200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000042 };
43
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044 DummyClientFace face(m_io, {false, false, override});
Davide Pesavento0f830802018-01-16 23:58:58 -050045 face.processEvents(200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000046 BOOST_CHECK(isOverrideInvoked);
47}
48
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080049BOOST_AUTO_TEST_CASE(BroadcastLink)
50{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050051 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
52 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080053 face1.linkTo(face2);
54
55 int nFace1Interest = 0;
56 int nFace2Interest = 0;
57 face1.setInterestFilter("/face1",
58 [&] (const InterestFilter&, const Interest& interest) {
59 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face1/data");
60 nFace1Interest++;
61 face1.put(ndn::tests::makeNack(interest, lp::NackReason::NO_ROUTE));
62 }, nullptr, nullptr);
63 face2.setInterestFilter("/face2",
64 [&] (const InterestFilter&, const Interest& interest) {
65 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face2/data");
66 nFace2Interest++;
67 face2.put(*ndn::tests::makeData("/face2/data"));
68 return;
69 }, nullptr, nullptr);
70
Davide Pesavento0f830802018-01-16 23:58:58 -050071 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080072
73 int nFace1Data = 0;
74 int nFace2Nack = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060075 face1.expressInterest(*makeInterest("/face2/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080076 [&] (const Interest& i, const Data& d) {
77 BOOST_CHECK_EQUAL(d.getName().toUri(), "/face2/data");
78 nFace1Data++;
79 }, nullptr, nullptr);
Junxiao Shib55e5d32018-07-18 13:32:00 -060080 face2.expressInterest(*makeInterest("/face1/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080081 [&] (const Interest& i, const Data& d) {
82 BOOST_CHECK(false);
83 },
84 [&] (const Interest& i, const lp::Nack& n) {
85 BOOST_CHECK_EQUAL(n.getInterest().getName().toUri(), "/face1/data");
86 nFace2Nack++;
87 }, nullptr);
88
Davide Pesavento0f830802018-01-16 23:58:58 -050089 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080090
91 BOOST_CHECK_EQUAL(nFace1Data, 1);
92 BOOST_CHECK_EQUAL(nFace2Nack, 1);
93 BOOST_CHECK_EQUAL(nFace1Interest, 1);
94 BOOST_CHECK_EQUAL(nFace2Interest, 1);
95}
96
97BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
98{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050099 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
100 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800101
102 face1.linkTo(face2);
103 face2.unlink();
104 BOOST_CHECK(face1.m_bcastLink == nullptr);
105
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500106 DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800107 face1.linkTo(face2);
108 face3.linkTo(face1);
109 face2.unlink();
110 BOOST_CHECK(face1.m_bcastLink != nullptr);
111}
112
113BOOST_AUTO_TEST_CASE(AlreadyLinkException)
114{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500115 DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true});
116 DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true});
117 DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true});
118 DummyClientFace face4(m_io, m_keyChain, DummyClientFace::Options{true, true});
Zhiyi Zhang34429cc2017-01-05 17:07:28 -0800119
120 face1.linkTo(face2);
121 face3.linkTo(face4);
122
123 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
124}
125
Junxiao Shic828dfc2016-09-15 13:26:22 +0000126BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
127BOOST_AUTO_TEST_SUITE_END() // Util
128
129} // namespace tests
130} // namespace util
131} // namespace ndn