blob: acd629730618bf3f6145db5ba29bd4d5874a2973 [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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 Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/make-interest-data.hpp"
26#include "tests/unit/identity-management-time-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)
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080035BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, ndn::tests::IdentityManagementTimeFixture)
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
Alexander Afanasyev80782e02017-01-04 13:16:54 -080045 DummyClientFace face(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
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080050BOOST_AUTO_TEST_CASE(BroadcastLink)
51{
52 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
53 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
54 face1.linkTo(face2);
55
56 int nFace1Interest = 0;
57 int nFace2Interest = 0;
58 face1.setInterestFilter("/face1",
59 [&] (const InterestFilter&, const Interest& interest) {
60 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face1/data");
61 nFace1Interest++;
62 face1.put(ndn::tests::makeNack(interest, lp::NackReason::NO_ROUTE));
63 }, nullptr, nullptr);
64 face2.setInterestFilter("/face2",
65 [&] (const InterestFilter&, const Interest& interest) {
66 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face2/data");
67 nFace2Interest++;
68 face2.put(*ndn::tests::makeData("/face2/data"));
69 return;
70 }, nullptr, nullptr);
71
Davide Pesavento0f830802018-01-16 23:58:58 -050072 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080073
74 int nFace1Data = 0;
75 int nFace2Nack = 0;
Junxiao Shib55e5d32018-07-18 13:32:00 -060076 face1.expressInterest(*makeInterest("/face2/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080077 [&] (const Interest& i, const Data& d) {
78 BOOST_CHECK_EQUAL(d.getName().toUri(), "/face2/data");
79 nFace1Data++;
80 }, nullptr, nullptr);
Junxiao Shib55e5d32018-07-18 13:32:00 -060081 face2.expressInterest(*makeInterest("/face1/data"),
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080082 [&] (const Interest& i, const Data& d) {
83 BOOST_CHECK(false);
84 },
85 [&] (const Interest& i, const lp::Nack& n) {
86 BOOST_CHECK_EQUAL(n.getInterest().getName().toUri(), "/face1/data");
87 nFace2Nack++;
88 }, nullptr);
89
Davide Pesavento0f830802018-01-16 23:58:58 -050090 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080091
92 BOOST_CHECK_EQUAL(nFace1Data, 1);
93 BOOST_CHECK_EQUAL(nFace2Nack, 1);
94 BOOST_CHECK_EQUAL(nFace1Interest, 1);
95 BOOST_CHECK_EQUAL(nFace2Interest, 1);
96}
97
98BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
99{
100 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
101 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
102
103 face1.linkTo(face2);
104 face2.unlink();
105 BOOST_CHECK(face1.m_bcastLink == nullptr);
106
107 DummyClientFace face3(io, m_keyChain, DummyClientFace::Options{true, true});
108 face1.linkTo(face2);
109 face3.linkTo(face1);
110 face2.unlink();
111 BOOST_CHECK(face1.m_bcastLink != nullptr);
112}
113
114BOOST_AUTO_TEST_CASE(AlreadyLinkException)
115{
116 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
117 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
118 DummyClientFace face3(io, m_keyChain, DummyClientFace::Options{true, true});
119 DummyClientFace face4(io, m_keyChain, DummyClientFace::Options{true, true});
120
121 face1.linkTo(face2);
122 face3.linkTo(face4);
123
124 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
125}
126
Junxiao Shic828dfc2016-09-15 13:26:22 +0000127BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
128BOOST_AUTO_TEST_SUITE_END() // Util
129
130} // namespace tests
131} // namespace util
132} // namespace ndn