2016-09-28 15:04:51 -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 "config.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include "Common.hpp"
|
2016-11-24 08:56:06 -06:00
|
|
|
#include "Protocol.hpp"
|
2016-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.hpp>
|
2016-09-28 15:04:51 -05:00
|
|
|
#include "helpers.hpp"
|
2016-10-15 16:41:53 -05:00
|
|
|
#include "countloolkits.hpp"
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
using namespace helpers;
|
|
|
|
|
|
|
|
class HTTPWSError : public CPPUNIT_NS::TestFixture
|
|
|
|
{
|
|
|
|
const Poco::URI _uri;
|
|
|
|
Poco::Net::HTTPResponse _response;
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPWSError);
|
|
|
|
|
2017-01-15 22:14:15 -06:00
|
|
|
CPPUNIT_TEST(testBadDocLoadFail);
|
2017-04-04 12:54:08 -05:00
|
|
|
// FIXME CPPUNIT_TEST(testMaxDocuments);
|
2016-09-28 17:16:09 -05:00
|
|
|
CPPUNIT_TEST(testMaxConnections);
|
2017-01-29 15:35:44 -06:00
|
|
|
CPPUNIT_TEST(testMaxViews);
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2017-01-15 22:14:15 -06:00
|
|
|
void testBadDocLoadFail();
|
2016-09-28 15:04:51 -05:00
|
|
|
void testMaxDocuments();
|
2016-09-28 17:16:09 -05:00
|
|
|
void testMaxConnections();
|
2017-01-29 15:35:44 -06:00
|
|
|
void testMaxViews();
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
HTTPWSError()
|
|
|
|
: _uri(helpers::getTestServerURI())
|
|
|
|
{
|
|
|
|
#if ENABLE_SSL
|
|
|
|
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);
|
2016-12-22 08:04:07 -06:00
|
|
|
Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
|
2016-09-28 15:04:51 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_SSL
|
|
|
|
~HTTPWSError()
|
|
|
|
{
|
|
|
|
Poco::Net::uninitializeSSL();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
2016-10-15 16:41:53 -05:00
|
|
|
testCountHowManyLoolkits();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
2016-10-15 16:41:53 -05:00
|
|
|
testNoExtraLoolKitsLeft();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-15 22:14:15 -06:00
|
|
|
void HTTPWSError::testBadDocLoadFail()
|
|
|
|
{
|
|
|
|
// Load corrupted document and validate error.
|
|
|
|
const auto testname = "docLoadFail ";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string documentPath, documentURL;
|
2017-02-28 15:49:24 -06:00
|
|
|
getDocumentPathAndURL("corrupted.odt", documentPath, documentURL, testname);
|
2017-01-15 22:14:15 -06:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
auto socket = connectLOKit(_uri, request, _response);
|
|
|
|
|
|
|
|
// Send a load request with incorrect password
|
|
|
|
sendTextFrame(socket, "load url=" + documentURL);
|
|
|
|
|
|
|
|
const auto response = getResponseString(socket, "error:", testname);
|
|
|
|
Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), tokens.count());
|
|
|
|
|
|
|
|
std::string errorCommand;
|
|
|
|
std::string errorKind;
|
|
|
|
LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand);
|
|
|
|
LOOLProtocol::getTokenString(tokens[2], "kind", errorKind);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("faileddocloading"), errorKind);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
void HTTPWSError::testMaxDocuments()
|
|
|
|
{
|
2017-01-03 16:31:49 -06:00
|
|
|
static_assert(MAX_DOCUMENTS >= 2, "MAX_DOCUMENTS must be at least 2");
|
2016-10-15 16:41:53 -05:00
|
|
|
const auto testname = "maxDocuments ";
|
2017-01-03 16:31:49 -06:00
|
|
|
|
2017-04-06 20:50:29 -05:00
|
|
|
if (MAX_DOCUMENTS > 20)
|
2017-01-03 16:31:49 -06:00
|
|
|
{
|
|
|
|
std::cerr << "Skipping " << testname << "test since MAX_DOCUMENTS (" << MAX_DOCUMENTS
|
|
|
|
<< ") is too high to test. Set to a more sensible number, ideally a dozen or so." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document.
|
2016-11-10 02:47:25 -06:00
|
|
|
std::vector<std::shared_ptr<LOOLWebSocket>> docs;
|
2016-09-28 15:04:51 -05:00
|
|
|
|
2016-10-23 10:51:10 -05:00
|
|
|
std::cerr << "Loading max number of documents: " << MAX_DOCUMENTS << std::endl;
|
2016-10-15 16:41:53 -05:00
|
|
|
for (int it = 1; it <= MAX_DOCUMENTS; ++it)
|
2016-09-28 15:04:51 -05:00
|
|
|
{
|
2016-10-15 16:41:53 -05:00
|
|
|
docs.emplace_back(loadDocAndGetSocket("empty.odt", _uri, testname));
|
2016-10-23 10:51:10 -05:00
|
|
|
std::cerr << "Loaded document #" << it << " of " << MAX_DOCUMENTS << std::endl;
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
|
2016-10-23 10:51:10 -05:00
|
|
|
std::cerr << "Loading one more document beyond the limit." << std::endl;
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
// try to open MAX_DOCUMENTS + 1
|
2016-10-18 01:02:03 -05:00
|
|
|
std::string docPath;
|
|
|
|
std::string docURL;
|
2017-01-05 21:46:00 -06:00
|
|
|
getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
|
2016-09-28 15:04:51 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
|
2017-01-03 16:31:49 -06:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
|
|
|
|
auto socket = std::make_shared<LOOLWebSocket>(*session, request, _response);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
2017-01-03 16:31:49 -06:00
|
|
|
// Send load request, which will fail.
|
|
|
|
sendTextFrame(socket, "load url=" + docURL, testname);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
2016-10-23 10:51:10 -05:00
|
|
|
assertResponseString(socket, "error:", testname);
|
|
|
|
|
2016-10-15 16:41:53 -05:00
|
|
|
std::string message;
|
2017-01-15 11:15:11 -06:00
|
|
|
const auto statusCode = getErrorCode(socket, message, testname);
|
2016-10-15 16:41:53 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION), statusCode);
|
|
|
|
|
2017-01-03 16:31:49 -06:00
|
|
|
socket->shutdown();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:16:09 -05:00
|
|
|
void HTTPWSError::testMaxConnections()
|
2016-09-28 15:04:51 -05:00
|
|
|
{
|
2017-01-03 16:02:53 -06:00
|
|
|
static_assert(MAX_CONNECTIONS >= 3, "MAX_CONNECTIONS must be at least 3");
|
2016-10-15 16:41:53 -05:00
|
|
|
const auto testname = "maxConnections ";
|
2017-01-03 16:02:53 -06:00
|
|
|
|
2017-04-06 20:50:29 -05:00
|
|
|
if (MAX_CONNECTIONS > 40)
|
2017-01-03 16:02:53 -06:00
|
|
|
{
|
|
|
|
std::cerr << "Skipping " << testname << "test since MAX_CONNECTION (" << MAX_CONNECTIONS
|
|
|
|
<< ") is too high to test. Set to a more sensible number, ideally a dozen or so." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
try
|
|
|
|
{
|
2016-10-23 12:42:20 -05:00
|
|
|
std::cerr << "Opening max number of connections: " << MAX_CONNECTIONS << std::endl;
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
// Load a document.
|
|
|
|
std::string docPath;
|
|
|
|
std::string docURL;
|
|
|
|
|
2017-01-05 21:46:00 -06:00
|
|
|
getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
|
2016-09-28 15:04:51 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
|
2016-10-15 16:41:53 -05:00
|
|
|
auto socket = loadDocAndGetSocket(_uri, docURL, testname);
|
2017-01-29 15:29:18 -06:00
|
|
|
std::cerr << "Opened connection #1 of " << MAX_CONNECTIONS << std::endl;
|
2016-09-28 15:04:51 -05:00
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
std::vector<std::shared_ptr<LOOLWebSocket>> views;
|
2017-01-03 16:02:53 -06:00
|
|
|
for (int it = 1; it < MAX_CONNECTIONS; ++it)
|
2016-09-28 15:04:51 -05:00
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
|
2016-11-10 02:47:25 -06:00
|
|
|
auto ws = std::make_shared<LOOLWebSocket>(*session, request, _response);
|
2016-09-28 15:04:51 -05:00
|
|
|
views.emplace_back(ws);
|
2017-01-29 15:29:18 -06:00
|
|
|
std::cerr << "Opened connection #" << (it+1) << " of " << MAX_CONNECTIONS << std::endl;
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
|
2016-10-23 12:42:20 -05:00
|
|
|
std::cerr << "Opening one more connection beyond the limit." << std::endl;
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
// try to connect MAX_CONNECTIONS + 1
|
2016-09-29 08:48:09 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
|
2017-01-03 16:02:53 -06:00
|
|
|
auto socketN = std::make_shared<LOOLWebSocket>(*session, request, _response);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
2017-01-03 16:02:53 -06:00
|
|
|
// Send load request, which will fail.
|
|
|
|
sendTextFrame(socketN, "load url=" + docURL, testname);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
|
|
|
std::string message;
|
2017-01-15 11:15:11 -06:00
|
|
|
const auto statusCode = getErrorCode(socketN, message, testname);
|
2016-10-15 16:41:53 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION), statusCode);
|
|
|
|
|
2017-01-03 16:02:53 -06:00
|
|
|
socketN->shutdown();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
2016-09-28 17:16:09 -05:00
|
|
|
}
|
2016-09-28 15:04:51 -05:00
|
|
|
|
2017-01-29 15:35:44 -06:00
|
|
|
void HTTPWSError::testMaxViews()
|
|
|
|
{
|
|
|
|
static_assert(MAX_CONNECTIONS >= 3, "MAX_CONNECTIONS must be at least 3");
|
|
|
|
const auto testname = "maxViews ";
|
|
|
|
|
2017-04-06 20:50:29 -05:00
|
|
|
if (MAX_CONNECTIONS > 40)
|
2017-01-29 15:35:44 -06:00
|
|
|
{
|
|
|
|
std::cerr << "Skipping " << testname << "test since MAX_CONNECTION (" << MAX_CONNECTIONS
|
|
|
|
<< ") is too high to test. Set to a more sensible number, ideally a dozen or so." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::cerr << "Opening max number of views: " << MAX_CONNECTIONS << std::endl;
|
|
|
|
|
|
|
|
// Load a document.
|
|
|
|
std::string docPath;
|
|
|
|
std::string docURL;
|
|
|
|
|
|
|
|
getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
|
|
|
|
auto socket = loadDocAndGetSocket(_uri, docURL, testname);
|
|
|
|
std::cerr << "Opened view #1 of " << MAX_CONNECTIONS << std::endl;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<LOOLWebSocket>> views;
|
|
|
|
for (int it = 1; it < MAX_CONNECTIONS; ++it)
|
|
|
|
{
|
|
|
|
views.emplace_back(loadDocAndGetSocket(_uri, docURL, testname));
|
|
|
|
std::cerr << "Opened view #" << (it+1) << " of " << MAX_CONNECTIONS << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Opening one more connection beyond the limit." << std::endl;
|
|
|
|
|
|
|
|
// try to connect MAX_CONNECTIONS + 1
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
|
|
|
|
auto socketN = std::make_shared<LOOLWebSocket>(*session, request, _response);
|
|
|
|
|
|
|
|
// Send load request, which will fail.
|
|
|
|
sendTextFrame(socketN, "load url=" + docURL, testname);
|
|
|
|
|
|
|
|
std::string message;
|
|
|
|
const auto statusCode = getErrorCode(socketN, message, testname);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION), statusCode);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSError);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|