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;
|
2016-10-15 16:41:53 -05:00
|
|
|
static int InitialLoolKitCount;
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPWSError);
|
|
|
|
|
|
|
|
CPPUNIT_TEST(testMaxDocuments);
|
2016-09-28 17:16:09 -05:00
|
|
|
CPPUNIT_TEST(testMaxConnections);
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-10-15 16:41:53 -05:00
|
|
|
void testCountHowManyLoolkits();
|
|
|
|
void testNoExtraLoolKitsLeft();
|
2016-09-28 15:04:51 -05:00
|
|
|
void testMaxDocuments();
|
2016-09-28 17:16:09 -05:00
|
|
|
void testMaxConnections();
|
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);
|
|
|
|
Poco::Net::SSLManager::instance().initializeClient(0, invalidCertHandler, sslContext);
|
|
|
|
#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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-15 16:41:53 -05:00
|
|
|
int HTTPWSError::InitialLoolKitCount = 1;
|
|
|
|
|
|
|
|
void HTTPWSError::testCountHowManyLoolkits()
|
|
|
|
{
|
|
|
|
InitialLoolKitCount = countLoolKitProcesses(InitialLoolKitCount);
|
|
|
|
CPPUNIT_ASSERT(InitialLoolKitCount > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPWSError::testNoExtraLoolKitsLeft()
|
|
|
|
{
|
|
|
|
const auto countNow = countLoolKitProcesses(InitialLoolKitCount);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(InitialLoolKitCount, countNow);
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
void HTTPWSError::testMaxDocuments()
|
|
|
|
{
|
|
|
|
#if MAX_DOCUMENTS > 0
|
2016-10-15 16:41:53 -05:00
|
|
|
const auto testname = "maxDocuments ";
|
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;
|
2016-09-28 15:04:51 -05:00
|
|
|
getDocumentPathAndURL("empty.odt", docPath, docURL);
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-11-10 02:47:25 -06:00
|
|
|
LOOLWebSocket socket(*session, request, _response);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
2016-09-29 08:48:09 -05:00
|
|
|
// send loolclient, load and partpagerectangles
|
2016-10-15 16:41:53 -05:00
|
|
|
sendTextFrame(socket, "loolclient ", testname);
|
|
|
|
sendTextFrame(socket, "load ", testname);
|
|
|
|
sendTextFrame(socket, "partpagerectangles ", testname);
|
|
|
|
|
2016-10-23 10:51:10 -05:00
|
|
|
assertResponseString(socket, "error:", testname);
|
|
|
|
|
2016-10-15 16:41:53 -05:00
|
|
|
std::string message;
|
|
|
|
const auto statusCode = getErrorCode(socket, message);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION), statusCode);
|
|
|
|
|
|
|
|
socket.shutdown();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:16:09 -05:00
|
|
|
void HTTPWSError::testMaxConnections()
|
2016-09-28 15:04:51 -05:00
|
|
|
{
|
|
|
|
#if MAX_CONNECTIONS > 0
|
2016-10-15 16:41:53 -05:00
|
|
|
const auto testname = "maxConnections ";
|
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;
|
|
|
|
|
|
|
|
getDocumentPathAndURL("empty.odt", docPath, docURL);
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
|
2016-10-15 16:41:53 -05:00
|
|
|
auto socket = loadDocAndGetSocket(_uri, docURL, testname);
|
2016-10-23 12:42:20 -05:00
|
|
|
std::cerr << "Opened connect #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;
|
2016-09-28 15:04:51 -05:00
|
|
|
for(int it = 1; it < MAX_CONNECTIONS; it++)
|
|
|
|
{
|
|
|
|
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);
|
2016-10-23 12:42:20 -05:00
|
|
|
std::cerr << "Opened connect #" << (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));
|
2016-11-10 02:47:25 -06:00
|
|
|
LOOLWebSocket socketN(*session, request, _response);
|
2016-10-15 16:41:53 -05:00
|
|
|
|
2016-09-29 08:48:09 -05:00
|
|
|
// send loolclient, load and partpagerectangles
|
2016-10-15 16:41:53 -05:00
|
|
|
sendTextFrame(socketN, "loolclient ", testname);
|
|
|
|
sendTextFrame(socketN, "load ", testname);
|
|
|
|
sendTextFrame(socketN, "partpagerectangles ", testname);
|
|
|
|
|
|
|
|
std::string message;
|
|
|
|
const auto statusCode = getErrorCode(socketN, message);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION), statusCode);
|
|
|
|
|
|
|
|
socketN.shutdown();
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-28 17:16:09 -05:00
|
|
|
}
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSError);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|