2016-04-20 20:14: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"
|
|
|
|
|
2016-04-25 01:25:23 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
#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/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>
|
2016-11-24 08:56:06 -06:00
|
|
|
#include <Protocol.hpp>
|
2016-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.hpp>
|
2016-04-28 21:47:09 -05:00
|
|
|
#include "helpers.hpp"
|
2016-04-30 09:23:26 -05:00
|
|
|
#include "countloolkits.hpp"
|
2016-04-28 21:47:09 -05:00
|
|
|
|
|
|
|
using namespace helpers;
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
/// 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;
|
2016-05-13 11:43:43 -05:00
|
|
|
static int InitialLoolKitCount;
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPCrashTest);
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
CPPUNIT_TEST(testBarren);
|
2016-04-20 20:14:51 -05:00
|
|
|
CPPUNIT_TEST(testCrashKit);
|
2016-11-11 21:38:14 -06:00
|
|
|
CPPUNIT_TEST(testCrashForkit);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-04-30 09:23:26 -05:00
|
|
|
void testCountHowManyLoolkits();
|
2016-04-24 11:35:29 -05:00
|
|
|
void testBarren();
|
2016-04-20 20:14:51 -05:00
|
|
|
void testCrashKit();
|
2016-11-11 21:38:14 -06:00
|
|
|
void testCrashForkit();
|
2016-04-30 09:23:26 -05:00
|
|
|
void testNoExtraLoolKitsLeft();
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2016-04-24 11:31:01 -05:00
|
|
|
static
|
2016-11-11 21:38:14 -06:00
|
|
|
void killLoKitProcesses(const char* exec_filename);
|
2016-04-24 11:31:01 -05:00
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
public:
|
|
|
|
HTTPCrashTest()
|
2016-05-03 02:40:25 -05:00
|
|
|
: _uri(helpers::getTestServerURI())
|
2016-04-20 20:14:51 -05:00
|
|
|
{
|
|
|
|
#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-04-20 20:14:51 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLE_SSL
|
|
|
|
~HTTPCrashTest()
|
|
|
|
{
|
|
|
|
Poco::Net::uninitializeSSL();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
2016-05-12 09:59:00 -05:00
|
|
|
testCountHowManyLoolkits();
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
2016-05-12 09:59:00 -05:00
|
|
|
testNoExtraLoolKitsLeft();
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-13 11:43:43 -05:00
|
|
|
int HTTPCrashTest::InitialLoolKitCount = 1;
|
2016-04-30 09:23:26 -05:00
|
|
|
|
|
|
|
void HTTPCrashTest::testCountHowManyLoolkits()
|
|
|
|
{
|
2016-05-13 11:43:43 -05:00
|
|
|
InitialLoolKitCount = countLoolKitProcesses(InitialLoolKitCount);
|
|
|
|
CPPUNIT_ASSERT(InitialLoolKitCount > 0);
|
2016-04-30 09:23:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPCrashTest::testNoExtraLoolKitsLeft()
|
|
|
|
{
|
2016-05-13 11:43:43 -05:00
|
|
|
const auto countNow = countLoolKitProcesses(InitialLoolKitCount);
|
2016-04-30 09:23:26 -05:00
|
|
|
|
2016-05-13 11:43:43 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(InitialLoolKitCount, countNow);
|
2016-04-30 09:23:26 -05:00
|
|
|
}
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
void HTTPCrashTest::testBarren()
|
|
|
|
{
|
|
|
|
// Kill all kit processes and try loading a document.
|
2016-10-22 10:35:22 -05:00
|
|
|
const auto testname = "barren ";
|
2016-04-24 11:35:29 -05:00
|
|
|
try
|
|
|
|
{
|
2016-11-11 21:38:14 -06:00
|
|
|
killLoKitProcesses("(loolkit)");
|
2016-11-14 07:05:12 -06:00
|
|
|
countLoolKitProcesses(0);
|
2016-04-24 11:35:29 -05:00
|
|
|
|
2016-05-12 09:59:00 -05:00
|
|
|
std::cerr << "Loading after kill." << std::endl;
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
// Load a document and get its status.
|
2016-10-22 10:35:22 -05:00
|
|
|
auto socket = loadDocAndGetSocket("hello.odt", _uri, testname);
|
|
|
|
|
|
|
|
sendTextFrame(socket, "status", testname);
|
|
|
|
assertResponseString(socket, "status:", testname);
|
2016-04-24 11:35:29 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
void HTTPCrashTest::testCrashKit()
|
|
|
|
{
|
2016-10-15 16:09:28 -05:00
|
|
|
const auto testname = "crashKit ";
|
2016-04-20 20:14:51 -05:00
|
|
|
try
|
|
|
|
{
|
2016-11-10 02:47:25 -06:00
|
|
|
auto socket = loadDocAndGetSocket("empty.odt", _uri, testname);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2016-11-11 21:38:14 -06:00
|
|
|
killLoKitProcesses("(loolkit)");
|
2016-11-14 07:05:12 -06:00
|
|
|
countLoolKitProcesses(0);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2016-10-15 16:09:28 -05:00
|
|
|
// We expect the client connection to close.
|
|
|
|
// In the future we might restore the kit, but currently we don't.
|
2016-05-12 09:59:00 -05:00
|
|
|
std::cerr << "Reading after kill." << std::endl;
|
|
|
|
|
2016-10-15 16:09:28 -05:00
|
|
|
// Drain the socket.
|
|
|
|
getResponseMessage(socket, "", testname, 1000);
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
// 5 seconds timeout
|
2016-11-10 02:47:25 -06:00
|
|
|
socket->setReceiveTimeout(5000000);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
// receive close frame handshake
|
2016-05-12 09:59:00 -05:00
|
|
|
int bytes;
|
|
|
|
int flags;
|
2016-11-25 03:46:01 -06:00
|
|
|
char buffer[READ_BUFFER_SIZE];
|
2016-04-20 20:14:51 -05:00
|
|
|
do
|
|
|
|
{
|
2016-11-25 03:46:01 -06:00
|
|
|
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
|
|
|
|
std::cerr << testname << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, bytes, flags) << std::endl;
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
|
|
|
|
// respond close frame
|
2016-11-10 02:47:25 -06:00
|
|
|
socket->shutdown();
|
2016-10-15 16:09:28 -05:00
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
// no more messages is received.
|
2016-11-25 03:46:01 -06:00
|
|
|
bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
|
2017-01-01 16:20:35 -06:00
|
|
|
std::cerr << testname << "Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, bytes, flags) << std::endl;
|
|
|
|
|
|
|
|
// While we expect no more messages after shutdown call, apparently
|
|
|
|
// sometimes we _do_ get data. Even when the receiveFrame in the loop
|
|
|
|
// returns a CLOSE frame (with 2 bytes) the one after shutdown sometimes
|
|
|
|
// returns a BINARY frame with the next payload sent by wsd.
|
|
|
|
// This is an oddity of Poco and is not something we need to validate here.
|
|
|
|
//CPPUNIT_ASSERT_MESSAGE("Expected no more data", bytes <= 2); // The 2-byte marker is ok.
|
|
|
|
//CPPUNIT_ASSERT_EQUAL(0x88, flags);
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:38:14 -06:00
|
|
|
void HTTPCrashTest::testCrashForkit()
|
|
|
|
{
|
|
|
|
const auto testname = "crashForkit ";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto socket = loadDocAndGetSocket("empty.odt", _uri, testname);
|
|
|
|
|
|
|
|
std::cerr << "Killing forkit." << std::endl;
|
|
|
|
killLoKitProcesses("(loolforkit)");
|
|
|
|
std::cerr << "Communicating after kill." << std::endl;
|
|
|
|
|
|
|
|
sendTextFrame(socket, "status", testname);
|
|
|
|
assertResponseString(socket, "status:", testname);
|
|
|
|
|
|
|
|
// respond close frame
|
|
|
|
socket->shutdown();
|
|
|
|
|
|
|
|
|
2016-11-14 07:05:12 -06:00
|
|
|
std::cerr << "Killing loolkit." << std::endl;
|
2016-11-11 21:38:14 -06:00
|
|
|
killLoKitProcesses("(loolkit)");
|
2016-11-14 07:05:12 -06:00
|
|
|
countLoolKitProcesses(0);
|
2016-11-11 21:38:14 -06:00
|
|
|
std::cerr << "Communicating after kill." << std::endl;
|
|
|
|
loadDocAndGetSocket("empty.odt", _uri, testname);
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPCrashTest::killLoKitProcesses(const char* exec_filename)
|
2016-04-24 11:31:01 -05:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
2016-11-11 21:38:14 -06:00
|
|
|
|
2016-04-24 11:31:01 -05:00
|
|
|
if (pid > 1 && endPos == fileName.length())
|
|
|
|
{
|
|
|
|
Poco::FileInputStream stat(procEntry.toString() + "/stat");
|
|
|
|
std::string statString;
|
|
|
|
Poco::StreamCopier::copyToString(stat, statString);
|
|
|
|
Poco::StringTokenizer tokens(statString, " ");
|
2016-11-11 21:38:14 -06:00
|
|
|
if (tokens.count() > 3 && tokens[1] == exec_filename)
|
2016-04-24 11:31:01 -05:00
|
|
|
{
|
2016-05-12 09:59:00 -05:00
|
|
|
std::cerr << "Killing " << pid << std::endl;
|
2016-04-25 01:25:23 -05:00
|
|
|
if (kill(pid, SIGKILL) == -1)
|
2016-04-24 11:31:01 -05:00
|
|
|
{
|
2016-11-11 21:38:14 -06:00
|
|
|
std::cerr << "kill(" << pid << ", SIGKILL) failed: " << std::strerror(errno) << std::endl;
|
2016-04-24 11:31:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPCrashTest);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|