loolwsd/test/helpers.hpp | 283 ++++++++++++++++++++++++++++++++++++++++++ loolwsd/test/httpwstest.cpp | 294 +++----------------------------------------- 2 files changed, 307 insertions(+), 270 deletions(-)
New commits: commit c164096c539775e15e96ddcc4f11ff3cb19d60e6 Author: Ashod Nakashian <ashod.nakash...@collabora.co.uk> Date: Thu Apr 28 21:41:00 2016 -0400 loolwsd: factored out common test helpers Change-Id: Iad2794f1b458f04bdd1fb9d3f965afd91adbade8 Reviewed-on: https://gerrit.libreoffice.org/24477 Reviewed-by: Ashod Nakashian <ashnak...@gmail.com> Tested-by: Ashod Nakashian <ashnak...@gmail.com> diff --git a/loolwsd/test/helpers.hpp b/loolwsd/test/helpers.hpp new file mode 100644 index 0000000..24ba1ac --- /dev/null +++ b/loolwsd/test/helpers.hpp @@ -0,0 +1,283 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "config.h" + +#include <algorithm> +#include <condition_variable> +#include <mutex> +#include <thread> +#include <regex> + +#include <Poco/DirectoryIterator.h> +#include <Poco/Dynamic/Var.h> +#include <Poco/FileStream.h> +#include <Poco/JSON/JSON.h> +#include <Poco/JSON/Parser.h> +#include <Poco/Net/AcceptCertificateHandler.h> +#include <Poco/Net/HTTPClientSession.h> +#include <Poco/Net/HTTPRequest.h> +#include <Poco/Net/HTTPResponse.h> +#include <Poco/Net/HTTPSClientSession.h> +#include <Poco/Net/InvalidCertificateHandler.h> +#include <Poco/Net/NetException.h> +#include <Poco/Net/PrivateKeyPassphraseHandler.h> +#include <Poco/Net/SSLManager.h> +#include <Poco/Net/Socket.h> +#include <Poco/Net/WebSocket.h> +#include <Poco/Path.h> +#include <Poco/StreamCopier.h> +#include <Poco/StringTokenizer.h> +#include <Poco/Thread.h> +#include <Poco/URI.h> + +#include <Common.hpp> +#include <UserMessages.hpp> +#include <Util.hpp> +#include <LOOLProtocol.hpp> + +/// Common helper testing functions. +/// Avoid the temptation to reuse from LOOL code! +/// These are supposed to be testing the latter. +namespace helpers +{ +static +void getDocumentPathAndURL(const char* document, std::string& documentPath, std::string& documentURL) +{ + documentPath = Util::getTempFilePath(TDOC, document); + documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); + + std::cerr << "Test file: " << documentPath << std::endl; +} + +static +void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string) +{ + socket.sendFrame(string.data(), string.size()); +} + +static +bool isDocumentLoaded(Poco::Net::WebSocket& ws) +{ + bool isLoaded = false; + try + { + int flags; + int bytes; + int retries = 30; + const Poco::Timespan waitTime(1000000); + + ws.setReceiveTimeout(0); + std::cout << "==> isDocumentLoaded\n"; + do + { + char buffer[READ_BUFFER_SIZE]; + + if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) + { + bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); + std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; + if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) + { + std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; + const std::string line = LOOLProtocol::getFirstLine(buffer, bytes); + const std::string prefixIndicator = "statusindicatorfinish:"; + const std::string prefixStatus = "status:"; + if (line.find(prefixIndicator) == 0 || line.find(prefixStatus) == 0) + { + isLoaded = true; + break; + } + } + retries = 10; + } + else + { + std::cout << "Timeout\n"; + --retries; + } + } + while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); + } + catch (const Poco::Net::WebSocketException& exc) + { + std::cout << exc.message(); + } + + return isLoaded; +} + + +// Connecting to a Kit process is managed by document broker, that it does several +// jobs to establish the bridge connection between the Client and Kit process, +// The result, it is mostly time outs to get messages in the unit test and it could fail. +// connectLOKit ensures the websocket is connected to a kit process. +static +std::shared_ptr<Poco::Net::WebSocket> +connectLOKit(Poco::URI uri, + Poco::Net::HTTPRequest& request, + Poco::Net::HTTPResponse& response) +{ + int flags; + int received = 0; + int retries = 3; + bool ready = false; + char buffer[READ_BUFFER_SIZE]; + std::shared_ptr<Poco::Net::WebSocket> ws; + + do + { +#if ENABLE_SSL + Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); +#else + Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); +#endif + std::cerr << "Connecting... "; + ws = std::make_shared<Poco::Net::WebSocket>(session, request, response); + + do + { + try + { + received = ws->receiveFrame(buffer, sizeof(buffer), flags); + if (received > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) + { + const std::string message = LOOLProtocol::getFirstLine(buffer, received); + std::cerr << message << std::endl; + if (message.find("ready") != std::string::npos) + { + ready = true; + break; + } + } + } + catch (const Poco::TimeoutException& exc) + { + std::cout << exc.displayText() << std::endl; + } + } + while (received > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); + } + while (retries-- && !ready); + + if (!ready) + throw Poco::Net::WebSocketException("Failed to connect to lokit process", Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY); + + return ws; +} + +static +void getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string& response, const bool isLine) +{ + try + { + int flags; + int bytes; + int retries = 20; + const Poco::Timespan waitTime(1000000); + + response.clear(); + ws.setReceiveTimeout(0); + std::cout << "==> getResponseMessage(" << prefix << ")\n"; + do + { + char buffer[READ_BUFFER_SIZE]; + + if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) + { + bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); + std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; + if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) + { + std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; + const std::string message = isLine ? + LOOLProtocol::getFirstLine(buffer, bytes) : + std::string(buffer, bytes); + + if (message.find(prefix) == 0) + { + response = message.substr(prefix.length()); + break; + } + } + retries = 10; + } + else + { + std::cout << "Timeout\n"; + --retries; + } + } + while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); + } + catch (const Poco::Net::WebSocketException& exc) + { + std::cout << exc.message(); + } +} + +static +std::shared_ptr<Poco::Net::WebSocket> loadDocAndGetSocket(const Poco::URI& uri, const std::string& documentURL) +{ + try + { + // Load a document and get its status. + Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); + Poco::Net::HTTPResponse response; + auto socket = connectLOKit(uri, request, response); + + sendTextFrame(*socket, "load url=" + documentURL); + CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(*socket)); + + return socket; + } + catch (const Poco::Exception& exc) + { + CPPUNIT_FAIL(exc.displayText()); + } + + // Really couldn't reach here, but the compiler doesn't know any better. + return nullptr; +} + +static +void SocketProcessor(std::string name, + const std::shared_ptr<Poco::Net::WebSocket>& socket, + std::function<bool(const std::string& msg)> handler) +{ + if (!name.empty()) + { + name += ' '; + } + + int flags; + int n; + do + { + char buffer[READ_BUFFER_SIZE]; + n = socket->receiveFrame(buffer, sizeof(buffer), flags); + if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) + { + std::cout << name + "Got " << n << " bytes: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl; + if (!handler(std::string(buffer, n))) + { + break; + } + } + else + { + std::cerr << name + "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl; + } + } + while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index c594c0d..f57d710 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -43,8 +43,11 @@ #include <Util.hpp> #include <LOOLProtocol.hpp> +#include "helpers.hpp" #include "countloolkits.hpp" +using namespace helpers; + /// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test. class HTTPWSTest : public CPPUNIT_NS::TestFixture { @@ -112,23 +115,6 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture void loadDoc(const std::string& documentURL); - static - void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string); - - static - bool isDocumentLoaded(Poco::Net::WebSocket& socket); - - static - void getResponseMessage(Poco::Net::WebSocket& socket, - const std::string& prefix, - std::string& response, - const bool isLine); - - static - void SocketProcessor(std::string name, - const std::shared_ptr<Poco::Net::WebSocket>& socket, - std::function<bool(const std::string& msg)> handler); - void checkTiles(Poco::Net::WebSocket& socket, const std::string& type); @@ -143,12 +129,6 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture void getPartHashCodes(const std::string response, std::vector<std::string>& parts); - std::shared_ptr<Poco::Net::WebSocket> - connectLOKit(Poco::Net::HTTPRequest& request, - Poco::Net::HTTPResponse& response); - - std::shared_ptr<Poco::Net::WebSocket> loadDocAndGetSocket(const std::string& documentURL); - public: HTTPWSTest() #if ENABLE_SSL @@ -181,15 +161,6 @@ public: void tearDown() { } - -protected: - void getDocumentPathAndURL(const char* document, std::string& documentPath, std::string& documentURL) - { - documentPath = Util::getTempFilePath(TDOC, document); - documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); - - std::cerr << "Test file: " << documentPath << std::endl; - } }; int HTTPWSTest::_initialLoolKitCount = 0; @@ -323,7 +294,7 @@ void HTTPWSTest::testCloseAfterClose() getDocumentPathAndURL("hello.odt", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -359,7 +330,7 @@ void HTTPWSTest::loadDoc(const std::string& documentURL) try { // Load a document and get its status. - auto socket = loadDocAndGetSocket(documentURL); + auto socket = loadDocAndGetSocket(_uri, documentURL); std::string status; std::string editlock; @@ -405,7 +376,7 @@ void HTTPWSTest::testBadLoad() getDocumentPathAndURL("hello.odt", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); // Before loading request status. sendTextFrame(socket, "status"); @@ -464,7 +435,7 @@ void HTTPWSTest::testSaveOnDisconnect() { // Load a document and get its status. Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); @@ -490,7 +461,7 @@ void HTTPWSTest::testSaveOnDisconnect() { // Load the same document and check that the last changes (pasted text) is saved. Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -540,7 +511,7 @@ void HTTPWSTest::testReloadWhileDisconnecting() { // Load a document and get its status. Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); @@ -564,7 +535,7 @@ void HTTPWSTest::testReloadWhileDisconnecting() { // Load the same document and check that the last changes (pasted text) is saved. Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -622,7 +593,7 @@ void HTTPWSTest::testExcelLoad() getDocumentPathAndURL("timeline.xlsx", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -670,7 +641,7 @@ void HTTPWSTest::testPaste() getDocumentPathAndURL("hello.odt", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -725,7 +696,7 @@ void HTTPWSTest::testLargePaste() getDocumentPathAndURL("hello.odt", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); sendTextFrame(socket, "status"); @@ -779,7 +750,7 @@ void HTTPWSTest::testRenderingOptions() const std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}"; Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL + " options=" + options); sendTextFrame(socket, "status"); @@ -833,7 +804,7 @@ void HTTPWSTest::testPasswordProtectedDocumentWithoutPassword() getDocumentPathAndURL("password-protected.ods", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); // Send a load request without password first sendTextFrame(socket, "load url=" + documentURL); @@ -869,7 +840,7 @@ void HTTPWSTest::testPasswordProtectedDocumentWithWrongPassword() getDocumentPathAndURL("password-protected.ods", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); // Send a load request with incorrect password sendTextFrame(socket, "load url=" + documentURL + " password=2"); @@ -905,7 +876,7 @@ void HTTPWSTest::testPasswordProtectedDocumentWithCorrectPassword() const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); // Send a load request with correct password sendTextFrame(socket, "load url=" + documentURL + " password=1"); @@ -937,7 +908,7 @@ void HTTPWSTest::testInsertDelete() getDocumentPathAndURL("insert-delete.odp", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); @@ -1053,7 +1024,7 @@ void HTTPWSTest::testClientPartImpress() getDocumentPathAndURL("setclientpart.odp", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); @@ -1078,7 +1049,7 @@ void HTTPWSTest::testClientPartCalc() getDocumentPathAndURL("setclientpart.ods", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket = *connectLOKit(request, _response); + Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response); sendTextFrame(socket, "load url=" + documentURL); CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket)); @@ -1100,10 +1071,10 @@ void HTTPWSTest::testSimultaneousTilesRenderedJustOnce() getDocumentPathAndURL("hello.odt", documentPath, documentURL); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::WebSocket socket1 = *connectLOKit(request, _response); + Poco::Net::WebSocket socket1 = *connectLOKit(_uri, request, _response); sendTextFrame(socket1, "load url=" + documentURL); - Poco::Net::WebSocket socket2 = *connectLOKit(request, _response); + Poco::Net::WebSocket socket2 = *connectLOKit(_uri, request, _response); sendTextFrame(socket2, "load url=" + documentURL); sendTextFrame(socket1, "tile part=42 width=400 height=400 tileposx=1000 tileposy=2000 tilewidth=3000 tileheight=3000"); @@ -1151,7 +1122,7 @@ void HTTPWSTest::testEditLock() try { std::cerr << "First client loading." << std::endl; - auto socket = loadDocAndGetSocket(documentURL); + auto socket = loadDocAndGetSocket(_uri, documentURL); std::string editlock1; SocketProcessor("First", socket, [&](const std::string& msg) { @@ -1211,7 +1182,7 @@ void HTTPWSTest::testEditLock() try { std::cerr << "Second client loading." << std::endl; - auto socket = loadDocAndGetSocket(documentURL); + auto socket = loadDocAndGetSocket(_uri, documentURL); std::string editlock1; SocketProcessor("Second", socket, [&](const std::string& msg) { @@ -1259,110 +1230,6 @@ void HTTPWSTest::testNoExtraLoolKitsLeft() CPPUNIT_ASSERT_EQUAL(_initialLoolKitCount, countNow); } -void HTTPWSTest::sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string) -{ - socket.sendFrame(string.data(), string.size()); -} - -bool HTTPWSTest::isDocumentLoaded(Poco::Net::WebSocket& ws) -{ - bool isLoaded = false; - try - { - int flags; - int bytes; - int retries = 30; - const Poco::Timespan waitTime(1000000); - - ws.setReceiveTimeout(0); - std::cout << "==> isDocumentLoaded\n"; - do - { - char buffer[READ_BUFFER_SIZE]; - - if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) - { - bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); - std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; - if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) - { - std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; - const std::string line = LOOLProtocol::getFirstLine(buffer, bytes); - const std::string prefixIndicator = "statusindicatorfinish:"; - const std::string prefixStatus = "status:"; - if (line.find(prefixIndicator) == 0 || line.find(prefixStatus) == 0) - { - isLoaded = true; - break; - } - } - retries = 10; - } - else - { - std::cout << "Timeout\n"; - --retries; - } - } - while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); - } - catch (const Poco::Net::WebSocketException& exc) - { - std::cout << exc.message(); - } - - return isLoaded; -} - -void HTTPWSTest::getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string& response, const bool isLine) -{ - try - { - int flags; - int bytes; - int retries = 20; - const Poco::Timespan waitTime(1000000); - - response.clear(); - ws.setReceiveTimeout(0); - std::cout << "==> getResponseMessage(" << prefix << ")\n"; - do - { - char buffer[READ_BUFFER_SIZE]; - - if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ)) - { - bytes = ws.receiveFrame(buffer, sizeof(buffer), flags); - std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n'; - if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) - { - std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n'; - const std::string message = isLine ? - LOOLProtocol::getFirstLine(buffer, bytes) : - std::string(buffer, bytes); - - if (message.find(prefix) == 0) - { - response = message.substr(prefix.length()); - break; - } - } - retries = 10; - } - else - { - std::cout << "Timeout\n"; - --retries; - } - } - while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); - } - catch (const Poco::Net::WebSocketException& exc) - { - std::cout << exc.message(); - } -} - void HTTPWSTest::checkTiles(Poco::Net::WebSocket& socket, const std::string& docType) { const std::string current = "current="; @@ -1440,7 +1307,6 @@ void HTTPWSTest::checkTiles(Poco::Net::WebSocket& socket, const std::string& doc } } - void HTTPWSTest::requestTiles(Poco::Net::WebSocket& socket, const int part, const int docWidth, const int docHeight) { // twips @@ -1620,118 +1486,6 @@ void HTTPWSTest::getPartHashCodes(const std::string response, } } -// Connecting to a Kit process is managed by document broker, that it does several -// jobs to establish the bridge connection between the Client and Kit process, -// The result, it is mostly time outs to get messages in the unit test and it could fail. -// connectLOKit ensures the websocket is connected to a kit process. - -std::shared_ptr<Poco::Net::WebSocket> -HTTPWSTest::connectLOKit(Poco::Net::HTTPRequest& request, - Poco::Net::HTTPResponse& response) -{ - int flags; - int received = 0; - int retries = 3; - bool ready = false; - char buffer[READ_BUFFER_SIZE]; - std::shared_ptr<Poco::Net::WebSocket> ws; - - do - { -#if ENABLE_SSL - Poco::Net::HTTPSClientSession session(_uri.getHost(), _uri.getPort()); -#else - Poco::Net::HTTPClientSession session(_uri.getHost(), _uri.getPort()); -#endif - std::cerr << "Connecting... "; - ws = std::make_shared<Poco::Net::WebSocket>(session, request, response); - - do - { - try - { - received = ws->receiveFrame(buffer, sizeof(buffer), flags); - if (received > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) - { - const std::string message = LOOLProtocol::getFirstLine(buffer, received); - std::cerr << message << std::endl; - if (message.find("ready") != std::string::npos) - { - ready = true; - break; - } - } - } - catch (const Poco::TimeoutException& exc) - { - std::cout << exc.displayText() << std::endl; - } - } - while (received > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); - } - while (retries-- && !ready); - - if (!ready) - throw Poco::Net::WebSocketException("Failed to connect to lokit process", Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY); - - return ws; -} - -std::shared_ptr<Poco::Net::WebSocket> HTTPWSTest::loadDocAndGetSocket(const std::string& documentURL) -{ - try - { - // Load a document and get its status. - Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); - Poco::Net::HTTPResponse response; - auto socket = connectLOKit(request, response); - - sendTextFrame(*socket, "load url=" + documentURL); - CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(*socket)); - - return socket; - } - catch (const Poco::Exception& exc) - { - CPPUNIT_FAIL(exc.displayText()); - } - - // Really couldn't reach here, but the compiler doesn't know any better. - return nullptr; -} - - -void HTTPWSTest::SocketProcessor(std::string name, - const std::shared_ptr<Poco::Net::WebSocket>& socket, - std::function<bool(const std::string& msg)> handler) -{ - if (!name.empty()) - { - name += ' '; - } - - int flags; - int n; - do - { - char buffer[READ_BUFFER_SIZE]; - n = socket->receiveFrame(buffer, sizeof(buffer), flags); - if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) - { - std::cout << name + "Got " << n << " bytes: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl; - if (!handler(std::string(buffer, n))) - { - break; - } - } - else - { - std::cerr << name + "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl; - } - } - while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); -} - CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSTest); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits