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