2015-10-28 04:34:20 -05:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2016-04-08 06:49:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-04-27 06:17:53 -05:00
|
|
|
#include <algorithm>
|
2016-04-24 13:59:23 -05:00
|
|
|
#include <regex>
|
|
|
|
|
2016-04-13 05:13:01 -05:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2016-04-13 05:19:26 -05:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
2016-04-13 05:13:01 -05:00
|
|
|
#include <Poco/FileStream.h>
|
2016-04-13 05:19:26 -05:00
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2016-03-21 03:37:39 -05:00
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
2016-04-08 04:24:52 -05:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
2016-04-13 05:19:26 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
2015-10-28 04:34:20 -05:00
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-04-13 05:19:26 -05:00
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
2016-03-21 03:37:39 -05:00
|
|
|
#include <Poco/Net/InvalidCertificateHandler.h>
|
2016-02-18 15:11:01 -06:00
|
|
|
#include <Poco/Net/NetException.h>
|
2016-03-22 13:32:21 -05:00
|
|
|
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
|
2016-03-21 03:37:39 -05:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
2016-04-13 05:19:26 -05:00
|
|
|
#include <Poco/Net/Socket.h>
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
2016-01-05 14:32:53 -06:00
|
|
|
#include <Poco/Path.h>
|
2016-04-13 05:13:01 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
2015-11-19 04:01:34 -06:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2016-04-13 05:13:01 -05:00
|
|
|
#include <Poco/Thread.h>
|
2015-10-28 04:34:20 -05:00
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
2016-03-19 16:35:24 -05:00
|
|
|
|
2016-01-05 14:32:53 -06:00
|
|
|
#include <Common.hpp>
|
2016-04-17 07:42:07 -05:00
|
|
|
#include <UserMessages.hpp>
|
2016-03-19 16:35:24 -05:00
|
|
|
#include <Util.hpp>
|
|
|
|
#include <LOOLProtocol.hpp>
|
2015-10-28 04:34:20 -05:00
|
|
|
|
2016-04-15 09:01:02 -05:00
|
|
|
#include "countloolkits.hpp"
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
/// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test.
|
|
|
|
class HTTPWSTest : public CPPUNIT_NS::TestFixture
|
|
|
|
{
|
2016-01-24 13:58:08 -06:00
|
|
|
const Poco::URI _uri;
|
2015-11-19 04:11:47 -06:00
|
|
|
Poco::Net::HTTPResponse _response;
|
2016-04-13 05:13:01 -05:00
|
|
|
static int _initialLoolKitCount;
|
2015-11-19 04:11:47 -06:00
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE(HTTPWSTest);
|
2016-04-13 05:22:35 -05:00
|
|
|
|
|
|
|
// This should be the first test:
|
2016-04-13 05:13:01 -05:00
|
|
|
CPPUNIT_TEST(testCountHowManyLoolkits);
|
2016-04-13 05:22:35 -05:00
|
|
|
|
2016-04-12 16:44:39 -05:00
|
|
|
CPPUNIT_TEST(testBadRequest);
|
2016-04-13 13:22:56 -05:00
|
|
|
CPPUNIT_TEST(testHandShake);
|
2016-04-18 18:12:26 -05:00
|
|
|
CPPUNIT_TEST(testCloseAfterClose);
|
2016-03-25 13:23:26 -05:00
|
|
|
CPPUNIT_TEST(testLoad);
|
2016-03-29 20:29:53 -05:00
|
|
|
CPPUNIT_TEST(testBadLoad);
|
2016-03-25 13:23:26 -05:00
|
|
|
CPPUNIT_TEST(testReload);
|
2016-04-17 19:34:19 -05:00
|
|
|
CPPUNIT_TEST(testSaveOnDisconnect);
|
2016-04-17 22:31:17 -05:00
|
|
|
CPPUNIT_TEST(testReloadWhileDisconnecting);
|
2016-03-25 13:23:26 -05:00
|
|
|
CPPUNIT_TEST(testExcelLoad);
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST(testPaste);
|
2016-01-18 04:44:35 -06:00
|
|
|
CPPUNIT_TEST(testLargePaste);
|
2015-11-19 04:01:34 -06:00
|
|
|
CPPUNIT_TEST(testRenderingOptions);
|
2016-03-03 09:31:13 -06:00
|
|
|
CPPUNIT_TEST(testPasswordProtectedDocumentWithoutPassword);
|
|
|
|
CPPUNIT_TEST(testPasswordProtectedDocumentWithWrongPassword);
|
|
|
|
CPPUNIT_TEST(testPasswordProtectedDocumentWithCorrectPassword);
|
|
|
|
CPPUNIT_TEST(testPasswordProtectedDocumentWithCorrectPasswordAgain);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_TEST(testInsertDelete);
|
2016-04-27 12:51:23 -05:00
|
|
|
CPPUNIT_TEST(testClientPartImpress);
|
2016-04-27 15:47:28 -05:00
|
|
|
CPPUNIT_TEST(testClientPartCalc);
|
2016-04-21 04:34:30 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
CPPUNIT_TEST(testSimultaneousTilesRenderedJustOnce);
|
|
|
|
#endif
|
2016-04-13 05:22:35 -05:00
|
|
|
|
|
|
|
// This should be the last test:
|
2016-04-13 05:13:01 -05:00
|
|
|
CPPUNIT_TEST(testNoExtraLoolKitsLeft);
|
2016-04-13 05:22:35 -05:00
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-04-13 05:13:01 -05:00
|
|
|
void testCountHowManyLoolkits();
|
2016-04-12 16:44:39 -05:00
|
|
|
void testBadRequest();
|
2016-04-13 13:22:56 -05:00
|
|
|
void testHandShake();
|
2016-04-18 18:12:26 -05:00
|
|
|
void testCloseAfterClose();
|
2016-03-25 13:23:26 -05:00
|
|
|
void testLoad();
|
2016-03-29 20:29:53 -05:00
|
|
|
void testBadLoad();
|
2016-03-25 13:23:26 -05:00
|
|
|
void testReload();
|
2016-03-29 20:29:53 -05:00
|
|
|
void testSaveOnDisconnect();
|
2016-04-17 22:31:17 -05:00
|
|
|
void testReloadWhileDisconnecting();
|
2016-03-25 13:23:26 -05:00
|
|
|
void testExcelLoad();
|
2015-10-28 04:34:20 -05:00
|
|
|
void testPaste();
|
2016-01-18 04:44:35 -06:00
|
|
|
void testLargePaste();
|
2015-11-19 04:01:34 -06:00
|
|
|
void testRenderingOptions();
|
2016-03-03 09:31:13 -06:00
|
|
|
void testPasswordProtectedDocumentWithoutPassword();
|
|
|
|
void testPasswordProtectedDocumentWithWrongPassword();
|
|
|
|
void testPasswordProtectedDocumentWithCorrectPassword();
|
|
|
|
void testPasswordProtectedDocumentWithCorrectPasswordAgain();
|
2016-04-24 13:59:23 -05:00
|
|
|
void testInsertDelete();
|
2016-04-27 12:51:23 -05:00
|
|
|
void testClientPartImpress();
|
2016-04-27 15:47:28 -05:00
|
|
|
void testClientPartCalc();
|
2016-04-21 04:34:30 -05:00
|
|
|
void testSimultaneousTilesRenderedJustOnce();
|
2016-04-13 05:13:01 -05:00
|
|
|
void testNoExtraLoolKitsLeft();
|
2015-10-28 04:34:20 -05:00
|
|
|
|
2016-04-17 19:33:26 -05:00
|
|
|
void loadDoc(const std::string& documentURL);
|
|
|
|
|
2016-01-24 13:58:08 -06:00
|
|
|
static
|
2015-10-28 04:34:20 -05:00
|
|
|
void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string);
|
2016-01-24 13:58:08 -06:00
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
static
|
|
|
|
bool isDocumentLoaded(Poco::Net::WebSocket& socket);
|
|
|
|
|
2016-02-20 15:13:35 -06:00
|
|
|
static
|
|
|
|
void getResponseMessage(Poco::Net::WebSocket& socket,
|
|
|
|
const std::string& prefix,
|
|
|
|
std::string& response,
|
|
|
|
const bool isLine);
|
2016-04-14 20:50:42 -05:00
|
|
|
|
2016-04-26 22:46:09 -05:00
|
|
|
static
|
2016-04-28 06:27:17 -05:00
|
|
|
void SocketProcessor(std::string name,
|
|
|
|
const std::shared_ptr<Poco::Net::WebSocket>& socket,
|
2016-04-26 22:46:09 -05:00
|
|
|
std::function<bool(const std::string& msg)> handler);
|
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
void checkTiles(Poco::Net::WebSocket& socket,
|
|
|
|
const std::string& type);
|
|
|
|
|
2016-04-27 12:51:23 -05:00
|
|
|
void requestTiles(Poco::Net::WebSocket& socket,
|
|
|
|
const int part,
|
|
|
|
const int docWidth,
|
|
|
|
const int docHeight);
|
|
|
|
|
|
|
|
void getTileMessage(Poco::Net::WebSocket& ws,
|
|
|
|
std::string& tile);
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
void getPartHashCodes(const std::string response,
|
|
|
|
std::vector<std::string>& parts);
|
|
|
|
|
2016-04-14 20:50:42 -05:00
|
|
|
std::shared_ptr<Poco::Net::WebSocket>
|
|
|
|
connectLOKit(Poco::Net::HTTPRequest& request,
|
|
|
|
Poco::Net::HTTPResponse& response);
|
|
|
|
|
2016-04-26 22:46:09 -05:00
|
|
|
std::shared_ptr<Poco::Net::WebSocket> loadDocAndGetSocket(const std::string& documentURL);
|
|
|
|
|
2015-11-19 04:11:47 -06:00
|
|
|
public:
|
|
|
|
HTTPWSTest()
|
2016-04-14 05:42:12 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-04 02:08:18 -05:00
|
|
|
: _uri("https://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER))
|
2016-04-08 04:24:52 -05:00
|
|
|
#else
|
|
|
|
: _uri("http://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER))
|
|
|
|
#endif
|
2015-11-19 04:11:47 -06:00
|
|
|
{
|
2016-04-14 05:42:12 -05:00
|
|
|
#if ENABLE_SSL
|
2016-03-21 03:37:39 -05:00
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
// Just accept the certificate anyway for testing purposes
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
Poco::Net::Context::Params sslParams;
|
|
|
|
Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
|
|
|
|
Poco::Net::SSLManager::instance().initializeClient(0, invalidCertHandler, sslContext);
|
2016-04-08 04:24:52 -05:00
|
|
|
#endif
|
2016-03-21 03:37:39 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 05:42:12 -05:00
|
|
|
#if ENABLE_SSL
|
2016-03-21 03:37:39 -05:00
|
|
|
~HTTPWSTest()
|
|
|
|
{
|
|
|
|
Poco::Net::uninitializeSSL();
|
2015-11-19 04:11:47 -06:00
|
|
|
}
|
2016-04-08 04:24:52 -05:00
|
|
|
#endif
|
2016-01-24 13:58:08 -06:00
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
}
|
2016-04-28 05:35:56 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-10-28 04:34:20 -05:00
|
|
|
};
|
|
|
|
|
2016-04-13 05:13:01 -05:00
|
|
|
int HTTPWSTest::_initialLoolKitCount = 0;
|
|
|
|
|
|
|
|
void HTTPWSTest::testCountHowManyLoolkits()
|
|
|
|
{
|
|
|
|
_initialLoolKitCount = countLoolKitProcesses();
|
|
|
|
CPPUNIT_ASSERT(_initialLoolKitCount > 0);
|
|
|
|
}
|
|
|
|
|
2016-04-12 16:44:39 -05:00
|
|
|
void HTTPWSTest::testBadRequest()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
|
|
|
const std::string documentURL = "file:///fake.doc";
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 05:42:12 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-12 16:44:39 -05:00
|
|
|
Poco::Net::HTTPSClientSession session(_uri.getHost(), _uri.getPort());
|
|
|
|
#else
|
|
|
|
Poco::Net::HTTPClientSession session(_uri.getHost(), _uri.getPort());
|
|
|
|
#endif
|
2016-04-17 10:06:18 -05:00
|
|
|
// This should result in Bad Request, but results in:
|
|
|
|
// WebSocket Exception: Missing Sec-WebSocket-Key in handshake request
|
|
|
|
// So Service Unavailable is returned.
|
2016-04-12 16:44:39 -05:00
|
|
|
|
|
|
|
request.set("Connection", "Upgrade");
|
|
|
|
request.set("Upgrade", "websocket");
|
|
|
|
request.set("Sec-WebSocket-Version", "13");
|
|
|
|
request.set("Sec-WebSocket-Key", "");
|
|
|
|
request.setChunkedTransferEncoding(false);
|
|
|
|
session.setKeepAlive(true);
|
|
|
|
session.sendRequest(request);
|
|
|
|
session.receiveResponse(response);
|
2016-04-17 07:42:07 -05:00
|
|
|
CPPUNIT_ASSERT(response.getStatus() == Poco::Net::HTTPResponse::HTTPResponse::HTTP_SERVICE_UNAVAILABLE);
|
2016-04-12 16:44:39 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-13 13:22:56 -05:00
|
|
|
void HTTPWSTest::testHandShake()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
int flags;
|
|
|
|
char buffer[1024];
|
|
|
|
// Load a document and get its status.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-13 13:22:56 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 05:42:12 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-13 13:22:56 -05:00
|
|
|
Poco::Net::HTTPSClientSession session(_uri.getHost(), _uri.getPort());
|
|
|
|
#else
|
|
|
|
Poco::Net::HTTPClientSession session(_uri.getHost(), _uri.getPort());
|
|
|
|
#endif
|
|
|
|
Poco::Net::WebSocket socket(session, request, response);
|
|
|
|
|
2016-04-17 07:42:07 -05:00
|
|
|
const char* fail = "error:";
|
2016-04-13 13:22:56 -05:00
|
|
|
std::string payload("statusindicator: find");
|
|
|
|
|
|
|
|
std::string receive;
|
2016-04-14 11:00:32 -05:00
|
|
|
socket.setReceiveTimeout(0);
|
2016-04-13 13:22:56 -05:00
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-04-26 04:52:43 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(payload), std::string(buffer, bytes));
|
2016-04-13 13:22:56 -05:00
|
|
|
CPPUNIT_ASSERT(flags == Poco::Net::WebSocket::FRAME_TEXT);
|
|
|
|
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-04-16 12:29:00 -05:00
|
|
|
if (!std::strstr(buffer, fail))
|
2016-04-13 13:22:56 -05:00
|
|
|
{
|
2016-04-16 12:29:00 -05:00
|
|
|
payload = "statusindicator: connect";
|
2016-04-27 02:20:25 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(payload, std::string(buffer, bytes));
|
2016-04-13 13:22:56 -05:00
|
|
|
CPPUNIT_ASSERT(flags == Poco::Net::WebSocket::FRAME_TEXT);
|
2016-04-16 12:29:00 -05:00
|
|
|
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
if (!std::strstr(buffer, fail))
|
|
|
|
{
|
|
|
|
payload = "statusindicator: ready";
|
2016-04-27 02:20:25 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(payload, std::string(buffer, bytes));
|
2016-04-16 12:29:00 -05:00
|
|
|
CPPUNIT_ASSERT(flags == Poco::Net::WebSocket::FRAME_TEXT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-17 07:42:07 -05:00
|
|
|
// check error message
|
|
|
|
CPPUNIT_ASSERT(std::strstr(buffer, SERVICE_UNAVALABLE_INTERNAL_ERROR) != nullptr);
|
2016-04-16 12:29:00 -05:00
|
|
|
CPPUNIT_ASSERT(flags == Poco::Net::WebSocket::FRAME_TEXT);
|
2016-04-17 07:42:07 -05:00
|
|
|
|
|
|
|
// close frame message
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
CPPUNIT_ASSERT(std::strstr(buffer, SERVICE_UNAVALABLE_INTERNAL_ERROR) != nullptr);
|
|
|
|
CPPUNIT_ASSERT((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
2016-04-16 12:29:00 -05:00
|
|
|
}
|
2016-04-13 13:22:56 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-17 07:42:07 -05:00
|
|
|
// check error message
|
|
|
|
CPPUNIT_ASSERT(std::strstr(buffer, SERVICE_UNAVALABLE_INTERNAL_ERROR) != nullptr);
|
2016-04-13 13:22:56 -05:00
|
|
|
CPPUNIT_ASSERT(flags == Poco::Net::WebSocket::FRAME_TEXT);
|
2016-04-17 07:42:07 -05:00
|
|
|
|
|
|
|
// close frame message
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
CPPUNIT_ASSERT(std::strstr(buffer, SERVICE_UNAVALABLE_INTERNAL_ERROR) != nullptr);
|
|
|
|
CPPUNIT_ASSERT((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) == Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
2016-04-13 13:22:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.shutdown();
|
|
|
|
Util::removeFile(documentPath);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-04-18 18:12:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPWSTest::testCloseAfterClose()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
int flags;
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
|
|
|
|
// Load a document and get its status.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-18 18:12:26 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
// send normal socket shutdown
|
|
|
|
socket.shutdown();
|
|
|
|
|
|
|
|
// 5 seconds timeout
|
|
|
|
socket.setReceiveTimeout(5000000);
|
|
|
|
|
|
|
|
// receive close frame handshake
|
|
|
|
do
|
|
|
|
{
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
}
|
|
|
|
while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
|
|
|
|
// no more messages is received.
|
|
|
|
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-04-26 00:15:40 -05:00
|
|
|
std::cout << "Received " << bytes << " bytes, flags: "<< std::hex << flags << std::dec << std::endl;
|
2016-04-18 18:12:26 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, bytes);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0, flags);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-04-13 13:22:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 19:33:26 -05:00
|
|
|
void HTTPWSTest::loadDoc(const std::string& documentURL)
|
2016-03-25 13:23:26 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
2016-04-26 22:46:09 -05:00
|
|
|
auto socket = loadDocAndGetSocket(documentURL);
|
2016-03-25 13:23:26 -05:00
|
|
|
|
|
|
|
std::string status;
|
2016-04-26 22:46:09 -05:00
|
|
|
std::string editlock;
|
2016-04-28 06:27:17 -05:00
|
|
|
SocketProcessor("", socket, [&](const std::string& msg)
|
2016-03-25 13:23:26 -05:00
|
|
|
{
|
2016-04-26 22:46:09 -05:00
|
|
|
const std::string prefix = "status: ";
|
|
|
|
if (msg.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
status = msg.substr(prefix.length());
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 20:29:53 -05:00
|
|
|
|
2016-04-26 22:46:09 -05:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Might be too strict, consider something flexible instread.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("type=text parts=1 current=0 width=12808 height=16408"), status);
|
|
|
|
// First session always gets the lock.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("editlock: 1"), editlock);
|
|
|
|
|
|
|
|
socket->shutdown();
|
2016-03-29 20:29:53 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-03-29 20:29:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 19:33:26 -05:00
|
|
|
void HTTPWSTest::testLoad()
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-17 19:33:26 -05:00
|
|
|
loadDoc(documentURL);
|
|
|
|
Util::removeFile(documentPath);
|
|
|
|
}
|
|
|
|
|
2016-03-29 20:29:53 -05:00
|
|
|
void HTTPWSTest::testBadLoad()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-03-29 20:29:53 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-29 20:29:53 -05:00
|
|
|
|
|
|
|
// Before loading request status.
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
|
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
|
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
2016-04-11 02:00:45 -05:00
|
|
|
|
|
|
|
// For some reason the server claims a client has the 'edit lock' even if no
|
|
|
|
// document has been successfully loaded
|
2016-04-13 13:22:56 -05:00
|
|
|
if (LOOLProtocol::getFirstToken(buffer, n) == "editlock:" ||
|
|
|
|
LOOLProtocol::getFirstToken(buffer, n) == "statusindicator:")
|
2016-04-11 02:00:45 -05:00
|
|
|
continue;
|
|
|
|
|
2016-03-29 20:29:53 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("error: cmd=status kind=nodocloaded"), line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
|
2016-03-25 13:23:26 -05:00
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-03-25 13:23:26 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-03-25 13:23:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPWSTest::testReload()
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-03-25 13:23:26 -05:00
|
|
|
for (auto i = 0; i < 3; ++i)
|
|
|
|
{
|
2016-04-17 19:33:26 -05:00
|
|
|
loadDoc(documentURL);
|
2016-03-25 13:23:26 -05:00
|
|
|
}
|
2016-04-17 19:33:26 -05:00
|
|
|
|
|
|
|
Util::removeFile(documentPath);
|
2016-03-25 13:23:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 20:29:53 -05:00
|
|
|
void HTTPWSTest::testSaveOnDisconnect()
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-17 19:34:19 -05:00
|
|
|
|
2016-03-29 20:29:53 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-29 20:29:53 -05:00
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "uno .uno:Delete");
|
|
|
|
sendTextFrame(socket, "paste mimetype=text/plain;charset=utf-8\naaa bbb ccc");
|
|
|
|
|
2016-04-28 06:27:17 -05:00
|
|
|
std::cerr << "Closing connection after pasting." << std::endl;
|
|
|
|
|
2016-04-17 19:34:19 -05:00
|
|
|
// Shutdown abruptly.
|
2016-03-29 20:29:53 -05:00
|
|
|
socket.shutdown();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-03-29 20:29:53 -05:00
|
|
|
}
|
|
|
|
|
2016-04-17 19:34:19 -05:00
|
|
|
// Allow time to save and destroy before we connect again.
|
|
|
|
sleep(5);
|
2016-04-28 06:27:17 -05:00
|
|
|
std::cerr << "Loading again." << std::endl;
|
2016-03-29 20:29:53 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load the same document and check that the last changes (pasted text) is saved.
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-29 20:29:53 -05:00
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
// Check if the document contains the pasted text.
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "gettextselection mimetype=text/plain;charset=utf-8");
|
|
|
|
std::string selection;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
const std::string prefix = "textselectioncontent: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
selection = line.substr(prefix.length());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-03-29 20:29:53 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-03-29 20:29:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 22:31:17 -05:00
|
|
|
void HTTPWSTest::testReloadWhileDisconnecting()
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-17 22:31:17 -05:00
|
|
|
|
|
|
|
int kitcount = -1;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "uno .uno:Delete");
|
|
|
|
sendTextFrame(socket, "paste mimetype=text/plain;charset=utf-8\naaa bbb ccc");
|
|
|
|
|
|
|
|
kitcount = countLoolKitProcesses();
|
|
|
|
|
|
|
|
// Shutdown abruptly.
|
|
|
|
socket.shutdown();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Loading again." << std::endl;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
// Should have no new instances.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(kitcount, countLoolKitProcesses());
|
|
|
|
|
|
|
|
// Check if the document contains the pasted text.
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "gettextselection mimetype=text/plain;charset=utf-8");
|
|
|
|
std::string selection;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
if (line.find("editlock: ") == 0)
|
|
|
|
{
|
|
|
|
// We must have the editlock, otherwise we aren't alone.
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("editlock: 1"), line);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string prefix = "textselectioncontent: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
selection = line.substr(prefix.length());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
|
|
|
Util::removeFile(documentPath);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 13:23:26 -05:00
|
|
|
void HTTPWSTest::testExcelLoad()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and make it empty.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("timeline.xlsx", documentPath, documentURL);
|
2016-03-25 13:23:26 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-25 13:23:26 -05:00
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
std::string status;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
std::string prefix = "status: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
status = line.substr(prefix.length());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-03-25 13:23:26 -05:00
|
|
|
// Expected format is something like 'type=text parts=2 current=0 width=12808 height=1142'.
|
|
|
|
Poco::StringTokenizer tokens(status, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count());
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-03-25 13:23:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
void HTTPWSTest::testPaste()
|
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
try
|
2015-10-28 04:34:20 -05:00
|
|
|
{
|
2016-03-29 20:29:53 -05:00
|
|
|
// Load a document and make it empty, then paste some text into it.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-02-18 15:11:01 -06:00
|
|
|
|
2016-03-13 12:23:12 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "uno .uno:Delete");
|
|
|
|
|
|
|
|
// Paste some text into it.
|
|
|
|
sendTextFrame(socket, "paste mimetype=text/plain;charset=utf-8\naaa bbb ccc");
|
|
|
|
|
|
|
|
// Check if the document contains the pasted text.
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "gettextselection mimetype=text/plain;charset=utf-8");
|
|
|
|
std::string selection;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
2015-10-28 04:34:20 -05:00
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
2016-02-22 05:48:15 -06:00
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2015-10-28 04:34:20 -05:00
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
2016-02-18 15:11:01 -06:00
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
const std::string prefix = "textselectioncontent: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
selection = line.substr(prefix.length());
|
|
|
|
break;
|
|
|
|
}
|
2015-10-28 04:34:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 15:11:01 -06:00
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection);
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-18 15:11:01 -06:00
|
|
|
}
|
2016-02-19 01:37:29 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2015-10-28 04:34:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 04:44:35 -06:00
|
|
|
void HTTPWSTest::testLargePaste()
|
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
try
|
2016-01-18 04:44:35 -06:00
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
// Load a document and make it empty.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-02-18 15:11:01 -06:00
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
sendTextFrame(socket, "uno .uno:SelectAll");
|
|
|
|
sendTextFrame(socket, "uno .uno:Delete");
|
|
|
|
|
|
|
|
// Paste some text into it.
|
|
|
|
std::ifstream documentStream(documentPath);
|
|
|
|
std::string documentContents((std::istreambuf_iterator<char>(documentStream)), std::istreambuf_iterator<char>());
|
|
|
|
sendTextFrame(socket, "paste mimetype=text/html\n" + documentContents);
|
|
|
|
|
|
|
|
// Check if the server is still alive.
|
|
|
|
// This resulted first in a hang, as respose for the message never arrived, then a bit later in a Poco::TimeoutException.
|
|
|
|
sendTextFrame(socket, "gettextselection mimetype=text/plain;charset=utf-8");
|
|
|
|
std::string selection;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
2016-01-18 04:44:35 -06:00
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-22 05:48:15 -06:00
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
2016-02-25 02:31:36 -06:00
|
|
|
std::cout << "Received message length " << n << ": " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
2016-02-18 15:11:01 -06:00
|
|
|
std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
std::string prefix = "textselectioncontent: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2016-01-18 04:44:35 -06:00
|
|
|
}
|
2016-02-18 15:11:01 -06:00
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-18 15:11:01 -06:00
|
|
|
}
|
2016-02-19 01:37:29 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-01-18 04:44:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 04:01:34 -06:00
|
|
|
void HTTPWSTest::testRenderingOptions()
|
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
try
|
2015-11-19 04:01:34 -06:00
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
// Load a document and get its size.
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hide-whitespace.odt", documentPath, documentURL);
|
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
const std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}";
|
2016-03-13 12:23:12 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL + " options=" + options);
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
|
|
|
|
std::string status;
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
do
|
2015-11-19 04:01:34 -06:00
|
|
|
{
|
2016-02-18 15:11:01 -06:00
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
2016-02-22 05:48:15 -06:00
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2015-11-19 04:01:34 -06:00
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << '\n';
|
2016-02-18 15:11:01 -06:00
|
|
|
std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
std::string prefix = "status: ";
|
|
|
|
if (line.find(prefix) == 0)
|
|
|
|
{
|
|
|
|
status = line.substr(prefix.length());
|
|
|
|
break;
|
|
|
|
}
|
2015-11-19 04:01:34 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 15:11:01 -06:00
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
// Expected format is something like 'type=text parts=2 current=0 width=12808 height=1142'.
|
|
|
|
Poco::StringTokenizer tokens(status, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count());
|
|
|
|
|
|
|
|
const std::string token = tokens[4];
|
|
|
|
const std::string prefix = "height=";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), token.find(prefix));
|
|
|
|
const int height = std::stoi(token.substr(prefix.size()));
|
|
|
|
// HideWhitespace was ignored, this was 32532, should be around 16706.
|
|
|
|
CPPUNIT_ASSERT(height < 20000);
|
|
|
|
}
|
2016-02-19 01:37:29 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2015-11-19 04:01:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 09:31:13 -06:00
|
|
|
void HTTPWSTest::testPasswordProtectedDocumentWithoutPassword()
|
2016-02-17 15:15:28 -06:00
|
|
|
{
|
2016-03-01 02:55:06 -06:00
|
|
|
try
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("password-protected.ods", documentPath, documentURL);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
2016-02-19 16:22:22 -06:00
|
|
|
// Send a load request without password first
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
2016-02-21 08:50:40 -06:00
|
|
|
std::string response;
|
2016-02-19 16:22:22 -06:00
|
|
|
|
2016-02-21 08:50:40 -06:00
|
|
|
getResponseMessage(socket, "error:", response, true);
|
2016-04-13 01:56:14 -05:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive an error: message as expected", !response.empty());
|
2016-02-17 15:15:28 -06:00
|
|
|
{
|
2016-02-21 08:50:40 -06:00
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), tokens.count());
|
|
|
|
|
|
|
|
std::string errorCommand;
|
|
|
|
std::string errorKind;
|
|
|
|
LOOLProtocol::getTokenString(tokens[0], "cmd", errorCommand);
|
|
|
|
LOOLProtocol::getTokenString(tokens[1], "kind", errorKind);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("passwordrequired:to-view"), errorKind);
|
|
|
|
}
|
2016-02-25 03:02:35 -06:00
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-25 03:02:35 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-02-25 03:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 09:31:13 -06:00
|
|
|
void HTTPWSTest::testPasswordProtectedDocumentWithWrongPassword()
|
2016-02-25 03:02:35 -06:00
|
|
|
{
|
2016-03-01 02:55:06 -06:00
|
|
|
try
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("password-protected.ods", documentPath, documentURL);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-02-17 15:15:28 -06:00
|
|
|
|
2016-02-25 03:02:35 -06:00
|
|
|
// Send a load request with incorrect password
|
2016-02-21 08:50:40 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL + " password=2");
|
2016-02-17 15:15:28 -06:00
|
|
|
|
2016-03-13 12:23:12 -05:00
|
|
|
std::string response;
|
2016-02-21 08:50:40 -06:00
|
|
|
getResponseMessage(socket, "error:", response, true);
|
2016-04-13 01:56:14 -05:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive an error: message as expected", !response.empty());
|
2016-02-21 08:50:40 -06:00
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), tokens.count());
|
|
|
|
|
|
|
|
std::string errorCommand;
|
|
|
|
std::string errorKind;
|
|
|
|
LOOLProtocol::getTokenString(tokens[0], "cmd", errorCommand);
|
|
|
|
LOOLProtocol::getTokenString(tokens[1], "kind", errorKind);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("wrongpassword"), errorKind);
|
|
|
|
}
|
2016-02-25 03:02:35 -06:00
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-25 03:02:35 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-02-25 03:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 09:31:13 -06:00
|
|
|
void HTTPWSTest::testPasswordProtectedDocumentWithCorrectPassword()
|
2016-02-25 03:02:35 -06:00
|
|
|
{
|
2016-03-01 02:55:06 -06:00
|
|
|
try
|
|
|
|
{
|
2016-03-19 16:35:24 -05:00
|
|
|
const std::string documentPath = Util::getTempFilePath(TDOC, "password-protected.ods");
|
2016-02-25 03:02:35 -06:00
|
|
|
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
|
2016-03-13 12:23:12 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-02-21 08:50:40 -06:00
|
|
|
|
2016-02-25 03:02:35 -06:00
|
|
|
// Send a load request with correct password
|
2016-02-21 08:50:40 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL + " password=1");
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document with correct password " + documentURL, isDocumentLoaded(socket));
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-19 16:22:22 -06:00
|
|
|
}
|
2016-02-21 08:50:40 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-02-17 15:15:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-03 09:31:13 -06:00
|
|
|
void HTTPWSTest::testPasswordProtectedDocumentWithCorrectPasswordAgain()
|
2016-03-03 09:13:57 -06:00
|
|
|
{
|
2016-03-03 09:31:13 -06:00
|
|
|
testPasswordProtectedDocumentWithCorrectPassword();
|
2016-03-03 09:13:57 -06:00
|
|
|
}
|
|
|
|
|
2016-04-24 13:59:23 -05:00
|
|
|
void HTTPWSTest::testInsertDelete()
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-04-24 13:59:23 -05:00
|
|
|
std::vector<std::string> parts;
|
|
|
|
std::string response;
|
|
|
|
|
2016-02-20 15:13:35 -06:00
|
|
|
// Load a document
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("insert-delete.odp", documentPath, documentURL);
|
2016-02-20 15:13:35 -06:00
|
|
|
|
2016-03-13 12:23:12 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
2016-04-14 20:50:42 -05:00
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-03-13 12:23:12 -05:00
|
|
|
|
2016-02-20 15:13:35 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
// check total slides 1
|
2016-04-24 13:59:23 -05:00
|
|
|
getResponseMessage(socket, "status:", response, false);
|
2016-04-13 01:56:14 -05:00
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count());
|
|
|
|
|
|
|
|
// Expected format is something like 'type= parts= current= width= height='.
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
2016-04-14 09:06:43 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(1, totalParts);
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert 10 slides
|
2016-03-01 02:55:06 -06:00
|
|
|
for (unsigned it = 1; it <= 10; it++)
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
sendTextFrame(socket, "uno .uno:InsertPage");
|
2016-04-24 13:59:23 -05:00
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
2016-04-24 13:59:23 -05:00
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete 10 slides
|
2016-03-01 02:55:06 -06:00
|
|
|
for (unsigned it = 1; it <= 10; it++)
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
sendTextFrame(socket, "uno .uno:DeletePage");
|
2016-04-24 13:59:23 -05:00
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
2016-04-24 13:59:23 -05:00
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo delete slides
|
2016-03-01 02:55:06 -06:00
|
|
|
for (unsigned it = 1; it <= 10; it++)
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
sendTextFrame(socket, "uno .uno:Undo");
|
2016-04-24 13:59:23 -05:00
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
2016-04-24 13:59:23 -05:00
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// redo inserted slides
|
2016-03-01 02:55:06 -06:00
|
|
|
for (unsigned it = 1; it <= 10; it++)
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
|
|
|
sendTextFrame(socket, "uno .uno:Redo");
|
2016-04-24 13:59:23 -05:00
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
2016-04-24 13:59:23 -05:00
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check total slides 1
|
|
|
|
sendTextFrame(socket, "status");
|
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
const std::string prefix = "parts=";
|
|
|
|
const int totalParts = std::stoi(tokens[1].substr(prefix.size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, totalParts);
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
getPartHashCodes(response, parts);
|
2016-04-24 13:59:23 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(totalParts, (int)parts.size());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.shutdown();
|
2016-04-12 13:34:25 -05:00
|
|
|
Util::removeFile(documentPath);
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-04-12 12:55:14 -05:00
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
2016-02-20 15:13:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 12:51:23 -05:00
|
|
|
void HTTPWSTest::testClientPartImpress()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("setclientpart.odp", documentPath, documentURL);
|
2016-04-27 12:51:23 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
|
|
|
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
checkTiles(socket, "presentation");
|
2016-04-27 12:51:23 -05:00
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
socket.shutdown();
|
|
|
|
Util::removeFile(documentPath);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 12:51:23 -05:00
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
void HTTPWSTest::testClientPartCalc()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
2016-04-28 13:01:28 -05:00
|
|
|
getDocumentPathAndURL("setclientpart.ods", documentPath, documentURL);
|
2016-04-27 12:51:23 -05:00
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::WebSocket socket = *connectLOKit(request, _response);
|
2016-04-27 12:51:23 -05:00
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
|
|
|
|
|
|
|
|
checkTiles(socket, "spreadsheet");
|
2016-04-27 12:51:23 -05:00
|
|
|
|
|
|
|
socket.shutdown();
|
|
|
|
Util::removeFile(documentPath);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 04:34:30 -05:00
|
|
|
void HTTPWSTest::testSimultaneousTilesRenderedJustOnce()
|
|
|
|
{
|
2016-04-28 05:35:56 -05:00
|
|
|
std::string documentPath, documentURL;
|
|
|
|
getDocumentPathAndURL("hello.odt", documentPath, documentURL);
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::WebSocket socket1 = *connectLOKit(request, _response);
|
|
|
|
sendTextFrame(socket1, "load url=" + documentURL);
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
Poco::Net::WebSocket socket2 = *connectLOKit(request, _response);
|
|
|
|
sendTextFrame(socket2, "load url=" + documentURL);
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
sendTextFrame(socket1, "tile part=42 width=400 height=400 tileposx=1000 tileposy=2000 tilewidth=3000 tileheight=3000");
|
|
|
|
sendTextFrame(socket2, "tile part=42 width=400 height=400 tileposx=1000 tileposy=2000 tilewidth=3000 tileheight=3000");
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
std::string response1;
|
|
|
|
getResponseMessage(socket1, "tile:", response1, true);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a tile: message as expected", !response1.empty());
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
std::string response2;
|
|
|
|
getResponseMessage(socket2, "tile:", response2, true);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a tile: message as expected", !response2.empty());
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
if (!response1.empty() && !response2.empty())
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens1(response1, " ");
|
|
|
|
std::string renderId1;
|
|
|
|
LOOLProtocol::getTokenString(tokens1, "renderid", renderId1);
|
|
|
|
Poco::StringTokenizer tokens2(response2, " ");
|
|
|
|
std::string renderId2;
|
|
|
|
LOOLProtocol::getTokenString(tokens2, "renderid", renderId2);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(renderId1 == renderId2 ||
|
|
|
|
(renderId1 == "cached" && renderId2 != "cached") ||
|
|
|
|
(renderId1 != "cached" && renderId2 == "cached"));
|
|
|
|
}
|
2016-04-21 04:34:30 -05:00
|
|
|
|
2016-04-22 05:26:32 -05:00
|
|
|
socket1.shutdown();
|
|
|
|
socket2.shutdown();
|
2016-04-21 04:34:30 -05:00
|
|
|
}
|
|
|
|
|
2016-04-13 05:13:01 -05:00
|
|
|
void HTTPWSTest::testNoExtraLoolKitsLeft()
|
|
|
|
{
|
|
|
|
int countNow = countLoolKitProcesses();
|
|
|
|
|
2016-04-14 09:06:43 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(_initialLoolKitCount, countNow);
|
2016-04-13 05:13:01 -05:00
|
|
|
}
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
void HTTPWSTest::sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string)
|
|
|
|
{
|
|
|
|
socket.sendFrame(string.data(), string.size());
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:11:01 -06:00
|
|
|
bool HTTPWSTest::isDocumentLoaded(Poco::Net::WebSocket& ws)
|
|
|
|
{
|
|
|
|
bool isLoaded = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int bytes;
|
2016-02-25 07:58:13 -06:00
|
|
|
int retries = 30;
|
2016-02-18 15:11:01 -06:00
|
|
|
const Poco::Timespan waitTime(1000000);
|
|
|
|
|
|
|
|
ws.setReceiveTimeout(0);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "==> isDocumentLoaded\n";
|
2016-02-18 15:11:01 -06:00
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
|
|
|
|
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
|
|
|
bytes = ws.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
2016-02-22 05:48:15 -06:00
|
|
|
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n';
|
2016-02-18 15:11:01 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 12:48:05 -06:00
|
|
|
retries = 10;
|
2016-02-18 15:11:01 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Timeout\n";
|
2016-02-18 15:11:01 -06:00
|
|
|
--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;
|
|
|
|
}
|
|
|
|
|
2016-02-20 15:13:35 -06:00
|
|
|
void HTTPWSTest::getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string& response, const bool isLine)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int bytes;
|
2016-02-25 07:57:52 -06:00
|
|
|
int retries = 20;
|
2016-02-20 15:13:35 -06:00
|
|
|
const Poco::Timespan waitTime(1000000);
|
|
|
|
|
|
|
|
response.clear();
|
|
|
|
ws.setReceiveTimeout(0);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "==> getResponseMessage(" << prefix << ")\n";
|
2016-02-20 15:13:35 -06:00
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
|
|
|
|
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
|
|
|
bytes = ws.receiveFrame(buffer, sizeof(buffer), flags);
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Got " << bytes << " bytes, flags: " << std::hex << flags << std::dec << '\n';
|
2016-02-22 05:48:15 -06:00
|
|
|
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2016-02-20 15:13:35 -06:00
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, bytes) << '\n';
|
2016-02-20 15:13:35 -06:00
|
|
|
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
|
|
|
|
{
|
2016-02-25 07:57:52 -06:00
|
|
|
std::cout << "Timeout\n";
|
2016-02-20 15:13:35 -06:00
|
|
|
--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();
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 15:11:01 -06:00
|
|
|
|
2016-04-27 15:47:28 -05:00
|
|
|
void HTTPWSTest::checkTiles(Poco::Net::WebSocket& socket, const std::string& docType)
|
|
|
|
{
|
|
|
|
const std::string current = "current=";
|
|
|
|
const std::string height = "height=";
|
|
|
|
const std::string parts = "parts=";
|
|
|
|
const std::string type = "type=";
|
|
|
|
const std::string width = "width=";
|
|
|
|
|
|
|
|
int currentPart = -1;
|
|
|
|
int totalParts = 0;
|
|
|
|
int docHeight = 0;
|
|
|
|
int docWidth = 0;
|
|
|
|
|
|
|
|
std::string response;
|
|
|
|
std::string text;
|
|
|
|
|
|
|
|
// check total slides 10
|
|
|
|
getResponseMessage(socket, "status:", response, false);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a status: message as expected", !response.empty());
|
|
|
|
{
|
|
|
|
std::cout << "status: " << response << std::endl;
|
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), tokens.count());
|
|
|
|
|
|
|
|
// Expected format is something like 'type= parts= current= width= height='.
|
|
|
|
text = tokens[0].substr(type.size());
|
|
|
|
totalParts = std::stoi(tokens[1].substr(parts.size()));
|
|
|
|
currentPart = std::stoi(tokens[2].substr(current.size()));
|
|
|
|
docWidth = std::stoi(tokens[3].substr(width.size()));
|
|
|
|
docHeight = std::stoi(tokens[4].substr(height.size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(docType, text);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(10, totalParts);
|
|
|
|
CPPUNIT_ASSERT(currentPart > -1);
|
|
|
|
CPPUNIT_ASSERT(docWidth > 0);
|
|
|
|
CPPUNIT_ASSERT(docHeight > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (docType == "presentation")
|
|
|
|
{
|
|
|
|
// first full invalidation
|
|
|
|
getResponseMessage(socket, "invalidatetiles:", response, true);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a invalidatetiles: message as expected", !response.empty());
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(response, ":", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("EMPTY"), tokens[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// request tiles
|
|
|
|
requestTiles(socket, currentPart, docWidth, docHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// random setclientpart
|
|
|
|
std::srand(std::time(0));
|
|
|
|
std::vector<int> vParts = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|
|
|
std::random_shuffle (vParts.begin(), vParts.end());
|
|
|
|
for (auto it : vParts)
|
|
|
|
{
|
|
|
|
if (currentPart != it)
|
|
|
|
{
|
|
|
|
// change part
|
|
|
|
text = Poco::format("setclientpart part=%d", it);
|
|
|
|
std::cout << text << std::endl;
|
|
|
|
sendTextFrame(socket, text);
|
|
|
|
|
|
|
|
// get full invalidation
|
|
|
|
getResponseMessage(socket, "invalidatetiles:", response, true);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("did not receive a invalidatetiles: message as expected", !response.empty());
|
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(response, ":", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("EMPTY"), tokens[0]);
|
|
|
|
}
|
|
|
|
requestTiles(socket, it, docWidth, docHeight);
|
|
|
|
}
|
|
|
|
currentPart = it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-27 12:51:23 -05:00
|
|
|
void HTTPWSTest::requestTiles(Poco::Net::WebSocket& socket, const int part, const int docWidth, const int docHeight)
|
|
|
|
{
|
|
|
|
// twips
|
|
|
|
const int tileSize = 3840;
|
|
|
|
// pixel
|
|
|
|
const int pixTileSize = 256;
|
|
|
|
|
|
|
|
int rows;
|
|
|
|
int cols;
|
|
|
|
int tileX;
|
|
|
|
int tileY;
|
|
|
|
int tileWidth;
|
|
|
|
int tileHeight;
|
|
|
|
|
|
|
|
std::string text;
|
|
|
|
std::string tile;
|
|
|
|
|
|
|
|
rows = docHeight / tileSize;
|
|
|
|
cols = docWidth / tileSize;
|
2016-04-28 01:23:30 -05:00
|
|
|
|
|
|
|
// Note: this code tests tile requests in the wrong way.
|
|
|
|
|
|
|
|
// This code does NOT match what was the idea how the LOOL protocol should/could be used. The
|
|
|
|
// intent was never that the protocol would need to be, or should be, used in a strict
|
|
|
|
// request/reply fashion. If a client needs n tiles, it should just send the requests, one after
|
|
|
|
// another. There is no need to do n roundtrips. A client should all the time be reading
|
|
|
|
// incoming messages, and handle incoming tiles as appropriate. There should be no expectation
|
|
|
|
// that tiles arrive at the client in the same order that they were requested.
|
|
|
|
|
|
|
|
// But, whatever.
|
|
|
|
|
2016-04-27 12:51:23 -05:00
|
|
|
for (int itRow = 0; itRow < rows; ++itRow)
|
|
|
|
{
|
|
|
|
for (int itCol = 0; itCol < cols; ++itCol)
|
|
|
|
{
|
|
|
|
tileWidth = tileSize;
|
|
|
|
tileHeight = tileSize;
|
|
|
|
tileX = tileSize * itCol;
|
|
|
|
tileY = tileSize * itRow;
|
|
|
|
text = Poco::format("tile part=%d width=%d height=%d tileposx=%d tileposy=%d tilewidth=%d tileheight=%d",
|
|
|
|
part, pixTileSize, pixTileSize, tileX, tileY, tileWidth, tileHeight);
|
|
|
|
|
|
|
|
sendTextFrame(socket, text);
|
|
|
|
getTileMessage(socket, tile);
|
|
|
|
// expected tile: part= width= height= tileposx= tileposy= tilewidth= tileheight=
|
|
|
|
Poco::StringTokenizer tokens(tile, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tile:"), tokens[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(part, std::stoi(tokens[1].substr(std::string("part=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(pixTileSize, std::stoi(tokens[2].substr(std::string("width=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(pixTileSize, std::stoi(tokens[3].substr(std::string("height=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tileX, std::stoi(tokens[4].substr(std::string("tileposx=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tileY, std::stoi(tokens[5].substr(std::string("tileposy=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tileWidth, std::stoi(tokens[6].substr(std::string("tileWidth=").size())));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(tileHeight, std::stoi(tokens[7].substr(std::string("tileHeight=").size())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPWSTest::getTileMessage(Poco::Net::WebSocket& ws, std::string& tile)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
int bytes;
|
|
|
|
int size = 0;
|
|
|
|
int retries = 10;
|
|
|
|
const Poco::Timespan waitTime(1000000);
|
|
|
|
|
|
|
|
ws.setReceiveTimeout(0);
|
|
|
|
std::cout << "==> getTileMessage\n";
|
|
|
|
tile.clear();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::vector<char> payload(READ_BUFFER_SIZE * 100);
|
|
|
|
if (retries > 0 && ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
|
|
|
bytes = ws.receiveFrame(payload.data(), payload.capacity(), flags);
|
|
|
|
payload.resize(bytes > 0 ? bytes : 0);
|
|
|
|
std::cout << "Got " << bytes << " bytes, flags: " << std::bitset<8>(flags) << '\n';
|
|
|
|
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
|
|
|
tile = LOOLProtocol::getFirstLine(payload.data(), bytes);
|
|
|
|
std::cout << "message: " << tile << '\n';
|
|
|
|
Poco::StringTokenizer tokens(tile, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
if (tokens.count() == 2 &&
|
|
|
|
tokens[0] == "nextmessage:" &&
|
|
|
|
LOOLProtocol::getTokenInteger(tokens[1], "size", size) &&
|
|
|
|
size > 0)
|
|
|
|
{
|
|
|
|
payload.resize(size);
|
|
|
|
bytes = ws.receiveFrame(payload.data(), size, flags);
|
|
|
|
tile = LOOLProtocol::getFirstLine(payload.data(), bytes);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-04-15 09:01:02 -05:00
|
|
|
int countLoolKitProcesses()
|
2016-04-13 05:13:01 -05:00
|
|
|
{
|
2016-04-15 09:01:02 -05:00
|
|
|
// Give polls in the lool processes time to time out etc
|
|
|
|
Poco::Thread::sleep(POLL_TIMEOUT_MS*5);
|
2016-04-13 07:33:56 -05:00
|
|
|
|
2016-04-13 05:13:01 -05:00
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
for (auto i = Poco::DirectoryIterator(std::string("/proc")); i != Poco::DirectoryIterator(); ++i)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-04-13 07:33:56 -05:00
|
|
|
Poco::Path procEntry = i.path();
|
|
|
|
const std::string& fileName = procEntry.getFileName();
|
|
|
|
int pid;
|
|
|
|
std::size_t endPos = 0;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pid = std::stoi(fileName, &endPos);
|
|
|
|
}
|
|
|
|
catch (const std::invalid_argument&)
|
|
|
|
{
|
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
if (pid > 1 && endPos == fileName.length())
|
|
|
|
{
|
2016-04-18 06:02:36 -05:00
|
|
|
Poco::FileInputStream stat(procEntry.toString() + "/stat");
|
|
|
|
std::string statString;
|
|
|
|
Poco::StreamCopier::copyToString(stat, statString);
|
|
|
|
Poco::StringTokenizer tokens(statString, " ");
|
|
|
|
if (tokens.count() > 3 && tokens[1] == "(loolkit)")
|
|
|
|
{
|
|
|
|
switch (tokens[2].c_str()[0])
|
|
|
|
{
|
|
|
|
case 'x':
|
|
|
|
case 'X': // Kinds of dead-ness.
|
|
|
|
case 'Z': // zombies
|
|
|
|
break; // ignore
|
|
|
|
default:
|
|
|
|
result++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// std::cout << "Process:" << pid << ", '" << tokens[1] << "'" << " state: " << tokens[2] << std::endl;
|
|
|
|
}
|
2016-04-13 07:33:56 -05:00
|
|
|
}
|
2016-04-13 05:13:01 -05:00
|
|
|
}
|
2016-04-13 07:33:56 -05:00
|
|
|
catch (const Poco::Exception&)
|
2016-04-13 05:13:01 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// std::cout << "Number of loolkit processes: " << result << std::endl;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-25 17:06:50 -05:00
|
|
|
void HTTPWSTest::getPartHashCodes(const std::string response,
|
|
|
|
std::vector<std::string>& parts)
|
|
|
|
{
|
|
|
|
Poco::RegularExpression endLine("[^\n\r]+");
|
|
|
|
Poco::RegularExpression number("^[0-9]+$");
|
|
|
|
Poco::RegularExpression::MatchVec matches;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
parts.clear();
|
|
|
|
while (endLine.match(response, offset, matches) > 0)
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1, (int)matches.size());
|
|
|
|
const auto str = response.substr(matches[0].offset, matches[0].length);
|
|
|
|
if (number.match(str, 0) > 0)
|
|
|
|
{
|
|
|
|
parts.push_back(str);
|
|
|
|
}
|
|
|
|
offset = static_cast<int>(matches[0].offset + matches[0].length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 20:50:42 -05:00
|
|
|
// 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;
|
2016-04-15 02:53:06 -05:00
|
|
|
int received = 0;
|
2016-04-14 20:50:42 -05:00
|
|
|
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
|
2016-04-28 06:27:17 -05:00
|
|
|
std::cerr << "Connecting... ";
|
2016-04-14 20:50:42 -05:00
|
|
|
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;
|
2016-04-28 06:27:17 -05:00
|
|
|
if (message.find("ready") != std::string::npos)
|
2016-04-14 20:50:42 -05:00
|
|
|
{
|
|
|
|
ready = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::TimeoutException& exc)
|
|
|
|
{
|
2016-04-28 06:27:17 -05:00
|
|
|
std::cout << exc.displayText() << std::endl;
|
2016-04-14 20:50:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:46:09 -05:00
|
|
|
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);
|
2016-04-28 06:27:17 -05:00
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
auto socket = connectLOKit(request, response);
|
2016-04-26 22:46:09 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-28 06:27:17 -05:00
|
|
|
void HTTPWSTest::SocketProcessor(std::string name,
|
|
|
|
const std::shared_ptr<Poco::Net::WebSocket>& socket,
|
|
|
|
std::function<bool(const std::string& msg)> handler)
|
2016-04-26 22:46:09 -05:00
|
|
|
{
|
2016-04-28 06:27:17 -05:00
|
|
|
if (!name.empty())
|
|
|
|
{
|
|
|
|
name += ' ';
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:46:09 -05:00
|
|
|
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)
|
|
|
|
{
|
2016-04-28 06:27:17 -05:00
|
|
|
std::cout << name + "Got " << n << " bytes: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
|
2016-04-26 22:46:09 -05:00
|
|
|
if (!handler(std::string(buffer, n)))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 06:27:17 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << name + "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
|
|
|
|
}
|
2016-04-26 22:46:09 -05:00
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSTest);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|