libreoffice-online/loolwsd/test/httpcrashtest.cpp
Ashod Nakashian fd99834147 loolwsd: always run the lokit process count tests
Change-Id: I47365609850c23843624f714d6600f07629461ae
Reviewed-on: https://gerrit.libreoffice.org/24527
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-04-30 14:37:06 +00:00

266 lines
8.2 KiB
C++

/* -*- 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 <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <cstring>
#include <Poco/DirectoryIterator.h>
#include <Poco/Dynamic/Var.h>
#include <Poco/FileStream.h>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/Socket.h>
#include <Poco/Net/WebSocket.h>
#include <Poco/Path.h>
#include <Poco/StreamCopier.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Thread.h>
#include <Poco/URI.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Common.hpp>
#include <UserMessages.hpp>
#include <Util.hpp>
#include <LOOLProtocol.hpp>
#include "helpers.hpp"
#include "countloolkits.hpp"
using namespace helpers;
/// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test.
class HTTPCrashTest : public CPPUNIT_NS::TestFixture
{
const Poco::URI _uri;
Poco::Net::HTTPResponse _response;
static int _initialLoolKitCount;
CPPUNIT_TEST_SUITE(HTTPCrashTest);
// This should be the first test:
CPPUNIT_TEST(testCountHowManyLoolkits);
CPPUNIT_TEST(testBarren);
CPPUNIT_TEST(testCrashKit);
// This should be the last test:
CPPUNIT_TEST(testNoExtraLoolKitsLeft);
CPPUNIT_TEST_SUITE_END();
void testCountHowManyLoolkits();
void testBarren();
void testCrashKit();
void testNoExtraLoolKitsLeft();
static
void killLoKitProcesses();
public:
HTTPCrashTest()
#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
{
#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
~HTTPCrashTest()
{
Poco::Net::uninitializeSSL();
}
#endif
void setUp()
{
}
void tearDown()
{
}
};
int HTTPCrashTest::_initialLoolKitCount = 0;
void HTTPCrashTest::testCountHowManyLoolkits()
{
_initialLoolKitCount = countLoolKitProcesses(1);
CPPUNIT_ASSERT(_initialLoolKitCount > 0);
}
void HTTPCrashTest::testNoExtraLoolKitsLeft()
{
const auto countNow = countLoolKitProcesses(_initialLoolKitCount);
CPPUNIT_ASSERT_EQUAL(_initialLoolKitCount, countNow);
}
void HTTPCrashTest::testBarren()
{
// Kill all kit processes and try loading a document.
try
{
killLoKitProcesses();
// Load a document and get its status.
const std::string documentPath = Util::getTempFilePath(TDOC, "hello.odt");
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response);
sendTextFrame(socket, "load url=" + documentURL);
sendTextFrame(socket, "status");
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
// 5 seconds timeout
socket.setReceiveTimeout(5000000);
std::string status;
int flags;
int n;
do
{
char buffer[READ_BUFFER_SIZE];
n = socket.receiveFrame(buffer, sizeof(buffer), flags);
std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
{
std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
const std::string line = LOOLProtocol::getFirstLine(buffer, n);
const std::string prefix = "status: ";
if (line.find(prefix) == 0)
{
status = line.substr(prefix.length());
// Might be too strict, consider something flexible instread.
CPPUNIT_ASSERT_EQUAL(std::string("type=text parts=1 current=0 width=12808 height=16408"), status);
break;
}
}
}
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
socket.shutdown();
}
catch (const Poco::Exception& exc)
{
CPPUNIT_FAIL(exc.displayText());
}
}
void HTTPCrashTest::testCrashKit()
{
try
{
int bytes;
int flags;
char buffer[READ_BUFFER_SIZE];
// Load a document and get its status.
const std::string documentPath = Util::getTempFilePath(TDOC, "hello.odt");
const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString();
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
Poco::Net::WebSocket socket = *connectLOKit(_uri, request, _response);
sendTextFrame(socket, "load url=" + documentURL);
sendTextFrame(socket, "status");
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
killLoKitProcesses();
// 5 seconds timeout
socket.setReceiveTimeout(5000000);
// receive close frame handshake
do
{
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
}
while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
// respond close frame
socket.shutdown();
// no more messages is received.
bytes = socket.receiveFrame(buffer, sizeof(buffer), flags);
CPPUNIT_ASSERT_EQUAL(0, bytes);
CPPUNIT_ASSERT_EQUAL(0, flags);
}
catch (const Poco::Exception& exc)
{
CPPUNIT_FAIL(exc.displayText());
}
}
void HTTPCrashTest::killLoKitProcesses()
{
// Crash all lokit processes.
for (auto it = Poco::DirectoryIterator(std::string("/proc")); it != Poco::DirectoryIterator(); ++it)
{
try
{
Poco::Path procEntry = it.path();
const std::string& fileName = procEntry.getFileName();
int pid;
std::size_t endPos = 0;
try
{
pid = std::stoi(fileName, &endPos);
}
catch (const std::invalid_argument&)
{
pid = 0;
}
if (pid > 1 && endPos == fileName.length())
{
Poco::FileInputStream stat(procEntry.toString() + "/stat");
std::string statString;
Poco::StreamCopier::copyToString(stat, statString);
Poco::StringTokenizer tokens(statString, " ");
if (tokens.count() > 3 && tokens[1] == "(loolkit)")
{
if (kill(pid, SIGKILL) == -1)
{
std::cerr << "kill(" << pid << ",SIGKILL) failed: " << std::strerror(errno) << std::endl;
}
}
}
}
catch (const Poco::Exception&)
{
}
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPCrashTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */