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 | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/util/dummy-client-face.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 23 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 24 | #include "tests/test-common.hpp" |
| 25 | #include "tests/unit/io-key-chain-fixture.hpp" |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace util { |
| 29 | namespace tests { |
| 30 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 31 | using namespace ndn::tests; |
| 32 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Util) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, IoKeyChainFixture) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 35 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(ProcessEventsOverride) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 37 | { |
| 38 | bool isOverrideInvoked = false; |
| 39 | auto override = [&] (time::milliseconds timeout) { |
| 40 | isOverrideInvoked = true; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(timeout, 200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 44 | DummyClientFace face(m_io, {false, false, override}); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 45 | face.processEvents(200_ms); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 46 | BOOST_CHECK(isOverrideInvoked); |
| 47 | } |
| 48 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 49 | BOOST_AUTO_TEST_CASE(BroadcastLink) |
| 50 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 51 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 52 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 53 | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 71 | advanceClocks(25_ms, 4); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 72 | |
| 73 | int nFace1Data = 0; |
| 74 | int nFace2Nack = 0; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 75 | face1.expressInterest(*makeInterest("/face2/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 76 | [&] (const Interest& i, const Data& d) { |
| 77 | BOOST_CHECK_EQUAL(d.getName().toUri(), "/face2/data"); |
| 78 | nFace1Data++; |
| 79 | }, nullptr, nullptr); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 80 | face2.expressInterest(*makeInterest("/face1/data"), |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 81 | [&] (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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 89 | advanceClocks(10_ms, 100); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 90 | |
| 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 | |
| 97 | BOOST_AUTO_TEST_CASE(BroadcastLinkDestroy) |
| 98 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 99 | DummyClientFace face1(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
| 100 | DummyClientFace face2(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 101 | |
| 102 | face1.linkTo(face2); |
| 103 | face2.unlink(); |
| 104 | BOOST_CHECK(face1.m_bcastLink == nullptr); |
| 105 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 106 | DummyClientFace face3(m_io, m_keyChain, DummyClientFace::Options{true, true}); |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 107 | face1.linkTo(face2); |
| 108 | face3.linkTo(face1); |
| 109 | face2.unlink(); |
| 110 | BOOST_CHECK(face1.m_bcastLink != nullptr); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(AlreadyLinkException) |
| 114 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 115 | 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 Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 119 | |
| 120 | face1.linkTo(face2); |
| 121 | face3.linkTo(face4); |
| 122 | |
| 123 | BOOST_CHECK_THROW(face2.linkTo(face3), DummyClientFace::AlreadyLinkedError); |
| 124 | } |
| 125 | |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 126 | BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace |
| 127 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 128 | |
| 129 | } // namespace tests |
| 130 | } // namespace util |
| 131 | } // namespace ndn |