2016-04-05 11:41:10 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-03-08 10:38:22 -06:00
|
|
|
|
2016-10-21 06:30:35 -05:00
|
|
|
#include <iostream>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Unit.hpp"
|
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
#include <cassert>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <dlfcn.h>
|
2016-05-01 09:24:31 -05:00
|
|
|
#include <fstream>
|
2019-11-06 03:07:32 -06:00
|
|
|
#include <sysexits.h>
|
2019-11-06 07:03:53 -06:00
|
|
|
#include <thread>
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2019-11-06 03:07:32 -06:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2016-04-06 13:50:55 -05:00
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
|
|
|
#include "Util.hpp"
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/SigUtil.hpp>
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
UnitBase *UnitBase::Global = nullptr;
|
2021-09-14 10:05:31 -05:00
|
|
|
UnitKit *GlobalKit = nullptr;
|
|
|
|
UnitWSD *GlobalWSD = nullptr;
|
2021-09-28 04:21:45 -05:00
|
|
|
UnitTool *GlobalTool = nullptr;
|
2020-01-17 14:49:38 -06:00
|
|
|
char * UnitBase::UnitLibPath;
|
2019-11-06 07:03:53 -06:00
|
|
|
static std::thread TimeoutThread;
|
|
|
|
static std::atomic<bool> TimeoutThreadRunning(false);
|
|
|
|
std::timed_mutex TimeoutThreadMutex;
|
2016-04-08 11:36:08 -05:00
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase *UnitBase::linkAndCreateUnit(UnitType type, const std::string &unitLibPath)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-04-05 11:41:10 -05:00
|
|
|
void *dlHandle = dlopen(unitLibPath.c_str(), RTLD_GLOBAL|RTLD_NOW);
|
|
|
|
if (!dlHandle)
|
|
|
|
{
|
2016-11-06 11:06:21 -06:00
|
|
|
LOG_ERR("Failed to load " << unitLibPath << ": " << dlerror());
|
2016-12-22 08:04:07 -06:00
|
|
|
return nullptr;
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
|
|
|
|
2020-01-17 14:49:38 -06:00
|
|
|
// avoid std:string de-allocation during failure / exit.
|
|
|
|
UnitLibPath = strdup(unitLibPath.c_str());
|
|
|
|
|
2016-12-22 08:04:07 -06:00
|
|
|
const char *symbol = nullptr;
|
2016-04-09 11:30:48 -05:00
|
|
|
switch (type)
|
|
|
|
{
|
2016-12-22 03:27:30 -06:00
|
|
|
case UnitType::Wsd:
|
2016-04-09 11:30:48 -05:00
|
|
|
symbol = "unit_create_wsd";
|
|
|
|
break;
|
2016-12-22 03:27:30 -06:00
|
|
|
case UnitType::Kit:
|
2016-04-09 11:30:48 -05:00
|
|
|
symbol = "unit_create_kit";
|
|
|
|
break;
|
2021-08-10 22:41:02 -05:00
|
|
|
case UnitType::Tool:
|
|
|
|
symbol = "unit_create_tool";
|
|
|
|
break;
|
2016-04-09 11:30:48 -05:00
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
CreateUnitHooksFunction* createHooks;
|
2016-04-09 11:30:48 -05:00
|
|
|
createHooks = reinterpret_cast<CreateUnitHooksFunction *>(dlsym(dlHandle, symbol));
|
2016-04-05 11:41:10 -05:00
|
|
|
if (!createHooks)
|
|
|
|
{
|
2016-11-06 11:06:21 -06:00
|
|
|
LOG_ERR("No " << symbol << " symbol in " << unitLibPath);
|
2016-12-22 08:04:07 -06:00
|
|
|
return nullptr;
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
2017-01-13 06:52:08 -06:00
|
|
|
UnitBase *hooks = createHooks();
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
if (hooks)
|
|
|
|
hooks->setHandle(dlHandle);
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
return hooks;
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
bool UnitBase::init(UnitType type, const std::string &unitLibPath)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2019-02-13 09:37:54 -06:00
|
|
|
#if !MOBILEAPP
|
2021-09-16 06:25:29 -05:00
|
|
|
assert(!get(type));
|
2019-02-13 09:37:54 -06:00
|
|
|
#else
|
2021-11-18 06:08:14 -06:00
|
|
|
// The COOLWSD initialization is called in a loop on mobile, allow reuse
|
2021-09-16 06:25:29 -05:00
|
|
|
if (get(type))
|
2019-02-13 09:37:54 -06:00
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
if (!unitLibPath.empty())
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
2021-09-16 06:25:29 -05:00
|
|
|
UnitBase* instance = linkAndCreateUnit(type, unitLibPath);
|
|
|
|
rememberInstance(type, instance);
|
|
|
|
LOG_DBG(instance->_testname << ": Initializing");
|
2021-09-14 10:05:31 -05:00
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
if (instance && type == UnitType::Kit)
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
2019-11-06 07:03:53 -06:00
|
|
|
TimeoutThreadMutex.lock();
|
2021-09-16 06:25:29 -05:00
|
|
|
TimeoutThread = std::thread([instance]{
|
2019-11-06 07:03:53 -06:00
|
|
|
TimeoutThreadRunning = true;
|
|
|
|
Util::setThreadName("unit timeout");
|
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
if (TimeoutThreadMutex.try_lock_for(instance->_timeoutMilliSeconds))
|
2019-11-06 07:03:53 -06:00
|
|
|
{
|
2021-09-16 06:25:29 -05:00
|
|
|
LOG_DBG(instance->_testname << ": Unit test finished in time");
|
2019-11-06 07:03:53 -06:00
|
|
|
TimeoutThreadMutex.unlock();
|
|
|
|
}
|
|
|
|
else
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
2021-09-16 06:25:29 -05:00
|
|
|
LOG_ERR(instance->_testname << ": Unit test timeout after "
|
|
|
|
<< instance->_timeoutMilliSeconds);
|
|
|
|
instance->timeout();
|
2016-04-09 11:30:48 -05:00
|
|
|
}
|
2019-11-06 07:03:53 -06:00
|
|
|
TimeoutThreadRunning = false;
|
2016-04-09 11:30:48 -05:00
|
|
|
});
|
|
|
|
}
|
2016-04-08 11:36:08 -05:00
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
else
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2016-12-22 03:27:30 -06:00
|
|
|
case UnitType::Wsd:
|
2021-09-16 06:25:29 -05:00
|
|
|
rememberInstance(UnitType::Wsd, new UnitWSD());
|
2016-04-09 11:30:48 -05:00
|
|
|
break;
|
2016-12-22 03:27:30 -06:00
|
|
|
case UnitType::Kit:
|
2021-09-16 06:25:29 -05:00
|
|
|
rememberInstance(UnitType::Kit, new UnitKit());
|
2016-04-09 11:30:48 -05:00
|
|
|
break;
|
2021-08-10 22:41:02 -05:00
|
|
|
case UnitType::Tool:
|
2021-09-28 04:21:45 -05:00
|
|
|
rememberInstance(UnitType::Tool, new UnitTool());
|
2021-08-10 22:41:02 -05:00
|
|
|
break;
|
2016-04-09 11:30:48 -05:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
return get(type) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitBase* UnitBase::get(UnitType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case UnitType::Wsd:
|
|
|
|
return GlobalWSD;
|
|
|
|
break;
|
|
|
|
case UnitType::Kit:
|
|
|
|
return GlobalKit;
|
|
|
|
break;
|
2021-09-28 04:21:45 -05:00
|
|
|
case UnitType::Tool:
|
|
|
|
return GlobalTool;
|
|
|
|
break;
|
2021-09-16 06:25:29 -05:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
return nullptr;
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
void UnitBase::rememberInstance(UnitType type, UnitBase* instance)
|
2021-09-14 10:05:31 -05:00
|
|
|
{
|
2021-09-16 06:25:29 -05:00
|
|
|
assert(instance->_type == type);
|
|
|
|
|
2021-09-14 10:05:31 -05:00
|
|
|
Global = instance;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case UnitType::Wsd:
|
|
|
|
GlobalWSD = static_cast<UnitWSD*>(instance);
|
|
|
|
break;
|
|
|
|
case UnitType::Kit:
|
|
|
|
GlobalKit = static_cast<UnitKit*>(instance);
|
|
|
|
break;
|
2021-09-28 04:21:45 -05:00
|
|
|
case UnitType::Tool:
|
|
|
|
GlobalTool = static_cast<UnitTool*>(instance);
|
|
|
|
break;
|
2021-09-14 10:05:31 -05:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 10:35:30 -05:00
|
|
|
bool UnitBase::isUnitTesting()
|
|
|
|
{
|
|
|
|
return Global && Global->_dlHandle;
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:00:55 -06:00
|
|
|
void UnitBase::setTimeout(std::chrono::milliseconds timeoutMilliSeconds)
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
2019-11-06 07:03:53 -06:00
|
|
|
assert(!TimeoutThreadRunning);
|
2016-04-08 11:36:08 -05:00
|
|
|
_timeoutMilliSeconds = timeoutMilliSeconds;
|
2021-04-12 13:40:38 -05:00
|
|
|
LOG_TST(getTestname() << ": setTimeout: " << _timeoutMilliSeconds);
|
2016-04-08 11:36:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase::~UnitBase()
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2016-04-06 13:50:55 -05:00
|
|
|
// FIXME: we should really clean-up properly.
|
|
|
|
// if (_dlHandle)
|
|
|
|
// dlclose(_dlHandle);
|
2016-12-22 08:04:07 -06:00
|
|
|
_dlHandle = nullptr;
|
2021-04-12 23:48:47 -05:00
|
|
|
_socketPoll->joinThread();
|
2016-04-05 11:41:10 -05:00
|
|
|
}
|
|
|
|
|
2020-12-20 21:11:33 -06:00
|
|
|
UnitWSD::UnitWSD(std::string testname)
|
2021-09-16 06:25:29 -05:00
|
|
|
: UnitBase(std::move(testname), UnitType::Wsd)
|
2020-12-20 21:11:33 -06:00
|
|
|
, _hasKitHooks(false)
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitWSD::~UnitWSD()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:25:23 -05:00
|
|
|
void UnitWSD::configure(Poco::Util::LayeredConfiguration &config)
|
|
|
|
{
|
2016-10-22 10:35:30 -05:00
|
|
|
if (isUnitTesting())
|
2016-10-21 15:25:23 -05:00
|
|
|
{
|
|
|
|
// Force HTTP - helps stracing.
|
|
|
|
config.setBool("ssl.enable", false);
|
2016-12-03 16:40:58 -06:00
|
|
|
// Use http:// everywhere.
|
|
|
|
config.setBool("ssl.termination", false);
|
2016-10-21 15:25:23 -05:00
|
|
|
// Force console output - easier to debug.
|
|
|
|
config.setBool("logging.file[@enable]", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 09:24:31 -05:00
|
|
|
void UnitWSD::lookupTile(int part, int width, int height, int tilePosX, int tilePosY,
|
2019-02-14 13:01:43 -06:00
|
|
|
int tileWidth, int tileHeight,
|
|
|
|
std::shared_ptr<std::vector<char>> &tile)
|
2016-05-01 09:24:31 -05:00
|
|
|
{
|
2021-04-29 05:15:36 -05:00
|
|
|
if (isUnitTesting())
|
|
|
|
{
|
|
|
|
if (tile)
|
|
|
|
onTileCacheHit(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
|
|
|
else
|
|
|
|
onTileCacheMiss(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
|
|
|
}
|
2016-05-01 09:24:31 -05:00
|
|
|
}
|
|
|
|
|
2021-09-14 10:05:31 -05:00
|
|
|
UnitWSD& UnitWSD::get()
|
|
|
|
{
|
2021-09-16 06:25:29 -05:00
|
|
|
assert(GlobalWSD);
|
2021-09-14 10:05:31 -05:00
|
|
|
return *GlobalWSD;
|
|
|
|
}
|
|
|
|
|
2021-04-22 20:58:49 -05:00
|
|
|
UnitKit::UnitKit(std::string testname)
|
2021-09-16 06:25:29 -05:00
|
|
|
: UnitBase(std::move(testname), UnitType::Kit)
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitKit::~UnitKit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-14 10:05:31 -05:00
|
|
|
UnitKit& UnitKit::get()
|
|
|
|
{
|
|
|
|
#if MOBILEAPP
|
|
|
|
if (!GlobalKit)
|
|
|
|
GlobalKit = new UnitKit();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(GlobalKit);
|
|
|
|
return *GlobalKit;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::exitTest(TestResult result)
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
2020-02-19 02:24:36 -06:00
|
|
|
if (_setRetValue)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:08:45 -06:00
|
|
|
if (result == TestResult::Ok)
|
2021-04-12 13:40:38 -05:00
|
|
|
LOG_INF(getTestname() << ": SUCCESS: exitTest: " << testResultAsString(result)
|
|
|
|
<< ". Flagging to shutdown.");
|
2020-12-22 18:08:45 -06:00
|
|
|
else
|
2021-04-12 13:40:38 -05:00
|
|
|
LOG_ERR(getTestname() << ": FAILURE: exitTest: " << testResultAsString(result)
|
|
|
|
<< ". Flagging to shutdown.");
|
2020-12-22 18:08:45 -06:00
|
|
|
|
2016-04-06 13:50:55 -05:00
|
|
|
_setRetValue = true;
|
2019-11-06 03:07:32 -06:00
|
|
|
_retValue = result == TestResult::Ok ? EX_OK : EX_SOFTWARE;
|
2020-02-24 01:12:38 -06:00
|
|
|
#if !MOBILEAPP
|
2021-04-12 21:26:06 -05:00
|
|
|
LOG_INF("Setting ShutdownRequestFlag: " << getTestname() << " test has finished.");
|
2021-01-10 15:44:24 -06:00
|
|
|
SigUtil::requestShutdown(); // And wakupWorld.
|
|
|
|
#else
|
2017-03-16 13:52:49 -05:00
|
|
|
SocketPoll::wakeupWorld();
|
2021-01-10 15:44:24 -06:00
|
|
|
#endif
|
2016-04-06 13:50:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::timeout()
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
2020-12-26 14:37:57 -06:00
|
|
|
// Don't timeout if we had already finished.
|
|
|
|
if (isUnitTesting() && !_setRetValue)
|
2017-03-17 02:13:12 -05:00
|
|
|
{
|
2021-11-29 23:26:56 -06:00
|
|
|
LOG_ERR(getTestname() << ": Timed out waiting for unit test to complete within "
|
|
|
|
<< _timeoutMilliSeconds);
|
2017-03-17 02:13:12 -05:00
|
|
|
exitTest(TestResult::TimedOut);
|
|
|
|
}
|
2016-04-08 11:36:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::returnValue(int &retValue)
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
|
|
|
if (_setRetValue)
|
|
|
|
retValue = _retValue;
|
|
|
|
|
2019-11-06 07:03:53 -06:00
|
|
|
// tell the timeout thread that the work has finished
|
|
|
|
TimeoutThreadMutex.unlock();
|
|
|
|
if (TimeoutThread.joinable())
|
|
|
|
TimeoutThread.join();
|
2016-04-08 11:36:08 -05:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
Global = nullptr;
|
2016-04-06 13:50:55 -05:00
|
|
|
}
|
|
|
|
|
2021-09-16 06:25:29 -05:00
|
|
|
void UnitKit::returnValue(int &retValue)
|
|
|
|
{
|
|
|
|
UnitBase::returnValue(retValue);
|
|
|
|
|
|
|
|
delete GlobalKit;
|
|
|
|
GlobalKit = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitWSD::returnValue(int &retValue)
|
|
|
|
{
|
|
|
|
UnitBase::returnValue(retValue);
|
|
|
|
|
|
|
|
delete GlobalWSD;
|
|
|
|
GlobalWSD = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|