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