2016-04-20 22:09:04 -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 <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/Net/FilePartSource.h>
|
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/InvalidCertificateHandler.h>
|
|
|
|
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
|
|
|
|
#include <Poco/Net/SSLManager.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include <Common.hpp>
|
|
|
|
#include <Util.hpp>
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
#include "countloolkits.hpp"
|
2016-05-02 11:41:57 -05:00
|
|
|
#include "helpers.hpp"
|
2016-04-26 04:32:01 -05:00
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
/// Tests the HTTP GET API of loolwsd.
|
2016-04-28 11:08:07 -05:00
|
|
|
class HTTPServerTest : public CPPUNIT_NS::TestFixture
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-02 11:25:39 -05:00
|
|
|
const Poco::URI _uri;
|
2016-04-28 11:08:07 -05:00
|
|
|
static int _initialLoolKitCount;
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPServerTest);
|
|
|
|
|
|
|
|
// This should be the first test:
|
|
|
|
CPPUNIT_TEST(testCountHowManyLoolkits);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST(testDiscovery);
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_TEST(testLoleafletGet);
|
|
|
|
CPPUNIT_TEST(testLoleafletPost);
|
|
|
|
CPPUNIT_TEST(testScriptsAndLinksGet);
|
|
|
|
CPPUNIT_TEST(testScriptsAndLinksPost);
|
|
|
|
|
|
|
|
// This should be the last test:
|
|
|
|
CPPUNIT_TEST(testNoExtraLoolKitsLeft);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void testCountHowManyLoolkits();
|
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
void testDiscovery();
|
2016-04-28 11:08:07 -05:00
|
|
|
void testLoleafletGet();
|
|
|
|
void testLoleafletPost();
|
|
|
|
void testScriptsAndLinksGet();
|
|
|
|
void testScriptsAndLinksPost();
|
|
|
|
|
|
|
|
void testNoExtraLoolKitsLeft();
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
#if ENABLE_SSL
|
|
|
|
public:
|
2016-04-28 11:08:07 -05:00
|
|
|
HTTPServerTest()
|
2016-05-02 11:25:39 -05:00
|
|
|
#if ENABLE_SSL
|
|
|
|
: _uri("https://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER))
|
|
|
|
#else
|
|
|
|
: _uri("http://127.0.0.1:" + std::to_string(DEFAULT_CLIENT_PORT_NUMBER))
|
|
|
|
#endif
|
2016-04-20 22:09:04 -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-28 11:08:07 -05:00
|
|
|
~HTTPServerTest()
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
|
|
|
Poco::Net::uninitializeSSL();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
int HTTPServerTest::_initialLoolKitCount = 0;
|
|
|
|
|
|
|
|
void HTTPServerTest::testCountHowManyLoolkits()
|
|
|
|
{
|
2016-04-30 09:23:26 -05:00
|
|
|
_initialLoolKitCount = countLoolKitProcesses(1);
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_ASSERT(_initialLoolKitCount > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPServerTest::testDiscovery()
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/hosting/discovery");
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
2016-04-20 22:09:04 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/xml"), response.getContentType());
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testLoleafletGet()
|
2016-04-24 15:41:51 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-24 15:41:51 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html?access_token=111111111");
|
|
|
|
Poco::Net::HTMLForm param(request);
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-24 15:41:51 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
2016-04-24 15:41:51 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/html"), response.getContentType());
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(html.find(param["access_token"]) != std::string::npos);
|
2016-05-02 11:25:39 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(std::string(LOOLWSD_VERSION)) != std::string::npos);
|
2016-04-24 15:41:51 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testLoleafletPost()
|
2016-04-24 19:59:58 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-24 19:59:58 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.set("access_token", "2222222222");
|
|
|
|
form.prepareSubmit(request);
|
2016-05-02 11:41:57 -05:00
|
|
|
std::ostream& ostr = session->sendRequest(request);
|
2016-04-28 11:08:07 -05:00
|
|
|
form.write(ostr);
|
2016-04-24 19:59:58 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(form["access_token"]) != std::string::npos);
|
2016-05-02 11:25:39 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(std::string(LOOLWSD_VERSION)) != std::string::npos);
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:17:37 -05:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void assertHTTPFilesExist(const Poco::URI& uri, Poco::RegularExpression& expr, const std::string& html, const std::string& mimetype = std::string())
|
|
|
|
{
|
|
|
|
Poco::RegularExpression::MatchVec matches;
|
2016-04-28 11:35:22 -05:00
|
|
|
bool found = false;
|
2016-04-28 11:17:37 -05:00
|
|
|
|
|
|
|
for (int offset = 0; expr.match(html, offset, matches) > 0; offset = static_cast<int>(matches[0].offset + matches[0].length))
|
|
|
|
{
|
2016-04-28 11:35:22 -05:00
|
|
|
found = true;
|
2016-04-28 11:17:37 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
|
|
|
|
Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
|
|
|
|
if (uriScript.getHost().empty())
|
|
|
|
{
|
|
|
|
std::string scriptString(uriScript.toString());
|
|
|
|
|
2016-04-28 11:35:22 -05:00
|
|
|
// ignore the branding bits, it's not an error when they are not
|
|
|
|
// present
|
2016-04-28 11:17:37 -05:00
|
|
|
if (scriptString.find("/branding.") != std::string::npos)
|
|
|
|
continue;
|
|
|
|
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
|
2016-04-28 11:35:22 -05:00
|
|
|
|
2016-04-28 11:17:37 -05:00
|
|
|
Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, scriptString);
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(requestScript);
|
2016-04-28 11:17:37 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse responseScript;
|
2016-05-02 11:41:57 -05:00
|
|
|
session->receiveResponse(responseScript);
|
2016-04-28 11:17:37 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
|
|
|
|
|
|
|
|
if (!mimetype.empty())
|
|
|
|
CPPUNIT_ASSERT_EQUAL(mimetype, responseScript.getContentType());
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 11:35:22 -05:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("No match found", found);
|
2016-04-28 11:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testScriptsAndLinksGet()
|
2016-04-25 20:01:46 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-28 11:08:07 -05:00
|
|
|
|
2016-04-25 20:01:46 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html");
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-25 20:01:46 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-25 20:01:46 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
|
|
|
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, script, html, "application/javascript");
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, link, html);
|
2016-04-25 20:01:46 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testScriptsAndLinksPost()
|
2016-04-25 20:40:24 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
|
|
|
|
std::string body;
|
|
|
|
request.setContentLength((int) body.length());
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request) << body;
|
2016-04-25 20:40:24 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-25 20:40:24 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, script, html, "application/javascript");
|
2016-04-25 20:40:24 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, link, html);
|
2016-04-25 20:40:24 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testNoExtraLoolKitsLeft()
|
|
|
|
{
|
2016-04-28 21:01:05 -05:00
|
|
|
const auto countNow = countLoolKitProcesses(_initialLoolKitCount);
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(_initialLoolKitCount, countNow);
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPServerTest);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|