blob: 5308e283990d28e6a14988c22b199fb5bc49aab4 [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
22#include "util/dummy-client-face.hpp"
23
24#include "boost-test.hpp"
Alexander Afanasyev80782e02017-01-04 13:16:54 -080025#include "../identity-management-time-fixture.hpp"
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080026#include "make-interest-data.hpp"
Junxiao Shic828dfc2016-09-15 13:26:22 +000027
28namespace ndn {
29namespace util {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(Util)
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080033BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, ndn::tests::IdentityManagementTimeFixture)
Junxiao Shic828dfc2016-09-15 13:26:22 +000034
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080035BOOST_AUTO_TEST_CASE(ProcessEventsOverride)
Junxiao Shic828dfc2016-09-15 13:26:22 +000036{
37 bool isOverrideInvoked = false;
38 auto override = [&] (time::milliseconds timeout) {
39 isOverrideInvoked = true;
Davide Pesavento0f830802018-01-16 23:58:58 -050040 BOOST_CHECK_EQUAL(timeout, 200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000041 };
42
Alexander Afanasyev80782e02017-01-04 13:16:54 -080043 DummyClientFace face(io, {false, false, override});
Davide Pesavento0f830802018-01-16 23:58:58 -050044 face.processEvents(200_ms);
Junxiao Shic828dfc2016-09-15 13:26:22 +000045 BOOST_CHECK(isOverrideInvoked);
46}
47
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080048BOOST_AUTO_TEST_CASE(BroadcastLink)
49{
50 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
51 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
52 face1.linkTo(face2);
53
54 int nFace1Interest = 0;
55 int nFace2Interest = 0;
56 face1.setInterestFilter("/face1",
57 [&] (const InterestFilter&, const Interest& interest) {
58 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face1/data");
59 nFace1Interest++;
60 face1.put(ndn::tests::makeNack(interest, lp::NackReason::NO_ROUTE));
61 }, nullptr, nullptr);
62 face2.setInterestFilter("/face2",
63 [&] (const InterestFilter&, const Interest& interest) {
64 BOOST_CHECK_EQUAL(interest.getName().toUri(), "/face2/data");
65 nFace2Interest++;
66 face2.put(*ndn::tests::makeData("/face2/data"));
67 return;
68 }, nullptr, nullptr);
69
Davide Pesavento0f830802018-01-16 23:58:58 -050070 advanceClocks(25_ms, 4);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080071
72 int nFace1Data = 0;
73 int nFace2Nack = 0;
74 face1.expressInterest(Interest("/face2/data"),
75 [&] (const Interest& i, const Data& d) {
76 BOOST_CHECK_EQUAL(d.getName().toUri(), "/face2/data");
77 nFace1Data++;
78 }, nullptr, nullptr);
79 face2.expressInterest(Interest("/face1/data"),
80 [&] (const Interest& i, const Data& d) {
81 BOOST_CHECK(false);
82 },
83 [&] (const Interest& i, const lp::Nack& n) {
84 BOOST_CHECK_EQUAL(n.getInterest().getName().toUri(), "/face1/data");
85 nFace2Nack++;
86 }, nullptr);
87
Davide Pesavento0f830802018-01-16 23:58:58 -050088 advanceClocks(10_ms, 100);
Zhiyi Zhang34429cc2017-01-05 17:07:28 -080089
90 BOOST_CHECK_EQUAL(nFace1Data, 1);
91 BOOST_CHECK_EQUAL(nFace2Nack, 1);
92 BOOST_CHECK_EQUAL(nFace1Interest, 1);
93 BOOST_CHECK_EQUAL(nFace2Interest, 1);
94}
95
96BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy)
97{
98 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
99 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
100
101 face1.linkTo(face2);
102 face2.unlink();
103 BOOST_CHECK(face1.m_bcastLink == nullptr);
104
105 DummyClientFace face3(io, m_keyChain, DummyClientFace::Options{true, true});
106 face1.linkTo(face2);
107 face3.linkTo(face1);
108 face2.unlink();
109 BOOST_CHECK(face1.m_bcastLink != nullptr);
110}
111
112BOOST_AUTO_TEST_CASE(AlreadyLinkException)
113{
114 DummyClientFace face1(io, m_keyChain, DummyClientFace::Options{true, true});
115 DummyClientFace face2(io, m_keyChain, DummyClientFace::Options{true, true});
116 DummyClientFace face3(io, m_keyChain, DummyClientFace::Options{true, true});
117 DummyClientFace face4(io, m_keyChain, DummyClientFace::Options{true, true});
118
119 face1.linkTo(face2);
120 face3.linkTo(face4);
121
122 BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError);
123}
124
Junxiao Shic828dfc2016-09-15 13:26:22 +0000125BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace
126BOOST_AUTO_TEST_SUITE_END() // Util
127
128} // namespace tests
129} // namespace util
130} // namespace ndn