Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 25 | #include "../identity-management-time-fixture.hpp" |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 26 | #include "make-interest-data.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | namespace tests { |
| 31 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 32 | using namespace ndn::tests; |
| 33 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Util) |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 35 | BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, ndn::tests::IdentityManagementTimeFixture) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 36 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(ProcessEventsOverride) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 38 | { |
| 39 | bool isOverrideInvoked = false; |
| 40 | auto override = [&] (time::milliseconds timeout) { |
| 41 | isOverrideInvoked = true; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(timeout, 200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 45 | DummyClientFace face(io, {false, false, override}); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 46 | face.processEvents(200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 47 | BOOST_CHECK(isOverrideInvoked); |
| 48 | } |
| 49 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 50 | BOOST_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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 72 | advanceClocks(25_ms, 4); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 73 | |
| 74 | int nFace1Data = 0; |
| 75 | int nFace2Nack = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 76 | face1.expressInterest(*makeInterest("/face2/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 77 | [&] (const Interest& i, const Data& d) { |
| 78 | BOOST_CHECK_EQUAL(d.getName().toUri(), "/face2/data"); |
| 79 | nFace1Data++; |
| 80 | }, nullptr, nullptr); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 81 | face2.expressInterest(*makeInterest("/face1/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 82 | [&] (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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 90 | advanceClocks(10_ms, 100); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 91 | |
| 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 | |
| 98 | BOOST_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 | |
| 114 | BOOST_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 Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 127 | BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace |
| 128 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 129 | |
| 130 | } // namespace tests |
| 131 | } // namespace util |
| 132 | } // namespace ndn |