From fe5545d5917e36ca46c93a401a64d00d009defc6 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sun, 19 Feb 2017 12:06:45 -0500 Subject: [PATCH] nb: websocket echo test The new test sends data of of 1 byte to N bytes and expects the exact same data returned in response. This tests both buffering sizes and websocket frames. Change-Id: Ic6232b4e899d82d90a0ee7c96e4852ffaaf8e958 Reviewed-on: https://gerrit.libreoffice.org/34441 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian --- Makefile.am | 4 +++- net/clientnb.cpp | 39 ++++++++++++++++++++++++++++++++++++--- net/loolnb.cpp | 26 ++++++++++++++++++-------- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7d5a4d403..6fdb08643 100644 --- a/Makefile.am +++ b/Makefile.am @@ -93,7 +93,9 @@ loolnb_SOURCES = net/loolnb.cpp \ common/Log.cpp \ common/Util.cpp -clientnb_SOURCES = net/clientnb.cpp +clientnb_SOURCES = net/clientnb.cpp \ + common/Log.cpp \ + common/Util.cpp # build a binary with no caps to help debugging loolforkit_nocaps_SOURCES = $(loolforkit_SOURCES) diff --git a/net/clientnb.cpp b/net/clientnb.cpp index 7aadaa106..5467fe9e2 100644 --- a/net/clientnb.cpp +++ b/net/clientnb.cpp @@ -37,6 +37,8 @@ #include #include +#include "Util.hpp" + using Poco::Net::HTTPClientSession; using Poco::Net::HTTPRequest; using Poco::Net::HTTPResponse; @@ -192,9 +194,9 @@ struct Client : public Poco::Util::Application snakes[i].join(); } - void testWebsocket() + void testWebsocketPingPong() { - std::cerr << "testwebsocket\n"; + std::cerr << "testWebsocketPingPong\n"; Session session("ws", EnableHttps); std::shared_ptr ws = session.getWebSocket(); @@ -213,6 +215,36 @@ struct Client : public Poco::Util::Application } } + void testWebsocketEcho() + { + std::cerr << "testwebsocketEcho\n"; + Session session("ws", EnableHttps); + std::shared_ptr ws = session.getWebSocket(); + + std::vector res; + for (size_t i = 1; i < (1 << 14); ++i) + { + std::cerr << "\n" << i; + const std::vector data = Util::rng::getBytes(i); + ws->sendFrame(data.data(), data.size(), WebSocket::SendFlags::FRAME_BINARY); + + res.resize(i); + int flags; + int recvd = ws->receiveFrame(res.data(), res.size(), flags); + assert(recvd == static_cast(i)); + + if (i == sizeof(size_t)) + { + assert(*reinterpret_cast(res.data()) == + *reinterpret_cast(data.data()) + 1); + } + else + { + assert(res == data); + } + } + } + public: int main(const std::vector& args) override { @@ -230,7 +262,8 @@ public: Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext); } - testWebsocket(); + testWebsocketPingPong(); + testWebsocketEcho(); testPing(); testLadder(); diff --git a/net/loolnb.cpp b/net/loolnb.cpp index b33ed7ebb..52f048d03 100644 --- a/net/loolnb.cpp +++ b/net/loolnb.cpp @@ -249,15 +249,25 @@ public: else std::cerr << " binary\n"; - // ping pong test - assert (data.size() >= sizeof(size_t)); - size_t *countPtr = reinterpret_cast(&data[0]); - size_t count = *countPtr; - count++; - std::cerr << "count is " << count << "\n"; std::vector reply; - reply.insert(reply.end(), reinterpret_cast(&count), - reinterpret_cast(&count) + sizeof(count)); + if (data.size() == sizeof(size_t)) + { + // ping pong test + assert (data.size() >= sizeof(size_t)); + size_t *countPtr = reinterpret_cast(&data[0]); + size_t count = *countPtr; + count++; + std::cerr << "count is " << count << "\n"; + reply.insert(reply.end(), reinterpret_cast(&count), + reinterpret_cast(&count) + sizeof(count)); + } + else + { + // echo tests + reply.insert(reply.end(), data.begin(), data.end()); + } + + std::cerr << "reply: " << reply.size() << std::endl; queueWSMessage(reply); }