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; |
| 73 | face1->onReceiveInterest += |
| 74 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
| 75 | face1->onReceiveData += |
| 76 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
| 77 | face1->onFail += |
| 78 | bind(&EndToEndFixture::face1_onFail, this); |
| 79 | |
| 80 | limitedIo.afterOp(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | face1_onReceiveInterest(const Interest& interest) |
| 85 | { |
| 86 | face1_receivedInterests.push_back(interest); |
| 87 | |
| 88 | limitedIo.afterOp(); |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | face1_onReceiveData(const Data& data) |
| 93 | { |
| 94 | face1_receivedDatas.push_back(data); |
| 95 | |
| 96 | limitedIo.afterOp(); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | face1_onFail() |
| 101 | { |
| 102 | face1.reset(); |
| 103 | limitedIo.afterOp(); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | client1_onOpen(websocketpp::connection_hdl hdl) |
| 108 | { |
| 109 | handle = hdl; |
| 110 | limitedIo.afterOp(); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | client1_onClose(websocketpp::connection_hdl hdl) |
| 115 | { |
| 116 | limitedIo.afterOp(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | client1_onFail(websocketpp::connection_hdl hdl) |
| 121 | { |
| 122 | limitedIo.afterOp(); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | client1_sendInterest(const Interest& interest) |
| 127 | { |
| 128 | const Block& payload = interest.wireEncode(); |
| 129 | client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | client1_sendData(const Data& data) |
| 134 | { |
| 135 | const Block& payload = data.wireEncode(); |
| 136 | client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | client1_onMessage(websocketpp::connection_hdl hdl, |
| 141 | websocketpp::config::asio_client::message_type::ptr msg) |
| 142 | { |
| 143 | bool isOk = true; |
| 144 | Block element; |
| 145 | const std::string& payload = msg->get_payload(); |
| 146 | isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(payload.c_str()), |
| 147 | payload.size(), element); |
| 148 | if (isOk) |
| 149 | { |
| 150 | try { |
| 151 | if (element.type() == tlv::Interest) |
| 152 | { |
| 153 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 154 | i->wireDecode(element); |
| 155 | client1_onReceiveInterest(*i); |
| 156 | } |
| 157 | else if (element.type() == tlv::Data) |
| 158 | { |
| 159 | shared_ptr<Data> d = make_shared<Data>(); |
| 160 | d->wireDecode(element); |
| 161 | client1_onReceiveData(*d); |
| 162 | } |
| 163 | } |
| 164 | catch (tlv::Error&) { |
| 165 | // Do something? |
| 166 | } |
| 167 | } |
| 168 | limitedIo.afterOp(); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | client1_onReceiveInterest(const Interest& interest) |
| 173 | { |
| 174 | client1_receivedInterests.push_back(interest); |
| 175 | limitedIo.afterOp(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | client1_onReceiveData(const Data& data) |
| 180 | { |
| 181 | client1_receivedDatas.push_back(data); |
| 182 | limitedIo.afterOp(); |
| 183 | } |
| 184 | |
| 185 | public: |
| 186 | LimitedIo limitedIo; |
| 187 | |
| 188 | shared_ptr<Face> face1; |
| 189 | std::vector<Interest> face1_receivedInterests; |
| 190 | std::vector<Data> face1_receivedDatas; |
| 191 | Client client1; |
| 192 | websocketpp::connection_hdl handle; |
| 193 | std::vector<Interest> client1_receivedInterests; |
| 194 | std::vector<Data> client1_receivedDatas; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
| 198 | { |
| 199 | WebSocketFactory factory1("9696"); |
| 200 | |
| 201 | shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070"); |
| 202 | |
| 203 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 204 | |
| 205 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1)); |
| 206 | |
| 207 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 208 | |
| 209 | Client client1; |
| 210 | // Clear all logging info from websocketpp library |
| 211 | client1.clear_access_channels(websocketpp::log::alevel::all); |
| 212 | |
| 213 | client1.init_asio(&getGlobalIoService()); |
| 214 | client1.set_open_handler(bind(&EndToEndFixture::client1_onOpen, this, _1)); |
| 215 | client1.set_close_handler(bind(&EndToEndFixture::client1_onClose, this, _1)); |
| 216 | client1.set_fail_handler(bind(&EndToEndFixture::client1_onFail, this, _1)); |
| 217 | client1.set_message_handler(bind(&EndToEndFixture::client1_onMessage, this, _1, _2)); |
| 218 | |
| 219 | websocketpp::lib::error_code ec; |
| 220 | Client::connection_ptr con = client1.get_connection("ws://127.0.0.1:20070", ec); |
| 221 | client1.connect(con); |
| 222 | |
| 223 | BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 224 | "WebSocketChannel error: cannot connect or cannot accept connection"); |
| 225 | |
| 226 | BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "ws://127.0.0.1:20070"); |
| 227 | |
| 228 | //BOOST_CHECK_EQUAL(face1->isLocal(), true); |
| 229 | |
| 230 | //BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), false); |
| 231 | |
| 232 | shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R"); |
| 233 | shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix"); |
| 234 | shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL"); |
| 235 | shared_ptr<Data> data2 = makeData("ndn:/XNBV796f"); |
| 236 | |
| 237 | client1_sendInterest(*interest1); |
| 238 | client1_sendInterest(*interest1); |
| 239 | client1_sendInterest(*interest1); |
| 240 | face1->sendData (*data1); |
| 241 | face1->sendInterest (*interest2); |
| 242 | client1_sendData (*data2); |
| 243 | client1_sendData (*data2); |
| 244 | client1_sendData (*data2); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame^] | 245 | size_t nBytesSent = data1->wireEncode().size() + interest2->wireEncode().size(); |
| 246 | size_t nBytesReceived = interest1->wireEncode().size() * 3 + data2->wireEncode().size() * 3; |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 247 | |
| 248 | BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 249 | "WebSocketChannel error: cannot send or receive Interest/Data packets"); |
| 250 | |
| 251 | BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 3); |
| 252 | BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3); |
| 253 | BOOST_REQUIRE_EQUAL(client1_receivedInterests.size(), 1); |
| 254 | BOOST_REQUIRE_EQUAL(client1_receivedDatas .size(), 1); |
| 255 | |
| 256 | BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest1->getName()); |
| 257 | BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName()); |
| 258 | BOOST_CHECK_EQUAL(client1_receivedInterests[0].getName(), interest2->getName()); |
| 259 | BOOST_CHECK_EQUAL(client1_receivedDatas [0].getName(), data1->getName()); |
| 260 | |
| 261 | const FaceCounters& counters1 = face1->getCounters(); |
| 262 | BOOST_CHECK_EQUAL(counters1.getNInInterests() , 3); |
| 263 | BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3); |
| 264 | BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1); |
| 265 | BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1); |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame^] | 266 | BOOST_CHECK_EQUAL(counters1.getNInBytes(), nBytesReceived); |
| 267 | BOOST_CHECK_EQUAL(counters1.getNOutBytes(), nBytesSent); |
Wentao Shang | 93ef6c9 | 2014-06-19 11:59:17 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 270 | BOOST_AUTO_TEST_SUITE_END() |
| 271 | |
| 272 | } // namespace tests |
| 273 | } // namespace nfd |