Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 24 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 25 | #include "face/unix-stream-factory.hpp" |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 26 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 28 | #include "tests/limited-io.hpp" |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 29 | #include "dummy-stream-sender.hpp" |
| 30 | #include "packet-datasets.hpp" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 34 | |
| 35 | using namespace boost::asio::local; |
| 36 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 37 | #define CHANNEL_PATH1 "unix-stream-test.1.sock" |
| 38 | #define CHANNEL_PATH2 "unix-stream-test.2.sock" |
| 39 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 40 | BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 41 | |
| 42 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 43 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 44 | UnixStreamFactory factory; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 45 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 46 | shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1); |
| 47 | shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 49 | std::string channel1uri = channel1->getUri().toString(); |
| 50 | BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator |
| 51 | BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1), |
| 52 | channel1uri.size() - std::string(CHANNEL_PATH1).size()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 53 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 54 | shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 55 | BOOST_CHECK_NE(channel1, channel2); |
| 56 | } |
| 57 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 58 | class EndToEndFixture : protected BaseFixture |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 59 | { |
| 60 | public: |
| 61 | void |
| 62 | client_onConnect(const boost::system::error_code& error) |
| 63 | { |
| 64 | BOOST_CHECK_MESSAGE(!error, error.message()); |
| 65 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 66 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 70 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 71 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 72 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 73 | face1 = static_pointer_cast<UnixStreamFace>(newFace); |
| 74 | face1->onReceiveInterest += |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 75 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 76 | face1->onReceiveData += |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 77 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
| 78 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 79 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | channel1_onConnectFailed(const std::string& reason) |
| 84 | { |
| 85 | BOOST_CHECK_MESSAGE(false, reason); |
| 86 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 87 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 88 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 89 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 90 | void |
| 91 | face1_onReceiveInterest(const Interest& interest) |
| 92 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 93 | face1_receivedInterests.push_back(interest); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 94 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 95 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 96 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 97 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 98 | void |
| 99 | face1_onReceiveData(const Data& data) |
| 100 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 101 | face1_receivedDatas.push_back(data); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 102 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 103 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
| 107 | face2_onReceiveInterest(const Interest& interest) |
| 108 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 109 | face2_receivedInterests.push_back(interest); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 110 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 111 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 112 | } |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 113 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 114 | void |
| 115 | face2_onReceiveData(const Data& data) |
| 116 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 117 | face2_receivedDatas.push_back(data); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 118 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 119 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 123 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 124 | { |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 125 | faces.push_back(static_pointer_cast<UnixStreamFace>(newFace)); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 126 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 127 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
| 131 | channel_onConnectFailed(const std::string& reason) |
| 132 | { |
| 133 | BOOST_CHECK_MESSAGE(false, reason); |
| 134 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 135 | limitedIo.afterOp(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | protected: |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 139 | LimitedIo limitedIo; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 140 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 141 | shared_ptr<UnixStreamFace> face1; |
| 142 | std::vector<Interest> face1_receivedInterests; |
| 143 | std::vector<Data> face1_receivedDatas; |
| 144 | shared_ptr<UnixStreamFace> face2; |
| 145 | std::vector<Interest> face2_receivedInterests; |
| 146 | std::vector<Data> face2_receivedDatas; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 147 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 148 | std::list< shared_ptr<UnixStreamFace> > faces; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | |
| 152 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 153 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 154 | UnixStreamFactory factory; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 155 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 156 | shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 157 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 158 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 159 | |
| 160 | shared_ptr<stream_protocol::socket> client = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 161 | make_shared<stream_protocol::socket>(boost::ref(g_io)); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 162 | client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1), |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 163 | bind(&EndToEndFixture::client_onConnect, this, _1)); |
| 164 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 165 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 166 | "UnixStreamChannel error: cannot connect or cannot accept connection"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 167 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 168 | BOOST_REQUIRE(static_cast<bool>(face1)); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 169 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(face1->getRemoteUri().getScheme(), "fd"); |
| 171 | BOOST_CHECK_NO_THROW(boost::lexical_cast<int>(face1->getRemoteUri().getHost())); |
| 172 | std::string face1localUri = face1->getLocalUri().toString(); |
| 173 | BOOST_CHECK_EQUAL(face1localUri.find("unix:///"), 0); // third '/' is the path separator |
| 174 | BOOST_CHECK_EQUAL(face1localUri.rfind(CHANNEL_PATH1), |
| 175 | face1localUri.size() - std::string(CHANNEL_PATH1).size()); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 176 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 177 | face2 = make_shared<UnixStreamFace>(client); |
| 178 | face2->onReceiveInterest += |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 179 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 180 | face2->onReceiveData += |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 181 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
| 182 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 183 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 184 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 185 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 186 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 187 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 188 | face1->sendInterest(*interest1); |
| 189 | face1->sendInterest(*interest1); |
| 190 | face1->sendInterest(*interest1); |
| 191 | face1->sendData (*data1 ); |
| 192 | face2->sendInterest(*interest2); |
| 193 | face2->sendData (*data2 ); |
| 194 | face2->sendData (*data2 ); |
| 195 | face2->sendData (*data2 ); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 196 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 197 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 198 | "UnixStreamChannel error: cannot send or receive Interest/Data packets"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 199 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 200 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 201 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 202 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 3); |
| 203 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 204 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 205 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 206 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 207 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 208 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 209 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 210 | const FaceCounters& counters1 = face1->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 1); |
| 212 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 213 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 3); |
| 214 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 215 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 216 | const FaceCounters& counters2 = face2->getCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(counters2.getNInInterests() , 3); |
| 218 | BOOST_CHECK_EQUAL(counters2.getNInDatas() , 1); |
| 219 | BOOST_CHECK_EQUAL(counters2.getNOutInterests(), 1); |
| 220 | BOOST_CHECK_EQUAL(counters2.getNOutDatas() , 3); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 224 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 225 | UnixStreamFactory factory; |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 226 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 227 | shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 228 | channel->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 229 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 230 | |
| 231 | shared_ptr<stream_protocol::socket> client1 = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 232 | make_shared<stream_protocol::socket>(boost::ref(g_io)); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 233 | client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1), |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 234 | bind(&EndToEndFixture::client_onConnect, this, _1)); |
| 235 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 236 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 237 | "UnixStreamChannel error: cannot connect or cannot accept connection"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 238 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 239 | BOOST_CHECK_EQUAL(faces.size(), 1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 240 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 241 | shared_ptr<stream_protocol::socket> client2 = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 242 | make_shared<stream_protocol::socket>(boost::ref(g_io)); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 243 | client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1), |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 244 | bind(&EndToEndFixture::client_onConnect, this, _1)); |
| 245 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 246 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 247 | "UnixStreamChannel error: cannot accept multiple connections"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 248 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(faces.size(), 2); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 250 | |
| 251 | // now close one of the faces |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 252 | faces.front()->close(); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 253 | |
| 254 | // we should still be able to send/receive with the other one |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 255 | face1 = faces.back(); |
| 256 | face1->onReceiveInterest += bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
| 257 | face1->onReceiveData += bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 258 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 259 | face2 = make_shared<UnixStreamFace>(client2); |
| 260 | face2->onReceiveInterest += bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
| 261 | face2->onReceiveData += bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 262 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 263 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 264 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 265 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 266 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 267 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 268 | face1->sendInterest(*interest1); |
| 269 | face1->sendData (*data1 ); |
| 270 | face2->sendInterest(*interest2); |
| 271 | face2->sendData (*data2 ); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 272 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 273 | BOOST_CHECK_MESSAGE(limitedIo.run(4, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 274 | "UnixStreamChannel error: cannot send or receive Interest/Data packets"); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 275 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 276 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 277 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1); |
| 278 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1); |
| 279 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 280 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 281 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest2->getName()); |
| 282 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 283 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getName(), interest1->getName()); |
| 284 | BOOST_CHECK_EQUAL(face2_receivedDatas [0].getName(), data1->getName()); |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 287 | static inline void |
| 288 | noOp() |
| 289 | { |
| 290 | } |
| 291 | |
| 292 | BOOST_FIXTURE_TEST_CASE(UnixStreamFaceLocalControlHeader, EndToEndFixture) |
| 293 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 294 | UnixStreamFactory factory; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 295 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 296 | shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 297 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 298 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 299 | |
| 300 | shared_ptr<stream_protocol::socket> client = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 301 | make_shared<stream_protocol::socket>(boost::ref(g_io)); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 302 | client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1), |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 303 | bind(&EndToEndFixture::client_onConnect, this, _1)); |
| 304 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 305 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 306 | "UnixStreamChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 307 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 308 | BOOST_REQUIRE(static_cast<bool>(face1)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 309 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 310 | face2 = make_shared<UnixStreamFace>(client); |
| 311 | face2->onReceiveInterest += |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 312 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 313 | face2->onReceiveData += |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 314 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
| 315 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 316 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 317 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 318 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 319 | face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID); |
| 320 | face1->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 321 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 322 | BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)); |
| 323 | BOOST_CHECK(face1->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 324 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 325 | face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID); |
| 326 | face2->setLocalControlHeaderFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 327 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 328 | BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID)); |
| 329 | BOOST_CHECK(face2->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 330 | |
| 331 | //////////////////////////////////////////////////////// |
| 332 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 333 | interest1->setIncomingFaceId(11); |
| 334 | interest1->setNextHopFaceId(111); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 336 | face1->sendInterest(*interest1); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 337 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 338 | data1->setIncomingFaceId(22); |
| 339 | data1->getLocalControlHeader().setNextHopFaceId(222); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 340 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 341 | face1->sendData(*data1); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 342 | |
| 343 | // |
| 344 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 345 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 346 | "UnixStreamChannel error: cannot send or receive Interest/Data packets"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 347 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 348 | BOOST_REQUIRE_EQUAL(face2_receivedInterests.size(), 1); |
| 349 | BOOST_REQUIRE_EQUAL(face2_receivedDatas .size(), 1); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 350 | |
| 351 | // sending allows only IncomingFaceId, receiving allows only NextHopFaceId |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 352 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false); |
| 353 | BOOST_CHECK_EQUAL(face2_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), false); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 354 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 355 | BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false); |
| 356 | BOOST_CHECK_EQUAL(face2_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 357 | |
| 358 | //////////////////////////////////////////////////////// |
| 359 | |
| 360 | using namespace boost::asio; |
| 361 | |
| 362 | std::vector<const_buffer> interestWithHeader; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 363 | Block iHeader = interest1->getLocalControlHeader().wireEncode(*interest1, true, true); |
| 364 | Block iPayload = interest1->wireEncode(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 365 | interestWithHeader.push_back(buffer(iHeader.wire(), iHeader.size())); |
| 366 | interestWithHeader.push_back(buffer(iPayload.wire(), iPayload.size())); |
| 367 | |
| 368 | std::vector<const_buffer> dataWithHeader; |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 369 | Block dHeader = data1->getLocalControlHeader().wireEncode(*data1, true, true); |
| 370 | Block dPayload = data1->wireEncode(); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 371 | dataWithHeader.push_back(buffer(dHeader.wire(), dHeader.size())); |
| 372 | dataWithHeader.push_back(buffer(dPayload.wire(), dPayload.size())); |
| 373 | |
| 374 | // |
| 375 | |
| 376 | client->async_send(interestWithHeader, bind(&noOp)); |
| 377 | client->async_send(dataWithHeader, bind(&noOp)); |
| 378 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 379 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 380 | "UnixStreamChannel error: cannot send or receive Interest/Data packets"); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 381 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 382 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 1); |
| 383 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 1); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 384 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasIncomingFaceId(), false); |
| 386 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getLocalControlHeader().hasNextHopFaceId(), true); |
| 387 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getNextHopFaceId(), 111); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 388 | |
Junxiao Shi | 7949416 | 2014-04-02 18:25:11 -0700 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasIncomingFaceId(), false); |
| 390 | BOOST_CHECK_EQUAL(face1_receivedDatas[0].getLocalControlHeader().hasNextHopFaceId(), false); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Alexander Afanasyev | 650028d | 2014-04-25 18:39:10 -0700 | [diff] [blame] | 393 | |
| 394 | class SimpleEndToEndFixture : protected BaseFixture |
| 395 | { |
| 396 | public: |
| 397 | void |
| 398 | onFaceCreated(const shared_ptr<Face>& face) |
| 399 | { |
| 400 | face->onReceiveInterest += |
| 401 | bind(&SimpleEndToEndFixture::onReceiveInterest, this, _1); |
| 402 | face->onReceiveData += |
| 403 | bind(&SimpleEndToEndFixture::onReceiveData, this, _1); |
| 404 | face->onFail += |
| 405 | bind(&SimpleEndToEndFixture::onFail, this, face); |
| 406 | |
| 407 | if (static_cast<bool>(dynamic_pointer_cast<LocalFace>(face))) { |
| 408 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 409 | LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID); |
| 410 | |
| 411 | static_pointer_cast<LocalFace>(face)->setLocalControlHeaderFeature( |
| 412 | LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID); |
| 413 | } |
| 414 | |
| 415 | limitedIo.afterOp(); |
| 416 | } |
| 417 | |
| 418 | void |
| 419 | onConnectFailed(const std::string& reason) |
| 420 | { |
| 421 | BOOST_CHECK_MESSAGE(false, reason); |
| 422 | |
| 423 | limitedIo.afterOp(); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | onReceiveInterest(const Interest& interest) |
| 428 | { |
| 429 | receivedInterests.push_back(interest); |
| 430 | |
| 431 | limitedIo.afterOp(); |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | onReceiveData(const Data& data) |
| 436 | { |
| 437 | receivedDatas.push_back(data); |
| 438 | |
| 439 | limitedIo.afterOp(); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | onFail(const shared_ptr<Face>& face) |
| 444 | { |
| 445 | limitedIo.afterOp(); |
| 446 | } |
| 447 | |
| 448 | public: |
| 449 | LimitedIo limitedIo; |
| 450 | |
| 451 | std::vector<Interest> receivedInterests; |
| 452 | std::vector<Data> receivedDatas; |
| 453 | }; |
| 454 | |
| 455 | |
| 456 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CorruptedInput, Dataset, |
| 457 | CorruptedPackets, SimpleEndToEndFixture) |
| 458 | { |
| 459 | UnixStreamFactory factory; |
| 460 | |
| 461 | shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1); |
| 462 | channel->listen(bind(&SimpleEndToEndFixture::onFaceCreated, this, _1), |
| 463 | bind(&SimpleEndToEndFixture::onConnectFailed, this, _1)); |
| 464 | |
| 465 | DummyStreamSender<stream_protocol, Dataset> sender; |
| 466 | sender.start(stream_protocol::endpoint(CHANNEL_PATH1)); |
| 467 | |
| 468 | BOOST_CHECK_MESSAGE(limitedIo.run(LimitedIo::UNLIMITED_OPS, |
| 469 | time::seconds(1)) == LimitedIo::EXCEED_TIME, |
| 470 | "Exception thrown for " + Dataset::getName()); |
| 471 | } |
| 472 | |
| 473 | |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 474 | BOOST_AUTO_TEST_SUITE_END() |
| 475 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 476 | } // namespace tests |
Davide Pesavento | bc4dd8c | 2014-02-14 20:01:01 +0100 | [diff] [blame] | 477 | } // namespace nfd |