Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "face/internal-client-face.hpp" |
| 27 | #include "face/internal-face.hpp" |
| 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/identity-management-fixture.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
| 34 | class InternalClientFaceFixture : public UnitTestTimeFixture |
| 35 | , public IdentityManagementFixture |
| 36 | { |
| 37 | public: |
| 38 | InternalClientFaceFixture() |
| 39 | : m_internalFace(make_shared<InternalFace>()) |
| 40 | , m_face(makeInternalClientFace(m_internalFace, m_keyChain)) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | protected: |
| 45 | shared_ptr<InternalFace> m_internalFace; |
| 46 | shared_ptr<InternalClientFace> m_face; |
| 47 | }; |
| 48 | |
| 49 | BOOST_FIXTURE_TEST_SUITE(FaceInternalClientFace, InternalClientFaceFixture) |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(ExpressInterest) |
| 52 | { |
| 53 | bool didReceiveDataBack = false; |
| 54 | bool didTimeoutCallbackFire = false; |
| 55 | Data receivedData; |
| 56 | |
| 57 | auto expressInterest = [&] (shared_ptr<Interest> interest) { |
| 58 | didReceiveDataBack = false; |
| 59 | didTimeoutCallbackFire = false; |
| 60 | m_face->expressInterest(interest->setInterestLifetime(time::milliseconds(100)), |
| 61 | [&] (const Interest& interest, Data& data) { |
| 62 | didReceiveDataBack = true; |
| 63 | receivedData = data; |
| 64 | }, |
| 65 | bind([&] { didTimeoutCallbackFire = true; })); |
| 66 | advanceClocks(time::milliseconds(1)); |
| 67 | }; |
| 68 | |
| 69 | expressInterest(makeInterest("/test/timeout")); |
| 70 | advanceClocks(time::milliseconds(1), 200); // wait for time out |
| 71 | BOOST_CHECK(didTimeoutCallbackFire); |
| 72 | |
| 73 | auto dataToSend = makeData("/test/data")->setContent(Block("\x81\x01\0x01", 3)); |
| 74 | expressInterest(makeInterest("/test/data")); |
| 75 | m_internalFace->sendData(dataToSend); // send data to satisfy the expressed interest |
| 76 | advanceClocks(time::milliseconds(1)); |
| 77 | BOOST_CHECK(didReceiveDataBack); |
| 78 | BOOST_CHECK(receivedData.wireEncode() == dataToSend.wireEncode()); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(InterestFilter) |
| 82 | { |
| 83 | bool didOnInterestCallbackFire = false; |
| 84 | Block receivedBlock, expectedBlock; |
| 85 | |
| 86 | auto testSendInterest = [&] (shared_ptr<Interest> interest) { |
| 87 | didOnInterestCallbackFire = false; |
| 88 | expectedBlock = interest->wireEncode(); |
| 89 | m_internalFace->sendInterest(*interest); |
| 90 | }; |
| 91 | |
| 92 | testSendInterest(makeInterest("/test/filter")); // no filter is set now |
| 93 | BOOST_CHECK(!didOnInterestCallbackFire); |
| 94 | |
| 95 | // set filter |
| 96 | auto filter = m_face->setInterestFilter("/test/filter", |
| 97 | bind([&](const Interest& interset) { |
| 98 | didOnInterestCallbackFire = true; |
| 99 | receivedBlock = interset.wireEncode(); |
| 100 | }, _2)); |
| 101 | advanceClocks(time::milliseconds(1)); |
| 102 | |
| 103 | testSendInterest(makeInterest("/test/filter")); |
| 104 | BOOST_CHECK(didOnInterestCallbackFire); |
| 105 | BOOST_CHECK(receivedBlock == expectedBlock); |
| 106 | |
| 107 | // unset filter |
| 108 | didOnInterestCallbackFire = false; |
| 109 | m_face->unsetInterestFilter(filter); |
| 110 | advanceClocks(time::milliseconds(1)); |
| 111 | testSendInterest(makeInterest("/test/filter")); |
| 112 | BOOST_CHECK(!didOnInterestCallbackFire); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(PutData) |
| 116 | { |
| 117 | bool didInternalFaceReceiveData = false; |
| 118 | Data receivedData; |
| 119 | m_internalFace->onReceiveData.connect([&] (const Data& data) { |
| 120 | didInternalFaceReceiveData = true; |
| 121 | receivedData = data; |
| 122 | }); |
| 123 | |
| 124 | auto dataToPut = makeData("/test/put/data"); |
| 125 | m_face->put(*dataToPut); |
| 126 | advanceClocks(time::milliseconds(1)); |
| 127 | BOOST_CHECK(didInternalFaceReceiveData); |
| 128 | BOOST_CHECK(receivedData.wireEncode() == dataToPut->wireEncode()); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_SUITE_END() |
| 132 | |
| 133 | } // namespace tests |
| 134 | } // namespace nfd |