File indexing completed on 2024-04-21 03:53:40

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org>
0005     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "kprocesstest_helper.h"
0011 #include <QFile>
0012 #include <QObject>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 #include <kprocess.h>
0016 
0017 #include <signal.h>
0018 #include <stdio.h>
0019 #include <stdlib.h>
0020 
0021 class KProcessTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025 private Q_SLOTS:
0026     void test_channels();
0027     void test_setShellCommand();
0028     void test_inheritance();
0029 };
0030 
0031 // IOCCC nomination pending
0032 
0033 static QString callHelper(KProcess::OutputChannelMode how)
0034 {
0035     QProcess p;
0036     p.setProcessChannelMode(QProcess::MergedChannels);
0037 
0038     QString helper = QCoreApplication::applicationDirPath() + QStringLiteral("/kprocesstest_helper");
0039 #ifdef Q_OS_WIN
0040     helper += QStringLiteral(".exe");
0041 #endif
0042 
0043     Q_ASSERT(QFile::exists(helper));
0044     p.start(helper, QStringList() << QString::number(how) << QStringLiteral("--nocrashhandler"));
0045     p.waitForFinished();
0046     return QString::fromLatin1(p.readAllStandardOutput());
0047 }
0048 
0049 #define EO EOUT "\n"
0050 #define EE EERR "\n"
0051 #define TESTCHAN(me, ms, pout, rout, rerr)                                                                                                                     \
0052     e = QStringLiteral("mode: " ms "\n" POUT pout ROUT rout RERR rerr);                                                                                        \
0053     a = QStringLiteral("mode: " ms "\n") + callHelper(KProcess::me);                                                                                           \
0054     QCOMPARE(a, e)
0055 
0056 void KProcessTest::test_channels()
0057 {
0058 #ifdef Q_OS_UNIX
0059     QString e;
0060     QString a;
0061     TESTCHAN(SeparateChannels, "separate", "", EO, EE);
0062     TESTCHAN(ForwardedChannels, "forwarded", EO EE, "", "");
0063     TESTCHAN(OnlyStderrChannel, "forwarded stdout", EO, "", EE);
0064     TESTCHAN(OnlyStdoutChannel, "forwarded stderr", EE, EO, "");
0065     TESTCHAN(MergedChannels, "merged", "", EO EE, "");
0066 #else
0067     Q_UNUSED(callHelper);
0068     QSKIP("This test needs a UNIX system");
0069 #endif
0070 }
0071 
0072 void KProcessTest::test_setShellCommand()
0073 {
0074     // Condition copied from kprocess.cpp
0075 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__GNU__)
0076     QSKIP("This test needs a free UNIX system");
0077 #else
0078     KProcess p;
0079 
0080     p.setShellCommand(QStringLiteral("cat"));
0081     QCOMPARE(p.program().count(), 1);
0082     QCOMPARE(p.program().at(0), QStandardPaths::findExecutable(QStringLiteral("cat")));
0083     QVERIFY(p.program().at(0).endsWith(QLatin1String("/cat")));
0084     p.setShellCommand(QStringLiteral("true || false"));
0085     QCOMPARE(p.program(), QStringList() << QStringLiteral("/bin/sh") << QStringLiteral("-c") << QString::fromLatin1("true || false"));
0086 #endif
0087 }
0088 
0089 void KProcessTest::test_inheritance()
0090 {
0091     KProcess kproc;
0092     QProcess *qproc = &kproc;
0093     const QString program = QStringLiteral("foobar");
0094     const QStringList arguments{QStringLiteral("meow")};
0095 
0096     kproc.setProgram(program, arguments);
0097     QCOMPARE(qproc->program(), program);
0098     QCOMPARE(qproc->arguments(), arguments);
0099     kproc.clearProgram();
0100     QCOMPARE(qproc->program(), QString());
0101     QCOMPARE(qproc->arguments(), QStringList());
0102 
0103     kproc << program << arguments;
0104     QCOMPARE(qproc->program(), program);
0105     QCOMPARE(qproc->arguments(), arguments);
0106     kproc.clearProgram();
0107     QCOMPARE(qproc->program(), QString());
0108     QCOMPARE(qproc->arguments(), QStringList());
0109 
0110 #ifdef Q_OS_UNIX
0111     kproc.setShellCommand(QStringLiteral("/bin/true meow"));
0112     QCOMPARE(qproc->program(), QStringLiteral("/bin/true"));
0113     QCOMPARE(qproc->arguments(), arguments);
0114     kproc.clearProgram();
0115     QCOMPARE(qproc->program(), QString());
0116     QCOMPARE(qproc->arguments(), QStringList());
0117 #endif
0118 }
0119 
0120 QTEST_MAIN(KProcessTest)
0121 
0122 #include "kprocesstest.moc"