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 | /* |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
| 32 | BOOST_AUTO_TEST_SUITE(Util) |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(TestDummyClientFace, ndn::tests::IdentityManagementTimeFixture) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 34 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(ProcessEventsOverride) |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 36 | { |
| 37 | bool isOverrideInvoked = false; |
| 38 | auto override = [&] (time::milliseconds timeout) { |
| 39 | isOverrideInvoked = true; |
| 40 | BOOST_CHECK_EQUAL(timeout, time::milliseconds(200)); |
| 41 | }; |
| 42 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 43 | DummyClientFace face(io, {false, false, override}); |
Junxiao Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 44 | face.processEvents(time::milliseconds(200)); |
| 45 | BOOST_CHECK(isOverrideInvoked); |
| 46 | } |
| 47 | |
Zhiyi Zhang | 34429cc | 2017-01-05 17:07:28 -0800 | [diff] [blame] | 48 | BOOST_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 | |
| 70 | advanceClocks(time::milliseconds(25), 4); |
| 71 | |
| 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 | |
| 88 | advanceClocks(time::milliseconds(10), 100); |
| 89 | |
| 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 | |
| 96 | BOOST_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 | |
| 112 | BOOST_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 Shi | c828dfc | 2016-09-15 13:26:22 +0000 | [diff] [blame] | 125 | BOOST_AUTO_TEST_SUITE_END() // TestDummyClientFace |
| 126 | BOOST_AUTO_TEST_SUITE_END() // Util |
| 127 | |
| 128 | } // namespace tests |
| 129 | } // namespace util |
| 130 | } // namespace ndn |