loolwsd: New test to check if request uris are valid

The current test tests if the embedded doc url recieved in websocket
URI endpoint is encoded or not.

Change-Id: I1e5d6639d6791be4e1e56701a9e444e33fc89aa2
This commit is contained in:
Pranav Kant 2016-11-17 23:04:52 +05:30
parent 1618f6be4d
commit c511cf1d76
2 changed files with 98 additions and 2 deletions

View file

@ -15,7 +15,7 @@ noinst_LTLIBRARIES = \
unit-timeout.la unit-prefork.la \
unit-storage.la unit-fonts.la \
unit-admin.la unit-tilecache.la \
unit-fuzz.la
unit-fuzz.la unit-requests.la
MAGIC_TO_FORCE_SHLIB_CREATION = -rpath /dummy
AM_LDFLAGS = -pthread -module $(MAGIC_TO_FORCE_SHLIB_CREATION)
@ -49,6 +49,7 @@ unittest_SOURCES = TileQueueTests.cpp WhiteBoxTests.cpp test.cpp $(wsd_sources)
unittest_LDADD = $(CPPUNIT_LIBS)
# unit test modules:
unit_requests_la_SOURCES = UnitRequests.cpp
unit_fuzz_la_SOURCES = UnitFuzz.cpp
unit_admin_la_SOURCES = UnitAdmin.cpp
unit_admin_la_LIBADD = $(CPPUNIT_LIBS)
@ -68,7 +69,7 @@ if HAVE_LO_PATH
check-local:
./run_unit.sh --log-file test.log --trs-file test.trs
# FIXME unit-fonts.la is unstable, disabled for now.
TESTS = unit-tilecache.la unit-storage.la unit-timeout.la unit-prefork.la unit-admin.la
TESTS = unit-tilecache.la unit-storage.la unit-timeout.la unit-prefork.la unit-admin.la unit-requests.la
else
TESTS = ${top_builddir}/test/test
endif

View file

@ -0,0 +1,95 @@
/* -*- 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 <iostream>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/URI.h>
#include "Log.hpp"
#include "Unit.hpp"
#include "UnitHTTP.hpp"
#include "helpers.hpp"
using namespace helpers;
// Inside the WSD process
class UnitRequests : public UnitWSD
{
enum {
PHASE_LOAD,
PHASE_FILTER
} _phase;
TestResult _testResult;
std::unique_ptr<UnitWebSocket> _ws;
public:
UnitRequests() :
_phase(PHASE_LOAD)
{
std::cerr << "UnitRequests startup\n";
}
virtual bool filterHandleRequest(
TestRequest type,
Poco::Net::HTTPServerRequest& request,
Poco::Net::HTTPServerResponse& /*response*/) override
{
if (type == UnitWSD::TestRequest::TEST_REQ_CLIENT)
{
std::string uri = request.getURI();
// Get the embedded document URL: '/lool/docUrl/ws/'
uri = uri.substr(uri.find("lool/") + std::string("lool/").size());
uri = uri.substr(0, uri.find("/ws"));
Poco::URI requestUri(uri);
_testResult = TestResult::TEST_OK;
// If this is a simple encoded string, it would be treated as
// relative, otherwise non-relative.
// We require this embedded url to be encoded as otherwise it would
// be treated as a resource on the server due to the presence of
// un-encoded '/'
if (!requestUri.isRelative())
{
_testResult = TestResult::TEST_FAILED;
}
}
return false;
}
void loadDocument()
{
std::string docPath;
std::string docURL;
getDocumentPathAndURL("empty.odt", docPath, docURL);
_ws = std::unique_ptr<UnitWebSocket>(new UnitWebSocket(docURL));
assert(_ws.get());
}
virtual void invokeTest()
{
switch(_phase)
{
case PHASE_LOAD:
_phase = PHASE_FILTER;
loadDocument();
break;
case PHASE_FILTER:
exitTest(_testResult);
break;
}
}
};
UnitBase *unit_create_wsd(void)
{
return new UnitRequests();
}