1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
launchertest.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include <config-gammaray.h>
#include <gammaray-test-config.h>
#include "testhelpers.h"
#include <launcher/core/injector/injectorfactory.h>
#include <launcher/core/launchoptions.h>
#include <launcher/core/launcher.h>
#include <launcher/core/probefinder.h>
#include <launcher/core/probeabi.h>
#include <launcher/core/probeabidetector.h>
#include <QDebug>
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include <memory>
#ifdef Q_OS_WIN
#include <qt_windows.h>
#endif
using namespace GammaRay;
using namespace TestHelpers;
class LauncherTest : public QObject
{
Q_OBJECT
private:
static bool hasInjector(const char *type)
{
auto injector = InjectorFactory::createInjector(type);
if (!injector)
return false;
return injector->selfTest();
}
private slots:
void testLauncher_data()
{
QTest::addColumn<QString>("injectorType", nullptr);
QTest::newRow("dummy") << QString(); // workaround for QTestlib asserting on empty test data sets
if (hasInjector("preload"))
QTest::newRow("preload") << QStringLiteral("preload");
if (hasInjector("windll"))
QTest::newRow("windll") << QStringLiteral("windll");
}
static void testLauncher()
{
QFETCH(QString, injectorType);
if (injectorType.isEmpty())
return;
LaunchOptions options;
options.setUiMode(LaunchOptions::NoUi);
QString exePath = QLatin1String(TESTBIN_DIR "/minimalcoreapplication");
#ifdef Q_OS_WIN
exePath += QLatin1String(".exe");
#endif
ProbeABIDetector detector;
options.setProbeABI(ProbeFinder::findBestMatchingABI(detector.abiForExecutable(exePath)));
options.setLaunchArguments(QStringList() << exePath);
options.setInjectorType(injectorType);
options.setProbeSetting(QStringLiteral("ServerAddress"), GAMMARAY_DEFAULT_LOCAL_TCP_URL);
Launcher launcher(options);
QSignalSpy startSpy(&launcher, &Launcher::started);
QVERIFY(startSpy.isValid());
QSignalSpy finishSpy(&launcher, &Launcher::finished);
QVERIFY(finishSpy.isValid());
QVERIFY(launcher.start());
startSpy.wait(60000);
QCOMPARE(startSpy.size(), 1);
QCOMPARE(finishSpy.size(), 0);
launcher.stop();
finishSpy.wait(10000);
QCOMPARE(finishSpy.size(), 1);
}
#ifdef HAVE_QT_WIDGETS
static void testLauncherStyle()
{
LaunchOptions options;
options.setUiMode(LaunchOptions::NoUi);
QString exePath = QLatin1String(TESTBIN_DIR "/minimalwidgetapplication");
#ifdef Q_OS_WIN
exePath += QLatin1String(".exe");
#endif
ProbeABIDetector detector;
options.setProbeABI(ProbeFinder::findBestMatchingABI(detector.abiForExecutable(exePath)));
options.setLaunchArguments(QStringList() << exePath);
options.setInjectorType(QStringLiteral("style"));
options.setProbeSetting(QStringLiteral("ServerAddress"), GAMMARAY_DEFAULT_LOCAL_TCP_URL);
Launcher launcher(options);
QSignalSpy startSpy(&launcher, &Launcher::started);
QVERIFY(startSpy.isValid());
QSignalSpy finishSpy(&launcher, &Launcher::finished);
QVERIFY(finishSpy.isValid());
QVERIFY(launcher.start());
startSpy.wait(60000);
QCOMPARE(startSpy.size(), 1);
QCOMPARE(finishSpy.size(), 0);
launcher.stop();
finishSpy.wait(10000);
QCOMPARE(finishSpy.size(), 1);
}
#endif
static void testAttach()
{
QProcess target;
auto cleanup = kdScopeGuard([&target] {
target.kill();
target.waitForFinished();
});
target.setProcessChannelMode(QProcess::ForwardedChannels);
target.start(QLatin1String(TESTBIN_DIR "/minimalcoreapplication"), {}, QProcess::ReadWrite);
QVERIFY(target.waitForStarted());
LaunchOptions options;
options.setUiMode(LaunchOptions::NoUi);
options.setProbeSetting(QStringLiteral("ServerAddress"), GAMMARAY_DEFAULT_LOCAL_TCP_URL);
options.setPid(target.processId());
QTest::qWait(5000); // give the target some time to actually load the QtCore DLL, otherwise ABI detection fails
ProbeABIDetector detector;
options.setProbeABI(ProbeFinder::findBestMatchingABI(detector.abiForProcess(options.pid())));
Launcher launcher(options);
QSignalSpy spy(&launcher, &Launcher::attached);
QVERIFY(spy.isValid());
QVERIFY(launcher.start());
spy.wait(30000);
QCOMPARE(spy.size(), 1);
}
};
QTEST_MAIN(LauncherTest)
#include "launchertest.moc"
|