Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 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 | |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 26 | #include "face/websocket-channel.hpp" |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 27 | #include "face/websocket-factory.hpp" |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 28 | #include "face/websocketpp.hpp" |
Davide Pesavento | 6ad890a | 2015-03-09 03:43:17 +0100 | [diff] [blame] | 29 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 30 | #include "tests/test-common.hpp" |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 31 | #include "tests/limited-io.hpp" |
| 32 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 33 | namespace nfd { |
| 34 | namespace tests { |
| 35 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Face) |
| 37 | BOOST_FIXTURE_TEST_SUITE(TestWebSocket, BaseFixture) |
| 38 | |
| 39 | using nfd::Face; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(GetChannels) |
| 42 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 43 | WebSocketFactory factory; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 44 | BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true); |
| 45 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 46 | std::vector<shared_ptr<const Channel>> expectedChannels; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 47 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070")); |
| 48 | expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071")); |
| 49 | expectedChannels.push_back(factory.createChannel("::1", "20071")); |
| 50 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 51 | for (const auto& i : factory.getChannels()) { |
| 52 | auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i); |
| 53 | BOOST_REQUIRE(pos != expectedChannels.end()); |
| 54 | expectedChannels.erase(pos); |
| 55 | } |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 56 | |
| 57 | BOOST_CHECK_EQUAL(expectedChannels.size(), 0); |
| 58 | } |
| 59 | |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate) |
| 61 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 62 | WebSocketFactory factory; |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 63 | |
| 64 | BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"), |
| 65 | ndn::nfd::FACE_PERSISTENCY_PERMANENT, |
| 66 | bind([]{}), |
| 67 | bind([]{})), |
| 68 | ProtocolFactory::Error); |
| 69 | |
| 70 | BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"), |
| 71 | ndn::nfd::FACE_PERSISTENCY_ON_DEMAND, |
| 72 | bind([]{}), |
| 73 | bind([]{})), |
| 74 | ProtocolFactory::Error); |
| 75 | |
| 76 | BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"), |
| 77 | ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| 78 | bind([]{}), |
| 79 | bind([]{})), |
| 80 | ProtocolFactory::Error); |
| 81 | } |
| 82 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 83 | class EndToEndFixture : protected BaseFixture |
| 84 | { |
| 85 | public: |
| 86 | void |
| 87 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
| 88 | { |
| 89 | BOOST_CHECK(!static_cast<bool>(face1)); |
| 90 | face1 = newFace; |
Junxiao Shi | c099ddb | 2014-12-25 20:53:20 -0700 | [diff] [blame] | 91 | face1->onReceiveInterest.connect(bind(&EndToEndFixture::face1_onReceiveInterest, this, _1)); |
| 92 | face1->onReceiveData.connect(bind(&EndToEndFixture::face1_onReceiveData, this, _1)); |
| 93 | face1->onFail.connect(bind(&EndToEndFixture::face1_onFail, this)); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 94 | |
| 95 | limitedIo.afterOp(); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | face1_onReceiveInterest(const Interest& interest) |
| 100 | { |
| 101 | face1_receivedInterests.push_back(interest); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 102 | limitedIo.afterOp(); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | face1_onReceiveData(const Data& data) |
| 107 | { |
| 108 | face1_receivedDatas.push_back(data); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 109 | limitedIo.afterOp(); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | face1_onFail() |
| 114 | { |
| 115 | face1.reset(); |
| 116 | limitedIo.afterOp(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | client1_onOpen(websocketpp::connection_hdl hdl) |
| 121 | { |
| 122 | handle = hdl; |
| 123 | limitedIo.afterOp(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | client1_onClose(websocketpp::connection_hdl hdl) |
| 128 | { |
| 129 | limitedIo.afterOp(); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | client1_onFail(websocketpp::connection_hdl hdl) |
| 134 | { |
| 135 | limitedIo.afterOp(); |
| 136 | } |
| 137 | |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 138 | bool |
| 139 | client1_onPing(websocketpp::connection_hdl hdl, std::string msg) |
| 140 | { |
| 141 | limitedIo.afterOp(); |
| 142 | // Return false to suppress the pong response, |
| 143 | // which will cause timeout in the websocket channel |
| 144 | return false; |
| 145 | } |
| 146 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 147 | void |
| 148 | client1_sendInterest(const Interest& interest) |
| 149 | { |
| 150 | const Block& payload = interest.wireEncode(); |
| 151 | client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | client1_sendData(const Data& data) |
| 156 | { |
| 157 | const Block& payload = data.wireEncode(); |
| 158 | client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | client1_onMessage(websocketpp::connection_hdl hdl, |
| 163 | websocketpp::config::asio_client::message_type::ptr msg) |
| 164 | { |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 165 | bool isOk = false; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 166 | Block element; |
| 167 | const std::string& payload = msg->get_payload(); |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 168 | std::tie(isOk, element) = Block::fromBuffer(reinterpret_cast<const uint8_t*>(payload.c_str()), |
| 169 | payload.size()); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 170 | if (isOk) |
| 171 | { |
| 172 | try { |
| 173 | if (element.type() == tlv::Interest) |
| 174 | { |
| 175 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 176 | i->wireDecode(element); |
| 177 | client1_onReceiveInterest(*i); |
| 178 | } |
| 179 | else if (element.type() == tlv::Data) |
| 180 | { |
| 181 | shared_ptr<Data> d = make_shared<Data>(); |
| 182 | d->wireDecode(element); |
| 183 | client1_onReceiveData(*d); |
| 184 | } |
| 185 | } |
| 186 | catch (tlv::Error&) { |
| 187 | // Do something? |
| 188 | } |
| 189 | } |
| 190 | limitedIo.afterOp(); |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | client1_onReceiveInterest(const Interest& interest) |
| 195 | { |
| 196 | client1_receivedInterests.push_back(interest); |
| 197 | limitedIo.afterOp(); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | client1_onReceiveData(const Data& data) |
| 202 | { |
| 203 | client1_receivedDatas.push_back(data); |
| 204 | limitedIo.afterOp(); |
| 205 | } |
| 206 | |
| 207 | public: |
| 208 | LimitedIo limitedIo; |
| 209 | |
| 210 | shared_ptr<Face> face1; |
| 211 | std::vector<Interest> face1_receivedInterests; |
| 212 | std::vector<Data> face1_receivedDatas; |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 213 | websocket::Client client1; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 214 | websocketpp::connection_hdl handle; |
| 215 | std::vector<Interest> client1_receivedInterests; |
| 216 | std::vector<Data> client1_receivedDatas; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
| 220 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 221 | WebSocketFactory factory1; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 222 | |
| 223 | shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 224 | channel1->setPingInterval(time::milliseconds(3000)); |
| 225 | channel1->setPongTimeout(time::milliseconds(1000)); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 226 | |
| 227 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 228 | |
| 229 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1)); |
| 230 | |
| 231 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 232 | |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 233 | // Clear all logging info from websocketpp library |
| 234 | client1.clear_access_channels(websocketpp::log::alevel::all); |
| 235 | |
| 236 | client1.init_asio(&getGlobalIoService()); |
| 237 | client1.set_open_handler(bind(&EndToEndFixture::client1_onOpen, this, _1)); |
| 238 | client1.set_close_handler(bind(&EndToEndFixture::client1_onClose, this, _1)); |
| 239 | client1.set_fail_handler(bind(&EndToEndFixture::client1_onFail, this, _1)); |
| 240 | client1.set_message_handler(bind(&EndToEndFixture::client1_onMessage, this, _1, _2)); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 241 | client1.set_ping_handler(bind(&EndToEndFixture::client1_onPing, this, _1, _2)); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 242 | |
| 243 | websocketpp::lib::error_code ec; |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 244 | auto con = client1.get_connection("ws://127.0.0.1:20070", ec); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 245 | client1.connect(con); |
| 246 | |
| 247 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 248 | "WebSocketChannel error: cannot connect or cannot accept connection"); |
| 249 | |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 251 | |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 252 | BOOST_REQUIRE(static_cast<bool>(face1)); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(face1->isLocal(), true); |
Yukai Tu | 731f0d7 | 2015-07-04 11:14:44 +0800 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(face1->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
Davide Pesavento | 9427941 | 2015-02-27 01:29:32 +0100 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(face1->isMultiAccess(), false); |
| 256 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "ws://127.0.0.1:20070"); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 257 | |
| 258 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 259 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 260 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 261 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
| 262 | |
| 263 | client1_sendInterest(*interest1); |
| 264 | client1_sendInterest(*interest1); |
| 265 | client1_sendInterest(*interest1); |
| 266 | face1->sendData (*data1); |
| 267 | face1->sendInterest (*interest2); |
| 268 | client1_sendData (*data2); |
| 269 | client1_sendData (*data2); |
| 270 | client1_sendData (*data2); |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 271 | |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 272 | size_t nBytesSent = data1->wireEncode().size() + interest2->wireEncode().size(); |
| 273 | size_t nBytesReceived = interest1->wireEncode().size() * 3 + data2->wireEncode().size() * 3; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 274 | |
| 275 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 276 | "WebSocketChannel error: cannot send or receive Interest/Data packets"); |
| 277 | |
| 278 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 3); |
| 279 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 280 | BOOST_REQUIRE_EQUAL(client1_receivedInterests.size(), 1); |
| 281 | BOOST_REQUIRE_EQUAL(client1_receivedDatas .size(), 1); |
| 282 | |
| 283 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest1->getName()); |
| 284 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 285 | BOOST_CHECK_EQUAL(client1_receivedInterests[0].getName(), interest2->getName()); |
| 286 | BOOST_CHECK_EQUAL(client1_receivedDatas [0].getName(), data1->getName()); |
| 287 | |
| 288 | const FaceCounters& counters1 = face1->getCounters(); |
| 289 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 3); |
| 290 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 291 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1); |
| 292 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 293 | BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesReceived); |
| 294 | BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent); |
Wentao Shang | 9873314 | 2014-09-17 12:13:57 -0700 | [diff] [blame] | 295 | |
| 296 | limitedIo.run(LimitedIo::UNLIMITED_OPS, time::seconds(8)); |
| 297 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Yukai Tu | 2d6d563 | 2015-10-26 11:06:02 -0700 | [diff] [blame] | 300 | BOOST_AUTO_TEST_SUITE_END() // TestWebSocket |
| 301 | BOOST_AUTO_TEST_SUITE_END() // Face |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 302 | |
| 303 | } // namespace tests |
| 304 | } // namespace nfd |