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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-02-18 15:11:01 -06:00
|
|
|
#include <Poco/Net/NetException.h>
|
2015-10-28 04:34:20 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2016-02-18 15:11:01 -06:00
|
|
|
#include <Poco/Net/Socket.h>
|
2016-01-05 14:32:53 -06:00
|
|
|
#include <Poco/Path.h>
|
2015-11-19 04:01:34 -06:00
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-10-28 04:34:20 -05:00
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include <LOOLProtocol.hpp>
|
2016-01-05 14:32:53 -06:00
|
|
|
#include <Common.hpp>
|
2016-01-06 12:47:03 -06:00
|
|
|
#include <ChildProcessSession.hpp>
|
2015-10-28 04:34:20 -05:00
|
|
|
|
2016-02-17 15:15:28 -06:00
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
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::HTTPClientSession _session;
|
|
|
|
Poco::Net::HTTPRequest _request;
|
|
|
|
Poco::Net::HTTPResponse _response;
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE(HTTPWSTest);
|
|
|
|
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-02-17 15:15:28 -06:00
|
|
|
CPPUNIT_TEST(testPasswordProtectedDocument);
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void testPaste();
|
2016-01-18 04:44:35 -06:00
|
|
|
void testLargePaste();
|
2015-11-19 04:01:34 -06:00
|
|
|
void testRenderingOptions();
|
2016-02-17 15:15:28 -06:00
|
|
|
void testPasswordProtectedDocument();
|
2015-10-28 04:34:20 -05:00
|
|
|
|
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);
|
|
|
|
|
2015-11-19 04:11:47 -06:00
|
|
|
public:
|
|
|
|
HTTPWSTest()
|
2016-01-05 14:32:53 -06:00
|
|
|
: _uri("http://127.0.0.1:" + std::to_string(ClientPortNumber)),
|
2015-11-24 02:41:59 -06:00
|
|
|
_session(_uri.getHost(), _uri.getPort()),
|
2016-02-18 15:11:01 -06:00
|
|
|
_request(Poco::Net::HTTPRequest::HTTP_GET, "/ws")
|
2015-11-19 04:11:47 -06:00
|
|
|
{
|
|
|
|
}
|
2016-01-24 13:58:08 -06:00
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
}
|
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-02-18 15:11:01 -06:00
|
|
|
Poco::Net::WebSocket socket(_session, _request, _response);
|
|
|
|
|
|
|
|
// Load a document and make it empty.
|
|
|
|
const std::string documentPath = TDOC "/hello.odt";
|
|
|
|
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (n > 0)
|
2015-10-28 04:34:20 -05:00
|
|
|
{
|
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);
|
|
|
|
sendTextFrame(socket, "disconnect");
|
|
|
|
socket.shutdown();
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aaa bbb ccc"), selection);
|
|
|
|
}
|
2016-02-19 01:37:29 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(exc.displayText(), false);
|
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
|
|
|
Poco::Net::WebSocket socket(_session, _request, _response);
|
|
|
|
|
|
|
|
// Load a document and make it empty.
|
|
|
|
std::string documentPath = TDOC "/hello.odt";
|
|
|
|
std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
sendTextFrame(socket, "disconnect");
|
|
|
|
socket.shutdown();
|
|
|
|
}
|
2016-02-19 01:37:29 -06:00
|
|
|
catch (const Poco::Exception& exc)
|
2016-02-18 15:11:01 -06:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(exc.displayText(), false);
|
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
|
|
|
Poco::Net::WebSocket socket(_session, _request, _response);
|
|
|
|
|
|
|
|
// Load a document and get its size.
|
|
|
|
const std::string documentPath = TDOC "/hide-whitespace.odt";
|
|
|
|
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
|
|
|
|
const std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}";
|
|
|
|
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);
|
|
|
|
if (n > 0)
|
2015-11-19 04:01:34 -06:00
|
|
|
{
|
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);
|
|
|
|
sendTextFrame(socket, "disconnect");
|
|
|
|
socket.shutdown();
|
|
|
|
// 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
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(exc.displayText(), false);
|
2015-11-19 04:01:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 15:15:28 -06:00
|
|
|
void HTTPWSTest::testPasswordProtectedDocument()
|
|
|
|
{
|
|
|
|
// Load a password protected document
|
|
|
|
const std::string documentPath = TDOC "/password-protected.ods";
|
|
|
|
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
|
|
|
|
// Send a load request without password first
|
|
|
|
sendTextFrame(_socket, "load url=" + documentURL);
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
int n;
|
|
|
|
int counter = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
n = _socket.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
std::string line = LOOLProtocol::getFirstLine(buffer, n);
|
|
|
|
StringTokenizer tokens(line, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
std::string errorCommand;
|
|
|
|
std::string errorKind;
|
|
|
|
if (counter == 0 &&
|
|
|
|
tokens[0] == "error:" &&
|
|
|
|
LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand) &&
|
|
|
|
LOOLProtocol::getTokenString(tokens[2], "kind", errorKind) )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand);
|
|
|
|
// TODO: Do a test for document requiring password to edit
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("passwordrequired:to-view"), errorKind);
|
|
|
|
|
|
|
|
// Send another load request with incorrect password
|
|
|
|
sendTextFrame(_socket, "load url=" + documentURL + " password=2");
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
else if (counter == 1 &&
|
|
|
|
tokens[0] == "error:" &&
|
|
|
|
LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand) &&
|
|
|
|
LOOLProtocol::getTokenString(tokens[2], "kind", errorKind) )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("wrongpassword"), errorKind);
|
|
|
|
|
|
|
|
// Send another load request with correct password
|
|
|
|
sendTextFrame(_socket, "load url=" + documentURL + " password=1");
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
else if (counter == 2 &&
|
|
|
|
tokens[0] == "status:")
|
|
|
|
{
|
|
|
|
// Entering correct password opened the document
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
int retries = 5;
|
|
|
|
const Poco::Timespan waitTime(1000000);
|
|
|
|
|
|
|
|
ws.setReceiveTimeout(0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
char buffer[READ_BUFFER_SIZE];
|
|
|
|
|
|
|
|
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
|
|
|
bytes = ws.receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
if (bytes > 0)
|
|
|
|
{
|
|
|
|
const std::string line = LOOLProtocol::getFirstLine(buffer, bytes);
|
|
|
|
const std::string prefixIndicator = "statusindicatorfinish:";
|
|
|
|
const std::string prefixStatus = "status:";
|
|
|
|
std::cout << line << std::endl;
|
|
|
|
if (line.find(prefixIndicator) == 0 || line.find(prefixStatus) == 0)
|
|
|
|
{
|
|
|
|
isLoaded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
retries = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-28 04:34:20 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSTest);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|