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