test: basic unit-test for WebSocket face
Change-Id: I41dcc2ecff57225db1aff8e359d507f0e89d4c96
Refs: #1553
diff --git a/daemon/face/websocket-channel.cpp b/daemon/face/websocket-channel.cpp
index 5cf98f6..234faff 100644
--- a/daemon/face/websocket-channel.cpp
+++ b/daemon/face/websocket-channel.cpp
@@ -44,6 +44,8 @@
m_server.set_open_handler(bind(&WebSocketChannel::handleOpen, this, _1));
m_server.set_close_handler(bind(&WebSocketChannel::handleClose, this, _1));
m_server.init_asio(&getGlobalIoService());
+ // Always set SO_REUSEADDR flag
+ m_server.set_reuse_addr(true);
this->setUri(FaceUri(localEndpoint, "ws"));
}
diff --git a/daemon/face/websocket-channel.hpp b/daemon/face/websocket-channel.hpp
index 05d8f99..62c4940 100644
--- a/daemon/face/websocket-channel.hpp
+++ b/daemon/face/websocket-channel.hpp
@@ -85,6 +85,9 @@
size_t
size() const;
+ bool
+ isListening() const;
+
private:
void
handleMessage(websocketpp::connection_hdl hdl, websocket::Server::message_ptr msg);
@@ -117,6 +120,12 @@
};
+inline bool
+WebSocketChannel::isListening() const
+{
+ return m_isListening;
+}
+
} // namespace nfd
#endif // NFD_DAEMON_FACE_WEBSOCKET_CHANNEL_HPP
diff --git a/tests/daemon/face/websocket.cpp b/tests/daemon/face/websocket.cpp
index 6897a2a..6eb8dc8 100644
--- a/tests/daemon/face/websocket.cpp
+++ b/tests/daemon/face/websocket.cpp
@@ -25,6 +25,12 @@
#include "face/websocket-factory.hpp"
#include "tests/test-common.hpp"
+#include "tests/limited-io.hpp"
+
+#include <websocketpp/config/asio_no_tls_client.hpp>
+#include <websocketpp/client.hpp>
+
+typedef websocketpp::client<websocketpp::config::asio_client> Client;
namespace nfd {
namespace tests {
@@ -56,6 +62,209 @@
BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
}
+class EndToEndFixture : protected BaseFixture
+{
+public:
+ void
+ channel1_onFaceCreated(const shared_ptr<Face>& newFace)
+ {
+ BOOST_CHECK(!static_cast<bool>(face1));
+ face1 = newFace;
+ face1->onReceiveInterest +=
+ bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
+ face1->onReceiveData +=
+ bind(&EndToEndFixture::face1_onReceiveData, this, _1);
+ face1->onFail +=
+ bind(&EndToEndFixture::face1_onFail, this);
+
+ limitedIo.afterOp();
+ }
+
+ void
+ face1_onReceiveInterest(const Interest& interest)
+ {
+ face1_receivedInterests.push_back(interest);
+
+ limitedIo.afterOp();
+ }
+
+ void
+ face1_onReceiveData(const Data& data)
+ {
+ face1_receivedDatas.push_back(data);
+
+ limitedIo.afterOp();
+ }
+
+ void
+ face1_onFail()
+ {
+ face1.reset();
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_onOpen(websocketpp::connection_hdl hdl)
+ {
+ handle = hdl;
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_onClose(websocketpp::connection_hdl hdl)
+ {
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_onFail(websocketpp::connection_hdl hdl)
+ {
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_sendInterest(const Interest& interest)
+ {
+ const Block& payload = interest.wireEncode();
+ client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
+ }
+
+ void
+ client1_sendData(const Data& data)
+ {
+ const Block& payload = data.wireEncode();
+ client1.send(handle, payload.wire(), payload.size(), websocketpp::frame::opcode::binary);
+ }
+
+ void
+ client1_onMessage(websocketpp::connection_hdl hdl,
+ websocketpp::config::asio_client::message_type::ptr msg)
+ {
+ bool isOk = true;
+ Block element;
+ const std::string& payload = msg->get_payload();
+ isOk = Block::fromBuffer(reinterpret_cast<const uint8_t*>(payload.c_str()),
+ payload.size(), element);
+ if (isOk)
+ {
+ try {
+ if (element.type() == tlv::Interest)
+ {
+ shared_ptr<Interest> i = make_shared<Interest>();
+ i->wireDecode(element);
+ client1_onReceiveInterest(*i);
+ }
+ else if (element.type() == tlv::Data)
+ {
+ shared_ptr<Data> d = make_shared<Data>();
+ d->wireDecode(element);
+ client1_onReceiveData(*d);
+ }
+ }
+ catch (tlv::Error&) {
+ // Do something?
+ }
+ }
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_onReceiveInterest(const Interest& interest)
+ {
+ client1_receivedInterests.push_back(interest);
+ limitedIo.afterOp();
+ }
+
+ void
+ client1_onReceiveData(const Data& data)
+ {
+ client1_receivedDatas.push_back(data);
+ limitedIo.afterOp();
+ }
+
+public:
+ LimitedIo limitedIo;
+
+ shared_ptr<Face> face1;
+ std::vector<Interest> face1_receivedInterests;
+ std::vector<Data> face1_receivedDatas;
+ Client client1;
+ websocketpp::connection_hdl handle;
+ std::vector<Interest> client1_receivedInterests;
+ std::vector<Data> client1_receivedDatas;
+
+ std::list< shared_ptr<Face> > faces;
+};
+
+BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
+{
+ WebSocketFactory factory1("9696");
+
+ shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
+
+ BOOST_CHECK_EQUAL(channel1->isListening(), false);
+
+ channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1));
+
+ BOOST_CHECK_EQUAL(channel1->isListening(), true);
+
+ Client client1;
+ // Clear all logging info from websocketpp library
+ client1.clear_access_channels(websocketpp::log::alevel::all);
+
+ client1.init_asio(&getGlobalIoService());
+ client1.set_open_handler(bind(&EndToEndFixture::client1_onOpen, this, _1));
+ client1.set_close_handler(bind(&EndToEndFixture::client1_onClose, this, _1));
+ client1.set_fail_handler(bind(&EndToEndFixture::client1_onFail, this, _1));
+ client1.set_message_handler(bind(&EndToEndFixture::client1_onMessage, this, _1, _2));
+
+ websocketpp::lib::error_code ec;
+ Client::connection_ptr con = client1.get_connection("ws://127.0.0.1:20070", ec);
+ client1.connect(con);
+
+ BOOST_CHECK_MESSAGE(limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS,
+ "WebSocketChannel error: cannot connect or cannot accept connection");
+
+ BOOST_CHECK_EQUAL(face1->getLocalUri().toString(), "ws://127.0.0.1:20070");
+
+ //BOOST_CHECK_EQUAL(face1->isLocal(), true);
+
+ //BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(face1)), false);
+
+ shared_ptr<Interest> interest1 = makeInterest("ndn:/TpnzGvW9R");
+ shared_ptr<Data> data1 = makeData("ndn:/KfczhUqVix");
+ shared_ptr<Interest> interest2 = makeInterest("ndn:/QWiIMfj5sL");
+ shared_ptr<Data> data2 = makeData("ndn:/XNBV796f");
+
+ client1_sendInterest(*interest1);
+ client1_sendInterest(*interest1);
+ client1_sendInterest(*interest1);
+ face1->sendData (*data1);
+ face1->sendInterest (*interest2);
+ client1_sendData (*data2);
+ client1_sendData (*data2);
+ client1_sendData (*data2);
+
+ BOOST_CHECK_MESSAGE(limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS,
+ "WebSocketChannel error: cannot send or receive Interest/Data packets");
+
+ BOOST_REQUIRE_EQUAL(face1_receivedInterests.size(), 3);
+ BOOST_REQUIRE_EQUAL(face1_receivedDatas .size(), 3);
+ BOOST_REQUIRE_EQUAL(client1_receivedInterests.size(), 1);
+ BOOST_REQUIRE_EQUAL(client1_receivedDatas .size(), 1);
+
+ BOOST_CHECK_EQUAL(face1_receivedInterests[0].getName(), interest1->getName());
+ BOOST_CHECK_EQUAL(face1_receivedDatas [0].getName(), data2->getName());
+ BOOST_CHECK_EQUAL(client1_receivedInterests[0].getName(), interest2->getName());
+ BOOST_CHECK_EQUAL(client1_receivedDatas [0].getName(), data1->getName());
+
+ const FaceCounters& counters1 = face1->getCounters();
+ BOOST_CHECK_EQUAL(counters1.getNInInterests() , 3);
+ BOOST_CHECK_EQUAL(counters1.getNInDatas() , 3);
+ BOOST_CHECK_EQUAL(counters1.getNOutInterests(), 1);
+ BOOST_CHECK_EQUAL(counters1.getNOutDatas() , 1);
+}
+
BOOST_AUTO_TEST_SUITE_END()
} // namespace tests
diff --git a/websocketpp b/websocketpp
index e7ce038..65cc376 160000
--- a/websocketpp
+++ b/websocketpp
@@ -1 +1 @@
-Subproject commit e7ce038207e2c1727a490421afbbe8e41b242318
+Subproject commit 65cc3765a892ee5928160ba478178e747233aa6c
diff --git a/wscript b/wscript
index 677d5e9..7b42d28 100644
--- a/wscript
+++ b/wscript
@@ -64,7 +64,7 @@
conf.check_cfg(package='libndn-cxx', args=['--cflags', '--libs'],
uselib_store='NDN_CXX', mandatory=True)
- boost_libs = 'system chrono program_options'
+ boost_libs = 'system chrono program_options random'
if conf.options.with_tests:
conf.env['WITH_TESTS'] = 1
conf.define('WITH_TESTS', 1);